$('.illustration').ready(function(){

var c = [ 'illustration1','illustration2','illustration3'];
$(".illustration").randomClass( c);

});

$(document).ready(function() {
	$.datepicker.setDefaults($.datepicker.regional['ru']);
	$.datepicker.setDefaults({dateFormat: "dd.mm.yy"});
	
	$('#bo_tour_search\\[departure_date\\]\\[earliest\\]').datepicker({
		showOn: "both",
		buttonImage: "/fileadmin/templates/images/calendar.jpg",
		buttonImageOnly: true});
	
	$('#bo_tour_search\\[departure_date\\]\\[latest\\]').datepicker({
		showOn: "both",
		buttonImage: "/fileadmin/templates/images/calendar.jpg",
		buttonImageOnly: true});

	BO_FormInitHelper.initFormPresets();
	$('#bo_tour_search\\[departure_id\\]').bind("change", function() {
		BO_FormInitHelper.initForm();
	});

	$('#children_selector').bind('click', function() {
		BO_FormInitHelper.initChildrenSelector();
	});
	
	$("#bo_tour_search\\[hotel_name\\]").autocomplete({
		source: function(request, response) {
			var data = {
				bo_tour_search: {
					departure_id: $('#bo_tour_search\\[departure_id\\]').val(),
					country_id: $('#bo_tour_search\\[country_id\\]').val(),
					hotel_name: $('#bo_tour_search\\[hotel_name\\]').val()
				}
			};

			$.ajax({
				url: "/?eID=bo_tour_search_simpleform&action=hotel_search",
				dataType: "json",
				data: data,
				success: function(data) {
					response($.map(data, function(item) {
						return {
							label: item.name,
							value: item.name,
							id: item.hotel_id
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
			if(ui.item)
			{
				$('#bo_tour_search\\[hotel_id\\]').val(ui.item.id);
				$('#bo_tour_search\\[hotel_name\\]').val(ui.item.label);
			}
		},
		open: function() {
		},
		close: function(event, ui) {
		},
		search: function()
		{
			$('#bo_tour_search\\[hotel_id\\]').val("");
		},
		change: function(event, ui) {
			$('#bo_tour_search\\[hotel_id\\]').val("");
		}
	});

	$('#hotel_search_rank').click(function() {
		BO_FormInitHelper.setRankSearch();
	});
	$('#hotel_search_name').click(function() {
		BO_FormInitHelper.setHotelSearch();
	});
/*	
	var $hotel_dialog = $('#hotel_search_dialog')
		.dialog({
			autoOpen: false,
			title: 'Тип поиска',
			resizable: false,
			draggable: false,
			close: function() {
				$('#hotel_search_dialog input').val([$('#remember_search_dialog_state').val()]);
			},
			buttons: {
				"отменить": function() {
					$(this).dialog("close"); 
				}, 
				"вот так!": function() {
					$('#remember_search_dialog_state').val($('#hotel_search_dialog input:checked').val());
					if($('#hotel_search_dialog input:checked').val() == "hotel")
						BO_FormInitHelper.setHotelSearch();
					else
						BO_FormInitHelper.setRankSearch();						
					$(this).dialog("close"); 
				}
			}
		});
*/	
	/*
	$('#hotel_search_button').bind("click", function() {
		$hotel_dialog.dialog('open');
		pos = $('#hotel_search_button').offset();
		height = $('#hotel_search_button').outerHeight() - $(document).scrollTop();
		$('#hotel_search_dialog').dialog( 'option', 'position', [pos.left, pos.top + height] );
	});
	*/
	
	$('#bo_tour_search_form').bind("submit", function() {
		if($('#hotel_search_mode').val() == "rank")
		{
			$('#bo_tour_search\\[hotel_name\\]').val("");
			$('#bo_tour_search\\[hotel_id\\]').val("");
		}
		else
		{
			$('#bo_tour_search\\[rank\\]').removeAttr("disabled");	
		}
		
		/*
		if($('#hotel_search_dialog input:checked').val() == "rank")
		{
			$('#bo_tour_search\\[hotel_name\\]').val("");
			$('#bo_tour_search\\[hotel_id\\]').val("");
		}

		if($('#hotel_search_dialog input:checked').val() == "hotel" && $('#bo_tour_search\\[hotel_name\\]').val() == "")
		{
			$('#bo_tour_search\\[rank\\]').removeAttr("disabled");	
		}
		*/
	});
});

$(window).load(function() {
	$('#ui-datepicker-div').wrap('<div class="bo" />');
})

$(window).load(function() {
	
	$('#hot-tour-button').bind("click", function() {
		$('#hot-tour-dialog').dialog('open');
		return false;
	});

	jQuery.validator.addMethod("phoneOrEmailHot", function(value, element, params) { 
		if($('#hot-tour-form #phone').val() == "" && $('#hot-tour-form #email').val() == "")
			return false;
		else
			return true;
	}, '&nbsp;');

	var v = $("#hot-tour-form").validate({
		rules: {
			name: "required",
			countries: "required",
			dates: "required",
			phone: "phoneOrEmailHot",
			email: {
				phoneOrEmailHot: true,
				email: true
			}
		}/*,
		messages: {
			name: '&nbsp;',
			phone: '&nbsp;',
			email: {
				required: '&nbsp;',
				email: '&nbsp;'
			}
		}*/,
		errorPlacement: function (error, element) {
			element.addClass("ui-state-error");
		},
		errorClass: "ui-state-error",
		success: function(label) { 
			// set   as text for IE
			label.prev().removeClass("ui-state-error");
		}
	});

	var $hot_tour_dialog = $('#hot-tour-dialog')
		.dialog({
			autoOpen: false,
			title: 'Заявка на горящий тур',
			resizable: false,
			modal: true,
			width: 500,
			height: 620,
			buttons: {
				"Отменить": function() {
					$(this).dialog("close"); 
				}, 
				"Жду горящих туров": function() {
					var data = {
						name: $('#hot-tour-form #name').val(),
						phone: $('#hot-tour-form #phone').val(),
						email: $('#hot-tour-form #email').val(),
						countries: $('#hot-tour-form #countries').val(),
						dates: $('#hot-tour-form #dates').val(),
						comment: $('#hot-tour-form #comment').val()
					};
					var options = {
						url: "/?eID=bo_tour_search_simpleform&action=hot_tour",
						dataType: "json",
						data: data,
						success: function (data) {
							$('#hot-tour-dialog').dialog("close"); 
						}
					};
					
					if($("#hot-tour-form").validate().form() == true) {
						$.ajax(options);
					}
				}
			}
		});

});

var BO_FormInitHelper = {
	initFormPresets: function() {
		BO_FormInitHelper.loadData();
	},
	initForm: function() {
		// remeber previously selected country_id
		preset_country_id = null;
		if($('#bo_tour_search\\[country_id\\]').val() != null)
			preset_country_id = $('#bo_tour_search\\[country_id\\]').val();
		BO_FormInitHelper.loadCountries($('#bo_tour_search\\[departure_id\\]').val(), preset_country_id);
		/*
		if($('#bo_tour_search\\[departure_id\\]').val() != "" && $('#bo_tour_search\\[country_id\\]').val() == null)
			BO_FormInitHelper.loadCountries($('#bo_tour_search\\[departure_id\\]').val());
		if($('#bo_tour_search\\[country_id\\]').val() != null)
			BO_FormInitHelper.loadData($('#bo_tour_search\\[departure_id\\]').val(), $('#bo_tour_search\\[country_id\\]').val());
		*/
	},
	
	loadData: function(departure_id, country_id) {
		var query = $.query.get();
		$.ajax({
			url: '/?eID=bo_tour_search_simpleform',
			dataType: 'json',
			data: query,
			success: function(result) {
				BO_FormInitHelper.initData(result)
			}
		});
	},

	initData: function(data) {
		BO_FormInitHelper.initCountries(data.countries);
		BO_FormInitHelper.initDurations(data.durations);
		BO_FormInitHelper.initAdults(data.adults);
		BO_FormInitHelper.initChildren(data.children);
		BO_FormInitHelper.initRanks(data.ranks);
		BO_FormInitHelper.initFoods(data.foods);
		BO_FormInitHelper.initBudget(data.budgets);

		if(data.presets)
		{
			if(data.presets.departure_id)
				$('#bo_tour_search\\[departure_id\\]').val(data.presets.departure_id);
			if(data.presets.country_id)
				$('#bo_tour_search\\[country_id\\]').val(data.presets.country_id);
			if(data.presets.departure_date)
				BO_FormInitHelper.initDates(data.presets.departure_date);
			if(data.presets.duration)
				$('#bo_tour_search\\[duration\\]').val(data.presets.duration);
			if(data.presets.adults)
				$('#bo_tour_search\\[adults\\]').val(data.presets.adults);
			if(data.presets.children)
			{
				if(data.presets.children[0] != "")
					$('#bo_tour_search\\[children\\]\\[0\\]').val(data.presets.children[0]);
				if(data.presets.children[1] != "")
					$('#bo_tour_search\\[children\\]\\[1\\]').val(data.presets.children[1]);
				if(data.presets.children[2] != "")
					$('#bo_tour_search\\[children\\]\\[2\\]').val(data.presets.children[2]);
			}

			if(data.presets.hotel_id && data.presets.hotel_id != "")
			{
				BO_FormInitHelper.setHotelSearch();
				$('#bo_tour_search\\[hotel_id\\]').val(data.presets.hotel_id);
				$('#bo_tour_search\\[hotel_name\\]').val(data.presets.hotel_name);
			}
			else if(data.presets.hotel_name && data.presets.hotel_name != "")
			{
				$('#bo_tour_search\\[hotel_name\\]').val(data.presets.hotel_name);
				BO_FormInitHelper.setHotelSearch();
			}
			else if(data.presets.rank && data.presets.rank != "")
			{
				BO_FormInitHelper.setRankSearch();	
				$('#bo_tour_search\\[rank\\]').val(data.presets.rank);
			}
		
			if(data.presets.food_id && data.presets.food_id != "")
				$('#bo_tour_search\\[food_id\\]').val(data.presets.food_id);

			if(data.presets.budget && data.presets.budget != "")
				$('#bo_tour_search\\[budget\\]').val(data.presets.budget);

			BO_FormInitHelper.initChildrenSelector(data.presets.children[0] > 0 || data.presets.children[1] > 0 || data.presets.children[2] > 0);

			if(data.presets.form_active == 0)
				$('.tabs').tabs('select', 1);
			else
				$('.tabs').tabs('select', 0);
		}
	},

	setRankSearch: function() {
		$('#bo_tour_search\\[hotel_name\\]').attr("disabled", true);
		$('#bo_tour_search\\[hotel_name\\]').hide();
		$('#bo_tour_search\\[rank\\]').removeAttr("disabled");
		$('#bo_tour_search\\[rank\\]').show();
		
		$('#hotel_search_name').show();
		$('#hotel_search_rank').hide();

		$('#hotel_search_mode').val("rank");
	},

	setHotelSearch: function() {
		$('#bo_tour_search\\[rank\\]').attr("disabled", true);
		$('#bo_tour_search\\[rank\\]').hide();
		$('#bo_tour_search\\[hotel_name\\]').removeAttr("disabled");
		$('#bo_tour_search\\[hotel_name\\]').show();
		
		$('#hotel_search_name').hide();
		$('#hotel_search_rank').show();

		$('#hotel_search_mode').val("hotel");
	},
	
	initChildrenSelector: function(check) {
		if(check == true)
			$('#children_selector').attr('checked', true);

		if($('#children_selector').is(':checked'))
		{
			$('#child').show();
			$('#tour-search').css('padding-top', '0px');
		}
		else
		{
			$('#child').hide();
			$('#tour-search').css('padding-top', '10px')
			$('#bo_tour_search\\[children\\]\\[0\\]').val("");
			$('#bo_tour_search\\[children\\]\\[1\\]').val("");
			$('#bo_tour_search\\[children\\]\\[2\\]').val("");	
		}
	},

	loadCountries: function(departure_id, preset_country_id) {
		$.ajax({
			url: '/?eID=bo_tour_search_simpleform&departure_id=' + departure_id,
			dataType: 'json',
			success: function(result) {
				BO_FormInitHelper.initCountries(result, preset_country_id)
			}
		});
	},
	
	initRanks: function(ranks) {
		$('#bo_tour_search\\[rank\\]*').find('option').remove()
		jQuery.each(ranks, function(id, rank) {
			jQuery('#bo_tour_search\\[rank\\]').append(
				jQuery('<option></option').val(id).text(rank)
			);
		});
	},

	initChildren: function(children) {
		$('#bo_tour_search\\[children\\]\\[0\\]').find('option').remove()
		jQuery.each(children, function(id, children) {
			jQuery('#bo_tour_search\\[children\\]\\[0\\]').append(
				jQuery('<option></option').val(id).text(children)
			);
		});

		$('#bo_tour_search\\[children\\]\\[1\\]').find('option').remove()
		jQuery.each(children, function(id, children) {
			jQuery('#bo_tour_search\\[children\\]\\[1\\]').append(
				jQuery('<option></option').val(id).text(children)
			);
		});
		
		$('#bo_tour_search\\[children\\]\\[2\\]').find('option').remove()
		jQuery.each(children, function(id, children) {
			jQuery('#bo_tour_search\\[children\\]\\[2\\]').append(
				jQuery('<option></option').val(id).text(children)
			);
		});

	},

	initAdults: function(adults) {
		$('#bo_tour_search\\[adults\\]').find('option').remove()
		jQuery.each(adults, function(id, adults) {
			jQuery('#bo_tour_search\\[adults\\]').append(
				jQuery('<option></option').val(id).text(adults)
			);
		});
	},

	initDurations: function(durations) {
		$('#bo_tour_search\\[duration\\]').find('option').remove()
		jQuery.each(durations, function(id, duration) {
			jQuery('#bo_tour_search\\[duration\\]').append(
				jQuery('<option></option').val(id).text(duration)
			);
		});

	},
	
	initCountries: function(countries, preset_country_id) {
		$('#bo_tour_search\\[country_id\\]').find('option').remove()
		jQuery.each(countries, function(id, country) {
			if(country.type == "country")
				jQuery('#bo_tour_search\\[country_id\\]').append(
						jQuery('<option></option').val(country.place_id).text(country.name).addClass('select-country')
				);
			else
				jQuery('#bo_tour_search\\[country_id\\]').append(
					jQuery('<option></option').val(country.place_id).html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + country.name).addClass('select-place')
				);

		});

		if(preset_country_id != null)
			$('#bo_tour_search\\[country_id\\]').val(preset_country_id);
	},

	initDates: function(dates) {
		var min_date = new Date(dates.earliest.y, dates.earliest.m - 1, dates.earliest.d);
		var max_date = new Date(dates.latest.y, dates.latest.m - 1, dates.latest.d);

		//$("#bo_tour_search\\[departure_date\\]\\[start\\]").datepicker("option", "minDate", min_date).datepicker("setDate", min_date);
		//$("#bo_tour_search\\[departure_date\\]\\[end\\]" ).datepicker("option", "maxDate", max_date).datepicker("setDate", max_date);
		$("#bo_tour_search\\[departure_date\\]\\[earliest\\]").datepicker("setDate", min_date);
		$("#bo_tour_search\\[departure_date\\]\\[latest\\]" ).datepicker("setDate", max_date);
	},

	initFoods: function(foods) {
		$('#bo_tour_search\\[food_id\\]').find('option').remove();
		jQuery.each(foods, function(id, foods) {
			jQuery('#bo_tour_search\\[food_id\\]').append(
				jQuery('<option></option').val(foods["food_id"]).text(foods["description"])
			);
		});
	},
	
	initBudget: function(budgets) {
		$('#bo_tour_search\\[budget\\]').find('option').remove()
		jQuery.each(budgets, function(id, budgets) {
			jQuery('#bo_tour_search\\[budget\\]').append(
				jQuery('<option></option').val(id).text(budgets)
			);
		});
	}
}

function show_tour_details(tour_id)
{
	$('#tour_details_open\\[' + tour_id +  '\\]').fadeOut("slow");
	$('#tour_details\\[' + tour_id +  '\\]').fadeIn("slow");
}

function hide_tour_details(tour_id)
{
	$('#tour_details_open\\[' + tour_id +  '\\]').fadeIn("slow");
	$('#tour_details\\[' + tour_id +  '\\]').fadeOut("slow");
}

$(window).load(function() {
	var $hotel = $('<div id="hotel_dialog"></div>')
		.dialog({
			autoOpen: false,
			title: 'Просмотр отеля',
			modal: true,
			resizable: true,
			bgiframe: true,
			height: 500,
			width: 500
		});
	
	//$dialog.parents(".ui-dialog:eq(0)").wrap('<div class="bo"></div>');
	//$dialog.parent('.ui-dialog').wrap('<div class="bo" />');
	//$dialog.dialog("open");
});

function show_hotel_info(url)
{
	var hotel_url = '' + url;
	hotel_url  = "/?eID=bo_tour_search_simpleform&url=" + url;
	 $('#hotel_dialog').load(hotel_url).dialog('open');
	/*
	$("#hotel_dialog").load(hotel_url, [], function(){
	       $("#hotel_dialog").dialog("open");
	});
	*/
}
/*
$(window).load(function() {
	$("#amount").val('$' + $("#budget-range").slider("values", 0) + ' - $' + $("#budget-range").slider("values", 1));
});
*/

