$(document).ready(function(){

    $('#weatherCountry').click(function(){
	$('#weatherCountry').hide();
	$("#weatherChooseCountryList").html('').parents('.jquery-selectbox').unselectbox();
	$("#weatherChooseCountryList").hide();
	$('#weatherChooseCountry').show();
    });

    $('#weatherCity').click(function(){
	$('#weatherCity').hide();
	$("#weatherChooseCityList").html('').parents('.jquery-selectbox').unselectbox();
	$("#weatherChooseCityList").hide();
	$('#weatherChooseCity').show();
    });

    $('#weatherChooseCountry').click(function()
    {
	$('#weatherChooseCountry').hide(); $('#weatherCountry').show();
	$("#weatherChooseCountryList").html('').parents('.jquery-selectbox').unselectbox();
	$.ajax(
	{
	    url : '/ajax/weather',
	    data : {action: 'countries', country_id: $('#weatherChooseCountry').attr('rel')},
	    cache : true,
	    dataType : 'json',
	    error : function(){alert('Request Timeout')},
	    success : function(data)
		    {
			$("#weatherChooseCountryList").html('').hide();

			options = '';//'<option>Выберите страну</option>';

			for(i in data)
			{
			    options += '<option value="'+ data[i].id + '">'+ data[i].name + '</option>'
			}

			$("#weatherChooseCountryList").html(options).val( $('#weatherChooseCountry').attr('rel') );
			$("#weatherChooseCountryList").selectbox().show();
			
			$("#weatherChooseCountryList").selectbox().bind('change', function()
			{
			    $('#weatherChooseCountry').attr('rel', $(this).val() ).text( $(this).find('option:selected').text() );
			    $("#weatherChooseCountryList").parents('.jquery-selectbox').unselectbox();
			    $("#weatherChooseCountryList").hide();
			    $('#weatherCountry').hide();$('#weatherChooseCountry').show();
			    $.cookie('weather_country', $(this).val(), {expires: 365*2, path: "/"});
			    
			    setTimeout(function() { $('#weatherChooseCity').click(); }, 1000);
			});
		    }
	});
	return false;
    });

    $('#weatherChooseCity').click(function(){
	$("#weatherChooseCityList").html('').parents('.jquery-selectbox').unselectbox();
	$('#weatherChooseCity').hide();$('#weatherCity').show();
	$.ajax(
	{
	    url : '/ajax/weather',
	    data : {action: 'cities', country_id: $('#weatherChooseCountry').attr('rel')},
	    cache : true,
	    dataType : 'json',
	    error : function(){alert('Request Timeout')},
	    success : function(data)
		    {
			$("#weatherChooseCityList").html('').hide();

			options = '';//<option>Выберите город</option>';

			for(i in data)
			{
			    options += '<option value="'+ data[i].id + '">'+ data[i].name + '</option>'
			}

			$("#weatherChooseCityList").html(options).val( $('#weatherChooseCity').attr('rel') );
			$("#weatherChooseCityList").selectbox().show();

			$("#weatherChooseCityList").selectbox().bind('change', function()
			{
			    
			    $('#weatherChooseCity').attr('rel', $(this).val() ).text( $(this).find('option:selected').text() );
			    $("#weatherChooseCityList").parents('.jquery-selectbox').unselectbox();
			    $("#weatherChooseCityList").hide();

			    $('#weatherCity').hide();$('#weatherChooseCity').show();
			    $.cookie('weather_city', $(this).val(), {expires: 365*2, path: "/"});

			    setTimeout(function() { location.reload();}, 1000);
			});
		    }
	});
	return false;
    });

});
