/*
 * core.js 
 * all jquery calls called from here
 */



$(document).ready(wireUpSearchForms).ready(primeTextBoxes).ready(validatePostCodeSearch).ready(hideDisabledLinks).ready(wireUpInterestAreas).ready(wireUpCards);

function positionAreasOfInterest(){
	$('.interestAreasHolder').positionFooter();
}

function hideDisabledLinks(){
	$('a[@disabled=true]').remove();
}

/*
* for top right search box and searchbox on searchresults page 
* stop searches when no search terms
*
*/
function wireUpSearchForms(){
	$('#topsearch').submit(function(){
		if($('#search').val().length==0){
			alert("Please enter search term");
			return false;
		}	
	});


	$('#searchPageSearchForm').submit(function(){
		if($('#searchPageSearchForm input#search').val().length==0){
			alert("Please enter search term");
			return false;			
		}		
	})
}

//due to safari bug this is called in body tag
function fixCols(){

	$('.interest-header').equalizeCols();	
	$("#header-1,#header-2,#header-3").equalizeCols();
	//$("#infobox-1,#infobox-2,#infobox-3").equalizeCols();

}

function clearBlankULs(){
 $('ul:empty').hide();
}

function fixIe6Hovers(){
	$.ie6CssFix();
}

function validatePostCodeSearch(){

var postCodeReg = new RegExp("^ ?(([BEGLMNSWbeglmnsw][0-9][0-9]?)|(([A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9]?)|(([ENWenw][0-9][A-HJKSTUWa-hjkstuw])|([ENWenw][A-HK-Ya-hk-y][0-9][ABEHMNPRVWXYabehmnprvwxy])))) ?[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$","i"); 

	$('#locationFinder #go').click(function(){
		if($('#postCode option:selected').val()==""){
			alert("Please select location");
			return false;
		}
		else if($('#locationFinder #q').val()=="" || $('#locationFinder #q').val()=="Enter your postcode"){
			alert("Please enter your location")
			return false;
		}
		else if($('#locationFinder #q').val().match(postCodeReg)==null){
			alert("Please enter valid post code");
			return false;
		}
		else{
		    if($('#postCode option:selected').val()!=null){
		     var splitArray = $('#postCode option:selected').val().split("|");
		     $('#id').val(splitArray[1]);
		     $('#postCode option:selected').val(splitArray[0]) ;
		    }
		     return true;
		}
	});
}

function primeTextBoxes(){
	$("input:text, textarea, input:password").each(function(){
		if(this.value == ''){
			this.value = this.title;
		}
	});
	$("input:text, textarea, input:password").focus(function(){
		if(this.value == this.title){
			this.value = '';
		}
	});
	$("input:text, textarea, input:password").blur(function(){
		if(this.value == ''){
			this.value = this.title;
		}
	});
	$("input:image, input:button, input:submit").click(function(){
		$(this.form.elements).each(function(){
			if(this.type =='text' || this.type =='textarea' || this.type =='password' ){
				if(this.value == this.title && this.title != ''){
					this.value='';
				}
			}
		});
	});

}
/*
loops through interest areas and wires up image hover 
so that the corresponding link goes into hover state
*/
function wireUpInterestAreas(){
var styleHover = "color:#B50A47;text-decoration:underline;";
var styleOut = "color:#999999;text-decoration:none;";

	$('.imageHolder a img').each(function(index){
		var xpath = "a.title:eq(" + index +")";
		$(this).mouseover(function(){	
			$(xpath).attr("style",styleHover);
		});

		$(this).mouseout(function(){
			$(xpath).attr("style",styleOut);
		});
	});

	$('div.interest-header a.title').each(function(index){
		$(this).mouseover(function(){	
			$(this).removeAttr("style");
		});
		$(this).mouseout(function(){	
			$(this).removeAttr("style");
		});
	});
}
/*
@ used to get hover effect on link button when card image is hovered over
*/
function wireUpCards(){
var styleLow = "background:transparent url(/images/higgs_arrow.png) no-repeat scroll right center;border:0.1em solid #999999;color:#666666;";

var styleHigh = "background:#B50A47 url(/images/higgs_arrow-hi.png) no-repeat scroll right center;border:0.1em solid #B50A47;color:#FFFFFF;";

	$('.link-image').mouseover(function(){
		$('.jq-card-link').attr("style",styleHigh);
	});

	$('.link-image').mouseout(function(){
		$('.jq-card-link').attr("style",styleLow);
	});

	$('.jq-card-link').mouseover(function(){	
			$(this).removeAttr("style");
	});

	$('.jq-card-link').mouseout(function(){	
			$(this).removeAttr("style");
	});
}

function fixFlicker(){
	//if($.browser.msie && $.browser.version=="6.0"){
		//var t = $('div.interest-header a.title:first');
		$('div.interest-header a.title:first').triggerHandler('mouseover');
		//$('div.interest-header a.title:first').trigger('mouseout');
	//}
}

