// JavaScript Document
Date.prototype.dbFormat = function() {
	return this.getFullYear() + '-' +
		(this.getMonth() < 9 ? '0' : '') + (this.getMonth()+1) + '-' +
		(this.getDate() < 10 ? '0' : '') + this.getDate();
};
	
$(function() {

	// Loop over each snippet, adding a click event that redirects to the first link inside it
	$( ".home-panels" ).each(function( intIndex ) {
		var link = $(this).contents().find("a").attr("href");
		$( this ).bind ("click", function(){
				window.location = link;
		});
		$(this).css('cursor','pointer');
	});
	
	// clear any default values when clicked	
	$('.default-value').each(function() {
		$(this).addClass('mute');
			var default_value = this.value;
			$(this).focus(function() {
				$(this).removeClass('mute');
					if(this.value == default_value) {
							this.value = '';
					}
			});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
				$(this).addClass('mute');
			}
		});
	});
	
	// remove x=, y= nonsense from search
	$("#search-form").submit(function() {
		$("#search-button").attr('disabled','disabled');
		return true;
	});
	
	// add colorbox popups
	$("a[rel='lightbox']").colorbox({photo:true, opacity:.3});
	$("a[rel='lightbox-video']").colorbox({photo:true, opacity:.3, iframe:true, innerWidth:600, innerHeight:404 });

	// add tooltips to timetables
	$("table.timetable td strong").mouseover(function() {
		var pos = $(this).offset();
		var left = pos.left;// + $(this).width();
		var top = pos.top + $(this).height() + 7;
		$(this).css('cursor','default');
		$(this).closest("td").find("ul").show().css({ float:'left', top:top, left:left });
	}).mouseout(function() {
		$(this).closest("td").find("ul").hide();
	});
	
	// hide & show school holiday timetables
	var holidays = [
		['2011-02-21','2011-02-27'],
		['2011-04-11','2011-04-25'],
		['2011-05-30','2011-06-03'],
		['2011-07-25','2011-08-31'],
		['2011-10-24','2011-10-30'],
		['2011-12-19','2012-01-02'],
		['2012-02-13','2012-02-19'],
		['2012-04-02','2012-04-15'],
		['2012-06-04','2012-06-10'],
		['2012-07-23','2012-09-03'],
		['2012-10-29','2012-11-04'],
		['2013-02-18','2013-02-24'],
		['2013-03-29','2013-04-14'],
		['2013-05-27','2013-06-02'],
		['2013-07-25','2013-09-04']
	];
	
	var date = new Date();
	var curDate = date.dbFormat();
	var sectionToHide = 'school-holiday-times';
	
	for (var i in holidays) {
		if (curDate >= holidays[i][0] && curDate <= holidays[i][1]) sectionToHide = 'term-time-times';
	}
	$('#'+sectionToHide).hide();

	
});
