$(document).ready(function(){

  /* **************************************************** *
  * IE 6 CSS BACKGROUND FLICKER FIX
  * ***************************************************** */

  try {
    document.execCommand("BackgroundImageCache", false, true);
  } catch(err) {};

  /* **************************************************** *
  * SEARCH FIELD VALUE SET AND REMOVAL
  * ***************************************************** */

  $(".search-field").val("Search").click(function(){
    $(this).val("");
  });

  /* **************************************************** *
  * HORIZONTAL NAVIGATION DROPDOWN MENU FEATURE
  * ***************************************************** */

  $("#primary-navigation li").hover(
    function(){$(this).addClass("ssfdd");},
    function(){$(this).removeClass("ssfdd");}
  );

  /* **************************************************** *
  * PRODUCTS A-Z FEATURE
  * ***************************************************** */

  $("#all-products").change(function(){
    window.location.href = $(this).find("option:selected").val();
  });

  /* **************************************************** *
  * ZEBRA STRIPING FEATURE
  * ***************************************************** */

  $(".zebra-striped tbody tr:odd td").addClass('zebra');

  /* **************************************************** *
  * ROUNDED CORNER FEATURE
  * ***************************************************** */

  $(".rounded-corners")
    .find(".content").css({padding: "0 10px", borderWidth: "0 1px"}).end()
    .find(".content :first-child").css({marginTop: "0", paddingTop: "0"}).end()
    .find(".content :last-child").css({marginBottom: "0", paddingBottom: "0"}).end()
    .prepend("<div class='top-corners'><span>&nbsp;</span></div>")
    .append("<div class='bottom-corners'><span>&nbsp;</span></div>");

  /* **************************************************** *
   * TAB FEATURE
   * **************************************************** */

  if($(".tabs").size() >= 1){
    var uri = window.location.href;
        uri = escape(uri);
        uri = uri.split('%23');
    var initParam = "#" + uri[1];
    $(".tabs").each(function(){
      if($(this).children("div.tab-content").size() >= 1){
        $(this)
          .children("div.tab-content").addClass("flip").end()
          .children("div.tab-content:first").addClass("flop").end()
          .find("ul.tab-buttons li:first").addClass("current").end()
          .find("ul.tab-buttons li a").click(function(){
            var tgt = $(this).attr("href");
            $(this).parent().siblings().parent().siblings("div.tab-content.flop").removeClass("flop");
            $(this).parent().siblings(".current").removeClass("current");
            $(tgt).addClass("flop");
            $(this).parent().addClass("current");
            $(this).blur();
            return false;
          });
        if(initParam != "#undefined"){
          $(this).find("ul.tab-buttons li a[@href=" + initParam + "]").parent().siblings().parent().siblings("div.tab-content.flop").removeClass("flop");
          $(this).find("ul.tab-buttons li a[@href=" + initParam + "]").parent().siblings(".current").removeClass("current");
          $(initParam).addClass("flop");
          $(this).find("ul.tab-buttons li a[@href=" + initParam + "]").parent().addClass("current");
          $(this).find("ul.tab-buttons li a[@href=" + initParam + "]").blur();
        };
      };
    });
  };

  /* **************************************************** *
   * EXPAND & COLLAPSE FEATURE
   * **************************************************** */

  if ($(".expandCollapse").size() >= 1) {
    var $ecContent = $(".expandCollapse div.ec-content").length;
    $(".expandCollapse")
      .eq(0).prepend("<div id='ecAll'><a id='expandAll' href='#expandAll'>Expand All</a> <a id='collapseAll' href='#collapseAll'>Collapse All</a></div>").end()
      .children("div.ec-content").addClass("flip").end()
      .children("p.ec-handle").hover(
        function(){$(this).css({cursor: "pointer"});},
        function(){$(this).css({cursor: "auto"})}).end()
      .find("#collapseAll").hide();
    $(".expandCollapse a[@href$='#expandAll']").click(function(){
      $(".expandCollapse")
        .children("div.ec-content").removeClass("flip").end()
        .children("p.ec-handle").addClass("collapse").end()
        .find("#collapseAll").show().end()
        .find("#expandAll").hide();
      $(this).blur;
      return false;
    });
    $(".expandCollapse a[@href$='#collapseAll']").click(function(){
      $(".expandCollapse")
        .children("div.ec-content").addClass("flip").end()
        .children("p.ec-handle").removeClass("collapse").end()
        .find("#collapseAll").hide().end()
        .find("#expandAll").show();
      $(this).blur;
      return false;
    });
    $(".expandCollapse").each(function(){
      $(this).children("p.ec-handle").click(function(){
        $(this).toggleClass("collapse");
        $(this).next("div.ec-content").toggleClass("flip");
        var $wFlip = $(".expandCollapse div.ec-content.flip").length;
        if($ecContent == $wFlip) {
          $(".expandCollapse ")
            .find("#collapseAll").hide().end()
            .find("#expandAll").show();
        } else if($wFlip == 0) {
          $(".expandCollapse ")
            .find("#collapseAll").show().end()
            .find("#expandAll").hide();
        } else {
          $(".expandCollapse ")
            .find("#collapseAll").show().end()
            .find("#expandAll").show();
        };
      });
    });
  };

  /* **************************************************** *
   * COLLAPSED FAQS FEATURE
   * **************************************************** */

  if ($(".collapsed-faqs").size() >= 1) {
    $("#page-body").prepend("<div id='faqAnswers'><a id='showHideAnswers' href='#showHideAnswers'>Show All Answers</a></div>").end()
    $(".collapsed-faqs")
      .find("dd").addClass("flip").end()
      .find("dt").addClass("anchor")
      .click(function(){
        if($(this).next("dd").is(".flip")){
          $(this).next("dd").removeClass("flip");
        } else {
          $(this).next("dd").addClass("flip");
        }
      });
    $("#faqAnswers > #showHideAnswers").toggle(
      function(){
        $(this).text("Hide All Answers").blur();
        $(".collapsed-faqs").children("dd").removeClass("flip");
        },
      function(){
        $(this).text("Show All Answers").blur();
        $(".collapsed-faqs").children("dd").not(".flip").addClass("flip");
      });
  };

});
