// Initializes User Scripts:

$(document).ready(function() {

  //pixelsilk2.renderSkin({skin: "["+"[MiniCart]"+"]", path: ""}, function(html) {
  //pixelsilk2.ajax("/api2/renderSkin", "POST", {skin: "["+"[MiniCart]"+"]", path: ""}, function(html) {
  pixelsilk2.ajaxPost("/api2/renderSkin", {skin: "["+"[MiniCart]"+"]", path: ""}, function(html) {
    $("#divMiniCart").append(html);
    var minicartPrice = $("#minicartPrice").text().replace('$', '').replace(',', '');
    var mcPrice = parseFloat(minicartPrice);
    $("#minicartPrice").text('$' + mcPrice.toFixed(2));
  });
  
  // Selects text in Search Box when focused:
  $('input.txtSearch').focus(function() {
    this.select();
  });

  // Footer Links - pop up in new window / tab:
  $('a#smartzLink').click(function() {
    window.open($(this).attr("href"), 'window2', '');
    return false;
  });

  if ( document.URL.indexOf('EditListItem.aspx') == -1 ) {
    $("img").each(function() {
      var img = $(this).attr("src");
      if ( typeof(img) == 'undefined' || img == '' || img == '/' || img.substr(0, 1) == '?' || img.substr(0, 2) == '/?' ) $(this).hide();
    });
    $("img").error(function() {
      $(this).hide();
    });
  }
  
  // Need to pull both MenuCategories AND Menu1
  pixelsilk2.renderSkin({skin: '['+'[MenuCategories]'+']', path: ""}, function(html) {
    $('#subNavigation').html(html);
    pixelsilk2.renderSkin({skin: '['+'[Menu1Dynamic]'+']', 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];
        var lihtml = li.innerHTML;
        li.innerHTML = lihtml + sections[z].innerHTML;
      }
      $("#Menu div.subMenu div.subMenu").remove();
       $('#Menu').navMenu({
        menuWidth: 250,
        containerElement: "#theme",
        menuElement: "div",
        rightArrow: ' »'
      });
      $("#Menu > li:last .subMenu").css('left', '-134px');
    });
  });
  
  if ( document.getElementById('column1') ) {
    if ( !document.getElementById('column2') ) {
      $("#column1").width(900);
    }
  }

  $('.storeSubCat:nth-child(2n)').css('margin-right','0px').after('<div class="clear"></div>');

  $("#contentLeftColumn li:first").css("border-top", "0px");
  $("#contentLeftColumn li:last").css("border-bottom", "1px #D2CFCA solid");
});

$(window).load(function() {
  $("#theme").append('<div id="themeBorderBottom"><img src="/border-bottom-left.gif" width="35" height="35" alt="" id="themeBottomLeft" /><img src="/border-bottom-right.gif" width="35" height="35" alt="" id="themeBottomRight" /></div><img src="/border-top-left.gif" width="35" height="199" alt="" id="themeUpperLeft" /><img src="/border-top-right.gif" width="35" height="199" alt="" id="themeUpperRight" />');
});
