var which = {
	scriptController:function()
	{
		//this function runs first and determines what scripts should be run depending on any pre-requisites
		//If there is a secondard nav within #tabs kick this off
		if($('#tabs ul').length > 0) which.navigationHoverEnhancer();
	},
	
	//Mag 20090226: This function controls the behaviour of switching the enabling or disabling of an element based on a select option value change
	//Arguments: The select's id, the option value that kicks off the switch, the id of the element to be switched
	//Example: which.selectDrivenEnable("problems", "Other", "other-problem") place this in the $(document).ready
	selectDrivenEnable:function(selectId, optionValue, enableId)
	{
		if($('#'+selectId).val()==optionValue) {$('#'+enableId).removeAttr("disabled")} //If the element to be switched is disabled on page load, enable it
		$('#'+selectId).change(function()
			{
				
				if($('#'+selectId).val()==optionValue) {$('#'+enableId).removeAttr("disabled");} //enable
				else //disable
				{
					$('#'+enableId).attr("disabled", "disabled");
					$('p.error[htmlfor='+enableId+']').remove();//Removes the error message that may have occurred
				} 
			}
		);
	},
	showForJS:function()
	{
		$("#page .show-for-js").show();
	},
	//Mag 200903: Adds print page link before the h1 and give the h1 a right padding so both can co-exist peacefully
	printPage:function() 
	{
		if (!document.getElementsByTagName) return;
		
		$("#content h1").attr("id","print-page-positioner").before('<p id="print-page-js"><a href="#">Print this page</a></p>');
		$("#print-page-js a").click( function() { window.print(); return false; } );
	},
	//Mag 200904: Added accesibility for users with mobility issues to delay dissapearance of css hover nav
	enlargeImage:function(enlargerSelector)
	{
		$(enlargerSelector+" a").click(
			function(){
				$("#enlarged-image").remove();//Remove previous enlarged images
				$(this).parent().before('<div id="enlarged-image"><img src="/assets/images/icons/close.png" alt="Close" class="close" /><img src="'+this.href+'" height="400" width="400" alt="" /></div>');
				$("#enlarged-image").click(
					function(){
						$(this).remove();
					}
				);
				return false; //Do not follow href
			}
		);
	}, 
	//Mag 200905: This function performs js tabbed content
	//It takes in 2 arguments: tabSelector is the css selector that identifies tabs content, tabTextSelector is the element that contains the tabs text for each tab
	//Example function call: which.tabController(".tab-container","h2");
	tabController:function(tabSelector, tabTextSelector)
	{
		tabTextSelector = tabSelector+" "+tabTextSelector;
		var firstTabText = $(tabTextSelector).eq(0).text();
		var currentTabText = "";
		
		$(tabSelector).eq(0).before('<ul id="inpage-nav"><li class="on"><a href="#"><span>'+firstTabText+'</span></a></li></ul>');
		$(tabTextSelector).eq(0).addClass("removed");//Remove first heading
		
		if ($(tabSelector).length > 1) {//Check there are two tab containers on the page
			for(i=1;i<=$(tabSelector).length-1;i++) {//Loop through all tabs content after the first one
				$(tabSelector).eq(i).hide();//Hide the tabs
				currentTabText = $(tabTextSelector).eq(i).text();//Get the text heading of the tab
				$(tabTextSelector).eq(i).addClass("removed");;//Hide the original text heading
				$("#inpage-nav").append('<li><a href="#"><span>'+currentTabText+'</span></a></li>');//Add new tab styled version of text heading
			}
		}
		
		$("#inpage-nav li").click(
			function(){
				$(tabSelector).hide();//Hides all tabs
				$("#inpage-nav li.on").removeClass("on");//Remove highlighted tab
				$(this).addClass("on");//Add highlighted tab css
				var showTabIndex = $("#inpage-nav li").index(this);
				$(tabSelector).eq(showTabIndex).show();
				return false; //Do not follow href*/
			}
		);
		
	}, 
	//Mag 200904: Added accesibility for users with mobility issues to delay dissapearance of css hover nav
	navigationHoverEnhancer:function()
	{
		if(!$.browser.opera){//Not working in Opera
			
			//Removes all other delayed hover sub nav displayed
			$("#tabs li").mouseover(
				function(){
					$("#tabs ul").removeClass("js-hover");
				}
			);
			
			//Delays the secondary navs focus on mouseleave to assist users with mobility issues
			$("#tabs ul").bind("mouseleave", 
				function(){
					$(this).addClass("js-hover");
					var t=setTimeout(function(){$("#tabs ul").removeClass("js-hover")},500);
				}
			);
		}
	}, 
	//Reports any Ajax problems via Ajax to be logged by Java
	ajaxLogReporter:function (strMessage, strAjaxURL) {
		var strAjaxLogURL = "service/util/log"
		var jsonLogMessage = '{"logMessage":{"message":"'+strMessage+'","ajaxRequestURL":"'+strAjaxURL+'"}}';
		$.ajax({type: "POST", url: strAjaxLogURL, data: jsonLogMessage, dataType: "json", contentType: "application/json; charset=utf-8"});
	},
	createCookie:function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	deleteCookie:function (name) {
		which.createCookie(name,"",-1);
	}
	//end of which closure
}
var pageInfo = {
	url: location.href,
	
	urlContains:function(value)
	{
		return (pageInfo.url.indexOf(value) > -1) ? true : false;
	}
}
/* ***************************************************************
	The following functions were moved to this file from jquery-specific.jsp
 * *******************************************************************/
