(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

// Initializes User Scripts:
$(document).ready(function() {
  var initialSearchText = "enter search term here";
  
  $("ul.slideshow li img").each(function(){
    if($(this).attr("src") == "") $(this).parent().remove();
  });
  
  $("ul.slideshow").cycle();
  
  $("#searchBox").focus(function(){
    if($(this).val() == initialSearchText){
      $(this).val("");
    }
  }).blur(function(){
    if($(this).val() == ""){
      $(this).val(initialSearchText);
    }
  });
  
  $(".rightButton a span").hide();
  
  $("img").each(function(){
    if($(this).attr("src") == "") $(this).hide();
  });
  
  $("#menu > li").hover(function(){$(this).addClass("hover");}, function(){$(this).removeClass("hover");});

  pixelsilk2.renderSkin({skin: '[' + '[Menu1]' + ']', path: ''}, function(html) {
    $("#navigation").after('<div id="subNavigation"></div>');
    $("#subNavigation").html(html);
    var sections = $("#subNavigation div");
    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").navMenu({
      menuWidth: 1,
      containerElement: "#container",
      menuElement: "ul",
      rightArrow: ' »'
    });
  });
  
  $("#menu > li").each(function(i){
    var color = "yellow";
    if(i >= 0) color = "blue";
    if(i >= 2) color = "green";
    if(i >= 5) color = "yellow";
    
    $.preLoadImages("/menu-item-bg-blue-hover.gif","/menu-item-bg-green-hover.gif","/menu-item-bg-yellow-hover.gif");
    
    $(this).removeClass("blue");
    $(this).addClass(color); 
  });
  
  $("#homeSlideShow").cycle({
    pager: "#homePager",
    timeout: 10000
  });
  
  $("#childShouldSeeDoctorLinks").change(function(){
    if($(this).val() != ""){
      document.location.pathname = $(this).val();
    }
  });
  $(".staffLeftContent ul, .staffLeftContent ul li").css("max-width","200px");
  $(".staffLeftContent ul").cycle({
    timeout: 7000
  });
});
