$(document).ready(function(){
	$('#jsMessage').hide();
	$("ul.sf-menu").superfish({firstOnClick: true});
	var totalCalendars = 10;
	//begin load categories from cookies if set
	$(".catToggle").each(function(){
		var name = $(this).attr("name");
		if( $.cookie( name ) == "true" )
		{
			$("." + name).show();
			$("#catAll_toggle").removeAttr("checked");
			$(this).attr('checked', true);
		}
		else
		{
			$("." + name).hide();
		}
	});
	
	if ( $(".catToggle").size() == 1 )
	{
		$(".cat1").show();
		$("#cat1_toggle").attr("checked" , true);
	}
	
	if ( $("#catAll_toggle").attr("checked") )
	{
		$("div.event").show();
	}
	
	toggleListDayTitle();
	//end load categories from cookies

	//Begin check if calendars are selected
	if (  $.query.get('view') == "select" )
	{
		var calendars;
		if ( $.query.get('calendars') != "" )
		{
			calendars = $.query.get('calendars') + ",";
		}
		else
		{
			calendars = "73,71,76,72,75,78,77,74";
		}
		
		jQuery.each( calendars.split(',') , function() {
		      $("#cb" + this).attr("checked","checked")
		});	

		if ( $("input:checkbox:checked").length >= totalCalendars )
		{
			$("input:checkbox").each(function(){
				if ( $(this).attr("checked") == false )
				{
					$(this).attr("disabled","disabled");
				}
			});
		}
	}
	
	$('#catAll_toggle').click(function(){
		if (this.checked){
			$("div.event").show();
			$(".catToggle").removeAttr("checked");
			//clear all set category cookies
			$(".catToggle").each(function(){
				var name = $(this).attr("name");
				$.cookie( name , null );
			});
		}
		else{
			$("div.event").hide();
		}
		
		toggleListDayTitle();
	});
	
	$(".catToggle").click(function(){
		$(".catToggle").each(function(){
			var name = $(this).attr("name");
			if($(this).attr("checked"))
			{
				$("." + name).show();
				$("#catAll_toggle").removeAttr("checked");
				$.cookie( name , 'true' );
			}
			else
			{
				$("." + name).hide();
				$.cookie( name , null );
			}
		});
		
		toggleListDayTitle();
		
	});
	
	$('.calendarCheckBox').click(function(){
		if ( $("input:checkbox:checked").length >= totalCalendars )
		{
			$("input:checkbox").each(function(){
				if ( $(this).attr("checked") == false )
				{
					$(this).attr("disabled","disabled");
				}
			});
		}
		else
		{
			$("input:checkbox").each(function(){
				$(this).removeAttr("disabled");
			});		
		}
	});
	
	$('#calendarSubmit').click(function(){
		var calendarIDs = "";
		$("input:checkbox:checked").each(function( index ){
			if ( $(this).attr("checked") )
			{
				calendarIDs += $(this).attr("value");
				if ( index < $("input:checkbox:checked").length - 1 )
				{
					calendarIDs += ",";
				}
			}
		});
		var url = "index.aspx?calendars=" + calendarIDs;
		location.href = url;
		
	});
	
	$('#calendarReset').click(function(){
			$("input:checkbox").each(function(){
				$(this).removeAttr("disabled");
			});	
	});
	
	function toggleListDayTitle()
	{
		$(".listingDay").each(function(index){
			var cssClass =  $(this).attr("class") ;
			if ( $( "." +  cssClass + ":eq(" + index + ") > .event:visible" ).length == 0 )
			{
				$( "." +  cssClass + ":eq(" + index + ") > .listingDayTitle" ).hide();
			}
			else
			{
				$( "." +  cssClass + ":eq(" + index + ") > .listingDayTitle" ).show();
			}
		});
	}
	
});