// Initializes User Scripts:
var isLoaded = false;
$(document).ready(function() {
  // Selects text in Search Box when focused:
  $('input.txtSearch').focus(function() {
    if ( $(this).val() == 'Search Store' ) {
      $(this).val('');
    }
    //this.select();
  });
  $('input.txtSearch').blur(function() {
    if ( $(this).val() == '' ) {
      $(this).val('Search Store');
    }
  });
  
  // Footer Links - pop up in new window / tab:
  $('a#smartzLink').click(function() {
    window.open($(this).attr("href"), 'window2', '');
    return false;
  });
  $("a#comparisonChartButton").click(function() {
    window.open('/comparison-chart/','Chart','width=800, height=600, menubar=no, scrollbars=yes, resizable=yes, status=no, toolbar=no');
    return false;
  });

  // Need to pull both MenuCategories AND Menu1
  pixelsilk2.renderSkin({skin: '[' + '[MenuCategories]' + ']', path: ''}, function(html) {
    $('#subNavigation').html(html);
    pixelsilk2.renderSkin({skin: '[' + '[Menu1]' + ']', path: ''}, function(html) {
      $('#subNavigation').append(html);
      var sections = $("#subNavigation ol > li > span");
      var sections2 = $('#subNavigation > div');
      for (i = 0; i < sections2.length; i++) {
        sections.push(sections2[i]);
      }      
      var topElements = $('#Menu li:not(.separator)');
      for (z = 0; z < sections.length; z++) {
        var li = topElements[z+1]; // "Home" is hard-coded and not in the queue
        var lihtml = li.innerHTML;
        li.innerHTML = lihtml + sections[z].innerHTML;
      }
      $('#Menu').navMenu({
        menuWidth: 225,
        containerElement: "#theme",
        menuElement: "ul",
        rightArrow: ' »'
      });
      if ( $.browser.msie && $.browser.version <= '6.0' ) {
        $("ul#Menu span:empty").remove();
      }
    });
  });
  
  $(".articleItem .articleItemImage").each(function() {
    if ( $(this).attr("src") == '' || $(this).attr("src") == '/' || $(this).attr("src") == '?w=150' ) {
      $(this).hide();
    }
  });
  
  // Set Resource Center blocks to have the same height; let's use the max of them
  var max = 0;
  $(".block").each(function() {
    if ( $(this).height() > max ) max = $(this).height();
  });
  if ( max ) $(".block").height(max);
  
  // breadcrumbs - if they are wider than 570 (wrap), set height
  var bcWidth = 0;
  $("#breadcrumbs a").each(function() {
    bcWidth += $(this).width();
  });
  $("#breadcrumbs .separator").each(function() {
    bcWidth += $(this).width();
  });
  bcWidth += $("#breadcrumbs span.last").width() + $("#breadcrumbs .rightCap").width();
  if ( bcWidth > 570 ) $("#breadcrumbs").height(48);
  
  if ( $.browser.msie && $.browser.version <= '6.0' ) {
    //$("a[href='https://store.aedbrands.com/MyAccount_Orders.aspx']").attr('href', 'https://store.aedbrands.com/login.aspx?ReturnURL=MyAccount_Orders.aspx');
    $("img.themeTop").attr('src', '/theme-top.gif');
    $("img.themeBottom").attr('src', '/theme-bottom2.gif');
    $("img.viewCartImage").attr('src', '/icon-view-cart.gif');
    $("img.myAccountImage").attr('src', '/icon-my-account.gif');
  }

  // IE6 Detector plugin:
  $('body').ieDetector();
});

$(window).load(function() {
  //alert("#theme: " + $("#theme").height() + "\n#themeInner: " + $("#themeInner").height());
  if ( $.browser.msie ) {
    if ( !isLoaded ) {setTimeout('', 500)}
  }
  if ( $("#contentColumn").height() < $("#navColumn").height() ) {
    $("#contentColumn").height($("#navColumn").height());
  }
  if ( $("#contentColumn").height() < ($("#rightColumn").height() + 55) ) {
    $("#contentColumn").height($("#rightColumn").height() + 55);
  }
  //$("img#LiveHelpStatus").width(105).height(46);
  //$("body.home img#LiveHelpStatus").width(73).height(32);
  //$("#theme").append('<img src="/theme-top.png" width="1004" height="40" alt="" class="themeTop" /><img src="/theme-bottom2.png" width="1004" height="40" alt="" class="themeBottom" />');
  //alert("#theme: " + $("#theme").height() + "\n#themeInner: " + $("#themeInner").height());
});