var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var nextYear = currentYear + 1;
var currMonthNumber = currentDate.getMonth();
var currMonthName = currentDate.getMonthName();
var currMonthNumberNew = 0;

Date.firstDayOfWeek = 0;

var markActiveDates = function($td, thisDate, month, year)
	{
		if ($td.is(".current-month") && activeDates[thisDate.getTime()]) {
			$td.addClass("calActivity");
		}
	}

var markActiveDatesPop = function($td, thisDate, month, year)
	{

       if ($td.is(".current-month") && activeDates[thisDate.getTime()]) {

            var eventDate = activeDates[thisDate.getTime()];
		    var eventDate2 = activeDates[thisDate.getTime() + 1];

            $td.addClass("calActivity");
			$td.bind("click", function(){

    			var eventDateDetail = eventDate.split("~");
				$(".detailInfoDate").html(eventDateDetail[0]);
				$(".detailInfoTime").html(eventDateDetail[1]);
				$(".detailInfoLoc").html(eventDateDetail[2]);
				$(".detailInfoLink").attr({href: "/TAKEDefense/Registration.action?takeDefenseClass.classId=" + eventDateDetail[3]});

                if(eventDate2 != null) {
                    document.getElementById("detailInfo2").style.display = "block";
                    var eventDateDetail2 = eventDate2.split("~");
                    $(".detailInfoDate2").html(eventDateDetail2[0]);
                    $(".detailInfoTime2").html(eventDateDetail2[1]);
                    $(".detailInfoLoc2").html(eventDateDetail2[2]);
                    $(".detailInfoLink2").attr({href: "/TAKEDefense/Registration.action?takeDefenseClass.classId=" + eventDateDetail2[3]});
                } else {
                    document.getElementById("detailInfo2").style.display = "none";
                }

 			});
		}
	}

var markActiveDatesPopReg = function($td, thisDate, month, year)
	{
		if ($td.is(".current-month") && activeDates[thisDate.getTime()]) {

			var eventDate = activeDates[thisDate.getTime()];
			var eventDate2 = activeDates[thisDate.getTime() + 1];
			
			$td.addClass("calActivity");
			$td.bind("click", function(){

				var eventDateDetail = eventDate.split("~");
				$(".detailInfoDateReg").html(eventDateDetail[0]);
				$(".detailInfoTimeReg").html(eventDateDetail[1]);
				$(".detailInfoLocReg").html(eventDateDetail[2]);
				$(".detailInfoLinkReg").attr({href: "javascript:setClassId('" + eventDateDetail[5] + "' , '" + eventDateDetail[1] + "' , '" + eventDateDetail[4] + "' , '" + eventDateDetail[3] +  "');"});

				if(eventDate2 != null) {
					$(".jqmWindow").css("position", "absolute").css("top", "40%");
					$(".detailInfoReg2").show();
					var eventDateDetail2 = eventDate2.split("~");
					$(".detailInfoDateReg2").html(eventDateDetail2[0]);
					$(".detailInfoTimeReg2").html(eventDateDetail2[1]);
					$(".detailInfoLocReg2").html(eventDateDetail2[2]);
					$(".detailInfoLinkReg2").attr({href: "javascript:setClassId('" + eventDateDetail2[5] + "' , '" + eventDateDetail2[1] + "' , '" + eventDateDetail2[4] + "' , '" + eventDateDetail2[3] +  "');"});					
				} else {
                    $(".detailInfoReg2").hide();
                    $(".jqmWindow").css("position", "fixed").css("top", "2%");
				}
				
			});
		}
	}

