$(document).ready(function() {

	$('span#home_call_out_link').html('<a href="#" class="home_call_out">&nbsp;</a>');

	$('.home_call_out').click(function(){
		if ( $('#layer').is(':visible') )
		{
			 $("#layer").slideUp("fast");
		}
		else
		{
			 $("#layer").slideDown("fast");
		}
		return false;
    });

	$(document).click(function(event) {
		var bLayerCheck = false;

		if ( event.target.id == "layer" )
		{
			bLayerCheck = true;
		}

		$(event.target).parents().each(function (i) {
			if ( this.id == "layer" )
			{
				bLayerCheck = true;
			}
		});

		if ( bLayerCheck == false )  {
			 $("#layer").slideUp("fast");
		}
	});

	$(document).keyup(function(event) {
		if ( ( event.keyCode == 27 ) && ( $('#layer').is(':visible') ) )
		{
			 $("#layer").slideUp("fast");
		}
	});

	/* Initialise the footer
	Function: /common/footer/footer.js
	----------------------------------------------*/
	initialiseFooter();

		
	/* --------------------------------------------
	- ACCORDIAN INITIALISE
	----------------------------------------------*/	
	
	var controlIndex=new Array();
	var contentIndex=new Array();
	
	/* Initialise all Collapseable elements
	----------------------------------------------*/
	$('.collapseContent').hide();
	$('.collapseControl').addClass("accordianClosed");
	$('.collapseControlOpen').addClass("accordianOpen");
	
	/* Loop through each "control" block and push index to an array
	----------------------------------------------*/	
	$(".collapseControl,.collapseControlOpen").each(function (i) {
		controlIndex.push($("*").index(this))	
	});

	/* Loop through each "content" block and push index to an array
	---------------------------------------------------------------*/		
	$(".collapseContent, .collapseContentOpen").each(function (i) {
		contentIndex.push($("*").index(this))		
	});
	
	/* Loop through each "content" block
	----------------------------------------------*/	
	$(".collapseContent, .collapseContentOpen").each(function (i) {
		
		//Loop through the array of "header" indexes
		for(x=0; x<=controlIndex.length; x++)
		{
			//If we are at the last position in the array
			if(x == controlIndex.length - 1)
			{
				if($("*").index(this) > controlIndex[x] )
				{
					if($(this).get(0).tagName == "LI")
					{
						$(this).removeClass("collapseContent");
						$(this).show();
						$(this).parent().addClass("collapseContentID" + x);
						$(this).parent().hide();
						
					}
					else
					{
						$(this).addClass("collapseContentID" + x);
					}
				}//End IF Statement
			}
			else{
				//determine where in the array we are
				if($("*").index(this) > controlIndex[x] && $("*").index(this) < controlIndex[x+1])
				{
					if($(this).get(0).tagName == "LI")
					{
						$(this).removeClass("collapseContent");
						$(this).show();
						$(this).parent().addClass("collapseContentID" + x);
						$(this).parent().hide();
					}
					else
					{
						$(this).addClass("collapseContentID" + x);
					}
					
				}//End IF Statement
			}//END IF Statement		
		}//END For Loop

	});
	
	/* Wrap the elements in an Outer DIV
	----------------------------------------------*/	
	$(".collapseControl, .collapseControlOpen").each(function (i) {
		$(this).before("<div class='accordianWrapper' id='accordianWrapper"+i+"'></div>")
		$(this).appendTo("#accordianWrapper" + i);
		$(".collapseContentID" + i).appendTo("#accordianWrapper" + i);
	});

	/* Loop through any Open content blocks
	----------------------------------------------*/	
	$(".collapseContentOpen").each(function (i) {
		if($(this).parent().get(0).tagName == "UL")
		{
			$(this).parent().show();
		}//END IF STATEMENT
		$(this).parent().css("background-color","#F5F5FC");
	});

	/* --------------------------------------------
	- ACCORDIAN EVENTS
	----------------------------------------------*/
	
	/* Hide/Show Elements
	----------------------------------------------*/	
	$('.collapseControl, .collapseControlOpen').click(function(){
	
		var indexOfThis = $(".collapseControl, .collapseControlOpen").index(this);
		
		
		if($(".collapseContentID" + indexOfThis).is(":visible")) {
			//...hide it
			$(".collapseContentID" + indexOfThis).hide("fast");
			$(this).removeClass("accordianOpen");
			$(this).addClass("accordianClosed");
			$(this).parent().css("background-color","#373ABE");
		}
		else {
			//...show it
			$(".collapseContentID" + indexOfThis).show("fast");
			$(this).removeClass("accordianClosed");
			$(this).addClass("accordianOpen");
			$(this).parent().css("background-color","#F5F5FC");
		}		
	
	});//END onClick of ".collapseControl"
	
 });
 
