$(document).ready(function(){

	// =====================================================================================
	// App wide
	// =====================================================================================
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers
  $("a.rollover").hover(
    function() {
      curr = $(this).find("img").attr("src");
      overlen = curr.length;
      over = curr.substr(0, overlen-4);
      over = over+'_on.png';
      $(this).find("img").attr({ src: over});
    },
    function() {
      $(this).find("img").attr({ src: curr});
    }
  )
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers - Preload images
  $("a.rollover").find("img").each(function(i){
    temp = this.src;
    prelen = temp.length;
    pre = temp.substr(0, prelen-4);
    pre = pre+'_on.png';
    preload_image_object = new Image();
    preload_image_object.src = pre;
  });





	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers JPEGs
  $("a.rolloverJpg").hover(
    function() {
      curr = $(this).find("img").attr("src");
      overlen = curr.length;
      over = curr.substr(0, overlen-4);
      over = over+'_on.jpg';
      $(this).find("img").attr({ src: over});
    },
    function() {
      $(this).find("img").attr({ src: curr});
    }
  )
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers - Preload images JPEGs
  $("a.rolloverJpg").find("img").each(function(i){
    temp = this.src;
    prelen = temp.length;
    pre = temp.substr(0, prelen-4);
    pre = pre+'_on.jpg';
    preload_image_object = new Image();
    preload_image_object.src = pre;
  });




	$("select#InsuranceTypeSelector").change(function(){
		var insurance_type_slug = $(this).attr("value");
		var new_url = insurance_type_slug;
		window.location = new_url;
	});
	
	

  // Form validation
  var validator = jQuery("form#InsuranceInfoRequestForm").validate({
      focusInvalid: true,
      rules: {
				'data[InsuranceInfoRequest][first_name]': {
					minlength: 1,
					required: true
				},
				'data[InsuranceInfoRequest][last_name]': {
					minlength: 1,
					required: true
				},
				'data[InsuranceInfoRequest][email]': {
					email: true,
					minlength: 10,
					required: true
				}
      },
      messages: {
				'data[InsuranceInfoRequest][first_name]': {
					minlength: "Invalid",
					required: "Required"
				},
				'data[InsuranceInfoRequest][last_name]': {
					minlength: "Invalid",
					required: "Required"
				},
				'data[InsuranceInfoRequest][email]': {
					email: "Invalid Email",
					minlength: "Invalid",
					required: "Required"
				}
      },
      submitHandler: function(form) {
          jQuery(form).ajaxSubmit({
              target: "div#InsuranceInfoRequestFormContainer"
              // beforeSubmit:  showRequest,  // pre-submit callback
              // success:       showResponse  // post-submit callback
              // url:       url         // override for form's 'action' attribute
              // type:      type        // 'get' or 'post', override for form's 'method' attribute
              // dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
              // clearForm: true        // clear all form fields after successful submit
              // resetForm: true        // reset the form after successful submit
              // $.ajax options can be used here too, for example:
              // timeout:   3000
          });
          // $('div#sidebar_patentCalculatorFormEntry').hide();
      }
  });
	
	
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// Home Slideshow
	$('div#slideshowSlides').cycle({ 
    fx: 'fade', 
		pause: 500,
		timeout: 6000
		//     pager:  '#slideshowNavList',
		//     pagerEvent: 'mouseover',
		// pagerAnchorBuilder: function(idx, slide) { 
		// 	// return selector string for existing anchor 
		// 	return '#slideshowNavList li:eq(' + idx + ') a'; 
		// }
	});
	// $("a.activeSlide").trigger('mouseenter'); 

  $('div.hoverSlide').hide(); // Hide the banners used for hover states
  $("ul#slideshowNavList li a").hover( // Pause and resume the slideshow when hovering the list
    function () { // Over - Pause slideshow
      $('div#slideshowSlides').cycle('pause');
    },
    function () { // Out - Resume slideshow
      $('div#slideshowSlides').cycle('resume');
    }
	);
  $("ul#slideshowNavList li a").hover( // Animate banners into slideshow area
    function () { // Over - Fade it in
			$('div#slideshowSlides').hide();
      $('div#hoverSlide_'+this.id).fadeIn('fast');
    },
    function () { // Out - Fade it out
      $('div#hoverSlide_'+this.id).fadeOut('fast');
			$('div#slideshowSlides').fadeIn('fast');
    }
	);



});

