/**
 * @author mmoloksher
 */

$(document).ready(function(){
	
	var subMenuHeight_opened = "100px";
	var subMenuHeight_closed = "20px";
	var animationSpeed = 800;
	var counter = 0;
	var closedStateSizes = new Array();
	openStateSizes = [70,128,80,119,103,73];
	
	if(isSpanish!=undefined) {
		if(isSpanish) {
			openStateSizes = [145,150,125,117,71,73];
		}
	}
	
	//Initiates Cufon
	
	initCufon();
	
	//Checks to see if the sub nav is present, if it is, centeres it
	if ($('#subnav_floatinit').length != 0) 
	{
		var subnavWidth = $("#subnav_floatinit").width()+10;
		$("#center_subnav").css({'width' : subnavWidth });
		$("#subnav_floatinit").show();
	}
	
	//Checks to see if its a news room page and if it displays animation
	if ($('#newsticker_content').length != 0) 
	{
		$('#newsticker_content').cycle({
			fx: 'fade',
			speed: '500',
			timeout: '5000'
		});
	}
	
	//Checks to see if its the homepage and if it is, displays homepage animation
	if ($('#hp_showcase').length != 0) 
	{
		$('#hp_showcase').cycle({
			fx: 'fade',
			speed: '1000',
			timeout: '4000'
		});
	}
	
	//Checks to see if the gallery div is availabe and if it is, displays gallery animation
	if ($('.gallery').length != 0) 
	{
		$('.gallery > p:first-child').cycle({
			fx: 'fade',
			speed: '500',
			timeout: '5000',
			pager:  '.gallery_nav'
		});
	}	
	
	
	//Assigned footer columns all right classes
	initiateFooterColumns();
	
	//Executes when the #footer_nav_button is clicked
	$("#footer_nav_button").click(function(){ 
			
		//Executes if the footer menu is closed
		if ($(this).attr("class") == "footer_closed") 
		{
			$(this).removeClass("footer_closed");
			$(this).addClass("footer_opened");
			
			var FCcount = 0;
			
			//Traverses through all children that are DL
			
			$("#footer_mainNavs").children("dl").each(function(){
										
				//Calls the function to expand
				expandColumn(".footer_column_"+FCcount, openStateSizes[FCcount]);
				
				//Increments the count
				FCcount++;							    
			 });			
						
			/*Pushes the RHS logo down to 106px
			$("#footer_rhs_logo").animate({
				marginTop: "106px"
			}, animationSpeed); */
			
			$("#footer_rhs_logo span").fadeIn();
		}
		//Executes if the footer menu is open
		else 
		{ 
			$(this).removeClass("footer_opened");
			$(this).addClass("footer_closed");
			
			var FCcount = 0;
			
			//Traverses through each child that is DL
			$("#footer_mainNavs").children("dl").each(function(){
				
				//Calls on a function to collaps all DL's
				collapseColumns(".footer_column_"+FCcount, closedStateSizes[FCcount]);				
				
				//Increments the count 
				FCcount++;							    
			});		
			
			//Pushes the RHS logo up to 12px from 106px
			$("#footer_rhs_logo").animate({
				marginTop: "12px"
			}, animationSpeed);
			
			$("#footer_rhs_logo span").fadeOut();
		}
	 
	 
	 }); // end of $("#footer_nav_button").click()
	 
	
/*
	NEWSROOM JS
*/
	$(".newsroom dl h2:first").addClass("active").parent().parent().next().slideDown();
	$(".newsroom dl h2").toggle(
		function()
		{				
			if ($(this).attr("class") == "active") 
			{
				$(this).removeClass("active");
				$(this).parent().parent().next().slideUp();
			}
			else 
			{
				$(this).addClass("active");
				$(this).parent().parent().next().slideDown();
			}			
		}, 
		function () 
		{	
			$(this).removeClass("active");
			$(this).parent().parent().next().slideUp();				
		}
	);
	 
/*
	END OF NEWSROOM JS
*/	
/*
	SR MANAGEMENT JS
*/
	//$(".srmanagement dl h3:first").addClass("active").parent().parent().next().slideDown();
	$(".srmanagement dl h3").toggle(
		function()
		{				
			if ($(this).attr("class") == "active") 
			{
				$(this).removeClass("active");
				$(this).parent().parent().next().slideUp();
			}
			else 
			{
				$(this).addClass("active");
				$(this).parent().parent().next().slideDown();
			}			
		}, 
		function () 
		{	
			$(this).removeClass("active");
			$(this).parent().parent().next().slideUp();				
		}
	);
	 
/*
	END OF SR MANAGEMENTJS
*/	
/*
	SEARCH BOX
*/	
	$("#search_button_off").click(
		function()
		{		
			$("#search_button_off").hide();
			$(".searchText").fadeIn("fast");
			$(".search_button").fadeIn("fast");	
		}
	);
	$('#hp_header, #header').click(
		function()
		{					
			
			if($("#search_button_off").css("display")== "none")
			{
				$(".search_button").hide();	
				$(".searchText").hide();
				$("#search_button_off").fadeIn("fast");
			}			
		}
	);
	
	$('#search_button_off').click(function(event){
     event.stopPropagation();
 	});
	$('.searchText').click(function(event){
     event.stopPropagation();
 	});
	$('.search_button').click(function(event){
     event.stopPropagation();
 	});
	
/*
	END OF SEARCH BOX
*/
/*
	CONTACT JS
*/
	$("#tab_contactForm").click(
		function()
		{				
			$(this).addClass("active");
			$("#tab_add_direct").removeClass("active");
			$("#contactus_directions_holder").hide();
			$("#contactus_form_holder").show();
		}
	);
	$("#tab_add_direct").click(
		function()
		{				
			$(this).addClass("active");
			$("#tab_contactForm").removeClass("active");
			$("#contactus_form_holder").hide();
			$("#contactus_directions_holder").show();			
		}
	);
	 
/*
	END OF NEWSROOM JS
*/	

	function expandColumn(objElement, width )
	{			
		$(objElement+" dd a").show();
		
		$(objElement).animate({
			width: width+"px",
			height: subMenuHeight_opened
		}, animationSpeed);
			
			$(objElement+" dd").animate({
				height: "100%",
				opacity: "1"
			}, animationSpeed);
		return;		
	}
	
	function collapseColumns(objElement, width)
	{
		$(objElement).animate({
			width: width+"px",
			height: subMenuHeight_closed
		}, animationSpeed);
		
			$(objElement+" dd").animate({
				height: "0",
				opacity: "0"
			}, animationSpeed);
			
			$(objElement+" dd a").hide();
	}
	
	function initiateFooterColumns()
	{
		var footerColumnCount = 0;
		$("#footer_mainNavs").children("dl").each(function(){
					
			//Pushes the original width of the DL into an array for later use when collapsing
			closedStateSizes.push($(this).width());
			
			//For each DL adds a class with a unique number to reference to
			$(this).addClass("footer_column_"+footerColumnCount);
			
			if(footerColumnCount == 0)
				$(this).addClass("nobg");
			
			//Increments the count
			footerColumnCount++;							    
		 });
	}
	
	
	
	function initCufon()
	{
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('dl dt h2');
		Cufon.replace('#content_sidebar h5');
		Cufon.replace('#careers_summary p strong');	

		$('h1').css('text-indent', '0px');
		$('h2').css('text-indent', '0px');
		$('h3').css('text-indent', '0px');
		$('h4').css('text-indent', '0px');
		$('dl dt h2').css('text-indent', '0px');
		$('#content_sidebar h5').css('text-indent', '0px');
		$('#careers_summary p strong').css('text-indent', '0px');
	}
	
	

}); //End of document.ready

function siteManagers(stateObj)
{
	if(stateObj=="__background") {
		$("#states_intro_text").fadeIn();
		$(".state").hide();
	} else {
		$("#states_intro_text").hide();
		$(".state").hide();
		$("#state_"+stateObj).fadeIn();
	}
}