var Rent = {

	run: function()
	{
		Rent.pickUp();
		Rent.airlineDiscount();
		Rent.discountCode();
		Rent.dates();
		Rent.carGroupsFormToogle();
		Rent.carGroupExtraPrice();
		Rent.carGroupTooltips();
	},

	pickUp: function()
	{
		var is = $j('#rent_form_is_dropoff_0');
		var check = $j('#rent_form_dt_dropoff_location, #rent_form_dd_dropoff_location');
		if (!is.is(':checked')) {
			check.hide();
		}
		is.click(function() {
			if (is.is(':checked')) {
				check.show();
			} else {
				check.hide();
			}
		});
		$j('#rent_form_pickup_location').change(Rent.loadPickUp);
	},
	
	loadPickUp: function()
	{
		var form = $j('#rent_form');
		var url = window.location.href;
		var hash = url.indexOf('?');
		if (hash != -1) {
			url = url.substring(0, hash);
		}
		url += '?ajax=getPickUpLocation';
		$j.ajax({
			url: url,
			dataType: 'html',
			type: 'POST',
			data: form.serialize(),
			success: function(data) {
				$j('#locationContent').html(data);
			}
		});
	},

	airlineDiscount: function()
	{
		var is = $j('#rent_form_airline');
		var check = $j('#rent_form_dt_flight_number, #rent_form_dd_flight_number');
		if (!is.val()) {
			check.hide();
		}
		is.change(function() {
			if (is.val()) {
				check.show();
			} else {
				check.hide();
			}
		});
	},

	discountCode: function()
	{
		var is = $j('#rent_form_is_discount_code_0');
		var check = $j('#rent_form_dt_discount_code, #rent_form_dd_discount_code');
		var input = $j('#rent_form_discount_code');
		if (!is.is(':checked')) {
			check.hide();
			input.val('');
		}
		is.click(function() {
			if (is.is(':checked')) {
				check.show();
			} else {
				check.hide();
				input.val('');
			}
		});
	},

	dates: function()
	{
		$j('#rent_form_from_date, #filter_form_from_date, #rent_form_to_date, #filter_form_to_date').datepicker({
			dateFormat: 'dd.mm.yy',
			numberOfMonths: 2,
			minDate: 0,
			gotoCurrent: true
		});
	},

	carGroupsFormToogle: function()
	{
		var links = $j('#carGroups td.continue a');
		links.css('visibility', 'visible');

		var trForms = $j('#carGroups tr.car_group_form');
		trForms.hide();

		links.click(function() {
			var link = $j(this);
			var trForm = link.closest('tr').next('tr.car_group_form');

			trForms.hide();
			trForm.show();

			links.css('visibility', 'visible');
			link.css('visibility', 'hidden');

			return false;
		});

		$j('#carGroups tr.car_group_form_show').show().prev('tr').find('td.continue a').css('visibility', 'hidden');
	},

	carGroupExtraPrice: function()
	{
		$j('.car_group_form form select').change(function() {
			var form = $j(this).closest('form');
			var url = window.location.href;
			var hash = url.indexOf('?');
			if (hash != -1) {
				url = url.substring(0, hash);
			}
			url += '?ajax=getCarGroupExtraPrice';
			$j.ajax({
				url: url,
				dataType: 'html',
				type: 'POST',
				data: form.serialize(),
				success: function(data) {
					form.closest('tr').prev('tr').find('td.price').html(data);
				}
			});
		});
	},

	carGroupTooltips: function()
	{
		$j('#carGroups .hover img.pic').hover(
			function() {
				$j(this).next('.tooltip').show();
			}, function() {
				$j(this).next('.tooltip').hide();
			}
		);
	}

}

$j(document).ready(Rent.run);
