// Initializes User Scripts:

var isLoaded = false;
$(document).ready(function() {
  // Add social media stuff
  var currentUrl = escape(document.URL);
  var currentTitle = escape(document.title);
  $(".share-fblike-mini").append('<iframe src="http://www.facebook.com/plugins/like.php?app_id=264212736930747&amp;href='+currentUrl+'&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px;" allowTransparency="true"></iframe>');
  $(".share-tweet-mini").append('<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?url='+currentUrl+'&amp;text='+currentTitle+'&amp;count=horizontal&amp;lang=en" style="border:none; overflow:hidden; width:130px; height:20px;"></iframe>');
  $(".share-stumble-mini").append('<iframe src="http://www.stumbleupon.com/badge/embed/1/?url='+currentUrl+'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px; background:transparent;" allowTransparency="true"></iframe>');
  $(".share-digg-mini").append('<a class="DiggThisButton DiggCompact" href="http://digg.com/submit?url='+currentUrl+'&amp;title='+currentTitle+'"></a>');
  $.getScript("http://widgets.digg.com/buttons.js");
  /*
  (function() {
    var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://widgets.digg.com/buttons.js';
    s1.parentNode.insertBefore(s, s1);
  })();
  */

  // 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
  if ( location.pathname == '/' ) {
    $('#Menu').navMenu({
      menuWidth: 225,
      containerElement: "#theme",
      menuElement: "ul",
      rightArrow: ' »'
    });
    if ( $.browser.msie && $.browser.version <= '6.0' ) {
      $("ul#Menu span:empty").remove();
    }
  } else {
    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');
    if ( $.cookie("ie6prompt") != 'true' ) {
      $("body").pixelsilkModal({
        loadMethod: "onload",
        contentType: "ajax", content: '', contentSource: "/ie6prompt.html",
        closeButtonHTML: '', showCloseButton: true,
        fadeInSpeed: 300, fadeOutSpeed: 300,
        modalWidth: 323, modalHeight: 355,
        modalBorder: 0
      });
      $.cookie("ie6prompt", 'true');
    }
  }

  // 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());
});
