$(function() {
		   
	js_on(); // Set class on body tag to say 'javascript is available'
	//ie7fix(); // If browser is IE7, fix max-width bug
	my_search_toolbar(); // functions for my-search toolbar
	IE6alert();
	createTextResizeAlert(); // Set up page for text resize events
	resizeDivs(); // Run initial resize on specific divs
	$(window).resize( function() { resizeDivs(); }); // Set up window resize event
	Func1Delay(); // run message box scripts after linky.js has run
	search_options_init() // hide sub menus
	search_options(); // add new search page functionality
		   
});



/*	FUNCTIONS FOR QUESTIONNAIRE MESSAGE BOX 
	To remove (archive) after questionnaire finished */
	
	// function to avoid linky.js rewrite
	function Func1Delay()
	{
	setTimeout('checkDate("July 16, 2011")', 1000);
	}


	function checkDate(dateVar) {
		var today = new Date();
		var cutoff = new Date(dateVar);
		diff = cutoff - today;
		var days = Math.round(diff/(1000*60*60*24));
		//alert (days);
		if (diff > 0) { addMessageBox(); }
	}
	
	function addMessageBox() {	
		markup = '<div id="message-box-container">'
		+ '<div id="message-box">'
		+ '<div class="comp2">'
		+ '<p><a href="http://www.surveymonkey.com/s/2WQ96XY" target="_blank">Take our website survey</a> for a chance to win one of two &pound;25 book token prizes</p>'
		+ '</div></div></div>';
		//alert ('hello');
		$("body").prepend(markup);
	};

        
                      
                      	
                        	
						






// CHECK FOR IE6

function IE6alert() {
	if($.browser.msie && parseInt($.browser.version) == 6) {
		$("body").prepend("<div id='ie6alert'>This website no longer supports Internet Explorer 6. <a href='http://www.heacademy.ac.uk/browsers'>Click here for more information</a>. </div>");
	}
}

// RESIZE FUNCTIONS...

function ie7fix() {	
	if($.browser.msie && parseInt($.browser.version) == 7) {//	Only run if browser is IE7
		
		//	SET VARIABLES
		$window_width = $(window).width(); // e.g. 1899px
		$current_em = $(".hasTextResized").width()/2; // e.g. 12px
		$window_width = $window_width/$current_em;
		
		if ($window_width < 77) {	// 77 seems to work. 			
			$(".ie7fix").css("width", $window_width - 6.5 + "em");
		} else {
			$targ_size = "77em";
			$(".ie7fix").css("width", $targ_size); // This replaces max-width with a fixed width. Boo hiss IE7!
		};
	}; 
};


function resizeDivs() {
	
	if ($('.resize').length) { // check if resize elements exist on page

	$('.resize').each(
		function () {
			//	RESET HEIGHT ON TARGET
			targInner = $(this).children('.comp2'); // target on inner div of target
			targInner.css('height','auto');
		});
	
	$('.resize').each(
		function () {
			//	SET VARIABLES
			targParent = $(this).closest('.resize_parent');
			targOuter = $(this);
			targInner = $(this).children('.comp2');			
			
			// GET PARENT OFFSET FROM TOP
			parentOffset = targParent.offset().top + targParent.innerHeight();
			
			//	GET 'THIS' OFFSET FROM TOP
			thisOffset = targOuter.offset().top + targOuter.height();
			
			//	WHAT IS DIFFERENCE?
			diff = parentOffset - thisOffset;
			diff = Math.floor(diff);
						
			//	INCREASE INNER DIV BY DIFFERENCE
			//	Troubleshooting: padding or margin on target.comp2 or parent can cause probs.
			targPadding = targOuter.height() - targInner.height();
			newHeight = targOuter.height() + diff - targPadding  + "px";
			targInner.css('height', newHeight);			
		}	 			 
	)
};
};

function returnOffset(t) {
	value = $(t).offset().top; // value = distance from top of document
	value += $(t).outerHeight(); // value += target height = target bottom distance from document top
	return Math.floor(value);	
};

/*	CREATE THE TEXT-RESIZE ALERT */
function createTextResizeAlert() {
	$("body").append(
	"<span class='hasTextResized' style='float:left; background: orange;" 
	+ "width: 2em; height: 2em; position: fixed; margin-left: -5000em;' >x</span>");

	$(".hasTextResized").resize(function(e){	//resize event from plugin.	
		resizeDivs ();
	});
};


function js_on() {
	$("body").addClass("js_on");// allows different styling for browsers with js on and off.
};

/*	Functions for New Search */
function search_options_init() {
	if ($("#search-options").length) {
		$("#search-options .sr-switch")
		//.attr("tabindex","0")
		.next(".sr-toggle").toggleClass("sr-hidden");
	};
};

function search_options() {
	// check search options exists
	if ($("#search-options").length) {

		$("#search-options").delegate(".sr-switch", "click", function(e){
			search_options_toggle($(this));
			//event.preventDefault();
			//return false;
			e.preventDefault();
			
		});
	};
};
/* support function for above */
function search_options_toggle($this) {
	
	//alert("Search Options Toggle!");
	//$this.parent().toggleClass("sr-hidden").toggleClass("sr-open");
	//alert ("yay!");
	$this.next(".sr-toggle").toggleClass("sr-hidden").toggleClass("sr-open");
	
	}

/* My-search-toolbar functions */

	function my_search_toolbar() {
			if ($("#my-academy-bar").length) {
				// add class to body to activate styling needed
				$("body").addClass("my-search-bar-on");
				
				//move toolbar to inside of body tag
				$("body").prepend($("#my-academy-bar"));
				
			};
	}
