$(document).ready(function(){

  $(".clearfix, #content, #contentbody, #main, .standard-form li").append('<div class="clearall"><!-- clear floats --></div>')

	$('ul li:first-child').addClass("first"); 
	$('ul li:last-child').addClass("last"); 

  $('.secondary-product-list p:first-child').addClass('list-leader');

  //$('.product-list li').addClass("dontsplit");
  //$('.product-list').columnize({ columns: 2, lastNeverTallest: true });

  $("#contenttabs").tabs({ fx: { opacity: 'toggle' } });

  $('ul.product-list li, .secondary-product-list li, ul.launchpad-channel-list li').biggerlink();

  $("ul.sf-menu").superfish({
    delay:  500,
    animation: {opacity:'show',height:'show'},
    speed: 'fast',
    dropShadows: false,
    disableHI: false, // set to true to disable hoverIntent detection
    autoArrows:  false
  });

  $("#nav 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
  });
        
  $('.roundcorners, .secondary-product-list .list-leader, ul.product-list li, .secondary-product-list ul li, .standard-form ul, ul#launchpad-category-list li,ul.launchpad-channel-list li,.lp-cta h2,.lp-cta-body,#contentctalinks')
    .css("position","relative")
    .prepend('<div class="tlcnr"></div>' + '<div class="trcnr"></div>')
    .append('<div class="blcnr"></div>' + '<div class="brcnr"></div>');   // this function adds a small 4px radius corner to a block on a white background

  $('#header, #footer')
    .css("position","relative")
    .prepend('<div class="tlcnr"></div>' + '<div class="trcnr"></div>');   // this function adds a small 4px radius corner to a block on a white background

  $('#nav')
    .css("position","relative")
    .append('<div class="blcnr"></div>' + '<div class="brcnr"></div>');   // this function adds a small 4px radius corner to a block on a white background

	$('ul li:first-child').addClass("first"); 
	$('ul li:last-child').addClass("last");

	$('table th:first-child')
	 .css({
	   '-moz-border-radius-topleft':'4px',
	   '-webkit-border-top-left-radius':'4px',
	   'border-top-left-radius':'4px',
	   '-moz-border-radius-bottomleft':'4px',
	   '-webkit-border-bottom-left-radius':'4px',
	   'border-bottom-left-radius':'4px'
	 });   // this function adds a small 4px radius corner to a block on a white background

	$('table th:last-child')
	 .css({
	   '-moz-border-radius-topright':'4px',
	   '-webkit-border-top-right-radius':'4px',
	   'border-top-right-radius':'4px',
	   '-moz-border-radius-bottomright':'4px',
	   '-webkit-border-bottom-right-radius':'4px',
	   'border-bottom-right-radius':'4px'
	 });   // this function adds a small 4px radius corner to a block on a white background


	$("ul.sf-menu li, #footerlogo a, .cta a, .togglelist li h4, a.ctalink, ul.product-list li, .secondary-product-list ul li,#launchpad-category-list li").hover(
		function() {
			$(this).addClass('hot');
		},
		function() {
			$(this).removeClass('hot');
		}
	);

  $(".togglelist li h4").click(function() {
    $(this).next().slideToggle('fast');
    $(this).toggleClass('item-open');
    return false;
  }).next().hide();

  $("#launchpad-category-list li").click(function() {
    $(this).children('ul.launchpad-channel-list').slideToggle('fast');
    $(this).toggleClass('item-open');
    return false;
  }).children('ul.launchpad-channel-list').hide();

  
// Forms JS

  $('#quote-request-form').validate({
		rules: {
			"quote_request[insurance][]": {
				required: true,
				minlength: 1
			}
		},
		messages: {
			"quote_request[insurance][]": "Please select at least 1 type of insurance required."
		},
		errorPlacement: function(error, element) {
			if ( element.is("table.form-checkbox-table :checkbox") )
				error.insertAfter( element.parents('table.form-checkbox-table') );
			else
				error.insertAfter( element );
		}
	});
	
	$('#lodge-a-claim-form').validate({
		rules: {
			"claim_request[insurance][]": {
				required: true,
				minlength: 1
			}
		},
		messages: {
			"claim_request[insurance][]": "Please select at least 1 type of insurance required."
		},
		errorPlacement: function(error, element) {
			if ( element.is("table.form-checkbox-table :checkbox") )
				error.insertAfter( element.parents('table.form-checkbox-table') );
			else
				error.insertAfter( element );
		}
	});
	
	$('.validate-form').validate();

  $('#insurance_other').click(function() {
   if ($('#insurance_other').is(':checked')) {
     $(this).parents('li').next('li.optional').slideDown('fast',function(){
      $(this).children('textarea').addClass('required');
     });
   } else {
     $(this).parents('li').next('li.optional').slideUp('fast',function(){
      $(this).children('textarea').removeClass('required');
     });
   }
  });
  
  $('#current_provider').change(function() {
   if ($('#current_provider').val() == 'Other') {
     $(this).parents('li').next('li.optional').slideDown('fast',function(){
      $(this).children('input').addClass('required');
     });
   } else {
     $(this).parents('li').next('li.optional').slideUp('fast',function(){
      $(this).children('input').removeClass('required');
     });
   }
  });

  $('#referrer').change(function() {
   if ($('#referrer').val() == 'Other') {
     $(this).parents('li').next('li.optional').slideDown('fast',function(){
      $(this).children('input').addClass('required');
     });
   } else {
     $(this).parents('li').next('li.optional').slideUp('fast',function(){
      $(this).children('input').removeClass('required');
     });
   }
  });
  
  $('#quote_request_renewal_date').datepicker();
  
  $('#claim_request_incident_date').datepicker();
  
  $('.block-ui-quote').click(function() {
    $.blockUI({      
      message: $('#block-ui-quote'),
      applyPlatformOpacityRules: false
    });
    return false;
  });
  
  $('.unblock-ui').click(function() {
    $.unblockUI();
  });
});