/**/
if (typeof sIFR == "function"){
   sIFR.replaceElement(
      ".searchresults .colhead h2.flashreplace, div.contentHead h3, div.articlePromoSplash .lightbg h3",
      named({sFlashSrc: "/Assets/Common/swf/sabon.swf", sColor: "#4A4A4A", sWmode: "transparent" /*/, sFlashVars: "offsetTop=-2" /**/ })
   );
   sIFR.replaceElement(
      "div.articlePromoSplash .darkbg h3",
      named({sFlashSrc: "/Assets/Common/swf/sabon.swf", sColor: "#ffffff", sWmode: "transparent" })
   );
   sIFR.replaceElement(
      "div.articleHead h3, h3.flashreplace",
      named({sFlashSrc: "/Assets/Common/swf/sabon.swf", sColor: "#333333", sWmode: "transparent" })
   );
   sIFR.replaceElement(
      ".mostpopularModule .mostpopularHeader h5.flashreplace, .splashText h4",
      named({sFlashSrc: "/Assets/Common/swf/avenir.swf", sColor: "#4A4A4A", sWmode: "transparent" })
   );
   sIFR.replaceElement(
      "div.navHeader h4.flashreplace",
      named({sFlashSrc: "/Assets/Common/swf/avenir2.swf", sColor: "#333333", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=1" })
   );
   sIFR.replaceElement(
      ".diagnosisLanding .threeColContainer h5.flashreplace, .diagnosisBody h4.flashreplace",
      named({sFlashSrc: "/Assets/Common/swf/avenir.swf", sColor: "#333333", sWmode: "transparent" })
   );
   sIFR.replaceElement(
      ".productDetailBlock .detailHead h5.flashreplace, .productDetailBlock .detailHead h4.flashreplace",
      named({sFlashSrc: "/Assets/Common/swf/sabon.swf", sColor: "#333333", sWmode: "transparent" })
   );
};
/**/



Hartz = {
   init: function(index){
      $(function(){
         Hartz.MainNav.init();
         // Think I can remove this, but not sure:
    /**/ if ($.browser.msie && ($.browser.version < 7)) {
            var sideNavBgColor = $("div.tipexVideo").length ? "#e0edf9" : "#e3e3e3"; // ugh
            $("div.navBody > ul > li").each(function(){
               var $li = $(this),
                   defaultColor = $li.css("background-color");
               $li.children("h5 a").hover(
                  function(){ $li.css("background-color", sideNavBgColor) },
                  function(){ $li.css("background-color", defaultColor) }
               )
            })
         }
    /**/
         Hartz.Toolbar.init();
         Hartz.SiteSearch.init();
         Hartz.MostPopular.init();
         Hartz.ProductLanding.init();
         Hartz.Recommendations.init();
         Hartz.ProductToutIEFix.init();
         Hartz.TipNav.init();
         
         
         // Error page back link functionality
         $("div.pgNotFound a.calloutLink[rel='Prev']").click(function(e){ history.go(-2); e.preventDefault(); });
         
         // Rollover for buttons
         $("img, input").each(function(){
           var	$this = $(this),
                 originalsrc = $this.attr("src");
         		
           if (originalsrc && (originalsrc.indexOf("_off.") > -1)) {
              var on_src = originalsrc.replace("_off.", "_on.");
              $("<img src='%1'/>".replace("%1", on_src));	// preload
         	
              $this.hover(
                 function(){ $this.attr("src", on_src) },
                 function(){ $this.attr("src", originalsrc) }
              );
           }
         });
         
         // Rollover for search button (uses bgimage)
         (function(){
            var $searchButton = $(".siteSearch input.image"),
                bgimage = $searchButton.css("background-image");
                
            $searchButton.hover(
               function(){ $searchButton.css("background-image", bgimage.replace(/\.(gif|jpg|png)/, "_hover.$1")) },
               function(){ $searchButton.css("background-image", bgimage) }
            )
            
         })();
         
         // initiate side nav hover states, including rounded corner bottom cap:
      
         $("div.sideNav div.navBody").append("<div class='bl'>&nbsp;</div><div class='br'>&nbsp;</div>")
                                     .find("li.subcatSelected h5").hover(
                                         function(){ $(this).parent().attr("style", "background-color: #e0e0e0") },
                                         function(){ $(this).parent().attr("style", "") }
                                     );
      
         
         // activate select module components:
         $("div.selectModule").each(function(){ $(this).can_has().selectmodule() } );
         
         // Add [H] icon to last item in articles:
         var finIcon = $("<img src='/Assets/Hartz/img/icon/icon_fin.gif' class='finIcon'/>"),
             lastArticleItem = $("div.articleText:not(.noicon)").children(":last").not(":empty, h5, h6, div").filter(function(){ 
               // filtering pages that also use div.articleText
               if ($("div.contentHead.chWardley").length) return false;
               else return this;
             });
         if (lastArticleItem.is("ol, ul, dl")) lastArticleItem = lastArticleItem.children(":last");
         lastArticleItem.append(finIcon);
         
         // This is a hacky fix to IE not rendering these consistently in the article block:
         if ($.browser.msie) $("div.articleText").children("ol").css("padding-left", "5px");
         
         // Aaand a hacky fix for *Firefox* because I can't figure out why it's messing up these tables
         if ($.browser.mozilla) $(".productDetailBlock .detailBody .description table").css({ "margin-left": 1, "width": 438 })
         
         // for popup windows:
         $("div.closeWindow a").click(function(e){ window.close(); e.preventDefault(); });
         
         // this might fix a weird error with postback removing sifr text:
         $("fieldset.questionCat select").change(function(){ sIFR.replaceElement( "div.contentHead h3", named({sFlashSrc: "/Assets/Common/swf/sabon.swf", sColor: "#4A4A4A", sWmode: "transparent" }) ) });
         
         // Make product tout bodies clickable
         $("div.productTout > div.productBody").click(function(){ window.location = $(this).find("div.productDescription > a:first-child").attr("href") });
         
         // Decorate external links and make them open a new window
         $("a[rel='external']").each(function(){
            var $this = $(this);
            $this.append("&nbsp;<img src='/Assets/Hartz/img/icon/icon_ext.gif'/>")
                 .click(function(e){ window.open($this.attr("href")); e.preventDefault(); })
                 .children("img").css("display", "inline");
         })
         
      })
   },
   TipNav: { 
      init: function(){
         var $tipNav = $(".tipex_tipNav"),
             $tipContainer = $tipNav.find("div.container_tipNav"),
             $tips = $tipNav.find("li.tip"),
             isScrollingTipNav = $tipNav.hasClass("tipNavScrolling"),
             $selected = $tips.filter(".selected");
         
         // scroll to selected
         if (isScrollingTipNav && $selected.length) $tipContainer.scrollTop($selected.offset().top - $tipContainer.offset().top);
         
         // Make whole tip item clickable
         $tips.click(function(){ window.location = $(this).find("a").attr("href") });
      }
   },
   ProductToutIEFix: {
      init: function(){
        if ($.browser.msie && (parseInt($.browser.version) < 7)) {
           $("img[src*='.ashx'], img[src*='.png']").each(function(){
              var $this = $(this);
              $this.css({
                 "height": $this.height(),
                 "width": $this.width(),
                 "filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='%1',sizingMethod='scale')".replace("%1", $this.attr("src"))
              }).attr("src", "/Assets/Common/img/transparent.gif");
           });
        }
      }
   },
   MostPopular: { 
      init: function(){
         var $module = $("div.mostpopularModule"),
             $tabs = $module.find("ul.tabs > li.tab");
             
         $tabs.each(function(){
            var $this = $(this),
                $li = $this.find("div.tabcontent > ol > li"),
                $li_last = $li.eq(4);
                
            $li.not(":eq(4)").hover(
               function(){ $(this).addClass("hover") },
               function(){ $(this).removeClass("hover") }
            );
            
            $li_last.hover(
               function(){ $(this).css("background", "transparent url(/Assets/Hartz/img/bg/bg_mostpopular_last_hover.gif) bottom left no-repeat");},
               function(){ $(this).css("background", ""); }
            );
             
         });
         
         
         $tabs.find("h6").click(function(e){
            var $this = $(this),
                $tab = $this.parent();
            if (!$tab.hasClass("selected"))  {
               $tab.siblings().removeClass("selected");
               $tab.addClass("selected");
            }
            e.preventDefault();
         })
      }
   },
   Recommendations: {
      init: function(){
         $("div.recommendBar:not(.disabled) > p.recBtn > a[params]").each(function(){ $(this).can_has().recommendlink() })
            .parent().hover(
               function(){ $(this).parent().addClass("hover") }, 
               function(){ $(this).parent().removeClass("hover") }
            );
      }
   },
   ProductLanding: {
      init: function(){
         $("div.productCategoryChip").each(function(){
            var $this = $(this),
                $chipHeader = $this.find("div.chipHeader"),
                $link = $this.find("div.chipBody a").attr("href");
            $chipHeader.click(function(){ window.location = $link; })
         })
      }
   },
   SiteSearch: {
      init: function(){
         $("div.siteSearch input[type='text']").each(function(){
            var $this = $(this);
            $this.can_has().watermark();
            $this.focus(function(){
               $(document).bind("keypress.search", function(e){
                  if (e.which == 13) {
                     document.location = "/search.aspx?q=" + $this.val();
                     e.preventDefault();
                  }
               });
            }).blur(function(){ $(document).unbind("keypress.search"); });
            $this.parent().click(function(){ $(this).children("input").focus() });
         })
      }
   },
   MainNav: {
      init: function(index){
         //$().bind("mouseover", function(e){ $(e.target).addClass("hover") }).bind("mouseout", function(e){ $(e.target).removeClass("hover") });
         $("ul.primaryNav a, ul.primaryNavL2 li").hover(function(){ $(this).addClass("hover") }, function(){ $(this).removeClass("hover") })
	   }
   },
   Toolbar: {
      init: function(){
         var $networks = $("li.networks"),
             $tools = $("ul.tools"),       
             $print = $tools.find("li.print"),
             $email = $tools.find("li.email"),
             $share = $("li.share"),
             $toggle = $share.find("li.toggle");
      
         $tools.css("visibility", "visible")
               .click(function(e){ e.preventDefault() })
               .find("li.email, li.print").hover(
                  function(){ var $this = $(this); $this.css("background-image", "url(/Assets/Common/img/icon/icon_%1_hover.gif)".replace("%1", $this.attr("class")) ).children().addClass("hover") },
                  function(){ var $this = $(this); $this.css("background-image", "url(/Assets/Common/img/icon/icon_%1.gif)".replace("%1", $this.attr("class")) ).children().removeClass("hover") }
               );
               
         $print.click(function(){ window.print() });
         $email.click(function(){ 
            window.open( 
               $email.find("a").attr("href"), "_blank",
               "width=480,height=600,addressbar=0,location=0,menubar=0,toolbar=0"
            );
         });
         
         $networks.find("li").each(function(){
            $this = $(this);
            $this.can_has().sharelink($this.attr("class"));
         });
         
         /*/ Safari has problems with this for some reason, so it's a simple hide/show for you guys /**/
         if($.browser.safari) {
            $networks.hide();
            $tools.bind("mouseleave", function(){ $networks.hide(); $share.removeClass("selected"); })
            $toggle.bind("mouseover", function(){ $networks.show(); $share.addClass("selected"); })
         }
      
         else {
            /*/ interesting, have to reassign in order for functions to work; should just move this to events /**/
            $networks = $("li.networks").can_has().slideout();
            $tools.bind("mouseleave", function(){ $networks.close() });
            $toggle.bind("mouseover", function(){ $networks.open() });
            $share.can_has().subscription($networks, "slideout.opened", function(){ $(this).addClass("selected") })
                            .can_has().subscription($networks, "slideout.closed", function(){ $(this).removeClass("selected") })
         }
         
      }
   }
}

Hartz.init();
