//
//	sdSS_SSSSSSbs   .S_sSSs     .S_SSSs      sSSs  sdSS_SSSSSSbs   .S    sSSs_sSSs     .S_sSSs    
//	YSSS~S%SSSSSP  .SS~YS%%b   .SS~SSSSS    d%%SP  YSSS~S%SSSSSP  .SS   d%%SP~YS%%b   .SS~YS%%b   
//	     S%S       S%S   `S%b  S%S   SSSS  d%S'         S%S       S%S  d%S'     `S%b  S%S   `S%b  
//	     S%S       S%S    S%S  S%S    S%S  S%S          S%S       S%S  S%S       S%S  S%S    S%S  
//	     S&S       S%S    d*S  S%S SSSS%S  S&S          S&S       S&S  S&S       S&S  S%S    S&S  
//	     S&S       S&S   .S*S  S&S  SSS%S  S&S          S&S       S&S  S&S       S&S  S&S    S&S  
//	     S&S       S&S_sdSSS   S&S    S&S  S&S          S&S       S&S  S&S       S&S  S&S    S&S  
//	     S&S       S&S~YSY%b   S&S    S&S  S&S          S&S       S&S  S&S       S&S  S&S    S&S  
//	     S*S       S*S   `S%b  S*S    S&S  S*b          S*S       S*S  S*b       d*S  S*S    S*S  
//	     S*S       S*S    S%S  S*S    S*S  S*S.         S*S       S*S  S*S.     .S*S  S*S    S*S  
//	     S*S       S*S    S&S  S*S    S*S   SSSbs       S*S       S*S   SSSbs_sdSSS   S*S    S*S  
//	     S*S       S*S    SSS  SSS    S*S    YSSP       S*S       S*S    YSSP~YSSY    S*S    SSS  
//	     SP        SP                 SP                SP        SP                  SP          
//	     Y         Y                  Y                 Y         Y                   Y           


function initCalendar() {
	oContainer = $("#calendarContainer");
	var iHeight = oContainer.innerHeight();
	oContainer.css("height",iHeight).css("overflow","hidden").data("origHeight",iHeight);
//	oContainer.css("height",iHeight).data("origHeight",iHeight);
	$("#dayDetails").css("display","block");
	
	initDays();
}

function initDays() {
	$(".dayClicker").click(function() {
		if($("#eventDetailsContainer").css("width") != "0px") {
			closeEvent();
		}
		
/*	Not using a current day hi-lite for this calendar							
			$(".dayCurrent").each(function() {
				$(this).addClass("dayClicker");
				$(this).removeClass("dayCurrent");
			});
			
			$(this).addClass("dayCurrent");
			$(this).removeClass("dayClicker");
*/		
		$.ajax({
			data: {date: $(this).attr("title")},
			url: "/mods/dayFetcher.php",
			type: "POST",
			success: function(data) {
				var oDay = $("#dayDetails");
				var oContainer = $("#calendarContainer");
	
				oDay.html(data);
				
				bindEventLinks("#dayDetails a.grey")
				
				if(parseInt(oDay.innerHeight()) > parseInt(oContainer.css("height"))) {
					oContainer.animate({height: oDay.innerHeight()}, 400);
				} else {
					oDay.css("height",parseInt(oContainer.css("height")) - 14);
				}
				oDay.animate({left: -3}, 400);
			}
		});

	});
}

function closeDay() {
	$("#dayDetails").animate({left: 313}, {
		duration: 400,
		complete: function() {
			$("#dayDetails").css("height","");
		}
	});
	var	oContainer = $("#calendarContainer");
	oContainer.animate({ height: oContainer.data("origHeight") }, 400);
}

function fetchMonth(leDate,dir) {
	$.ajax({
		data: {dir: dir, date: leDate},
		url: "/mods/monthFetcher.php",
		type: "POST",
		success: function(data) {
			var oCal = $("#calendar");
			oCal.html(data);
			if(oCal.innerHeight() != $("#calendarContainer").innerHeight()) {
				$("#calendarContainer").css("height",oCal.innerHeight());
			}
			initDays();
		}
	});
}


function closeEvent() {
	var oCol = $("#leftColumn");
	var oContainer = $("#eventDetailsContainer");
	var oRight = $("#rightColumn");
	var iOrigHeight = parseInt(oRight.data("curHeight"));
	if(parseInt(oContainer.css("height")) > iOrigHeight) {
		oContainer.css("height",iOrigHeight);
		oCol.css("height", oCol.data("origHeight"));
	}
	$("#eventDetailsContainer").animate({ width: 0 }, {
		duration: 400,
		complete: function() {
			$(this).css("border-left",0);
			
		}
	});

}

function viewEvent(eventID,sURL) {
	$.ajax({
		data: { id: eventID},
		url: sURL,
		type: "POST",
		success: function(data, textStatus) {
			var oDetails = $("#eventDetails");
			var oContainer = $("#eventDetailsContainer");
			oDetails.html(data);
			oContainer.css("border-left","3px solid #000").animate({width: 630}, 300);

			if($(document).scrollTop() > 147) {
				$.scrollTo("#mainNav",400);
			}

			var iDetailsHeight = parseInt(oDetails.innerHeight()) + 40;
			var oRight = $("#rightColumn");
			var iRightHeight = parseInt(oRight.innerHeight()) - 147;
			oRight.data("curHeight",iRightHeight);

			if(iDetailsHeight > parseInt(oContainer.css("height"))) {
				$("#leftColumn").css("height",iDetailsHeight + 147);
				oContainer.css("height",iDetailsHeight - 3);
			} else {
				oContainer.css("height",iRightHeight);	
			}
		}
	});	
}

function bindNewsLinks() {
	$(".newsListItem a.arrowRight").click(function(e) {
		e.preventDefault();
		viewEvent($(this).attr("href").substr(1),"/mods/newsDetailsFetcher.php");
	});
}

function bindEventLinks(sSelector) {
	$(sSelector).click(function(e) {
		e.preventDefault();
		viewEvent($(this).attr("href").substr(1),"/mods/eventFetcher.php");
	});
}

function oogaBooga() { alert("ooga booga"); }

$(document).ready(function() {
	// Init Calendar
	if($("#calendar").length > 0) {
		$("#monthPrevButton").click(function() {
			fetchMonth($("#sqlDate").attr("title"),"prev");
		});
	
		$("#monthNextButton").click(function() {
			fetchMonth($("#sqlDate").attr("title"),"next");
		});
		
		initCalendar();
	}
	
	// Init Upcoming Events links
	if($("#upcomingEvents").length > 0) {
		bindEventLinks("#upcomingEvents a.grey");	
	}
	
//	if($("#mapCanvas")
	var oContainer = $("#eventDetailsContainer");
	var oCol = $("#leftColumn");
	var iColHeight = parseInt(oCol.innerHeight());
	
	oCol.data("origHeight",iColHeight);
	oContainer.css("height",iColHeight - 147);
	oContainer.data("origHeight",oContainer.css("height"));
});
