function togglePersonalize() {
    $(".weather-personalize").css({ "visibility": $(".weather-personalize").css("visibility") == "hidden" ? "visible" : "hidden" });
    return false;
}

function doPersonalize(prefix) {
    var today   = new Date();
    var expires = new Date(today.getTime() + 1000 * 3600 * 24 * 365 * 5);

    document.cookie = "city=" + escape($("form.personalizeform select").val()) + "; path=/; expires=" + expires.toGMTString();
    $(".weather-personalize").css({ "visibility": "hidden" });
    
    location.href = prefix;
    
    return false;
}


function doPersonalizeTemperature(prefix) {
    var today   = new Date();
    var expires = new Date(today.getTime() + 1000 * 3600 * 24 * 365 * 5);
    
    if( $("#temperature li.cur").find('span').html() == '°F' ){
        document.cookie = "temperature=degC; path=/; expires=" + expires.toGMTString();
    } else {
		document.cookie = "temperature=degF; path=/; expires=" + expires.toGMTString();
	} 
    
    location.href = prefix;
    
    return false;
}


function doPersonalizeSpeed(prefix) {
    var today   = new Date();
    var expires = new Date(today.getTime() + 1000 * 3600 * 24 * 365 * 5);
	
    if( $("#speed li.cur").find('span').html() == 'Km/h' ){
        document.cookie = "speed=mile/h; path=/; expires=" + expires.toGMTString();
    } else {
        document.cookie = "speed=km/h; path=/; expires=" + expires.toGMTString();
    }
    
    location.href = prefix;
    
    return false;
}


function doPersonalizePressure(prefix) {
    var today   = new Date();
    var expires = new Date(today.getTime() + 1000 * 3600 * 24 * 365 * 5);

    if( $("#pressure li.cur").find('span').html() == 'mm' ){
        document.cookie = "pressure=mbar; path=/; expires=" + expires.toGMTString();
    } else {
        document.cookie = "pressure=mm_hg; path=/; expires=" + expires.toGMTString();
    }
    
    location.href = prefix;
    
    return false;
}


$(window).load(function(){
	     
		var time_delay = 10; // Задержка в секундах
	    var timeOut = null;
	 
	    $('.but_night, .but_day').click(function(e,simulated){
	         
	        // Параметр simulated устанавливается методом trigger
	        if(!simulated){
	             
	            // Произошло реальное событие click. Сбрасываем таймер.
	            clearTimeout(timeOut);
	        }
	    });
        
		// При загрузке карты мы сразу не меняем её на противоположную
		var run = 0;
		
        // Самовыполняющаяся функция:
	    (function autoAdvance(){
		    if( run ){
				if( $(".map").hasClass('night') ) {
					// Имитация события click на кнопке "следующий".
		            $('.but_day').trigger('click',[true]);
					
				} else {
					$('.but_night').trigger('click',[true]);
				} // if 
			} // if 
			 
			run = 1;
			
	        // Устанавливаем таймер на time_delay секунд.
	        timeOut = setTimeout(autoAdvance, time_delay * 1000);
            
	    })();
        
});


function doRetro(url, form) {
    location.href = url + '/retro/' + form.year.value + '-' + form.month.value + '-' + form.day.value + '/';

    return false;
}

