﻿// Fire off page specific init method
$j(document).ready(function() {
	if (typeof initPage == "function"){
		initPage();
	}
	externalLinksInit();
	resize();
});
$j(window).resize(function(){
	resize();});

/* Run these things last otherwise they can't get bound to functions exicuted during initPage
----------------------------------------------- */
function externalLinksInit() {
	$j("a.external").each(function(){
		$j(this).click(function(){return !window.open($j(this).attr("href"));});
	});
};

/* Ajax methods */
function ajaxReplace(href, containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	$j.ajax({
		url: href + "&" + isAjaxParamName + "=true",
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);	}	});}
			
function ajaxEmailSignup(containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	email = $j('#Master_uiEmailSignup_txtEmail').val();
	$j.ajax({
		url: "?email="+ email +"&" + isAjaxParamName + "=true",
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);
		},
		error: function(response){
		    $j(containerId).html(response);	}	});}
		    
/* Postcode ajax method */
function ajaxPostcodeSearch(containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";	
	$j("#loading").show();	
	var urlToUse = '';	
	
	if( jQuery.browser.msie && (jQuery.browser.version < 7) )	{
		//IE6 acts differently and takes the client val div if present into the url so we need to remove it.		
		urlToUse = location.href.replace('#ClientValidationSummary', '').replace('http://www.hillarys.ie', '');								
		var postcode = $j('#Master_MainContentPlaceHolder_ucAddressFinder_Postcode').val();					
                
		if(urlToUse.substr(urlToUse.length-4,4) == ".htm")		{				    
		    $j.ajax({ 
	            url: urlToUse + '?type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true",
		        dataType: "html",
		        success: function(response){
		            $j(containerId).html(response);	}	});		}
		else {//we need to amend the url if it already has a query string  
		    $j.ajax({ 
	            url: urlToUse + '&type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true",
		        dataType: "html",
		        success: function(response){
		            $j(containerId).html(response);	}	});		}   }	    
	else {
	    var postcode = $j('#Master_MainContentPlaceHolder_ucAddressFinder_Postcode').val();				    
		$j.ajax({ 
	        url: urlToUse + '?type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true",
		    dataType: "html",
		    success: function(response){
		        $j(containerId).html(response);	}	});	}}		        		        
		        
/* Ajax postcode select */
function ajaxPostcodeSelect(href, containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	$j("#loading").show();
	$j.ajax({
		url: href + "&" + isAjaxParamName + "=true",
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);
			$j('#Master_MainContentPlaceHolder_ucAddressFinder_fullAddress').show();}	});}
			
function showTab(divToShow, tabToActivate){
	//deactivate all tabs
	$j("#Tabs > ul > li").removeClass("active");

	//hide all tab bodys
	$j("#Tabs > div").hide();

	//show and activate the tab
	$j("#"+ tabToActivate).addClass("active");
	$j("#"+ divToShow).show();}
/* resize detection fuinction */
function resize() { 
	var documentWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		documentWidth = window.innerWidth; /* Non-IE */
	} else if ( document.documentElement && ( document.documentElement.clientWidth ) ) {
		documentWidth = document.documentElement.clientWidth; /* IE 6+ in 'standards compliant mode' */
	} else if( document.body && ( document.body.clientWidth ) ) {
		documentWidth = document.body.clientWidth; /* IE 4 compatible */
	}
	if(documentWidth <= 980){
		$j("body").addClass("lores");
		toggleWords("off");
	}else if (documentWidth > 980){
		$j("body").removeClass("lores");
		toggleWords("on");	}}
function toggleWords(state){
	if(state == "off"){
		$j("#NavigationSecondary ul li a span").hide();
	}else if (state == "on"){
		$j("#NavigationSecondary ul li a span").show();	}}