//delay plugin
jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};
//extend logging plugin
jQuery.fn.log = function (msg) {  
  console.log("%s: %o", msg, this);
  return this;
};

$(document).ready(function(){	
						   
	//highlight current area on nav - must be done before Cufon call otherwise it won't highlight
	var strPath = location.pathname ;
	var strFolder = strPath.split("/")[1];	
/*	if (strFolder == "staples")
		{
			//staging environment
			$("#a-"+strPath.split("/")[2].replace("-","")).addClass("activearea");
			
		}
	else
		{
			//for live
			$("#a-"+strPath.split("/")[2].replace("-","")).addClass("activearea"); 
		}*/
	
	//activate elements previously hidden via CSS to prevent FOUC
	$("html.js #nav li a").show().css({"visibility":"visible", "display":"block"});
	
	$("#content h1, #content h2").show().css({"visibility":"visible"});
	//replace nav				
	
	
	
	
	Cufon.replace('#nav > li:child > a', {
    hover: true,
	textShadow: '#fff 1px 1px'});
	//nav children
	//Cufon.replace('#nav li ul li a');
	
	
	 //replace headings
	 Cufon.replace(['#content h1', '#content h2', '#content h3']);

	
//	
//	//set menu up to trigger on nav ul li a - hover
//	//console.log($("#nav li a").each(function(){$(this).attr("id")}))
//   	$("#nav > li > a").hover(function () {		
//		//close any existing menus, kill the dropshadow and remove selected class (used to target load of dropshadow)
//		$("#nav li ul[id^=child-]").fadeOut("fast").removeShadow().removeClass("selected");
//		 if ($.browser.msie && $.browser.version < 8)
//						{//IE6/7 z-index issue means users cannot select text 
//						 //so change z-index on content hover
//							
//							$("#content").css({"z-index":"100"});
//							
//						}	
//		//is the ul next to trigger li a hidden?	  
//				  if ($(this).next("ul[id^=child]").is(":hidden"))  
//				  {//yes - find it, add selected class, fade it in and apply rounded corners to last LI, then add dropshadow               
//				  if ($.browser.msie && $.browser.version < 8)
//						{//IE6/7 z-index issue - when using menu set it ahead of content							
//							$("#content").css({"z-index":"-1"});
//							
//						}	
//					$(this).next("ul[id^='child']").addClass("selected").css({"opacity":"0.8"}).fadeIn(100).bind("mouseleave", function(){$(this).fadeOut(100).removeShadow().removeClass("selected").removeClass("activemenu") 
//									 if ($.browser.msie && $.browser.version < 8)
//						{//IE6/7 z-index issue - when moving off menu set content ahead of menu							
//							$("#content").css({"z-index":"100"});
//							
//						}																																													 
//																																																					 });					
//					//apply drop shadow  
//					addshadow();						
//				 // $(this).addClass("activemenu");
//				  }
//				  else
//				  {  //don't close it if it's the UL selected  
//				  //get the ID of current anchor 				   
//					  if($(this).next("ul:not(.selected)"))
//              {					  
//               $("#nav li ul[id^=child-]").fadeOut(100).removeShadow().removeClass("selected");  
//			   if ($.browser.msie && $.browser.version < 8)
//						{//IE6 z-index issue means users cannot select text 
//						 //so change z-index on content hover
//							
//							$("#content").css({"z-index":"100"});
//							
//						}	
//              // $(this).removeClass("activemenu");
//              }         
//			}}					  
//		);	
//	
//	//hide dynamic menu if mouse moves above the nav
//	$("#topheader").hover(function(){  $("#nav li ul[id^=child-]").fadeOut(100).removeShadow().removeClass("selected")
//			   if ($.browser.msie && $.browser.version < 8)
//					{//IE6 z-index issue means users cannot select text 
//					//so change z-index on content hover
//					
//					$("#content").css({"z-index":"100"});
//					}	
//	 });
//	
//	
//	
	//now check if a menu is visible but not in a mouse over state... if it is, wait 2 seconds and then fade out
	//the element used here is the show/hide element directly, not the trigger parent anchors
	//**************** Disabled for now. This wasn't a great user experience. Might work with some more finessing. The trouble is adding/removing the dropshadow in time with the other elements and getting the event to work on the top level UL element instead of subsequent elements.
//	var toclose=true;	
//														 
//	$("#nav li ul[id^=child]").hover(function () {
//	//don't do anything if it's in hover state
//	//console.log("runnning")
//	}, function(){//not on hover? fade out	
//	//first set events to run on the visible ul so we can check if the mouse is on or off the area
//		$("#nav li ul[id^=child]:visible").mouseout(function(){
//			toclose=true;	
//			//console.log("mouseOUT true! " + $(this).attr("id"));
//		});
//		$("#nav li ul[id^=child]:visible").mouseenter(function(){
//			toclose=false;				 
//			//console.log("mouseENTER true! " + $(this).attr("id"));
//		});
//		//about to fade out. Check it's still visible and then close it with a delay of 1 second. After that delay only close if the mouse hasn't come back into the area.		
//		if($(this).is(":visible"))
//			{
//					$(this).delay(1000, function(){//still visible?
//									if(toclose == false)	
//									{//check it's not being hovered over								
//										//console.log("keep it!")	;								
//										return false;
//									}
//									else
//									{//close it
//										//console.log("hide it!");
//										$("#nav li ul[id^=child]").fadeOut("fast").removeShadow().removeClass("selected");
//									}
//									
//									});
//			}
//		}
//						   );
	
		//add arrow to selected area
		strID = $("body").attr("id");		
		strID = strID.replace(/-/g,"");//global replace
		strID ="#a-" + strID; 
		//console.log(strID + " " + $(strID).parent().parent().attr("id"))
		if ($(strID).parent().parent().attr("id")!="nav")
		{
			$(strID).parent().addClass("page_on");
			//$(strID).attr("style","visibility:visible; display:block; background:red !important");
		}	
		//initiate scrolling window for content heavy pages. Which pages actually show the scrollbar depend on the height of the content area (trigger is css height)
		
		strID = strID.replace("#a-","");//strip strID back and use it to only implement scrolling on lower level pages
		//if there is a page which doesn't have content (eg meet the team which is only flash) add it to the list otherwise it will generate IE6 error
		if (strID!="staplesjobshomepage" && strID!="about" && strID!="benefits" && strID!="ourpeople" && strID!="businessareas" &&strID!="roles" && strID!="vacancies" && strID!="meettheteam")
		{
	  		$('.full-content').jScrollPane();	  
		}
	  //JS footer
		//$("#footer").positionFooter(true);	
		//set opacity for accordion		
		$("#jobwidgetcontent").hide().css({"opacity":"0.85"});		
		
		//activate accordion for jobs widget
		$("#jobwidgetcontent").accordion({
		collapsible: true,
		event: "click",
		autoHeight: false,
		active:false
		});
			
		$("#jobwidget").toggle(function(){
				$("#jobwidgetcontent").slideDown(500,function callback() {
 					 //$(this).dropShadow({left: 2, top: 2, opacity: 0.5, blur: 2, color: "#000"}) <-- shadow does not resize quickly enough on accordion state change
				});			
		},
			function(){
					//$("#jobwidgetcontent").slideUp(500).removeShadow();		
					$("#jobwidgetcontent").slideUp(500)	
			});	
			
		
  });  		
		
