$(function(){
    /* Missing image test and replacment */
    (function ($) {
      $.extend($.fn, {
        brokenImage: function (options) {
          options = $.extend(options);
          return this.each(function () {
            // Replace the image with a placeholder if:
            // loading fails with an error event
            var image = this;
            $(image).bind('error', function () {
               insertPlaceholder();
            });
            function insertPlaceholder() {
              options.replacement ? image.src = options.replacement : $(image).css({visibility: 'hidden'});
            }
          });
        }
      });
          $('ul li img').brokenImage({replacement: '/assets/images/pat/pat_no_image.gif'});
    })(jQuery);
/* Footer Report Card */
    if($("#footer > ul").length > 0){
     $.ajax({
       type: "GET",
       url: "/assets/ajax_pages/pat/cs_report.html",
       success: function(html){
        $("#footer > ul").after(html);
        $(".btn-slide").click(function(){
            var currentDate = new Date()
            var months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
            var month = currentDate.getMonth();
            var day = currentDate.getDate();
            var year = currentDate.getFullYear();
            var updatedDate = months[month] + " " + day + ", " + year;
            $("span.date","#cs-report").html(updatedDate);
            if( $("#cs-report").is(":hidden")){
                $("#footer-wrap").height(700);

            }
            $("#cs-report").slideToggle(1000, function(){
                if( $("#cs-report").is(":hidden")){
                    $("#footer-wrap").animate({height: "300px"}, "normal");
                }else{
                    $('html, body').animate({
                        scrollTop: $(".disclaimer")
                        .offset().top }, "normal");
                }
            });
            $(this).toggleClass("active");
            return false;
         });
        }
     });
    }

  
    if($("ul.prodcollection-desc").length){
      categoryPage.init("subCat");
    }
    if($(".category-page").length){
       categoryPage.init("cat");

    }

   setCurrentTab.init();
    
});

