$(document).ready(function(){ 		
	$('.trip_date').each(function () {
		var id = $(this).attr('id');
		var date = $(this).val();
		if (id.indexOf('departure') > 0) {
			$(this).datepicker({
				minDate: '0',
				onSelect: function(textDate, inst) {
					id = id.replace(/departure/, 'return');
					$('#'+id).datepicker('option', 'minDate', sub_date(textDate));
				}
			});
			id_other = id.replace(/departure/, 'return');
			date = $('#'+id_other).val();
			if (date) $(this).datepicker('option', 'maxDate', sub_date(date));		
		}
		if (id.indexOf('return') > 0) {
			$(this).datepicker({
				minDate: '0',
				onSelect: function(textDate, inst) {
					id = id.replace(/return/, 'departure');
					$('#'+id).datepicker('option', 'maxDate', sub_date(textDate));
				}
			});
			id_other = id.replace(/return/, 'departure');
			date = $('#'+id_other).val();			
			if (date) $(this).datepicker('option', 'minDate', sub_date(date))		
		}		
	});	
	
	$('.choice_city,.choice_city_h').bind('keyup paste cut', function () {
		var trip = $(this).attr('name');
		var city = $(this).val();
		var typetrip = $(this).attr('typetrip'); 
		
		var list = $(this).next();
		
		if (city.length > 2) {
			$.post(surl+'ajax/choice-city.php', {'city' : city, 'name_field': trip, 'typetrip': typetrip}, 
				function(data) {
					if (data.html) {
						list.css({display: 'block'}).html(data.html);						
if ($.browser.msie && $.browser.version <= '6.0') {
  $('select').css('visibility', 'hidden');
}					
					//	list.css({display: 'block'}).html(list.html() + data.html);

					} else {
						list.css({display: 'block'}).html("Sorry, request no result");
					}
				}, 'json'
			);			
		} else {
			list.css('display', 'none').html('');
		}
	});
	$('.cmtp').bind('click', function () { 
		var mtp = $(this).attr('mtp').toLowerCase(); 
		var ttp = $('form.active_type_trip').attr('name')
		$("form.active_type_trip").removeClass('active_type_trip');
		$("form[ttp='"+mtp+"']").addClass('active_type_trip');
		$('div.cmtp_active').removeClass('cmtp_active');
		$(this).addClass('cmtp_active');
		
		if ($(this).hasClass('mini_s_b')) {
			$('img.b_search').attr('ttp', mtp);
		}
	});
	
	$("li[l_name_field]").live('mouseover', function () {
		$("li[l_name_field]").removeClass('active_ref_city');
		$(this).addClass('active_ref_city');
	});
	
	$('.choice_city,.choice_city_h').bind('blur', function () {
		var code = $('div.list_c li.active_ref_city').attr('code_a');
		var field = $('div.list_c li.active_ref_city').attr('l_name_field');
		if (!code) {
			var code = $('div.list_c li:first').attr('code_a');
			var field = $('div.list_c li:first').attr('l_name_field');			
		}
		$("input[name='"+field+"']").val(code);
		if (field == 'f_trip_from') {
			$("#alert_one span").html(code);
		}
		var from = $("input[name='f_trip_from']").val();
		var to = $("input[name='f_trip_to']").val();
		if (from)
			$("#alert_two span").html(from.substr(from.indexOf('(')+1, 3) +' to ' + to.substr(to.indexOf('(')+1, 3));

		if ($.browser.msie && $.browser.version <= '6.0') {
		  $('select').css('visibility', 'visible');
		}		
		$('div.list_c').css('display', 'none').html('');
	});
	
	$('#trip_destinations,#trip_cruiseline').bind('change', cruise_start);	

	$('.b_search').live('click', function() {
		var ttp = $(this).attr('ttp'); 
		if (!$(this).hasClass('mini_s_i')) {
			switch (ttp) {
				case 'flights':
					if (!$('#f_trip_from').val()) {
						alert("Please enter a departure airport.");
						return;
					}
					if (!$('#f_trip_to').val()) {
						alert("Please enter an arrival airport.");
						return;
					}	
					if (!$('#f_trip_departure').val()) {
						alert("Please input data departure.");
						return;
					}
					if (!$('#f_trip_return').val() && $("#if_round").attr('checked')) {
						alert("Please input data return.");
						return;
					}
						break;
				case 'hotels':
					if (!$("input[name='h_trip_city']").val()) {
						alert('Please enter the city');
						return;
					}
					if (!$("input[name='h_trip_checkin_date']").val()) {
						alert('Please select check indate');
						return;
					}
					if (!$("input[name='h_trip_checkout_date']").val()) {
						alert('Please select check outdate');
						return;
					}
					break;
				case 'vacations':
					if (!$('#v_trip_from').val()) {
						alert("Please enter a departure airport.");
						return;
					}
					if (!$('#v_trip_to').val()) {
						alert("Please enter an arrival airport.");
						return;
					}	
					if (!$('#v_trip_departure').val()) {
						alert("Please input data departure.");
						return;
					}
					if (!$('#v_trip_return').val()) {
						alert("Please input data return.");
						return;
					}				
					break;
				case 'cruise':
					if ($('#trip_destinations').val() == 0) {
						alert("Please select destinations.");
						return;
					}
					if ($('#trip_departure_c').val() == 0) {
						alert("Please select a date.");
						return;
					}			
					break;
				case 'cars':
					if (!$("input[name='c_trip_city']").val()) {
						alert('Please enter the city');
						return;
					}
					if (!$("input[name='c_trip_departure']").val()) {
						alert('Please select check indate');
						return;
					}
					if (!$("input[name='c_trip_return']").val()) {
						alert('Please select check outdate');
						return;
					}
					break;							
			}
			if ($(this).hasClass('empty_checkbox')) {
				$(this).removeClass('empty_checkbox');			
			} else {
				if ($(this).hasClass('s_expand') && !$("form[ttp='"+ttp+"'] input.trip_adv:checked").length) {
					alert("Please select at least one website. Most users select at least three to compare.");
					return;
				}
			}
		}
		if ($(this).hasClass('s_expand')) {
			$('input.hidden_adv').removeClass('hidden_adv');
		}
//		$("form[ttp='"+ttp+"']").submit();
//alert(ttp);
		$("form.active_type_trip").submit();
	});
	if ($(":radio[name='f_type_trip'][value='round']").attr('checked')) {
		$('div.trip_radio_nochecked[val="round"]').removeClass('trip_radio_nochecked').addClass('trip_radio_checked');
		$('div.trip_radio_checked[val="one"]').removeClass('trip_radio_checked').addClass('trip_radio_nochecked');
		hideDataReturn('round');
	}	
	if ($(":radio[name='f_type_trip'][value='one']").attr('checked')) {
		$('div.trip_radio_nochecked[val="one"]').removeClass('trip_radio_nochecked').addClass('trip_radio_checked');
		$('div.trip_radio_checked[val="round"]').removeClass('trip_radio_checked').addClass('trip_radio_nochecked');
		hideDataReturn('one');
	}
	
	$('div.trip_radio_nochecked').live('click', function () {
		var val = $(this).attr('val');
		hideDataReturn(val);
		$('div.trip_radio_checked').removeClass('trip_radio_checked').addClass('trip_radio_nochecked');
		$(this).removeClass('trip_radio_nochecked').addClass('trip_radio_checked');
		$("input:radio[name='f_type_trip',checked]").removeAttr('checked');
		$("input:radio[name='f_type_trip'][value='"+val+"']").attr('checked', 'checked');
	});
	
	$("label.fl_trip").bind('click', function () {
		var val = $(this).attr('for');
		val = val.replace('if_', '');
		hideDataReturn(val);
		if ($('div.trip_radio_nochecked[val="'+val+'"]').hasClass('trip_radio_nochecked')) {
			$('div.trip_radio_checked').removeClass('trip_radio_checked').addClass('trip_radio_nochecked');		
			$('div.trip_radio_nochecked[val="'+val+'"]').removeClass('trip_radio_nochecked').addClass('trip_radio_checked');
		}		
	});

	$('#sm_subscribe').bind('click', function () {
		$("form[name='subscribe']").submit();
	});
	$('div.redtxt').bind('click', function () {
		$(this).prev().parent().submit();		
	});	


	$('.blue_deals').bind('mouseover', function() {
		if ($(this).hasClass('blue_deals_top')) {
			$(this).next().addClass('active');
		}else if ($(this).hasClass('blue_deals_bottom')) {
			$(this).prev().addClass('active');
		}
		$(this).addClass('active');
	});
	$('.blue_deals').bind('mouseout', function() {
		if ($(this).hasClass('blue_deals_top')) {
			$(this).next().removeClass('active');
		}else if ($(this).hasClass('blue_deals_bottom')) {
			$(this).prev().removeClass('active');
		}		
		$(this).removeClass('active');
	});
	
/*	$('#your_mail').bind('focus', function() {
		if ($(this).val() == 'enter your email address')
			$(this).val('');
	});
	$('#your_mail').bind('blur', function() {
		if (!$(this).val())
			$(this).val('enter your email address');
	});*/
	//cruise_start();
});
hideDataReturn = function (val) {
	if (val == 'round') {
		$('.data_return').css('display', 'block');//.parent().prev().css('display', 'block');
		date = $('#f_trip_return').val();
		if (date) $('#f_trip_departure').datepicker('option', 'maxDate', sub_date(date));
		date = $('#f_trip_departure').val();
		if (date) $('#f_trip_return').datepicker('option', 'minDate', sub_date(date));						
	} else {
		$('.data_return').css('display', 'none');//.parent().prev().css('display', 'none');
		$('#f_trip_departure').datepicker('option', 'maxDate', null);	
	}		
};
cruise_start = function () {
	var dest = $('#trip_destinations').val();
	var cruise = $('#trip_cruiseline').val();
	$.post(surl+'ajax/choice-adv_dest.php', {'dest': dest, 'cruise': cruise},
		function (data) {
			$("#id_ttp_cruise").html(data.adv);
		}, 'json'
	);
};
sub_date = function(date) {
	return Math.ceil((new Date(date).getTime() - new Date().getTime())/(24*3600000));
}
