/*** COPYRIGHT 2011  BY FDM4 INTERNATIONAL INC. - ALL RIGHTS RESERVED ********/
/*** header.js -- Header JavaScript file                                   ***/
/*****************************************************************************/
/* T010000 02/19/11 JZ - Added to RT. TSClub mods.                           */
/*****************************************************************************/

/*************************/
/* TSC Header Javascript */
/*************************/

 $(document).ready(function(){
   getProdDropdown('/tsc10/catalog.xml',true);
   var vLoc = getParam("location");
   if(siteContent.fdmLoggedIn && vLoc != "logout-fm.w" ){
     $("#hdrLogin").hide();
     $("#loginName").html(siteContent.fdmLoggedIn);
     var vCartItems = 0;
     var vCartTotal = 0.00;
     if(vOrderHeader && vOrderHeader.total) vCartTotal = vOrderHeader.total;
     if(vOrderLine && vOrderLine.length ){
       $(vOrderLine).each(function(){ vCartItems += parseFloat(this.quantity); });
     }
     $("#cartItems").html(vCartItems);
     $("#cartTotal").html("$" + parseFloat(vCartTotal).toFixed(2) );

     if(vFreightInfo && vFreightInfo.length )
     {
      if(!vFreightInfo[0].promoReached) $("#ffDistance").html(vFreightInfo[0].distance + " to go");
      else $("#ffDistance").html("FREE FREIGHT!");
     }

     if (vCustomerInfo.clubMember)
     {
      $(".hdrPoints").css("visibility","visible");
      $("#hdrClubSales").html("$" + parseFloat(vCustomerInfo.clubSales).toFixed(2));
      $("#hdrClubDistance").html("$" + parseFloat(vCustomerInfo.clubDistance).toFixed(2));
     }
     else
      $("#hdrClubInfo").hide();

     $("#hdrLoggedIn").show();
     if($("#loginName").height()>15){
        $("#loginName").css("line-height","12px");
        $("#welcome").css("margin-top","5px");
     }
   }else{
     var vCookieName="saveLogin"+fdmCookie+fdmSponsor;
     var vCookieValue= getCookie(vCookieName);
     hdrGetSavedLogin();
   }

   $("#header input").inputHelp();

   if($.browser.msie && $.browser.version < 7) {
       $("#menuListhdrNav li").hover(function(){
           $(this).addClass("over");
       }, function(){
           $(this).removeClass("over");
       });
   }

  });
  //===--- Header Login Functions ---===//
  function hdrUpdateLogin(vChecked){
    if(vChecked){
      var expirydate = new Date();
      expirydate.setTime( expirydate.getTime() + (3650 * 24 * 60 * 60 * 1000) );
      if(vCookieValue!=""){ deleteCookie(vCookieName, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null); }
      setCookie(vCookieName, document.hdrLogin.username.value + "|" + document.hdrLogin.password.value, expirydate, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null);
    }else{
      deleteCookie(vCookieName, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null);
    }
  }
  function hdrGetSavedLogin() {
   if(vCookieValue!=""&&vCookieValue!=null&&vCookieValue!=";"){
     var login_password=vCookieValue.split("|");
     document.hdrLogin.username.value=login_password[0];
     document.hdrLogin.password.value=login_password[1];
     document.hdrLogin.rememberMe.checked=true;
    }
  }
  //===--- Header Product XML based dropdown ---===//
  function getProdDropdown(vXMLPath,vShort){
    var vCategory = {};
    var vBrands = {};
    $.ajax({
      type: "GET",
      url: vXMLPath,
      dataType: "xml",
      complete: function(data) {
         var catalogHTML = "";
         var vLvlWidth = 0;
         var catalog = $.xmlToJSON(data.responseXML);
         if (!catalog.division) return; // If division level is undefined then return
         catalog.division.sort(function(a,b){ return a.sortOrder - b.sortOrder });
         $(catalog.division).each(function(){
           if(vShort)
             this.Link = this.sLink;
           else
             this.Link = this.dLink;
           this.category.sort(function(a,b){ return a.sortOrder - b.sortOrder });
           if(this.description=="Category") vCategory = this; // objLvl catalog description must be Category
           if(this.description=="Brands")   vBrands   = this; // objLvl catalog description must be Brands
         });

         //CATEGORY
         var categoryHTML = '<div id="hdrCatSub"><div class="upScrollArrow"><img src="' + vWebServer + '/images/upScrollArrow.jpg" style="display:none;" /></div><div class="hdrProdSubInner"><ul>';
         $(vCategory.category).each(function(){
            this.Link = vShort ? this.sLink : this.dLink;
            if(this.noLink=="false")
             categoryHTML += '<li><a href="' + this.Link + '">&gt; ' + this.description + '</a>';
            else
             categoryHTML += '<li>&gt; ' + this.description;
            categoryHTML += '</li>';
         });
         categoryHTML += '</ul></div><div class="downScrollArrow"><img src="' + vWebServer + '/images/downScrollArrow.jpg" /></div></div>';
         $("#menuListhdrNav0 > li:nth-child(1)").append(categoryHTML).hover(function(){ $("#hdrCatSub").show(); },function(){ $("#hdrCatSub").hide(); });

         //BRANDS
         var brandsHTML = '<div id="hdrBrandsSub"><div class="upScrollArrow"><img src="' + vWebServer + '/images/upScrollArrow.jpg" style="display:none;" /></div><div class="hdrProdSubInner"><ul>';
         $(vBrands.category).each(function(){
            this.Link = vShort ? this.sLink : this.dLink;
            if(this.noLink=="false")
             brandsHTML += '<li><a href="' + this.Link + '">&gt; ' + this.description + '</a>';
            else
             brandsHTML += '<li>&gt; ' + this.description;
            brandsHTML += '</li>';
         });
         brandsHTML += '</ul></div><div class="downScrollArrow"><img src="' + vWebServer + '/images/downScrollArrow.jpg" /></div></div>';
         $("#menuListhdrNav0 > li:nth-child(2)").append(brandsHTML).hover(function(){ $("#hdrBrandsSub").show(); },function(){ $("#hdrBrandsSub").hide(); });

         $(".downScrollArrow").click(function(){
           var vHeight = 0;
           var vTop;
           $(this).siblings('.hdrProdSubInner').find('ul').css('top',function(idx,val){
             //Stupid UL height is always 0, need to add up all LI heights
             $(this).find('li').each(function(){
               vHeight += $(this).height();
             });
             if(vHeight-250+parseFloat(val)>0){
               vTop = parseFloat(val)-25;
               return parseFloat(val)-25;
             }
             else{
               vTop = parseFloat(val);
               return parseFloat(val);
             }
           });
           if (vTop + vHeight - 250 <= 0 )
            $(this).find('img').hide();
           else
            $(this).siblings('.hdrProdSubInner').siblings('.upScrollArrow').find('img').show();
         });
         $(".upScrollArrow").click(function(){
           var vTop;
           $(this).siblings('.hdrProdSubInner').find('ul').css('top',function(idx,val){
             if(parseFloat(val)<0){
               vTop = parseFloat(val)+25;
               return parseFloat(val)+25;
             }
             else{
               vTop = parseFloat(val);
               return parseFloat(val);
             }
           });
           if(vTop>=0)
            $(this).find('img').hide();
           else
            $(this).siblings('.hdrProdSubInner').siblings('.downScrollArrow').find('img').show();
         });

         if($.browser.msie) {
           $(".downScrollArrow").dblclick(function(){ $(this).click(); });
           $(".upScrollArrow").dblclick(function(){ $(this).click(); });
         }
      }
    });
  }