function loadCalendars() {
	for (i=0; i<24; i++) {
		if (i <= 11) {
			$(".calendar" + i).renderCalendar({month:i, year:currentYear, renderCallback:markActiveDates, showHeader:$.dpConst.SHOW_HEADER_NONE});
			$(".pop" + i).css("visibility", "visible");
		}
		else if (i <= 22) {
			$(".calendar" + i).renderCalendar({month:i-12, year:nextYear, renderCallback:markActiveDates, showHeader:$.dpConst.SHOW_HEADER_NONE});
			$(".pop" + i).css("visibility", "visible");
		}
		else if (i == 23) {
			$(".calendar" + i).renderCalendar({month:i-12, year:nextYear, renderCallback:markActiveDates, showHeader:$.dpConst.SHOW_HEADER_NONE});
			$(".pop" + i).css("visibility", "visible");
			$(".currYearHolder").unblock();
		}
	}

	$(".other-month").filter(".weekend").css("color", "#cccccc");
	$(".other-month").filter(".weekday").css("color", "#ffffff");
	$(".weekend").css("background-color", "#cccccc");
	$(".weekend").mouseover(function() {
		$(this).css("background-color", "#cccccc");
	});
	$(".today").css("background-color", "#666666");
	$(".today").css("color", "#ffffff");
	$(".today").mouseover(function() {
		$(this).css("background-color", "#666666");
		$(this).css("color", "#ffffff");
	});
	$(".weekend").filter(".calActivity").css("background-color", "#f21790");
	$(".weekend").filter(".calActivity").mouseover(function() {
		$(this).css("background-color", "#f21790");
	});

	$(".jCalendar").mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	$(".jCalendar").mouseout(function(){
		$(this).css("cursor", "default");
	});
}

function colorDates() {
	$(".detailCal .today").css("background-color", "#666666");
	$(".detailCal .today").css("color", "#ffffff");
	$(".detailCal .weekend").filter(".calActivity").css("background-color", "#f21790");
	$(".detailCal .other-month").filter(".weekend").css("color", "#cccccc");
	$(".detailCal .other-month").filter(".weekday").css("color", "#ffffff");

	$(".detailCal .weekend").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCal .weekend").mouseout(function() {
		$(this).css("background-color", "#cccccc");
	});
	$(".detailCal .weekday").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCal .weekday").mouseout(function() {
		$(this).css("background-color", "#ffffff");
	});
	$(".detailCal .today").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCal .today").mouseout(function() {
		$(this).css("background-color", "#666666");
	});
	$(".detailCal .weekend").filter(".calActivity").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCal .weekend").filter(".calActivity").mouseout(function() {
		$(this).css("background-color", "#f21790");
	});
	$(".detailCal .weekday").filter(".calActivity").mouseout(function() {
		$(this).css("background-color", "#f21790");
	});
	$(".detailCal .other-month").filter(".weekend").mouseover(function(){
		$(this).css("background-color", "#cccccc");
		$(this).css("color", "#cccccc");
	});
	$(".detailCal .other-month").filter(".weekday").mouseover(function(){
		$(this).css("background-color", "#ffffff");
		$(this).css("color", "#ffffff");
	});

	$(".jCalendar").mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	$(".jCalendar").mouseout(function(){
		$(this).css("cursor", "default");
	});
}

function colorDatesReg() {
	$(".detailCalReg .today").css("background-color", "#666666");
	$(".detailCalReg .today").css("color", "#ffffff");
	$(".detailCalReg .weekend").filter(".calActivity").css("background-color", "#f21790");
	$(".detailCalReg .other-month").filter(".weekend").css("color", "#cccccc");
	$(".detailCalReg .other-month").filter(".weekend").css("background-color", "#cccccc");
	$(".detailCalReg .other-month").filter(".weekday").css("color", "#ffffff");

	$(".detailCalReg .weekend").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCalReg .weekend").mouseout(function() {
		$(this).css("background-color", "#cccccc");
	});
	$(".detailCalReg .weekday").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCalReg .weekday").mouseout(function() {
		$(this).css("background-color", "#ffffff");
	});
	$(".detailCalReg .today").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCalReg .today").mouseout(function() {
		$(this).css("background-color", "#666666");
	});
	$(".detailCalReg .weekend").filter(".calActivity").mouseover(function() {
		$(this).css("background-color", "#f497c5");
	});
	$(".detailCalReg .weekend").filter(".calActivity").mouseout(function() {
		$(this).css("background-color", "#f21790");
	});
	$(".detailCalReg .weekday").filter(".calActivity").mouseout(function() {
		$(this).css("background-color", "#f21790");
	});
	$(".detailCalReg .other-month").filter(".weekend").mouseover(function(){
		$(this).css("background-color", "#cccccc");
		$(this).css("color", "#cccccc");
	});
	$(".detailCalReg .other-month").filter(".weekday").mouseover(function(){
		$(this).css("background-color", "#ffffff");
		$(this).css("color", "#ffffff");
	});

	$(".jCalendar").mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	$(".jCalendar").mouseout(function(){
		$(this).css("cursor", "default");
	});
}

