var scheduleLoaded = new Array();
scheduleLoaded["open"] = false;
scheduleLoaded["sunday"] = false;
scheduleLoaded["monday"] = false;
scheduleLoaded["tuesday"] = false;
scheduleLoaded["wednesday"] = false;
scheduleLoaded["thursday"] = false;
scheduleLoaded["friday"] = false;
scheduleLoaded["saturday"] = false;
	
$(document).ready(function(){  
  
	// Dropdown Menu
	$('ul.top_nav li').hover(function() {
			//show its submenu
			$('ul', this).fadeIn(150);

		}, 
		function() {
			//hide its submenu
			$('ul', this).fadeOut(150);			
	});
	
	
	// Schedule
	$('h3.dayHeader').click(function(){
		$('#calBody').html($('#'+$(this).attr('id')+"-content").html());
		$('.activeDay').removeClass('activeDay');
		$(this).addClass('activeDay');
	});
	
	
	// Slideshow
	
	
	// Schedule
	$('div.event_entry').live('click',function(e){
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		$('#schedule_tt').css({
			'top': posy+'px',
			'left': posx+'px'
		});
		var tempData = '<h1>Loading information...<img src="'+window.documentRoot+'img/spinner.gif" /></h1>';
		$('#tt_content').html(tempData);
		$('#schedule_tt').fadeIn();
		$.get(window.documentRoot+"ajax/calitem/"+$(this).find('p.event_title').attr('id'), function(data){
			$('#tt_content').html(data);
		});	
		$('.selectedEvent').removeClass('selectedEvent');
		$(this).addClass('selectedEvent');
	});
	
	
	$(document).click(function(e){
		if($('#schedule_tt').length>0){
			if($('#schedule_tt').css('display')!='none'){
				var targ;
				if (!e) var e = window.event;
				if (e.target) targ = e.target;
				else if (e.srcElement) targ = e.srcElement;
				if(!$(targ).hasClass('event_entry') && !$(targ).parent().hasClass('event_entry')){
					$('#schedule_tt').fadeOut();
					$('.selectedEvent').removeClass('selectedEvent');
				}
			}
		}
	});
	
	$('#ageRange').change(function(){
		$("#filterLoading").show();
		$.get(window.documentRoot+"ajax/filter/"+$(".activeDay").attr('id')+"/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('open',data);});
		$.get(window.documentRoot+"ajax/filter/sunday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('sunday',data);});
		$.get(window.documentRoot+"ajax/filter/monday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('monday',data);});
		$.get(window.documentRoot+"ajax/filter/tuesday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('tuesday',data);});
		$.get(window.documentRoot+"ajax/filter/wednesday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('wednesday',data);});
		$.get(window.documentRoot+"ajax/filter/thursday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('thursday',data);});
		$.get(window.documentRoot+"ajax/filter/friday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('friday',data);});
		$.get(window.documentRoot+"ajax/filter/saturday/"+$(this).val()+"/"+$("#filterText").val(), function(data){doneLoading('saturday',data);});
	});
	
	$("#filterText").keyup(function(){
		$("#filterLoading").show();
		var keyString = $(this).val();
		keyString = keyString.replace(/ /g, '_');
		$.get(window.documentRoot+"ajax/filter/"+$(".activeDay").attr('id')+"/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('open',data);});
		$.get(window.documentRoot+"ajax/filter/sunday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('sunday',data);});
		$.get(window.documentRoot+"ajax/filter/monday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('monday',data);});
		$.get(window.documentRoot+"ajax/filter/tuesday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('tuesday',data);});
		$.get(window.documentRoot+"ajax/filter/wednesday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('wednesday',data);});
		$.get(window.documentRoot+"ajax/filter/thursday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('thursday',data);});
		$.get(window.documentRoot+"ajax/filter/friday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('friday',data);});
		$.get(window.documentRoot+"ajax/filter/saturday/"+$("#ageRange").val()+"/"+keyString, function(data){doneLoading('saturday',data);});
	});
	
	
});


function ieTour(page){
	if(page == 'tour'){
		alert("The virtual tour is not viewable in your current browser (Internet Explorer 6).  Please upgrade to a new version of Internet Explorer to see this feature.");
	}
}

function doneLoading(day,data){
	var parsedFilterString = parseFilterString(data);
	if(parsedFilterString[0] == $("#filterText").val()){
		if(day=="open"){
			$("#calBody").html(parsedFilterString[1]);
		}
		else{
			$("#"+day+"-content").html(parsedFilterString[1]);
		}
		var scObj = window.scheduleLoaded;
		scObj[day] = true;
		var numLoaded = 0;
		if(scObj['open']){numLoaded++;}
		if(scObj['sunday']){numLoaded++;}
		if(scObj['monday']){numLoaded++;}
		if(scObj['tuesday']){numLoaded++;}
		if(scObj['wednesday']){numLoaded++;}
		if(scObj['thursday']){numLoaded++;}
		if(scObj['friday']){numLoaded++;}
		if(scObj['saturday']){numLoaded++;}
		
		if(numLoaded == 8){
			$("#filterLoading").hide();
		}
	}
}

function parseFilterString(data){
	var returnArray = new Array();
	var pos = data.indexOf("~", 0);
	returnArray[0] = data.substring(0,pos);
	returnArray[1] = data.substring(pos+1);
	return returnArray;
}

