(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)

function replaceButtonImage(elem, search, replace) {
  src = $(elem).attr("src").replace(/_hover/g, "").replace(/_down/g, "");
  src = src.replace(search, replace);
  $(elem).attr("src", src);
}

function preloadButtonImage(elem, search, replace) {
  src = $(elem).attr("src").replace(/_hover/g, "").replace(/_down/g, "");
  src = src.replace(search, replace);
  $.preLoadImages(src);
}

$(function() {
  $("div#frontMain").pngFix({"fix": ["front_main_right.png"]});
  
  $(".button img, img.button, input[type=image].button").each(function(i) {
    $.preLoadImages($(this).attr('src'));
    preloadButtonImage(this, ".png", "_hover.png");
    preloadButtonImage(this, ".png", "_down.png");
  });
  
  $(".button img, img.button, input[type=image].button").hover(function() {
    replaceButtonImage(this, ".png", "_hover.png");
  }, function() {
    replaceButtonImage(this, "_hover.png", ".png");
  }).mousedown(function() {
    replaceButtonImage(this, ".png", "_down.png");
  }).mouseup(function() {
    replaceButtonImage(this, "_down.png", ".png");
  });
  
  $(".sf-menu").superfish();  
  
  /* Form Hints */
  
  $("#loginBar input#top-username").focusin(function() {
    if ($(this).val() == "username") $(this).val("");
  }).focusout(function() {
    if ($(this).val() == "") $(this).val("username");
  });
  
  $("#loginBar input#top-password").focusin(function() {
    if ($(this).val() == "password") $(this).val("");
  }).focusout(function() {
    if ($(this).val() == "") $(this).val("password");
  });
  
  $("input#fields_fname").focusin(function() {
    if ($(this).val() == "First Name") $(this).val("");
  }).focusout(function() {
    if ($(this).val() == "") $(this).val("First Name");
  });
  
  $("input#fields_lname").focusin(function() {
    if ($(this).val() == "Last Name") $(this).val("");
  }).focusout(function() {
    if ($(this).val() == "") $(this).val("Last Name");
  });
  
  $("input#fields_email").focusin(function() {
    if ($(this).val() == "Email Address") $(this).val("");
  }).focusout(function() {
    if ($(this).val() == "") $(this).val("Email Address");
  });
  
  /* iContact Init */

  icpForm12138 = document.getElementById('icpsignup12138');

  if (document.location.protocol === "https:")
    icpForm12138.action = "https://app.icontact.com/icp/signup.php";
});

function verifyRequired12138() {
  if (icpForm12138["fields_email"].value == "" || icpForm12138["fields_email"].value == "Email Address") {
    icpForm12138["fields_email"].focus();
    alert("The Email field is required.");
    return false;
  }
  return true;
}