$(function () {
    $.browser.msie && $('#id table').click(function() { document.location.href = $(this).parent().attr('href'); });
    $.browser.opera && $('.textmode ul').addClass('opera');
    
    $("#temperature li").click(function(){
        if( $(this).find('A') ) {
            var curent_tab = $("#temperature li.cur").attr("id");
            if( curent_tab == 't_left' ){
                $("#temperature").find('.cur').html('<i></i><a href="javascript:void 0;">'+ $("#temperature li.cur").find('span').html() +'</a>');
            } else {
                $("#temperature").find('.cur').html('<a class="wb" href="javascript:void 0;">'+ $("#temperature li.cur").find('span').html() +'</a><b></b>');
            }
            
            $("#temperature").find('.cur').removeClass('cur');
            $(this).addClass('cur');
            
            if( curent_tab == 't_left' ){
                $(this).html('<span>'+ $(this).find('A').html() +'</span><b></b>');
            } else {
                $(this).html('<span>'+ $(this).find('A').html() +'</span><i></i>');
            }
        }
    return false
    })
    
    $("#speed li").click(function(){
        if( $(this).find('A') ) {
            var curent_tab = $("#speed li.cur").attr("id");
            if( curent_tab == 's_left' ){
                $("#speed").find('.cur').html('<i></i><a href="javascript:void 0;">'+ $("#speed li.cur").find('span').html() +'</a>');
            } else {
                $("#speed").find('.cur').html('<a class="wb" href="javascript:void 0;">'+ $("#speed li.cur").find('span').html() +'</a><b></b>');
            }
            
            $("#speed").find('.cur').removeClass('cur');
            $(this).addClass('cur');
            
            if( curent_tab == 's_left' ){
                $(this).html('<span>'+ $(this).find('A').html() +'</span><b></b>');
            } else {
                $(this).html('<span>'+ $(this).find('A').html() +'</span><i></i>');
            }
        }
    return false
    })
    
    
    $("#pressure li").click(function(){
        if( $(this).find('A') ) {
            var curent_tab = $("#pressure li.cur").attr("id");
            if( curent_tab == 'p_left' ){
                $("#pressure").find('.cur').html('<i></i><a href="javascript:void 0;">'+ $("#pressure li.cur").find('span').html() +'</a>');
            } else {
                $("#pressure").find('.cur').html('<a class="wb" href="javascript:void 0;">'+ $("#pressure li.cur").find('span').html() +'</a><b></b>');
            }
            
            $("#pressure").find('.cur').removeClass('cur');
            $(this).addClass('cur');
            
            if( curent_tab == 'p_left' ){
                $(this).html('<span>'+ $(this).find('A').html() +'</span><b></b>');
            } else {
                $(this).html('<span>'+ $(this).find('A').html() +'</span><i></i>');
            }
        }
    return false
    })
    
    
    $("#d_left, #d_right").click(function(){
        if( $(this).find('A') ) {

            var curent_tab = $("#day_forecast li.cur").attr("id");
            
            if( curent_tab == 'd_left' ){
                $("#day_forecast").find('.cur').html('<i></i><a href="javascript:void 0;">'+ $("#day_forecast li.cur").find('span').html() +'</a>');
                
            } else {
                $("#day_forecast").find('.cur').html('<a class="wb" href="javascript:void 0;">'+ $("#day_forecast li.cur").find('span').html() +'</a>');
                
            }
            
            $("#day_forecast").find('.cur').removeClass('cur');
            $(this).addClass('cur');
            $(this).html('<span>'+ $(this).find('A').html() +'</span>');
            
            
            if( curent_tab != $("#day_forecast").find('.cur').attr("id") ){
                 if( document.getElementById('weather_10_day').style.display ){
                    document.getElementById('weather_10_day').style.display = '';
                 } else {
                    document.getElementById('weather_10_day').style.display = 'none';
                 }
            }
            
            
        }
    return false
    })
   
    // Нажимаем кнопку  "День"  
    $(".but_day").click(function(){
    
        var is_night_map  = $(".map").hasClass('night');
        var is_real_night = $("#map_day").val();
        
        
        
        if( is_night_map ){
            $(".map").removeClass('night');
            $(".but_night").removeClass('cur');
            $(this).addClass('cur');

            if( is_real_night ){
                $(".t_next_day").show();
                $(".t_night").hide();
                $(".t_curent").hide();
            } else {
                $(".t_next_day").hide();
                $(".t_night").hide();
                $(".t_curent").show();
            }
            
            
        } else {
            $(".but_night").removeClass('cur');
            $(this).addClass('cur');
        
            if( is_real_night ){
                $(".t_curent").hide();
                $(".t_next_day").show();
                $(".t_night").hide();
            } else {
                $(".t_curent").show();
                $(".t_next_day").hide();
                $(".t_night").hide();
            }

        }
 
    return false
    })
    
    
    // Нажимаем кнопку  "Ноч"  
    $(".but_night").click(function(){
    
        var is_night_map = $(".map").hasClass('night');
        var is_real_night = $("#map_day").val();
        
        if( is_night_map ){

            $(".but_day").removeClass('cur');
            $(this).addClass('cur');
            
            if( is_real_night ){
                $(".t_curent").show();
                $(".t_next_day").hide();
                $(".t_night").hide();
            } else {
                $(".t_curent").hide();
                $(".t_next_day").hide();
                $(".t_night").show();
            }
            
        } else {
            $(".map").addClass('night');
            $(".but_day").removeClass('cur');
            $(this).addClass('cur');
            
            if( is_real_night ){
                $(".t_curent").show();
                $(".t_next_day").hide();
                $(".t_night").hide();
            } else {
                $(".t_curent").hide();
                $(".t_next_day").hide();
                $(".t_night").show();
            }
            
        }
 
    return false
    })
    
    
});

