var time='AM';
$(document).ready(function(){

    function changeWeather(){
        type='';
        if($(".mapBox .mapTools ul li a.btn-green,.mapBoxFull .mapTools ul li a.btn-green").hasClass('temperature')){
            type='temperature';
        }else if($(".mapBox .mapTools ul li a.btn-green,.mapBoxFull .mapTools ul li a.btn-green").hasClass('wind')){
            type='wind';
        }
        $(".icon").each(function(){
            $(this).find("p span").html("");
        });
        if(type==="temperature"){
            for(var i in weather){
                if(typeof(weather[i].weatherAM)!=="undefined" && time==='AM'){
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Weather iconTypeWeather"+weather[i].weatherAM);
                    });
                    $("#city_"+weather[i].id).find(".dataTempCelsius").html(weather[i].tempAM);
                    $(".timeStep").removeClass("current");
                    $("#amComment").addClass("current");
                }else if(typeof(weather[i].weatherPM)!=="undefined" && (time==='AM' || time==='PM')){
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Weather iconTypeWeather"+weather[i].weatherPM);
                    });
                    $("#city_"+weather[i].id).find(".dataTempCelsius").html(weather[i].tempPM);
                    $(".timeStep").removeClass("current");
                    $("#pmComment").addClass("current");
                }else if(typeof(weather[i].weatherEvening)!=="undefined" && (time==='AM' || time==='PM' ||time==='Evening')){
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Weather iconTypeWeather"+weather[i].weatherEvening);
                    });
                    $("#city_"+weather[i].id).find(".dataTempCelsius").html(weather[i].tempEvening);
                    $(".timeStep").removeClass("current");
                    $("#eveningComment").addClass("current");
                }else{
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Weather iconTypeWeatherN"+weather[i].weatherNight);
                    });
                    $("#city_"+weather[i].id).find(".dataTempCelsius").html(weather[i].tempNight);
                    $(".timeStep").removeClass("current");
                    $("#nightComment").addClass("current");
                }
            }
        }else if(type==="wind"){
            for(var i in weather){
                if(weather[i].windPower>55){
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Wind iconTypeWind_"+weather[i].windDirection+"_rouge");
                    });
                }else{
                    $("#city_"+weather[i].id).find("span .iconSize40_Weather,span .iconSize40_Wind,a .iconSize40_Weather,a .iconSize40_Wind").each(function(){
                        $(this).removeClass();
                        $(this).addClass("iconSize40_Wind iconTypeWind_"+weather[i].windDirection+"_bleu");
                    });
                }
                $("#city_"+weather[i].id+" span").html(weather[i].windPower);
            }
        }
    }

    var cacheImage = document.createElement('img');
    cacheImage.src =WEBROOT+"/ressources/images/icon/icon-weather-40.png";
    
    changeWeather();
    $(".mapBox .mapTools ul li,.mapBoxFull .mapTools ul li").click(function(){
        if($(this).find('a').hasClass('temperature') || $(this).find('a').hasClass('wind')){
            $(".mapBox .mapTools ul li a,.mapBoxFull .mapTools ul li a").not(this).each(function(){
                $(this).removeClass("btn-green");
            });
            $(this).find('a').addClass("btn-green");
            changeWeather();
            return false;
        }
    });

    $("#amComment").click(function(){
        time="AM";
        changeWeather();
        $(".timeStep").removeClass("current");
        $("#amComment").addClass("current");
    });
    $("#pmComment").click(function(){
        time="PM";
        changeWeather();
        $(".timeStep").removeClass("current");
        $("#pmComment").addClass("current");
    });
    $("#eveningComment").click(function(){
        time="Evening";
        changeWeather();
        $(".timeStep").removeClass("current");
        $("#eveningComment").addClass("current");
    });
    $("#nightComment").click(function(){
        time="Night";
        changeWeather();
        $(".timeStep").removeClass("current");
        $("#nightComment").addClass("current");
    });
    $(".moreTabs ul").hide();
    $(".moreTabs").hover(function(){
        if($(this).find("ul").is(":hidden")){
            $(this).find("ul").slideDown(200);
        }
    },function(){
        if($(this).find("ul").is(":visible")){
            $(this).find("ul").slideUp(200);
        }
    });
    //fonction cluetip
    setTimeout(function(){
        $('area, .mapWeatherData .icon a, .mapWeatherData .icon span:not(.dataTempCelsius)').cluetip({
            tracking : true,
            splitTitle: '|',
            cursor:'',
            clickThrough : true,
            showTitle : false,
            positionBy : 'mouse',
            topOffset : '-45',
            leftOffset : '-32'
        });
    },200);
});

