// Initializes User Scripts:

$(document).ready(function() {
  // Selects text in Search Box when focused:
  $('input[type=text].clearText').each(function() {
    var txtSearchText = $(this).val();
    $(this).focus(function() {
      if ($(this).val() == txtSearchText)
        $(this).val('');
    });
    $(this).blur(function() {
      if ($(this).val() == '')
        $(this).val(txtSearchText);
    });
  });

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

  // hide text in spans (text replacement). Adds sprite class for 'image'.
  $('.imgText').each(function() {
    $(this).find('>span').hide();
    $(this).addClass('spriteTheme');
  });

  // Smartz Logo
  $('#smartzLink')
    .find('>span')
    .html('&nbsp;')
    .addClass('spriteTheme');
  
  $("#topMenu>li:not(:last)").after('<li class="separator"></li>');
  if ( !($.browser.msie && $.browser.version <= '6.0') ) {
    $("#siteMenu").after('<div id="siteMenuBottom"><div id="siteMenuBottomCorner" class="spriteTheme"></div></div>');
    $("#topMenu>li>a").append('<span class="corner"></span>');
  } else {
    $("#siteMenu").after('<div id="siteMenuBottom"><img src="/spacer.gif" width="1" height="5" alt="" /></div>');
  }
  $("body.home #mainColumn h3").append('<span class="spriteTheme"></span>');

  pixelsilk2.renderSkin({skin:"["+"[Menu1]"+"]", path:""}, function(html) {
    $("#siteMenu").after('<div id="subNavigation" style="display:none;"></div>');
    $("#subNavigation").html(html);
    var sections = $("#subNavigation div");
    var topElements = $("#topMenu li:not(.separator)");
    for (z = 0; z < sections.length; z++) {
      var li = topElements[z];
      var lihtml = li.innerHTML;
      li.innerHTML = lihtml + sections[z].innerHTML;
    }
    $("#topMenu").navMenu({
      menuWidth: 200,
      containerElement: "#theme",
      menuElement: "ul",
      rightArrow: ' &raquo;'
    });
    $("#subNavigation").remove();
  });
  
  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();
    });
  }
  
  if ( $.browser.msie && $.browser.version <= '6.0' ) {
    var username = location.search.replace(/.*(\?|\&)username\=/g, '');
    if ( username.length ) {
      $("a").each(function() {
        var href = $(this).attr('href');
        if ( !(href.indexOf('http://') > -1 && href.indexOf('prostorsystems.com') == -1) && href.indexOf('logout.aspx') == -1 ) {
          if ( href.indexOf('?') > -1 ) {
            href += '&username=' + username;
          } else {
            href += '?username=' + username;
          }
          $(this).attr('href', href);
        }
      });
    }
  }
});

$(window).load(function() {
  if ( $("#leftColumn").height() < $("#mainColumn").height() ) {
    $("#leftColumn").height($("#mainColumn").height());
  } else if ( $("#leftColumn").height() > $("#mainColumn").height() ) {
    $("#mainColumn").height($("#leftColumn").height());
  }
});
