
$(document).ready(function() {
	
	ajaxRoot = "http://www.ahhyoga.com";

	$('#pageProtector').safeAjax({ debug: false, login: true, ajaxConnector: ajaxRoot+ '/lib/safeAjax/rConnector.php' });

	$('#CalendarHolder').data('calendarData', ajaxRoot+ '/lib/safeAjax/rConnector.php');

	var options = {
		jMonthContainerId: "CalendarHolder",
		onMonthChanging: function(dateIn){ unloadCalendar(); },
		onMonthChanged: function(dateIn){ loadCalendar(dateIn,true); },
		onEventBlockClick: function(theEvent) {
			$("#debugPane").html(JSON.stringify(theEvent));
			schedules = $("#CalendarHolder").data('schedules'); theSchedule = schedules[theEvent.EventID];
			locations = $("#CalendarHolder").data('locations'); theLocation = locations[theSchedule.location_id]; 
			teachers = $("#CalendarHolder").data('teachers'); theTeacher = teachers[theSchedule.teacher_id];
			classes = $("#CalendarHolder").data('classes'); theClass = classes[theSchedule.class_id];
			if (theEvent.CssClass != "Skipped") {
				$(".classShow").show(); $(".classHide").hide();
				$("#ClassName").html(theClass.fullName);
				$("#ClassDesc").html(theClass.description);
				$("#ClassColor").css("background-color",'#'+theClass.color);
				$("#ClassTitle").html("[Time/Town] "+ theEvent.Description+" "+theSchedule.classBegins+" - "+theSchedule.classEnds+" / "+theLocation.city);
				$("#ClassLoc").html("[Location] "+theLocation.fullName);
				$("#ClassRun").html(" ").html(theEvent.runPos);
				showMap(theLocation.id); // execute _before_ jScrollPane
				$('#ClassDesc').jScrollPane({showArrows:true, scrollbarWidth:10, scrollbarMargin:10});
			} else {
				$(".skippedShow").show(); $(".skippedHide").hide();
				showMap(0);
			}
		}
	};

	//$('.classBlob').live('click',function(){
	//	class_id = $(this).data('class_id');
	//	teachers = $("#CalendarHolder").data('teachers'); 
	//	classes = $("#CalendarHolder").data('classes'); 
	//	$(".blobShow").show(); $(".blobHide").hide();
	//	$("#ClassName").html(classes[class_id].fullName);
	//	$("#ClassDesc").html(classes[class_id].description);
	//	$("#ClassColor").css("background-color",'#'+classes[class_id].color);
	//	$('#ClassDesc').jScrollPane({showArrows:true, scrollbarWidth:10, scrollbarMargin:10}); showMap(0);
	//	$("#ClassDesc").show();
	//});

	$.jMonthCalendar.Initialize(options,[]);	

	loadCalendar(new Date(),true); 
	showMap(0);
	
	$("#emailSignup").hide().jqm({ modal: true });
	$("#emailSignupButton").click(function() { $("#emailSignup").jqmShow(); });
	$("#Cancel").click(function(){ $("#emailSignup").jqmHide(); $(".emailItem").val(""); });

	$("#CC_Captured").click(function(){ $("#emailSignup").jqmHide(); $(".emailItem").val(""); });

	$("#Captured").click(function(){
		dataItems = {};
		dataItems.emailName = $("#emailName").val();
		dataItems.emailAddr1 = $("#emailAddr1").val();
		dataItems.emailAddr2 = $("#emailAddr2").val();
		dataItems.emailPhone = $("#emailPhone").val();
		dataItems.emailEMail = $("#emailEMail").val();
		$.getJSON(ajaxRoot+ '/lib/email/forwardEMail.php?set=email&act=forward',dataItems,function(json){
			alert("Thank You!"); $("#debugPane").html(JSON.stringify(json)); 
		});
		$("#emailSignup").jqmHide();
		$(".emailItem").val(""); 
	});

	$.getJSON(ajaxRoot+ '/lib/safeAjax/rConnector.php?set=chunks&act=list&chunkId=1',function(data){
		$("#calendarNote").html(data.description); 
	});

	$.getJSON(ajaxRoot+ '/lib/safeAjax/rConnector.php?set=chunks&act=list&chunkId=3',function(data){
		$("#emailJoinUs").html(data.description); 
	});

	$.getJSON(ajaxRoot+ '/lib/safeAjax/rConnector.php?set=chunks&act=list&chunkId=4',function(data){
		$("#emailDisclaimer").html(data.description); 
	});
});

function showMap (whichOne) {
	
	if (whichOne == 0) {
		$("#ClassMap").hide();
		$("#ClassData").hide();
		$("#calendarNote").show();
	} else {	
		$("#calendarNote").hide();
		$("#ClassData").show();
		$("#ClassMap").show();
		locations = $("#CalendarHolder").data('locations');
		$("#debugPane").html(JSON.stringify(locations[whichOne]));
		markerText = locations[whichOne].shortName
		+"<br>"+ locations[whichOne].addr
		+"<br>"+ locations[whichOne].city +", "+ locations[whichOne].st +"  "+ locations[whichOne].zip
		+"<br>"+ locations[whichOne].phone;
		if (locations[whichOne].link.length > 0) {
			markerText += '<span id="fullMapClick"><br><br><a target="full-map-view" href="' +locations[whichOne].link+ '">Click here for a full map view.</a></span>';
		}
		$("#ClassMap").googleMap(locations[whichOne].lat,locations[whichOne].lng,14,{
			controls: ["GSmallMapControl", "GMapTypeControl"],
			markers: [{ lat: locations[whichOne].lat, lng: locations[whichOne].lng, txt: markerText}],
			showMarkers: true 
		});
	}		
}