/********************************
* jQuery Input Helper Plugin v0.2
* Author: Adam Young
********************************/
(function( $ ){
 $.fn.inputHelp = function() {

   var createClone = function(el){
      var el = $(el);
      var clone = $("<input type='text' />");
      clone.insertAfter(el).attr({
         'class':el.attr('class'),
         'style':el.attr('style')
      });
      return clone;
   };
   var $clone;

   this.bind("focus",function(){
     var $this = $(this);
     if(this.value==$this.attr("alt")){
	   this.value="";
	   $this.removeClass("inputHelp");


    }
    })
    .bind("blur",function(){
      var $this = $(this);
      if(this.value==""){
	      $this.addClass("inputHelp");
	      if($this.attr("type")=="password"){
           $this.hide()
                .siblings('.inputHelpClone').show();
	      }else{
	        this.value=$this.attr("alt");
	      }
	  }
   });
   this.each(function(){
     var $this = $(this);
     if(this.value=="")
        this.value=$this.attr("alt");
	   if(this.value==$this.attr("alt") && $this.attr("alt")!="")
	     $this.addClass("inputHelp");
	   if($this.attr("type")=="password"){
	     $this.addClass("inputHelpPw");
       $clone = $("<input type='text' />");
       $clone.attr({
         'class':$this.attr('class'),
         'style':$this.attr('style'),
         'value':$this.attr('alt')
       }).addClass("inputHelp inputHelpClone").insertAfter(this).hide();
       if($this.hasClass('inputHelp')){
         $this.hide();
         $clone.show();
       }
       $clone.bind("focus",function(){
         $(this).hide()
                .siblings('.inputHelpPw').show().focus();
       });
     }


   });

   // Replace input help value with blank before submitting forms
   var vFirst = true;
   var vSelector = this.selector;
   if(vFirst){
     $("form").bind("submit",function(){
       $(this).find(vSelector).each(function(){
         if(this.value==$(this).attr("alt"))
           this.value="";
       });
       //$(".inputHelpClone").remove();
     });
     vFirst=false;
   }

  };
})( jQuery );

/*****************************************************************************/

function goClub()
{
 if (vCustomerInfo && vCustomerInfo.clubMember)
  golink('olc/club-home.w','main');
 else
  golink('mcopy.w?mcopy=club-signup','main');
} // goClub()

