/**
 * PEPS ROLLOVER FUNCTION
 **/

PEPS = {};
PEPS.rollover = 
{
   init: function()
   {
      this.preload();
      
      $(".ro").children("img").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); }, 
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').children("img").each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   { 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '1' + src.match(/(\.[a-z]+)$/)[0]; 
   },

   oldimage: function( src )
   { 
      return src.replace(/1\./, '.'); 
   }
};



$(function()
{

     //fixFlashHeight();
     //$(window).bind('resize', fixFlashHeight); //in FF, this reloads flash everytime you resize.

    
     if(scripts_isIE6) $(document).pngFix();

     PEPS.rollover.init();
     
     initBKR();
     initHays53();

     if(dl=="")
	dl = checkForDeepLink();
     else 
	show53Lyr(dl);


     //-------------------------------------------------------------------------------------------
     // CLICK TRACKING FOR EXTERNAL LINKS
     //------------------------------------------------------------------------------------------
     $('[rel="external"]').click(function() {
	     var pageName = getExternalLinkName($(this).attr("href"));
	     trackPage(pageName);
	     return true;
     });


     $("a[rel^='lightbox']").prettyPhoto({
			animationSpeed: 'slow',
			padding: 40,
			opacity: 0.85,
			showTitle: false,
			allowresize: true,
			counter_separator_label: '/',
			theme: 'light_rounded'
     });


     if(scripts_isIE6){
          $("#construction-li > ul").css("display", "none");
          $("#about-li > ul").css("display", "none");
     }

});

function initHays53()
{
     $(".moreLink").click(function() {
          expandLayer();
     });


     //Open Nutrition Layer
     $(".nutritionLyr").click(function(){
          show53Lyr('nutrition');
     });
     
     //Close Nutrition Layer
     $(".lyrCloseNutr").click(function(){
          hide53Lyr('nutrition');
     });
     
     //Open Nutrition Inquiry Layer
     $(".nutritionInquiry").click(function(){
          openInquiry("nutrition", "white");
     });
     


     //Open Xfactor Layer
     $(".xfactorLyr").click(function(){
          show53Lyr('xfactor');
     });
     
     //Close Xfactor Layer
     $(".lyrCloseXfactor").click(function(){
          hide53Lyr('xfactor');
     });
     
     //Open Xfactor Inquiry Layer
     $(".xfactorInquiry").click(function(){
          openInquiry("xfactor", "black");
     });
     
     
     
     //Open Rescue ink Layer
     $(".rescueLyr").click(function(){
          show53Lyr('rescue');
     });
     
     //Close Rescue ink Layer
     $(".lyrCloseRescue").click(function(){
          hide53Lyr('rescue');
     });
     
     //Open Rescue ink Inquiry Layer
     $(".rescueInquiry").click(function(){
          openInquiry("rescue", "black2");
     });    
}

function show53Lyr(lyrid)
{
   $('#flashMasthead').fadeOut('fast', function() {
       $("#Hays53").remove();
       $("#lyr-"+lyrid).show("slow");
       changeWindowHash(lyrid);
       $('#flashMasthead').append('<img src="/images/hays53/top.jpg" width="942" height="247" alt="Hays53" border="0" />');
  });
}

function hide53Lyr(lyrid)
{
   $('#flashMasthead').show();
   $("#lyr-"+lyrid).hide("slow");
   changeWindowHash("");
   var so = new SWFObject("swf/Hays53.swf", "Hays53", "942", "247", "9", "#000000");
   so.addParam("wmode", "transparent");
   so.write("flashMasthead");
}


function initBKR()
{
     $(".navLi").hover(function(){
          $(this).toggleClass("sel");
     }, function(){
          $(this).toggleClass("sel");
     });
          
}

