(function($) { // get around the jQuery no conflict

	jQuery.fn.extend({
	  scrollTo : function(speed, easing) {
	    return this.each(function() {
	      var targetOffset = $(this).offset().top-50;
	      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
	    });
	  }
	});

	jQuery.easing['jswing'] = jQuery.easing['swing'];
	jQuery.extend( jQuery.easing,
	{
		def: 'easeOutQuad',
		swing: function (x, t, b, c, d) {
			//alert(jQuery.easing.default);
			return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
	});


	$(function(){ // DOM ready
		// check they really want to leave
		// if (textfields.value().length)... window.onleave
		
		$(".datepicker").datepicker({ 
			yearRange: "-100:+20", 
			dateFormat: "dd/mm/y",
		    showOn: "both", 
		    buttonImage: "../images/car-calculator/calendar.gif", 
		    buttonImageOnly: true 
		});
		
		// Wizard
		if (!$('.form_builder .required-alert').length) { 		
			buttonHTML = ' <div class="frmcontrols"><button class="frmback">Back</button> <button class="frmnext">Next</button></div> ';
			$('.form_builder div.fields').after(buttonHTML); // set up the buttons

			$('.form_builder .frmback').eq(0).remove(); // remove 1st back button
			$('.form_builder .frmnext').slice(-1).remove(); // remove last next button
	
			$('.form_builder fieldset').hide().eq(0).show(); // hide the fields and show the 1st
	
			$('.form_builder .frmnext, .form_builder .frmback').click(function() { // on button click
				var controls = $(this).parent();
				var thisfieldset  = controls.parent();
				var otherfieldset = (($(this).attr("class") == 'frmnext') ? controls.parent().next() : controls.parent().prev());
				
				thisfieldset.slideUp("normal", function () { // hide fields in this fieldset
					otherfieldset.slideDown("normal", function() { // show other fieldset's fields
						$(otherfieldset).scrollTo(500); // scroll to the other field when its expanded
						$(otherfieldset).children('div.fields').children().children(':input').get(0).focus();
					}) 
				});
				
				return false; // dont submit form!
			});
			
		}// end of wizard stuff
		
		// Table of contents
		$(".form_toc a").click(function(){
			var otherfieldset = $( $(this).attr("href") ); // the 1 to go to
			if (!$($(this).attr("href") + ':visible').length) { // if the 1 to go to 1 isnt visible already
                var thisfieldset = $('fieldset:visible');
				thisfieldset.slideUp("normal", function () { // hide fields in this fieldset
					otherfieldset.slideDown("normal", function() { // show other fieldset's fields
						$(otherfieldset).scrollTo(500); // scroll to the other field when its expanded
						$(otherfieldset).children('div.fields').children().children(':input').get(0).focus();
					}) 
				});
			} else { // if the 1 to go to 1 is visible already
				$(otherfieldset).scrollTo(500); // scroll to the other field when its expanded
				$(otherfieldset).children('div.fields').children().children(':input').get(0).focus();
			}
		});			

	});//end of DOM ready
})(jQuery);
