// Set date format for datePick function
Date.format = 'dd-mm-yyyy';
// Date.format = 'yyyy-mm-dd';

$(document).ready(function(){

  // clear floats on block element
  $(".clearfix").append('<div class="clearall"><!-- clear floats --></div>');

  // add first and last classes to all lists, table rows, table heads and table cells in the DOM
  $('ul li:first-child,table tr:first-child,tr td:first-child, tr th:first-child').addClass("first");
  $('ul li:last-child,table tr:last-child,tr td:last-child, tr th:last-child').addClass("last");

  // add end class to specific row ends when display lists of product items - removes right margins
  $("#latest-products ul.product-list li:nth-child(2n)").addClass("end");
  $("ul.product-list li:nth-child(4n)").addClass("end");
  $(".entrygallery a:nth-child(7n)").addClass("end");
  
  // initialise the drop-down menus on main navigation
  $("ul.sf-menu").superfish({
      delay:  500,
      animation: {opacity:'show',height:'show'},
      speed: 'fast',
      autoArrows:  false
  });
  
  // change placement of drop-down menus to center under it's parent item, rather aligned to left margin of parent item
  $("ul.sf-menu li ul").each(function(){         // this function centres the drop-down menus under each of their top level nav bar items
      var pwidth = $(this).parent().width();          // get width of LI parent
      var cwidth = $(this).width();                   // get width of drop-down menu UL
      var marginleft = ((cwidth - pwidth) / 2);       // this is the value that the item needs to be negatively set on left margin
      $(this).css("margin-left",(0 - marginleft));    // set the new margin-left value directly onto the selector
  });
  
  $('.product-list li').biggerlink();

  $("#nav li a").hover(
    function() {
      $(this).addClass('hot');
    },
    function() {
      $(this).removeClass('hot');
    }
  );

  $('#hero ul')
      .after('<div id="heronav">')
      .cycle({ 
        cleartypeNoBg: true,
        timeout: 10000,
        speed: 350,
        pause: 1,
        pager: '#heronav',
        pagerAnchorBuilder: function(idx, slide) {
          return '<a href="#">' + $(slide).find('a').attr('title') +  '</a>';
        } 
  });

  $('#smallhero ul')
      .cycle({ 
        cleartypeNoBg: true,
        speed: 350,
        timeout: 6000,
        pause: 1
  });

  $('#productgallery ul')
      .cycle({ 
        fx: 'fade',
        speed: 350,
        cleartypeNoBg: true,
        timeout: 1000000,
        pause: 1,
        pager: '#productthumbs ul',
        pagerAnchorBuilder: function(idx, slide) { 
          return '#productthumbs li:eq(' + (idx) + ') a';
        }
  });
  $('#productgallery ul').cycle('pause');

  $("#productgallery ul li a").append('<span class="magnify"></span>')

  $('.validate-form').validate();
  /* $('#entryform').validate(); */

  $('.date-pick').datePicker({startDate:'01/01/1996', clickInput:true});

  // prettyphoto gallery init
  $("a[rel^='prettyPhoto']").prettyPhoto({
      animationSpeed: 'fast', /* fast/slow/normal */
      padding: 1, /* padding for each side of the picture */
      opacity: 0.5, /* Value betwee 0 and 1 */
      showTitle: true, /* true/false */
      allowresize: true, /* true/false */
      counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
      theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
      callback: function(){}
  });
  
  
  // change style on selected product when radio button array changes
  $("#productoptions input[name='productline']").change(function() {
    if ($("input[@name='productline']:checked").val())
      $(this).parents("ul").find("li.selected").removeClass("selected");
      $(this).parent().addClass("selected");
      var price = $(this).parent().find(".price").html();
      $("#selected-price .price").text(price);
    }
  );
  
  // switch image in gallery to correct colour when radio item is clicked
  $("#productoptions input[name='productline']").click(function() {
      var prodid = $(this).parent().attr("id");
      var prodid = parseInt(prodid.replace("prod_",""));
      $('#productgallery ul').cycle(prodid);
  });
  
  // change style on selected product when page loads
  $("input[@name='productline']:checked").each(function(){
    $(this).parents("ul").find("li.selected").removeClass("selected");
    $(this).parent().addClass("selected");
    var price = $(this).parent().find(".price").html();
    $("#selected-price .price").text(price);
  });
  
  $("#productextras").tabs();
  
  $(".productextra,.product-info a").equalHeights();

  $('.input-example').example(function() {
      return $(this).attr('title');
  });
    
  $('#shipping_info').slideUp('fast');

  $('#billing_for_shipping').click(function() {
   if ($('#billing_for_shipping').is(':checked')) {
     $('#shipping_info').slideUp('fast',function(){
      $('#shipping_first_name').removeClass('required');
      $('#shipping_last_name').removeClass('required');
      $('#shipping_address').removeClass('required');
      $('#shipping_city').removeClass('required');
      $('#shipping_state').removeClass('required');
      $('#shipping_zip').removeClass('required');
     });
   } else {
     $('#shipping_info').slideDown('fast',function(){
      $('#shipping_first_name').addClass('required');
      $('#shipping_last_name').addClass('required');
      $('#shipping_address').addClass('required');
      $('#shipping_city').addClass('required');
      $('#shipping_state').addClass('required');
      $('#shipping_zip').addClass('required');
     });
   }
  });
  
  
  // Tools JS Utils
  
  $('#warranty-search-form').validate({
		rules: {
			keywords: {
				required: true,
				minlength: 3
			}
		},
		messages: {
			keywords: "Search terms must be at least 3 characters in length."
		},
		submitHandler: function(form) {
	       // do other stuff for a valid form
			$('#warranty-search-form').ajaxSubmit({ 
        // target identifies the element(s) to update with the server response 
        target: '#warranty-search-results',
        beforeSubmit: function() {
					$("#warranty-search-results").html('<p id="warranty-searching-progress">Searching for results&hellip;</p>');
				},
        success: function() { 
        } 
			});
		}
	});

});
