jQuery.fn.delay = function(time,func){
    return this.each(function(){
        setTimeout(func,time);
    });
};

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);



$(document).ready(function() {

  // Show/Hide Textbox Hints
  $('input.txbhint').focus(function() {
    if ($(this).val() == "" || $(this).val() == $(this).attr('title')) {
      $(this).val("").css("color", "#000");
    }
  }).blur(function() {
    if ($(this).val() == "") {
      $(this).val($(this).attr('title')).css("color", "#aaa");
    }
  }).filter(function() {
    if ($(this).val() == "" || $(this).val() == $(this).attr('title')) {
      $(this).val($(this).attr('title')).css("color", "#aaa");
    }
  });


  // Alternate Product Views
  $('ul#altimages a').click(function() {
    var swapimg = $(this).attr('href');
    var swapimgmed = swapimg.replace('large', 'medium');
    $('ul#altimages a').removeClass('active');
    $(this).addClass('active');
    $('#productimage').attr('href', swapimg);
    $('#productimage img').attr('src', swapimgmed);
    return false;
  });


  // Open links with rel="external" in new window - like ye ole' target="_blank"
  $('a[rel="external"]').click(function() { window.open($(this).attr('href')); return false; });


  // Fade out notification messages...
  $('.notification').delay(3000, function() {
    $('.notification').fadeOut('slow');
  });



  // Zebra-stripe data tables
  $("table.data tbody").each(function() {
    $(this).removeClass("odd even");
    $("tr:odd", this).addClass("odd");
    $("tr:even", this).addClass("even");
  });

  // Initialize Lightbox (Product Thumbnails)
  $(".lightbox").lightbox();
  /*
  // Tabs...used in checkout.
  var tabContainers = $('div.tabs > div');
  $('div.tabs ul.tabnav a').click(function() {
  tabContainers.hide().filter(this.hash).show();
  $('div.tabs ul.tabnav li').removeClass('active');
  $(this).parent('li').addClass('active');
  return false;
  }).filter(':first').click();
  */

  // Homepage logo image slider.
  $("ul.logoslider").shuffle();
  $(".storelogos").jCarouselLite({
    auto: 2500,
    speed: 1000
  });

  $("select#ctl00_EquipmentBrowserPanel_ddlModel").change(function() {
    var mfr = $("select#ctl00_EquipmentBrowserPanel_ddlManufacturer").val();
    if (mfr != "" && this.value != "") {
      window.location = "/models.aspx?mfr=" + mfr + "&model=" + this.value;
    }
  });

  $("div.loginbox input").blur(function() {
    $("#CustomerPanel_cvLogin").hide();
  });

  $("#frmSearch a.searchbutton").click(function() {
    $("#frmSearch").submit();

    return false;
  });
});
