var dateObjectName = new Date();
/**
 *
 *
 */
function get_country()
{
	jQuery.getJSON('/json/country/?'+dateObjectName.getTime(), '', onAjaxCountrySuccess);
}
function onAjaxCountrySuccess(obj) {
	var country_id = readCookie('country_id');
	var country = jQuery("select#country_select");
	country.empty();
	for (var i=0; i<obj.length; i++)
	{
		var opt = jQuery("<option></option>");
		opt.attr("value", obj[i].id).html(obj[i].name);
		if (obj[i].id == country_id)
		{
			opt.attr("selected", "selected");
		}
		opt.appendTo(country);	
	}
	var id = country.val();
	get_cities(id);
}

function set_country(id) {
	createCookie('country_id', id, 0);
	get_cities(id);
}

/**
 *
 *
 */
function get_cities(country) {
	jQuery.getJSON('/json/cities/'+ country + '/?'+dateObjectName.getTime(), '', onAjaxCitiesSuccess);
}
function onAjaxCitiesSuccess(obj) {
	var city_id = readCookie('city_id');
	var cities = jQuery("select#cities");
	cities.empty();
	for (var i=0; i<obj.length; i++)
	{
		var opt = jQuery("<option></option>");
		opt.attr("value", obj[i].id).html(obj[i].name);
		if (obj[i].id == city_id)
		{
			opt.attr("selected", "selected");
		}
		opt.appendTo(cities);	
	}
	var id = cities.val();
	createCookie('city_id', id, 0);
	get_hotels(id); // получаем список гостиниц
	get_metro(id); // получаем список метро
}

/**
 *
 *
 */
function set_city(id) {
	createCookie('city_id', id, 0);
	get_hotels(id); // получаем список гостиниц
	get_metro(id); // получаем список метро	
}

/**
 *
 *
 */
function get_hotels(id) {
	jQuery.getJSON('/json/hotels/'+ id + '/?'+dateObjectName.getTime(), '', onAjaxHotelsSuccess);
}
function onAjaxHotelsSuccess(obj) {
	var hotel_id = readCookie('hotel_id');
	var hotels = jQuery("select#hotels");
	hotels.empty();
	
	jQuery("<option></option>").attr("value", 0).html("All").appendTo(hotels); // по умолчанию
	for (var i=0; i<obj.length; i++)
	{
		var opt = jQuery("<option></option>");
		opt.attr("value", obj[i].id).html(obj[i].name);
		if (obj[i].id == hotel_id)
		{
			opt.attr("selected", "selected");
		}
		opt.appendTo(hotels);	
	}
	var id = hotels.val();
	createCookie('hotel_id', id, 0);
}

function set_hotel(id) {
	createCookie('hotel_id', id, 0);
}

/**
 *
 *
 */
function get_metro(id) {
	jQuery.getJSON('/json/metro/'+ id + '/?'+dateObjectName.getTime(), '', onAjaxMetroSuccess);
}
function onAjaxMetroSuccess(obj) {
	var metro_id = readCookie('metro_id');
	var metro_stations = jQuery("select#metro");
	metro_stations.empty();
	
	jQuery("<option></option>").attr("value", 0).html("Choose station").appendTo(metro_stations); // по умолчанию
	for (var i=0; i<obj.length; i++)
	{
		var opt = jQuery("<option></option>");
		opt.attr("value", obj[i].id).html(obj[i].name);
		if (obj[i].id == metro_id)
		{
			opt.attr("selected", "selected");
		}
		opt.appendTo(metro_stations);	
	}
	var id = metro_stations.val();
	createCookie('metro_id', id, 0);
}

function set_metro(id) {
	createCookie('metro_id', id, 0);
}

/**
 * Выполняется в момент загрузки страницы
 *
 */
jQuery(document).ready(function()
{
	//get_country();
});
