// Mini Plugin to preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
  // Preload the menu images
  $(".rolloverbg").each(function() {
    var img = $(this).css("background-image")
    // Chop off the 'url(' and ')' parts of the css image definition
    img = img.substr(4, img.length - 5);
    $.preloadImages(img);
  });

  $("#menu > ul > li").each(function() {
    $(this).mouseover(function(){
      $(this).find(".rolloverbg").show();
      $(this).find("ul").show();
    });

    $(this).find("ul").mouseout(function(){
      $(this).hide();
      $(this).parent().find(".rolloverbg").hide();
    });
  })

  set_other_state_country_sync();
  add_classes_to_inputs();
  search_links_trigger_focus_search();
  disable_submits_on_click();
  confirm_cancel();
});

function search_links_trigger_focus_search() {
  $('.search_link').click(function() {
    focus_search();
    return false;
  });
}

function focus_search() {
  $('#search_form #q').focus();
  $('#search_form').fadeTo('medium', 0.5);
  $('#search_form').fadeTo('fast', 1.0);
}

function add_classes_to_inputs() {
  $('input').each(function() {
    var type_attribute = $(this).attr('type');
    if (type_attribute) {
      if (type_attribute == "text" || type_attribute == "password") {
        $(this).attr('class', $(this).attr('class') + ' text');
      } else {
        $(this).attr('class', $(this).attr('class') + ' button');
      }
    }
  });
}

function show_popup(uri) {
  newwindow = window.open(uri,'Project Alert Help','height=400,width=420,top=100,left=100,resizable');
  if (window.focus) { newwindow.focus() }
}

function set_other_state_country_sync() {
  var other_state = "80";
  var other_country = "3"
  var names = ['mailing', 'shipping', 'billing'];

  $(names).each(function (i) {
    var name = names[i];

    $('#' + name + '_address_state_id').change(function () {
      if ($(this).val() == other_state &&
        $('#' + name + '_address_country_id') != other_country)
        $('#' + name + '_address_country_id').val(other_country);
    });

    $('#' + name + '_address_country_id').change(function () {
      if ($(this).val() == other_country &&
          $('#' + name + '_address_state_id') != other_state)
        $('#' + name + '_address_state_id').val(other_state);
    });
  });
}

function disable_submits_on_click() {
  $('form').submit(disable_clicks);
}

function disable_clicks() {
    var cancel = function() { return false; };
    $('input[type=submit]', this).click(cancel);
    $('input[type=image]', this).click(cancel);
}

function confirm_cancel() {
    $('form input[name=cancel]').click(function() {
        return confirm('Are you sure you want to cancel?');
    });
}

function bookmark_site(title,url){
  try {
  if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
  else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
  } 
  else if(document.all)// ie
    window.external.AddFavorite(url, title);
  } catch(err) {
    alert("Error adding bookmark. Perhaps your browser does not support adding bookmarks directly.  Please try bookmarking this page manually");
  }
}