/*
# initialiseFooter()
# =============================
# function called from /common/includes/script.asp
#
# used to initialise the footer across halifax.co.uk
#
*/
function initialiseFooter()
{
	
	/* Display the +/- icon when JS is enabled.
	----------------------------------------*/	
	$('#exploreheader').css("background","url(/common/images/footer/collapse.gif) no-repeat center left");
	$('#exploreheader').css("padding","5px 0 5px 26px");
	$('#exploreheader').css("cursor","pointer");
	
	/* Determine whether to hide or show the footer on page load.
	-------------------------------------------------------------*/
		
		ftGlobalStatus = $("#globalFooterStatus").val();
		
		footerCookie = getCookie('halifaxFooter');

		/*If Footer cookie exists then set status from that
		---------------------------------------------------*/
		if (footerCookie!=null && footerCookie!="")
		{
			
			if (footerCookie == "footerState=closed"){
				$("#footer_link_panel_container").hide();
				$("#exploreheader").css("background","url(/common/images/footer/expand.gif) no-repeat center left");
			}
			else{
				$("#footer_link_panel_container").show();
			}//END IF STATEMENT
		}//END IF STATEMENT

		/* Set status from global setting
		---------------------------------------------------*/	
		else
		{
			if(ftGlobalStatus == "open"){
				$("#footer_link_panel_container").show();
			}
			else{
				$("#footer_link_panel_container").hide();
				$("#exploreheader").css("background","url(/common/images/footer/expand.gif) no-repeat center left");
			}//END IF STATEMENT
		}//END IF STATEMENT
	
 
	/* On Click of +/-
	----------------------------------------*/
	$('#exploreheader').click(function() {
		
		if($('#footer_link_panel_container').is(':visible')){
			
      			$("#footer_link_panel_container").hide("slow");
			$("#exploreheader").css("background","url(/common/images/footer/expand.gif) no-repeat center left");
			setFooterCookie("halifaxFooter","closed","90");
		}
		else{
			
			$("#footer_link_panel_container").toggle();
			$('html, body').animate({scrollTop: $(document).height()+'px'}, 280);
			$("#exploreheader").css("background","url(/common/images/footer/collapse.gif) no-repeat center left");
			setFooterCookie("halifaxFooter","open","90");
		}
	
	});//END onClick Function
    
}//END FUNCTION

/*----------------------------------------
- FUNCTION: getCookie
------------------------------------------*/
function getCookie(c_name)
{
	
	if (document.cookie.length > 0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		
		//alert(document.cookie)
		
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length+1;
			//alert("c_start: " + c_start);
			c_end = document.cookie.indexOf(";",c_start);
			//alert("c_end: " + c_end);
			if (c_end==-1)
			{
				c_end=document.cookie.length;
			}//END IF STATEMENT
			
			return unescape(document.cookie.substring(c_start,c_end));
		
		}//END IF STATEMENT
		
	}//END IF STATEMENT
	
	return "";
}//END FUNCTION


/*----------------------------------------
- FUNCTION: setFooterCookie
-	Sets the value of the cookie everytime a user expands or collapses the footer.
-
- 	c_name: 	Cookie Name	"halifaxFooter"
- 	status: 	Status	 	"open/closed"
- 	expiredays: 	Expiry		90 days
------------------------------------------*/
function setFooterCookie(c_name,status,expiredays)
{
	//Calculate new date
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);

	//Set Cookie	
	document.cookie = "halifaxFooter=footerState=" + status + ((expiredays==null) ? "" : ";path=/;expires="+exdate.toUTCString());
	
}//END FUNCTION

