$(document).ready(function() {
	//Usage: just pass the hstr parameter in the url; e.g. lib.uchicago.edu/hours/whereverweputthem/index.php?hstr=cehlrs
	//c closes crerar, e closes eckhart, h harper, l law, r regenstein, s ssa. All can be reopened once the hours are displayed - this is just default behavior.
	//order of letters in hstr doesn't matter - we're just doing an indexOf search (cehlrs = chlrse = lrechs = ...)
	//empty hstr param opens all library hours
	
	$('#hourscontainer').show(); //only show if user has js 
	
	var hidestr = $.url.param("hstr");
	
	var isVisible = {	"crerar_data":(hidestr.indexOf('c') == -1) ? true : false,
						"eck_data":(hidestr.indexOf('e') == -1) ? true : false,
						"harper_data":(hidestr.indexOf('h') == -1) ? true : false,
						"law_data":(hidestr.indexOf('l') == -1) ? true : false,
						"reg_data":(hidestr.indexOf('r') == -1) ? true : false,
						"ssa_data":(hidestr.indexOf('s') == -1) ? true : false
					};
	
	function onRefresh(data) {
		$('#hours').html($(data)); //grab returned html
			
		$('#hours').fadeTo('fast',1); //fade in hours table
		$('#loader').remove(); //remove loader image
		
		//hide some of the library hours by default
			$('.subtitle').each(function(){
				//alert($(this).parent().nextAll('tbody[id!=""]').attr('id'));
				if(!isVisible[$(this).parent().next().attr('id')]) {
					$(this).parent().next().hide();
					$(this).css('backgroundPosition','0 0');
				}
			});
		
		//hide/close on subtitle click
		$('.subtitle').click(function(){
			$nextSection = $(this).parent().next();
			if($nextSection.css("display") == "none") {
				$nextSection.show().fadeTo("fast", 1);
				$(this).css('backgroundPosition','0 -23px'); // minus
				isVisible[$(this).parent().next().attr('id')] = true; //sets (crerar/eck/harper/law/reg/ssa)_data value in isVisible obj
			} else {
				$nextSection.fadeTo("fast", 0, function(){$(this).hide();});
				$(this).css('backgroundPosition','0 0'); // plus
				isVisible[$(this).parent().next().attr('id')] = false; //set (crerar/eck/harper/law/reg/ssa)_data value in isVisible obj
			}
		});
		
		//zebra
		$('.subtitle').parent().next().children(':not(.specialnotes)').each(function(){
			($(this).parent().children().index($(this)) % 2 == 0) ? $(this).addClass('z_even') : $(this).addClass('z_odd');
		});
		
		
		//paint today column
		var col = $('.days td').index($('.today'));
		$('tr:not(.specialnotes):not(.subtitle)').each(function(){ //set all column
				$(this).children('td:eq('+col+')').addClass('today_column');
		});
		
		$('.hours_footer:not(:has(p))').hide(); //hides special notes divs that do not contain p tags (i.e. empty includes)
		
	}

	function ajaxCall(d,m,y) {
		$('#hours').fadeTo('fast',.5); //fade out hours table
		$('#chooseDateForm > fieldset').append('<img src="loading.gif" alt="loading" style="display:inline; float:left; margin-left:2px; margin-top:6px;" id="loader" />');
		
		(d) ? ajaxData = "day_select="+d+"&month_select="+m+"&year_select="+y+"&submitted=1" : ajaxData = "";
		$.ajax({ 
			type: "POST",
			cache: false,
			data: ajaxData,
			url: "hrv2.php",
			success: function(data) {
				onRefresh(data);
			}
		});
	}
	
	ajaxCall();
	
	now = new Date();
	
	$('.date-pick').datePicker({startDate:'01/01/2008'}).val(now.asString()).trigger('change');
	$('.date-pick').bind( //ajaxCall when date is updated
		'dateSelected',
		function(e, selectedDate, $td) {
			now = selectedDate;
			day = now.asString().split('/')[1];
			month = now.asString().split('/')[0];
			year = now.asString().split('/')[2];
			ajaxCall(day,month,year);
	});
		
	$('#next').click(function(){
		now = now.addDays(7);
		$('.date-pick').dpSetSelected(now.asString()); //changes date-pick, which is bound to ajaxCall
	});
	
	$('#prev').click(function(){
		now = now.addDays(-7);
		$('.date-pick').dpSetSelected(now.asString()); //changes date-pick, which is bound to ajaxCall
	});
	
	$('#chooseDateForm').submit(function(){ 
		inputstr = $('.date-pick').val();
		now = Date.fromString(inputstr); //set now Date obj based on input value
		day = inputstr.split('/')[1]; //find d/m/y for ajaxCall
		month = inputstr.split('/')[0];
		year = inputstr.split('/')[2];
		if (year.length == 2) year = "20"+year;
		ajaxCall(day,month,year);
		return false; //don't submit form
	});
	
});