//		
//	
//
//function addshadow()
//{
//	if($.browser.msie && jQuery.browser.version.substr(0,3)=="6.0" )
//	{//don't load dropshadow, it's buggy with IE6
//	}
//	else
//	{
//		
//	$(".selected").dropShadow({left: 2, top: 2, opacity: 0.5, blur: 2, color: "#000"});
//	}
//}
//this is the positioned footer script

(function($){  
	/*$.fn.positionFooter = function(center){  
	var element = this; 
	var originalPosition = $(element).offset();
	var originalTopPosition = originalPosition.top;  
	
	positionTheFooter();  
	  
	$(window).bind("resize", function(){  
		positionTheFooter();  
	});
	*/
	
	/*
	function positionTheFooter(){ 
	  
	  var elementPosition = $(element).offset();
	  var elementPaddingTop = $(element).css("padding-top");
	  var elementPaddingBottom = $(element).css("padding-bottom");
	  var elementPaddingleft = $(element).css("padding-left");
	  var elementPaddingRight = $(element).css("padding-right");
	  var elementHeight = $(element).height();
	  var elementWidth = $(element).width();
	  
	  var windowHeight = $(window).height();
	  var windowWidth = $(window).width();
	  
	  elementPaddingTop = elementPaddingTop.replace(/px/,"");
	  elementPaddingBottom = elementPaddingBottom.replace(/px/,"");
	  elementPaddingleft = elementPaddingleft.replace(/px/,"");
	  elementPaddingRight = elementPaddingRight.replace(/px/,"");
	  
	  var newPosition = (parseInt(windowHeight) - (parseInt(elementHeight) + (parseInt(elementPaddingTop) + parseInt(elementPaddingBottom) + 57)));
	  //!!! added the 57 to compensate for clipboard poking up above footer
	  var currentPosition = elementPosition.top - (parseInt(elementHeight) + (parseInt(elementPaddingTop) + parseInt(elementPaddingBottom)));
		
	  if(originalTopPosition < windowHeight){
		
		$(element).css({  
		  "position" : "absolute",
		  "top" : newPosition
		});
		
		if(center == true){
		  $(element).css({  
			"left" : windowWidth / 2 - (((elementWidth + parseInt(elementPaddingleft) + parseInt(elementPaddingRight)) / 2))
		  });
		}
	  
	  }
	  
	  if(newPosition <= originalTopPosition){
	
		$(element).css({  
		  "position" : "absolute",
		  "top" : originalTopPosition
		});
		
		if(center == true){
		  $(element).css({  
			"left" : windowWidth / 2 - (((elementWidth + parseInt(elementPaddingleft) + parseInt(elementPaddingRight)) / 2))
		  });
		}
	  
	  }
	  
	};
	 
	};  
	  */
})(jQuery); 
//}) //* Put in when other code was commented out *//

// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder