jQuery.noConflict();
jQuery(document).ready(function(){

        var weather;
        var lat;
        var lng;
        var weatherLat;
        var weatherLng;
        var coordsArray = new Array(undefined,undefined);
        var currentWeather;
        var currentLanguage;
        var weatherEnum = new Array();
        weatherEnum = ['chance_of_rain', 'sunny','mostly_sunny','partly_cloudy','cloudy',
                        'mostly_cloudy','chance_of_storm','rain','chance_of_snow','mist',
                        'storm','thunderstorm','chance_of_tstorm','sleet','snow','icy',
                        'dust','fog','smoke','haze','showers','flurries','clear',
                        'chance_of_showers','light_rain','partly_sunny'];

        

        coordsArray = geolocateGoogle(); // get lat lng and city and country based google map api
        lat = coordsArray[0];
        lng = coordsArray[1];
        if(lat == undefined || lng == undefined){
//            coordsArray = geolocateBrowser(); // get lat lng and city and country based browser
            lat = coordsArray[0];
            lng = coordsArray[1];
       }

       jQuery(window).load(function(){
           currentLanguage = jQuery('.currentLang').attr('id');
        jQuery.ajax({
            type: "POST",
            url: "http://technolen.eu/index.php/"+jQuery('.currentLang').attr('id')+"/weather/",
            data: "lat="+lat+"&lng="+lng+"&lang="+currentLanguage,
            success: function(data, status, xmlhttp){
                var obj = jQuery.parseJSON(data);
                var temperature;
                if(obj){
                    if(obj.temperature){
                        temperature = obj.temperature;
                        jQuery('#temperature').html(temperature+'°C');
                    }else{
                        jQuery('#temperature').html('temperature unavailable');
                        jQuery('#temperature').hide();
                    }
                    if(obj.condition){
                        currentWeather = obj.condition;
                        jQuery('#condition').html(obj.translated_condition);
                    }else{
                        jQuery('#condition').html('condition unavailable');
                    }
                }
                },
                error: function(xmlhttp, status, error){
                    //alert(error);
                    //renderPage(xmlhttp);
                },
                beforeSend: function(){
                    if(currentLanguage == 'en'){
                        jQuery('#condition').html('loading weather');
                    }else if(currentLanguage == 'cs'){
                        jQuery('#condition').html('načítání počasí');
                    }else if(currentLanguage == 'de'){
                        jQuery('#condition').html('Laden Wetter');
                    }
                    jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/sunny/'+currentLanguage+'.png) center center no-repeat');
                    jQuery("html").css('background-color','#FFFF99');
                    jQuery("#weatherContainer").css('background','none');
                },
                complete: function(){
                    //jQuery('#condition').hide();
                    currentLanguage = jQuery('.currentLang').attr('id');
                    if(currentWeather == undefined || !in_array(currentWeather,weatherEnum)){
                        //set random weather
                        currentWeather = weatherEnum[Math.floor(Math.random()*weatherEnum.length)];
                        
                        if(currentLanguage == 'en'){
                            jQuery('#condition').html('random weather');
                        }else if(currentLanguage == 'cs'){
                            jQuery('#condition').html('náhodné počasí');
                        }else if(currentLanguage == 'de'){
                            jQuery('#condition').html('Random Wetter');
                        }
                        //jQuery('#condition').show();
                    }

                    //currentWeather = 'fog';//testing

                    if(currentWeather == 'sunny'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#FFFF99');
                        jQuery("#weatherContainer").css('background','none');
                    }
                    else if(currentWeather == 'partly_sunny'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#FFFF99');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }
                    else if(currentWeather == 'clear'){//clear ma bejt sunny
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#FFFF99');
                        jQuery("#weatherContainer").css('background','none');
                    }
                    else if(currentWeather == 'haze'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#99CCFF');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) bottom center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }
                    else if(currentWeather == 'icy'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','white');
                    }
                    else if(currentWeather == 'light_rain'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#BFDDF3');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                        resumeRain();
                    }else if(currentWeather == 'mostly_sunny'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#FFFF99');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }else if(currentWeather == 'partly_cloudy'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#BFDDF3');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }else if(currentWeather == 'mostly_cloudy' || currentWeather == 'cloudy'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#BFDDF3');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }
                    else if(currentWeather == 'sleet' || currentWeather == 'storm' || currentWeather == 'chance_of_storm' || currentWeather == 'chance_of_tstorm' || currentWeather == 'thunderstorm'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#9C9B9B');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                        if(currentWeather == 'sleet' || currentWeather == 'storm' || currentWeather == 'thunderstorm'){
                            resumeRain();
                        }
                    }
                    else if(currentWeather == 'fog' || currentWeather == 'mist' || currentWeather == 'smoke' || currentWeather == 'dust'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#B6D0E7');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) bottom center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                    }
                    else if(currentWeather == 'chance_of_rain' || currentWeather == 'chance_of_showers' || currentWeather == 'chance_of_snow' || currentWeather == 'snow'
                        || currentWeather == 'flurries' || currentWeather == 'rain' || currentWeather == 'showers'){
                        jQuery("html").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/'+currentLanguage+'.png) center center no-repeat');
                        jQuery("html").css('background-color','#BFDDF3');
                        jQuery("#weatherContainer").css('background','url(http://technolen.eu/themes/Common/img/weather/'+currentWeather+'/cloud.png) top center no-repeat');
                        jQuery("#weatherContainer").css('background-attachment','fixed');
                        if(currentWeather == 'rain' || currentWeather == 'showers'){
                            resumeRain();
                        }
                        if(currentWeather == 'snow' || currentWeather == 'chance_of_snow'){
                            resumeSnow();
                        }
                    }

                    //set ico image
                    jQuery("#weatherIco").css('background','url(http://technolen.eu/themes/Common/img/weather/icons/'+currentWeather+'.png)');
                }
          });
       });

       rain(); 
       snow();
        
        
        
        
  

  

});

function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;}
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
     return false;
}

function rain () {
    rain = new RainStorm();
    rain.stop();
}

function resumeRain () {
    rain.init();
    rain.active = true;
    jQuery("#bottomDiv").css('background-color','#6699cc');//'#6699cc'
    jQuery('body').everyTime(14000,function() {
                   var divHeight = jQuery("#bottomDiv").height()+1;
                   jQuery("#bottomDiv").height(divHeight+'px');
      jQuery('#weatherContainer').height(jQuery('#weatherContainer').height()-1 +'px');
                    rain.resizeHandler();
     });
}


function resumeSnow () {
    snow.init();
    snow.active = true;
    jQuery("#bottomDiv").css('background-color','white');
    jQuery('body').everyTime(20000,function() {
           var divHeight = jQuery("#bottomDiv").height()+1;
           jQuery("#bottomDiv").height(divHeight+'px');
           jQuery('#weatherContainer').height(jQuery('#weatherContainer').height()-1 +'px');
           snow.resizeHandler();
     });
}

function snow () {
    snow = new SnowStorm();
    snow.stop();
}