//Opens links with rel="external" in a new window
function externalLinks() 
{
	$('a[rel*=external]').live("click", function() {
		window.open(this.href);
		return false;
	});
} 

//Adds js for print page to print text that stays on the page regardless of js on/off
//If a link is purely for 
function printPageLinks() 
{
	if (!document.getElementsByTagName) return;
 	var spans = document.getElementsByTagName("span");
 	
 	for (var i=0; i < spans.length; i++) 
 	{
   		var span = spans[i];
	   	if ($(span).attr("class") == "js-print") 
	   	{
	   		$(span).wrap('<a href="#"></a>');
	   		$(span).parent('a').click( function() { window.print(); return false; } );
	   	}
	 }
}
//Hides for JS version by reference to .js-hide
function hideForJS() 
{
	$('.js-hide').css({ position:"absolute", left:"-99999px" });
} 
//Mag Leahy 20080604: This function goes upwards in seach of element with class of strErrorContainerClass's value
//Arguments: Element that is causing the error
//There is limit of numMaxAscensions to limit steps up. This may need to be revisited. 
var numCounter = 0, numMaxAscensions = 4, strErrorContainerClass = 'er', strErrorElement = 'body';
function findErrorDiv(element){
	while (numCounter < numMaxAscensions){
		numCounter++; 
		var bolErrorHolder = element.hasClass(strErrorContainerClass); 
		if (bolErrorHolder){numCounter = 0; return element;}			
		else element = element.parent();
	}
	return $(strErrorElement); //If there's no match for error placement return default tag. 
}
//Mag Leahy 20080718: JQuery Custom Methods for validation
//http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage
if(jQuery.validator) //Error checks that validation js is on the page
{
	//addMehtod: money checks if the value entered is numeric but allows "," and "£" in their correct use and 2 decimal places (optional).
	jQuery.validator.addMethod("money", function(value, element) {return this.optional(element) || /^£?((\d{1,}(\.\d{1,2})?$)|((\d{1,3}(\,\d{3})*)((\.\d{2})?)$))/.test(value);}, "Please enter a money value (e.g. 100,000.00 or 100000)");
	//addMehtod: moneywhole checks if the value entered is numeric but allows "," and "£" in their correct use but decimal places are not allowed.
	jQuery.validator.addMethod("moneywhole", function(value, element) {return this.optional(element) || /^£?((\d{1,}$)|((\d{1,3}(\,\d{3})*)$))/.test(value);}, "Please enter a whole money value (e.g. 100,000 or 100000)");
	//addMehtod: alphanumeric checks if the value entered is alphanumeric
	jQuery.validator.addMethod("alphanumeric", function(value, element) {return this.optional(element) || /^\w+$/i.test(value);}, "Please enter an alphanumeric value");
}
/* ***************************************************************
	The following functions were moved to this file from tools.js
 * *******************************************************************/
//Mag Leahy 20080604: This function loops though all radio buttons and performs show/hide actions.
//Arguments: Name of the radio button group 
//If a radio button is checked (e.g. #radio1) it shows the corresponding extra information (e.g. radio1-extra).
//All other extra information for non-checked radio buttons is hidden
function radioButtonControl(name){
	$('input[name='+name+']').each(	
	function(){
		
		if($(this).is(":checked")) $('#'+$(this).attr('id')+'-extra').css({ position:"static", left:"auto" });
		else $('#'+$(this).attr('id')+'-extra').css({ position:"absolute", left:"-99999px" });
		
	});
}
//Mag Leahy 20080604: Sorts out show/hide on load, of radio button extra information.
//Adds code to the onclick event
//Arguments: Name of the radio button group 
//Example call: radioClickEvent('buttongroupname');
function radioClickEvent(name){

	radioButtonControl(name);
	$('input[name='+name+']').click(function() {radioButtonControl(name);});
	
}
//Global function calls
$(document).ready(function (){
	which.scriptController();
	which.showForJS();	
	externalLinks();
	printPageLinks(); //Need to replace with new function when compare goes live
	hideForJS();
});