$(function(){
	$(".pop0").click(function(){
		popMonth("0", currentYear, "January", "current");
	});
	$(".pop1").click(function(){
		popMonth("1", currentYear, "February", "current");
	});
	$(".pop2").click(function(){
		popMonth("2", currentYear, "March", "current");
	});
	$(".pop3").click(function(){
		popMonth("3", currentYear, "April", "current");
	});
	$(".pop4").click(function(){
		popMonth("4", currentYear, "May", "current");
	});
	$(".pop5").click(function(){
		popMonth("5", currentYear, "June", "current");
	});
	$(".pop6").click(function(){
		popMonth("6", currentYear, "July", "current");
	});
	$(".pop7").click(function(){
		popMonth("7", currentYear, "August", "current");
	});
	$(".pop8").click(function(){
		popMonth("8", currentYear, "September", "current");
	});
	$(".pop9").click(function(){
		popMonth("9", currentYear, "October", "current");
	});
	$(".pop10").click(function(){
		popMonth("10", currentYear, "November", "current");
	});
	$(".pop11").click(function(){
		popMonth("11", currentYear, "December", "current");
	});
	$(".pop12").click(function(){
		popMonth("0", nextYear, "January", "next");
	});
	$(".pop13").click(function(){
		popMonth("1", nextYear, "February", "next");
	});
	$(".pop14").click(function(){
		popMonth("2", nextYear, "March", "next");
	});
	$(".pop15").click(function(){
		popMonth("3", nextYear, "April", "next");
	});
	$(".pop16").click(function(){
		popMonth("4", nextYear, "May", "next");
	});
	$(".pop17").click(function(){
		popMonth("5", nextYear, "June", "next");
	});
	$(".pop18").click(function(){
		popMonth("6", nextYear, "July", "next");
	});
	$(".pop19").click(function(){
		popMonth("7", nextYear, "August", "next");
	});
	$(".pop20").click(function(){
		popMonth("8", nextYear, "September", "next");
	});
	$(".pop21").click(function(){
		popMonth("9", nextYear, "October", "next");
	});
	$(".pop22").click(function(){
		popMonth("10", nextYear, "November", "next");
	});
	$(".pop23").click(function(){
		popMonth("11", nextYear, "December", "next");
	});

	$(".btnSelectClass").click(function(){
		popMonthReg();
	});
});

function popMonth(monthPop, yearPop, monthNamePop, divName) {
	$(".detailCalHolder").renderCalendar({month:monthPop, year:yearPop, renderCallback:markActiveDatesPop, showHeader:$.dpConst.SHOW_HEADER_NONE});
	$(".detailCalHead p").html(monthNamePop);
	colorDates();
}

function popMonthReg() {
	for (i=0; i<12; i++) {
		if (currMonthNumber < 12){
			//alert(currMonthNumber + " curr Year");
			$(".cal" + i).renderCalendar({month:currMonthNumber, year:currentYear, renderCallback:markActiveDatesPopReg, showHeader:$.dpConst.SHOW_HEADER_NONE});
			$(".month" + i + " p").html(Date.monthNames[currMonthNumber]);
			currMonthNumber = (currMonthNumber*1) + 1;
		}
		else if (currMonthNumber > 11){
			currMonthNumberNew = (currMonthNumber*1) - 12;
			//alert(currMonthNumberNew + " Next Year");
			$(".cal" + i).renderCalendar({month:currMonthNumberNew, year:((currentYear*1) + 1), renderCallback:markActiveDatesPopReg, showHeader:$.dpConst.SHOW_HEADER_NONE});
			$(".month" + i + " p").html(Date.monthNames[currMonthNumberNew]);
			currMonthNumber = ((currMonthNumber*1) + 1);
		}
	}
	colorDatesReg();
	currMonthNumber = currentDate.getMonth();
}

$(document).ready(function(){
	$(".detailClose").click(function(){
		$(".detailInfoDate").html("&nbsp");
		$(".detailInfoTime").html("&nbsp");
		$(".detailInfoLoc").html("Please select a date from the calendar");
		$(".detailInfoLink").attr({href: "javascript:;"});
	});

	$(".detailCloseReg").click(function(){
		$(".detailInfoDateReg").html("&nbsp");
		$(".detailInfoTimeReg").html("&nbsp");
		$(".detailInfoLocReg").html("Select a date from the calendar above");
		$(".detailInfoLinkReg").attr({href: "javascript:;"});
		$(".detailInfoReg2").hide();
		$(".jqmWindow").css("position", "fixed").css("top", "2%");
	});
});