$(document).ready(function(){

  // Hide search form's label
  $('#search label').hide();

  // Clear search input on focus if it contains default text
  $('#search input').focus(function(i){
    if(this.value == 'Search') this.value = '';
  });

  // extend the default setting to always include the zebra widget. 
  $.tablesorter.defaults.widgets = ['zebra','colhighlight']; 

  // Highlight active columns in sortable table
  $.tablesorter.addWidget({
    id: 'colhighlight',
    format: function(table) {
      $(table).find("td").removeClass('sorted'); // reset column highlighting
      $.each(table.config.sortList, function(item){
        $(table).find("tbody td:nth-child(" + (this[0]+1) + ")").addClass('sorted');
        });
      }
    });

  // call the tablesorter plugin 
  $("table#products").tablesorter(); 
  
  // Submit the cart update form when shipping type changes
  $('#shipping_type_id').change(function(){
    $('#cart_form').submit();
  });

	$("#main ul.brands li").vjustify();
	$("#main ul.purchase li").vjustify();

  // If request quote button is clicked, submit cart form with ajax instead
  $("#quote-button a").click(function(){
    // Show the box that will get the response message. Has a border
    $('#request_quote_response').show();
    var options = {
      target: '#request_quote_response',
      // Add background colour and fade to white
      success: function(){
        $('#request_quote_response').css({backgroundColor: '#ff0'});
          setTimeout(function() {
            $('#request_quote_response').animate({backgroundColor: '#fff'}, 1000);
        }, 1000);
      }
    };
    $('#addextras-form').ajaxSubmit(options);
  });

  // Payment form
  $('#cvn_field a').click(function(){
    $('#cvn_field small').show();
  })
  $('#cvn_field small').hide();
  
  $('#type').change(function(){
    action = (this.value.match(/solo|switch/)) ? 'show' : 'hide';
    $('#issue_number_field,#start_date_field')[action]();
    $('#amex_cvn_info')[(this.value == 'american_express') ? 'show' : 'hide']();
  })
  $('#type').change();

});
