$(document).ready(function(){	
	$(".close_item").click(function(){ 
		ResetCalendar(); 
		});

	//clicking a class item in the calendar
	/*
	$(".class_link").click(function(event){
		//hide all open details
		ResetCalendar();
		ID = $(this).attr("id");
		ClassIDtoFetch = ID.replace(/classid_/, "");
		ClassIDDetails = ID + "_details";
		ClassIDText = ID + "_text";
		JsonCalendar("/json/classes/" + ClassIDtoFetch, ID, ClassIDDetails, ClassIDText);
		event.preventDefault();
		});
*/

	//clicking a normal calendar event in the calendar
//	$(".calendar_link").click(function(event){
//		//hide all open details
//		ResetCalendar();
//		ID = $(this).attr("id");
//		CalendarIDtoFetch = ID.replace(/calendarid_/, "");
//		CalendarIDDetails = ID + "_details";
//		CalendarIDText = ID + "_text";
// 		//JsonCalendar("/json/calendar/" + CalendarIDtoFetch, CalendarIDtoFetch, CalendarIDDetails, CalendarIDText);
//		JsonCalendarDetail("/json/calendar/" + CalendarIDtoFetch, CalendarIDtoFetch, CalendarIDDetails, CalendarIDText);
//		event.preventDefault();
//		});
	
//	$(".workshop").click(function(event) {
//		$("." + $(this).attr('id')).slideToggle("fast");
//		event.preventDefault();
//		});

	/*
	$(".view_item").click(function(event){
		$("#itemCalendar").slideUp("fast");
		ID = $(".view_item > a").attr("id");
		CalendarIDtoFetch = ID.replace(/detail_calendarid_/, "");
 		JsonCalendarDetail("/json/calendar/" + CalendarIDtoFetch, CalendarIDtoFetch, "itemCalendar");
		event.preventDefault();
		});
	*/

	//auto load the mini calendars
/*	YearMonth = $("#calendarPreviews").attr("title").replace(/class/, "").replace(/_/, "/");	
	$.get("/minicalendar/index/" + YearMonth, function(data){
	  $("#calendarPreviews").html(data);
	});*/
});

//Close all / any popped open detail on the calendar
function ResetCalendar() {
	$(".class_details").hide("fast");
	$(".calendar_details").hide("fast");
	$("#itemCalendar").slideUp("fast");
}


/*
//Fetch the details from the JSON controller and load up the pop display
function JsonCalendar(URL, ID, Box, Text) {
	$.getJSON(URL, function(data){
		$.each(data, function(i,item){
			ItemLink = "<a id='detail_calendarid_" + ID + "' href='/calendar/specific/" + ID + "'>" + item.Title + "</a>";
			ItemTitle = ""; //"<div class='item_title'></div>";
			ItemDescription = ""; //item.Description;
			ItemDate = "<div class='item_date'>" + item.Date + "</div>";
			ItemTime = "<div class='item_time'>" + item.Time + "</div>";
			
			$("#" + Text + ", .view_item").html(ItemLink);
			$("#" + Text).html(ItemTitle + ItemDescription + ItemDate + ItemTime);
			$("#" + Box).toggle("fast");
			});
		});	
}
*/


//Fetch the details from th JSON controller and load up the slide down display
function JsonCalendarDetail(URL, ID, Box, Text) {
	$.getJSON(URL, function(data){
		$.each(data, function(i,item){
			ItemTitle = "<div class='item_title'>" + item.Title + "</div>";
			ItemDescription = item.Description;
			if (ItemDescription == null) { ItemDescription = ""; }

			ItemDate = "";
			ThisDate = parse_date(item.Date);
			if (ThisDate != "") { ItemDate = "<div class='item_date'>" + ThisDate.format("EE MMM d, yyyy") + "</div>"; }
			
			ItemTime = "";
			ThisTime = item.Time;
			if (ThisTime == null) { ThisTime = ""; }
			ThisTime = parse_time(ThisTime);
			if (ThisTime != "") { ItemTime = "<div class='item_time'>" + ThisTime.format("h:mm a") + "</div>"; }

			ItemExternal = "";
			if (item.Link != null) {
				var LinkText = (item.LinkText || item.Link);
				ItemExternal = "<div class='item_external_link'><a href='" + item.Link + "' target='_blank'>" + LinkText + "</a></div>";
				}
			ViewMore = "<div class='item_external_link'><a href='/calendar/specific/"+item.CalendarID+"' title='View More'>View More Details</a></div>";

			$("#" + Text).html(ItemTitle + ItemDate + ItemTime + ItemExternal + ItemDescription + ViewMore);
			$("#" + Box).toggle("fast");
			});
		});	
}


function parse_date(string) {
    var date = new Date();
	if (string != "") {
		var parts = String(string).split(/[-]/);
		date.setFullYear(parts[0]);
		date.setMonth(parts[1]-1);
		date.setDate(parts[2]);
	}
	return date;
}

function parse_time(string) {
	var time = string;
	if (string != "") {
		time = new Date();
		var parts = String(string).split(/[:]/);
		time.setHours(parts[0]);
		time.setMinutes(parts[1]);
		time.setSeconds(parts[2]);
	}
    return time;
}