function resizeFooter()
{
	pageWidth=$(document).width();
	$('.footer').css('width',pageWidth+'px');
}

$(document).ready(function()
{
	//footer width
	resizeFooter();
	
	$(window).resize(function() {
	resizeFooter();
	});
	
	//EXTERNAL LINK
		$('a[rel="external"]').live('click', function() {
			window.open( $(this).attr('href') );
			return false;
		});
	
	$('.spinlogo').hover(function()
	{
		$(this).find('span').slideToggle('slow');
	},function()
	{
		$(this).find('span').slideToggle();
	});
	
	$('.locationarea li').click(function()
	{
		if($(this).hasClass('active'))
		{
			$(this).find('.location_info').slideToggle();
			$(this).removeClass('active');
		}
		else
		{
			$('.locationarea li').each(function()
			{
				$(this).removeClass('active');
				$(this).find('.location_info').hide('slow');
			});
			$(this).find('.location_info').slideToggle();
			$(this).addClass('active');
			return false;
		}
	});
	
	
	//reservation form date picker
	
	var dates = $( "#date_from, #date_to" ).datepicker({
			hideIfNoPrevNext: true,
			showOn: "both",
			buttonImage: "/images/cal.gif",
			buttonText: "",
			buttonImageOnly: true,
			defaultDate: "+3d",
			changeMonth: false,
			numberOfMonths: 2,
			minDate:"+3d",
			maxDate:"+1Y",
			dateFormat: 'dd/mm/yy',
			onSelect: function( selectedDate ) {
				var option = this.id == "date_from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
		
		var age_clicked=0;
		
		$('.age').click(function()
		{
			if(age_clicked==0)
			{
				age_clicked=1;
				myVal=$(this).attr('value');
				$('.age').attr('value','');
			}
		});
		
		
		
		//reservation - calculate data
		
		function calculateRate()
		{			
			var airport_subtotal=0;
			var this_airport_percentage=0;
			var airport_charge=false;
			if($('#airport_fee_percentage').length>0)
			{
				this_airport_percentage= parseFloat($('#airport_fee_percentage').html());
				airport_charge=true;
			}
			if($('#sales_tax_percentage').length>0)
			{
				sales_tax_percentage= parseFloat($('#sales_tax_percentage').html());
			}
			var this_num_days=parseInt($('#num_days').val());
			var this_car_subtotal=parseFloat($('#car_subtotal').html());
			var additionalprice=0;
			$('.additional_options').each(function()
			{
				if($(this).prop('checked'))
				{
					var this_item_price=parseFloat($(this).attr('dailyprice'))*this_num_days;
					additionalprice+=this_item_price;
					$(this).parent('div').parent('div').find('.price').find('.additional_item_price').html(this_item_price.toFixed(2));
				}
				else
				{
					$(this).parent('div').parent('div').find('.price').find('.additional_item_price').html('0.00');
				}
			});
			$('#additional_items_subtotal').html(additionalprice.toFixed(2));			
			var total_subtotal = this_car_subtotal+additionalprice;
			if(airport_charge)
			{
				airport_subtotal=(this_airport_percentage/100)*total_subtotal;
			}
			total_subtotal+=airport_subtotal;
			tax_subtotal=(sales_tax_percentage/100)*total_subtotal;	
			tax_total=tax_subtotal+airport_subtotal;
			$('#tax_subtotal').html(tax_total.toFixed(2));
			$('#sales_tax_amount').html(tax_subtotal.toFixed(2));
			grand_total=tax_subtotal+total_subtotal;
			$('#grand_total').html(grand_total.toFixed(2));
			
			down_payment_amount=grand_total/2;
			$('#amount_down_payment').html(down_payment_amount.toFixed(2));
			if(airport_charge) $('#airport_fee_amount').html(airport_subtotal.toFixed(2));
			
		}
		
		$('.additional_options').click(function()
		{
			calculateRate();
		});
		
		if($('#num_days').length>0) calculateRate();
		
		
		$('.reservation-submit').click(function()
		{
			$('#reservation-form').submit();
			return false;
		});
		
		$('.step4_go_back').click(function()
		{
			$('#reservation-form').attr('action','/reservation/step3').submit();
			return false;
		});
		
		$('.step3_go_back').click(function()
		{
			window.location.href = "/reservation/step2";
			return false;
		});
		
		
		
		
		//VALIDATE RESERVATION FORM
		$("#reservation-form").validate({
		  rules: {
			reservation_creditcard_number: {
			  required: true,
			  creditcard: true
			},
			reservation_creditcard_expiration_date: {
			  required: true
			},
			reservation_firstname: {
			  required: true
			},
			reservation_country: {
			  required: true
			},
			reservation_lastname: {
			  required: true
			},
			accept_terms: {
			  required: true
			},
			reservation_email: {
			  required: true,
			  email: true
			}
		  }
		});
		
		
		
		
		
});


