$(document).ready(function () {
	$(".noEvents ").html(' ');
	// Event Filter
	if (window.name.length > 0) {
		if ($("#eventFilterBox input").length > 0) {
            $("#eventFilterBox input").removeProp("checked");
			var checkedList = window.name.split(",");
            $.each(checkedList, function (idx, val) {
                $("#" + val).prop("checked", "true");
			});
			updateCalendar();
		}
		else {
			// hide the tabs & navigation
			$(".eventMode").hide();
			$(".calNav").hide();
		}
	} else {
		if ($("#eventFilterBox input").length > 0) {
            $("#eventFilterBox input").prop("checked", "true");
			applyEventFilter();
		}
		else {
			// hide the tabs & navigation
			$(".eventMode").hide();
			$(".calNav").hide();
		}
	}


    $(".eventType input").bind("click", function () {
		applyEventFilter();
		formatEventList();
	});

	// Assign onchange event for date dropdown selector and call new page element via ajax
	/*$(".eventsViewModebox select").change(function(){
	submitDateDropDown();
	})*/
	initCalendarEvents();

	if ($(".eventCal tr:last td:first").hasClass("weekend-hidden")) {
		$(".eventCal tr:last").hide();
	}
});

function applyEventFilter() {

	var checkedList = [];
    var IsAllEventTypeUnchecked = false;
	$("#eventFilterBox input").each(function (i) {
        var eventType = $(this).prop("id");
        var isChecked = $(this).prop("checked");
        if (isChecked == true) {
            checkedList.push(eventType);
            IsAllEventTypeUnchecked = true;
        }
	});
    if (IsAllEventTypeUnchecked) {
	  window.name = checkedList.join(",");
	}
    else {
        window.name = "AllEventsUnchecked";
	}

	updateCalendar();
}

function updateCalendar() {

    $(".event").each(function () {
        var events = $(this).prop("class").split(" ");
		var eventVisible = false;
        $.each(events, function (key, val) {
            if (val == "event" || val == "isVisible" || val == "") { return true; }
            //alert( "value : " + $("#" + val).prop("checked") );
            if ($("#" + val).prop("checked")) { eventVisible = true; return false; }
		});
        if (eventVisible) {
			$(this).addClass("isVisible");
		} else {
			$(this).removeClass("isVisible");
		}
	});
	//checking all the row based on filters for LIST
	$("table[id$=eventListTable] tbody tr.isVisible").show();
	$("table[id$=eventListTable] tbody tr:not(.isVisible)").hide();

	//checking all the column based on filters for CAL
    $("table[id$=eventCal] td").each(function () {
	    if ($(this).find(".eventSummary").length > 0 && $(this).find(".eventSummary .isVisible").length > 0) {
	        if ($(this).is(":first-child")) {
	            $(this).removeClass("saturday");
	        } else if ($(this).is(":last-child")) {
	            $(this).removeClass("sunday");
	        } else {
	            $(this).removeClass("calDayFont");
	        }
	        $(this).addClass("eventBackground");
			$(this).find('.eventSummary').show();
	    } else {
	        $(this).removeClass("eventBackground");
			$(this).find('.eventSummary').hide();
	        if (!($(this).hasClass("weekend-hidden") || $(this).hasClass("hidden"))) {
	            if ($(this).is(":first-child")) {
	                $(this).addClass("saturday");
	            } else if ($(this).is(":last-child")) {
	                $(this).addClass("sunday");
	            } else {
	                $(this).addClass("calDayFont");
	            }
	        }
	    }
	});

    if ($("#eventListTable").length > 0) {
        if ($("#eventListTable tbody tr.isVisible").length == 0) {
			$(".calendarContainer").hide();
			$(".noEvents").html($(".hiddenNoEventText").val());

			$(".eventListNav").hide();
        } else {
			$(".calendarContainer").show();
			$(".noEvents").html(' ');
			$(".eventListNav").show();
		}
	}

    if ($(".calendarWidgetContainer").length > 0) {
        if ($(".calendarWidgetContainer .event.isVisible").length == 0) {
			$(".calendarWidgetContainer").hide();
			$(".noEvents").html($(".hiddenNoEventText").val());
			$(".eventListNav").hide();
        } else {
			$(".calendarWidgetContainer").show();
			$(".noEvents").html(' ');
			$(".eventListNav").show();
		}
	}
    $(" .event").each(function () {

        if (($(this)).hasClass("isVisible")) {
            if (($(this).parent()).hasClass("eventSummary")) {
                $(this).parent().show();
            }
        }
        else {
            if (($(this).parent()).hasClass("eventSummary")) {
                $(this).parent().hide();
            }
        }
    })
}

//// Ajax call to update calendar if user selected a new month or year
//function submitDateDropDown(){
//	var month = $("#optMonth option:selected").prop("text");
//	var year = $("#optYear option:selected").prop("text");
//	var ajaxUrl = $("#ajaxUrl").prop("value");

//	$.get(ajaxUrl,{"month":month,"year":year},
//	function(returned_data){
//		$("#calendarContainer").html(returned_data)
//		applyEventFilter();
//		applyShader("#calendarContainer");
//		formatEventList();
//	});
//}

// Function to load an event module into a container using Ajax.
// propibutes:
// element - id of element to replace
// url - page to load
function loadPageIntoElement(element, url) {

    $.get(url, function (page) {
        $("#" + element).html(page)

		// Add code here to execute when ajax call is finished
		applyEventFilter();
        applyShader("#" + element);
	});
}

function formatEventList() {
	// Event List pages: Remove dividing line under last item in event list table
	$("#eventListTable td").removeClass("cancelEventListDividerLine");
	$("#eventListTable .isVisible:last td").addClass("cancelEventListDividerLine");
}

function initCalendarEvents() {
	// Event List pages: Remove dividing line under last item in event list table and make it dynamic for the event filter
	$("#eventListTable td").removeClass("cancelEventListDividerLine");
	$("#eventListTable tr").addClass("isVisible");
	$("#eventListTable tr:last td").addClass("cancelEventListDividerLine");
}


function submitMonthDropDown() {
    var url = $("#optMonth option:selected").prop("value");
    if (url != "") {
        location.href = (url);
	}
}

function submitYearDropDown() {
    var url = $("#optYear option:selected").prop("value");
    if (url != "") {
        location.href = (url);
	}
}

function getEvents(url) {
    if (url != "") {
        location.href = (url);
	}
}
