// Initializes User Scripts:
$(document).ready(function() {
  $('input.txtSearch').focus(function() {
    if ( $(this).val() == 'search' ) {
      $(this).val('');
    }
  });
  $('input.txtSearch').blur(function() {
    if ( $(this).val() == '' ) {
      $(this).val('search');
    }
  });

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

  // Links - pop up in new window / tab:
  $(".lnkBlank").click(function() {
    window.open($(this).attr("href"), 'window2', '');
    return false;
  });
  
  $("a.shorturl").pixelsilkModal({
    contentType:"iframe",
    modalHeight:500,
    closeButtonHTML:""
  });
  $("a[href^='http://downloads.vantageclinicalsolutions.com/']").each(function() {
    if ( $.cookie('registeredDL') == null || $.cookie('registeredDL') == '' ) {
      $(this).attr('href', $(this).attr('href') + '/reg');
    }
    $(this).pixelsilkModal({
      contentType:"iframe",
      modalHeight:500,
      closeButtonHTML:""
    });
  });
  
  $(".ctaDiv:eq(1)").css('margin-left', '7px');
  $(".ctaDiv:eq(2)").css('margin-left', '8px');
  
  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();
    });
  }
  
  $("#leftNavMenu ul").addClass('collapsible');
  $("#leftNavMenu li:has(ul.collapsible):not(:first)").addClass('hasChild');
  $("#leftNavMenu a[href='" + location.pathname + "']").addClass('current');
  // If "current" wasn't set, check next menu item up in hierarchy? (6th-level non-diaplaying menus)
  if ( $("#leftNavMenu li").length && !$("#leftNavMenu a[href='" + location.pathname + "']").length ) {
    var newurl = location.pathname;
    while ( !$("#leftNavMenu a[href='" + newurl + "']").length ) {
      //newurl = newurl.substr(0, newurl.length-1);
      newurl = newurl.substr(0, newurl.substr(0, newurl.length-1).lastIndexOf('/')+1);
      $("#leftNavMenu a[href='" + newurl + "']").addClass('current');
    }
  }
  $("#leftNavMenu ul li:not(.hasChild) .arrow").css('background-image', 'none');
  $("#leftNavMenu").accordionMenu();
  
  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):not(.custom)");
    for (z = 0; z < sections.length; z++) {
      var li = topElements[z];
      var lihtml = li.innerHTML;
      li.innerHTML = lihtml + sections[z].innerHTML;
    }
    $("#topMenu li.custom li").addClass('noHover');
    $("#topMenu>li:has(ul) a").addClass('hasChild');
    $("#topMenu").navMenu({
      menuWidth: 200,
      containerElement: "#theme",
      menuElement: "ul",
      rightArrow: ' &raquo;'
    });
    $("#subNavigation").remove();
  });
});