var categoryPage = {
    init: function(catType){
        if(catType = "subCat"){
            $(".quick-view",".prodcollection-desc li").each(function(){
				$(this).find('span.sale').each(function(){
					if ($(this).text().toLowerCase().indexOf('on sale') >= 0){
						$(this).parents('li:first').append("<p class='sale sale-flag'>On Sale!</p>");
					}
					if ($(this).text().toLowerCase().indexOf('on clearance') >= 0){
						$(this).parents('li:first').append("<p class='sale sale-flag'>On Clearance!</p>");
					}
				});
			});
			$(".quick-view",".prodcollection-desc li").hide();
            $("<a href='#' class='view'>View Details</a>").addClass("open").unbind("click").bind("click",function(){
                categoryPage.showHideSubCat(this);
                return false;
            }).prependTo(".prodcollection-desc li");
            categoryPage.buildBuyNow();
        }
        if(catType = "cat"){
           $("div.product-details-hover",".prodcollection-static").addClass("hide"); 
           categoryPage.displayTab();
           if($(".prodcollection-static").length){
             categoryPage.showHideCat();
           }
        }
    },
    showHideSubCat: function(link){
        if ($(".quick-view", $("li")).is(":visible")){
            $(".quick-view:visible", $("li").not($(link).parent("li"))).animate({height: 'toggle'});
            clearTimeout(categoryPage.showHideCatTimer);
            $("a.view").not(link).removeClass("close");
            $("a.view").not(link).addClass("open").unbind("click").bind("click",function(){
              categoryPage.showHideSubCat(this);
              return false;
            });
        }
        $(".quick-view", $(link).parent("li")).animate({height: 'toggle'})
        if ($(".quick-view", $(link).parent("li").not(":visible"))){
            $(link).removeClass("open");
            clearTimeout(categoryPage.showHideCatTimer);
            $(link).addClass("close").click(function(){
              clearTimeout(categoryPage.showHideCatTimer);
              $(link).addClass("open").unbind("click").bind("click",function(){
                categoryPage.showHideSubCat(this);
                return false;
              });
              $(link).removeClass("close");
              return false;
            });
            categoryPage.showHideCatTimer = setTimeout(
                function() {
                  $(link).removeClass("close");
                  $(".quick-view", $(link).parent("li")).animate({height: 'toggle'})
                  $(link).addClass("open").unbind("click").bind("click",function(){
                    categoryPage.showHideSubCat(this);
                    return false;
                  });
                }, 7000
            );
        }else{
           $(".quick-view", $(link).parent("li")).animate({height: 'toggle'});
        }
    },
    showHideCat: function(){
        $(".product-image img",".content-slide").hover(function(){
          var img = this;
          $("img.not-active").removeClass("not-active");
          $(".product-image img",".content-slide").each(function(){
            if($(this).attr("src").indexOf("_over") != -1){
              clearTimeout(categoryPage.showHideCatTimer);
              this.src = this.src.replace("_over.jpg",".jpg");
              $(this).parent().nextAll("div.product-details-hover:eq(0)").addClass("hide").removeClass("show");
            }
          });

          if($(img).attr("src").indexOf("_over") == -1) {
            $(img).fadeTo(250,0.25, function(){
                img.src = img.src.replace(".jpg","_over.jpg");
              $(img).parent().nextAll("div.product-details-hover:eq(0)").addClass("show").removeClass("hide").fadeIn("slow");
            }).fadeTo(250,1);
          }
          $(this).parents("li").nextAll().children("a.product-image").children("img").addClass("not-active");
          $(this).parents("li").prevAll().children("a.product-image").children("img").addClass("not-active");
        },
        function(){
          var elem = this;
          categoryPage.showHideCatTimer = setTimeout(function(){
            categoryPage.closeTimeOut(elem)
          }, 2000);
        });
        $("div.product-details-hover",categoryPage.currentProduct).hover(function(){
           clearTimeout(categoryPage.showHideCatTimer);
        },
        function(){
          var elem = $(this).prevAll(".product-image").children("img");
          categoryPage.showHideCatTimer = setTimeout(function(){
            categoryPage.closeTimeOut(elem);
          }, 2000);
          return false;
        });
    },
    showHideCatTimer: null,
    closeTimeOut: function(elem){
      var isJQuery = elem instanceof jQuery;
      if($(elem).attr("src").indexOf("_over.jpg") != -1) {
        $(elem).fadeTo(250,0.25, function(){
          if(isJQuery){
            $(elem).attr("src", $(elem).attr("src").replace("_over.jpg",".jpg"))
          }else{
            elem.src = elem.src.replace("_over.jpg",".jpg");
          }
          $(elem).parent().nextAll("div.product-details-hover:eq(0)").addClass("hide").removeClass("show");
        }).fadeTo(250,1);
      }
    },
    currentProduct: null,
    buildBuyNow: function(){
        var buynow = "";
        $(".quick-view",".prodcollection-desc li").each(function(){
             buynow = "<a href='"+ $(this).siblings(".product-name").attr("href") +"' class='buy-now'>"
              buynow += "Buy Now";
              buynow += "</a>";
            $(this).append(buynow);
        });
    },
    displayTab: function(){
        $(".content-slide:not(:eq(0))").hide();
        $("a:eq(0)","#cat-nav").addClass("active");
        $("a","#cat-nav").each(function(i){
           $(this).click(function(){
               $("a:not(" + i + ")", "#cat-nav").removeClass("active");
               $(this).addClass("active");
               $(".content-slide:not(:eq("+ i +"))").hide();
               $(".content-slide:eq(" + i +")").show();
                sIFR.replace(oblimey, {
                  selector: '.content-slide h2',
                  css: [
                    '.sIFR-root {color: #e02731; font-size: 25px; }',
                  '.symbol {font-size: 14px;}'
                  ],
                  wmode: "transparent"
                });
               return false;
           })
        })
    }
    
}

var setCurrentTab = {
  init: function(){
    hrefString = document.location.href ? document.location.href : document.location;
    if (document.getElementById("primary-nav")!=null)
          setCurrentTab.setActiveMenu(document.getElementById("primary-nav").getElementsByTagName("a"), setCurrentTab.extractPageName(hrefString));
  },
  extractPageName: function(hrefString){
    if (hrefString.indexOf("http://") >= 0){
        hrefString = hrefString.replace("http://","");
        if (hrefString.indexOf("www.") >= 0){
           hrefString = hrefString.replace("www.","");
        }
        if(hrefString.indexOf(".cp") >= 0){
            hrefString = hrefString.replace(".cp","");
        }
    }
    var arr = hrefString.split('/', 2);
    return  (arr.length<2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();
  },
  setActiveMenu: function(arr, crtPage){
    for (var i=0; i<arr.length; i++){
        var navUrl = setCurrentTab.extractPageName(arr[i].href).split(".cat",1);
        if(navUrl[0] == crtPage){
            if (arr[i].parentNode.tagName != "DIV"){
                   arr[i].className = "current";
                   arr[i].parentNode.className = "current";
            }
        }
    }
  }
}
