$(document).ready(function() {

  $("#topbar li:first").css('border-left', 'none');

  $('#headerbar_slideshow').cycle({
    fx:    'fade',
    speed:  2000,
    pause:  4500
  });

  // button rollovers (assumes there's an equivilent filename with '_f2' just before the 3-letter extension)
  $(".btnrollover").hover(function() {
    var src = $(this).attr('src');
    var charsToExtension = src.length - 4;
    var src1 = src.substr(0, charsToExtension);
    var src2 = src.substring(charsToExtension);
    $(this).attr('src', src1 + '_f2' + src2);
  }, function() {
    var src = $(this).attr('src');
    var charsToExtension = src.length - 4;
    var src1 = src.substr(0, charsToExtension-3);
    var src2 = src.substring(charsToExtension);
    $(this).attr('src', src1 + src2);
  });

  // site search
  $("#sitesearch_text").focus(function() {
    if ($(this).val() == 'Keyword / Item') {
      $(this).css('color', '#000');
      $(this).val('');
    }
  });
  $("#sitesearch_text").blur(function() {
    if ($(this).val() == '') {
      $(this).css('color', '#888');
      $(this).val('Keyword / Item');
    }
  });

  // newsletter subscription
  $("#subscribe_text").focus(function() {
    if ($(this).val() == 'Enter your email') {
      $(this).css('color', '#000');
      $(this).val('');
    }
  });
  $("#subscribe_text").blur(function() {
    if ($(this).val() == '') {
      $(this).css('color', '#888');
      $(this).val('Enter your email');
    }
  });

  // product list options
  $("#numresults_perpage").change(function() {
    $("#numresults_submit").remove();
    $("#numresults").submit();
  });
  $("#sortresults_sort").change(function() {
    $("#sortresults_submit").remove();
    $("#sortresults").submit();
  });

  // receipt popup
  $(".invoicelink").click(function() {
    day = new Date();
    id = day.getTime();
    url = $(this).attr('href');
    eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=640,height=600,left = 550,top = 225');");
    return false;
  });

});

function UpdatePriceAndStock(prodID, optionGroups) {
  var urlparams = '';
  $("#prod_liveinfo").html('<div style="text-align:center; height:50px;"><img src="'+path_to_site+'images/template/ajax-loader.gif" width="16" height="16" style="margin-top:10px;" /></div>');

  // if an array has been passed in we need to get the option values
  if (optionGroups != '') {
    urlparams = "&options=true";
    for (i=0; i < optionGroups.length; i++) {
      urlparams += "&" + optionGroups[i] + "=" + $("#"+optionGroups[i]).val();
    }
  }

  $("#prod_liveinfo").load(path_to_site+"includes/ajax/prodinfo.php?id="+prodID+urlparams);
}

function CheckSubscription() {
  if ($("#subscribe_text").val().length < 4) {
    alert('Please enter your email address.');
    return false;
  } else if ($("#subscribe_text").val() == 'Enter your email') {
    alert('Please enter your email address.');
    return false;
  }
  return true;
}