function expandLayer()
{
  if($("#investment > .inner-box .mid").height() != 194) 
  { 
     $("a.moreLink").html('less <span><img src="/images/hays53/more-link2.gif" alt="" border="0" /></span>');
     
     $("#investment > .inner-box .mid .bkr-content").height("220px");
     $("#investment > .inner-box .mid").height("194px");
     
     /*$("#investment > .inner-box .mid .bkr-content").animate({
          height: '220px'
     }, 1000, function() {});
     
     $("#investment > .inner-box .mid").animate({
           height: '194px'
     }, 1000, function() {});*/
     
  }else{
  
    $("a.moreLink").html('more <span><img src="/images/hays53/more-link.gif" alt="" border="0" /></span>');
    
     $("#investment > .inner-box .mid .bkr-content").height("90px");
     $("#investment > .inner-box .mid").height("56px");
    
    /*$("#investment > .inner-box .mid .bkr-content").animate({
          height: '90px'
     }, 1000, function() {});
    
     $("#investment > .inner-box .mid").animate({
           height: '56px'
     }, 1000, function() {});*/
  }
}

function openInquiry(lyr, color)
{
     $("."+color).fadeIn();
     $("#"+lyr).fadeIn();
     
     $("."+color).bind("click", function(){
          $("."+color).fadeOut();
          $("#"+lyr).fadeOut();
     });
     
     $(".inquiryClose").click(function(){
	closeInquiry(color,lyr);
     });
}

function closeInquiry(color,lyr)
{
          $("."+color).fadeOut();
          $("#"+lyr).fadeOut();
}

//-------------------------------------------------------------------------------------------
// LOAD HTML CONTENT FROM EXTERNAL FILE
//------------------------------------------------------------------------------------------
function loadContent(divID,fileName,fileDivID,callback)
{
   //alert("\ndivID="+divID+"\nfileName="+fileName+ "\nfileDivID="+fileDivID+ "\ncallback="+callback);

  if(fileDivID!="" && callback=="")
     $("#"+divID).load("."+fileName+" #"+fileDivID);
  else if(callback!="" && fileDivID!="")
     $("#"+divID).load("."+fileName+" #"+fileDivID, callback);
  else
     $("#"+divID).load("."+fileName);
}

function removeContent(fileDivID)
{
  $("#"+fileDivID).remove();
}


function fixFlashHeight()
{
    var doc_ht = $(document).height();
    var win_ht = $(window).height();
    var new_ht = 0;
    var oth_ht = 125;

    var new_ht = win_ht-125;

    //alert("doc_ht="+doc_ht+" win_ht="+win_ht);
    if(new_ht>640){
	$('#landingShell').animate({
	  height: new_ht+"px"
	}, 10 );
    } else {
	$('#landingShell').animate({
	  height: "640px"
	}, 10 );
    }
}


function swfOpenCarousel()
{
  //called when carousel is opened.

}

function swfCloseCarousel ()
{
  //called when carousel is closed.
}

function changeWindowHash(u){
	//so URLs will be unique and can be bookmarked.

	if(window.top)
	  var wnd = window.top;
	else
	  var wnd = window;

	if(u.indexOf('/')==-1){
		wnd.location.hash = u;
		//alert("changing hash to "+u);
	} else {
		var li   = u.lastIndexOf('/');
		var anch = u.substring(li+1,u.length);
		wnd.location.hash = anch;
		//alert("changing hash to "+anch);
	}
}

function checkForDeepLink()
{

	var du = window.location.hash;

	//deeplinking for iframes
	if(du.indexOf("#") != -1){

		var li = du.lastIndexOf('#');
		var dl = du.substring(li+1,du.length);

		if(dl=="rescue" || dl == "xfactor" || dl == "nutrition"){ //this is a deep-link!

			show53Lyr(dl); // open deep-linked panel

			return dl;
			
		}
	}

	return "";
}

function trackPage(pageName)
{
   try { 
	//alert("tracking page view: "+pageName);
	pageTracker._trackPageview(pageName);
   } catch(err) {}
}


function getExternalLinkName(url)
{
   var str = url;
   str = str.replace(/http:\/\//i, "");
   str = str.replace(/www\./i, "");
   str = "External_URL_"+str;
   return str;
}
