     //only location need to be saved for a year, other will be disappeared after closed
     /*
     These are the three cookies we need to save
         $.cookie("locationWeather", locationWeather, { expires: 365 });
                $.cookie("currentWeather_icon",  currentWeather["icon"]);
                $.cookie("currentWeather_temp_f",  currentWeather["temp_f"]);
                $.cookie("currentWeather_temp_c",  currentWeather["temp_c"]); 
                $.cookie("weatherUnit", CurrentUnit, { expires: 365 }); 
                */
 
    var imageServerURL="http://www.google.ca";
    var locationWeather="";
    var currentWeather;
    var forecastWeather;
    var forecastDayWeather;
    var CurrentUnit = 'c';
    
    
     function getWeather(){
        if($('#txtWeatherAddress').val().length<5){
            alert ("Please enter your location in format of city,country or zip,country then submit");
            return;
        }else{
              $("#indicator_weather").show("fast");
              $("#divWeatherOut").hide("slow");
              getCurrentWeatherFromLocation($('#txtWeatherAddress').val(),CurrentUnit);
        }
        
     }
    
    
    
    function getCurrentWeatherFromLocation(address,unit){
        $('#txtWeatherAddress').val(address)
        $.post("/getWeathers", {location: escape(address)}, 
        function(data){   
            if($(data).find("problem_cause").attr("data")==""){
                alert ("Sorry, we can not find weather forecast information for the location you provided.\nPlease enter location in format of \"city,country\" in ENGLISH and try again.");
                 $("#indicator_weather").hide("fast");       
            }else{
       
                locationWeather = $(data).find("forecast_information").find("city").attr("data") +"";
                //current condition
                currentWeather=new Array();  
                if($(data).find("current_conditions").find("icon").attr("data")==""){
                   currentWeather["icon"] = "/images/weatherDefault.png"; 
                }else{
                   currentWeather["icon"] =imageServerURL  + $(data).find("current_conditions").find("icon").attr("data");  
                }
                currentWeather["humidity"] = $(data).find("current_conditions").find("humidity ").attr("data"); 
                currentWeather["temp_f"]=  $(data).find("current_conditions").find("temp_f").attr("data"); 
                currentWeather["temp_c"]=  $(data).find("current_conditions").find("temp_c").attr("data"); 
                currentWeather["condition"] =  $(data).find("current_conditions").find("condition").attr("data"); 
                currentWeather["wind_condition"] =  $(data).find("current_conditions").find("wind_condition").attr("data"); 
                
               // alert ("wind condi s " + currentWeather["wind_condition"] )
               
                //only location need to be saved for a year, other will be disappeared after closed
                $.cookie("locationWeather", locationWeather, { expires: 365 });
                $.cookie("currentWeather_icon",  currentWeather["icon"]);
                $.cookie("currentWeather_temp_f",  currentWeather["temp_f"]);
                $.cookie("currentWeather_temp_c",  currentWeather["temp_c"]);
                $.cookie("wind_condition",  currentWeather["wind_condition"]);  
                $.cookie("weatherUnit", unit, { expires: 365 }); 
                
             
                //weather forecast
                forecastWeather = new Array();   
                forecastDayWeather=0;
                $(data).find("forecast_conditions").each(function(){                    
                    forecastWeather[forecastDayWeather]=new Array();
                    forecastWeather[forecastDayWeather]["condition"] =  $(this).find("condition").attr("data");
                    forecastWeather[forecastDayWeather]["day_of_week"] = getWeekDayFull($(this).find("day_of_week").attr("data"));   
                    forecastWeather[forecastDayWeather]["low"] =  $(this).find("low").attr("data");
                    forecastWeather[forecastDayWeather]["high"] =  $(this).find("high").attr("data"); 
                    if($(this).find("icon").attr("data")==""){
                       forecastWeather[forecastDayWeather]["icon"]  = "/images/weatherDefault.png"; 
                    }else{
                        forecastWeather[forecastDayWeather]["icon"] = imageServerURL + $(this).find("icon").attr("data");
                    }
                    forecastDayWeather++;
                });
                
                updateWeather(); 
               // showCurrentWeather();   
            }                    
        });
    }
    
    
    function updateWeather(){
           $('#divLocation').html(locationWeather); 
           //current
            $('#imgCurrent').attr("src",  currentWeather["icon"]); 
         
           //forecast - today
           $('#imgDay_0').attr("src",  forecastWeather[0]["icon"]);   
           $('#forecastDesc_0').html(forecastWeather[0]["condition"]);
             //forecast - tomorrow
           $('#forecastDay_1').html(forecastWeather[1]["day_of_week"]);   
           $('#imgDay_1').attr("src",  forecastWeather[1]["icon"]);   
           $('#forecastDesc_1').html(forecastWeather[1]["condition"]);
           //forecast - day after tomorrow
           $('#forecastDay_2').html(forecastWeather[2]["day_of_week"]);   
           $('#imgDay_2').attr("src",  forecastWeather[2]["icon"]);   
           $('#forecastDesc_2').html(forecastWeather[2]["condition"]);
            //forecast - day after tomorrow
           $('#forecastDay_3').html(forecastWeather[3]["day_of_week"]);   
           $('#imgDay_3').attr("src",  forecastWeather[3]["icon"]);   
           $('#forecastDesc_3').html(forecastWeather[3]["condition"]);
           updateWeatherFigure();
           $("#divWeatherOut").show("slow");
           $("#indicator_weather").hide("fast");       
    }
    
    function updateWeatherFigure(){
    
        if($.cookie("weatherUnit")!=null){
           CurrentUnit =  $.cookie("weatherUnit");
        }
        
            $('#currentTemp').html( CurrentUnit=='c'?currentWeather["temp_c"]+" &deg;"+"C":currentWeather["temp_f"]+" &deg;"+"F" ); 
            $('#forecastTemp_0').html( CurrentUnit=='c'?( getCelsiusFromFahren(forecastWeather[0]["low"])+" &deg;"+"C" +" | "+getCelsiusFromFahren(forecastWeather[0]["high"]) +" &deg;"+"C") :( forecastWeather[0]["low"]+" &deg;"+"F" +" | "+ forecastWeather[0]["high"] +" &deg;"+"F" ) );
            $('#forecastTemp_1').html( CurrentUnit=='c'?( getCelsiusFromFahren(forecastWeather[1]["low"])+" &deg;"+"C" +" | "+getCelsiusFromFahren(forecastWeather[1]["high"]) +" &deg;"+"C") :( forecastWeather[1]["low"]+" &deg;"+"F" +" | "+ forecastWeather[1]["high"] +" &deg;"+"F" ) );
            $('#forecastTemp_2').html( CurrentUnit=='c'?( getCelsiusFromFahren(forecastWeather[2]["low"])+" &deg;"+"C" +" | "+getCelsiusFromFahren(forecastWeather[2]["high"]) +" &deg;"+"C") :( forecastWeather[2]["low"]+" &deg;"+"F" +" | "+ forecastWeather[2]["high"] +" &deg;"+"F" ) );
            $('#forecastTemp_3').html( CurrentUnit=='c'?( getCelsiusFromFahren(forecastWeather[3]["low"])+" &deg;"+"C" +" | "+getCelsiusFromFahren(forecastWeather[3]["high"]) +" &deg;"+"C") :( forecastWeather[2]["low"]+" &deg;"+"F" +" | "+ forecastWeather[3]["high"] +" &deg;"+"F" ) );
            
            if(CurrentUnit=='c'){
               $('#hrefUsingCent').removeClass('hrefWeatherUnSelectedUnit').addClass('hrefWeatherSelectedUnit');
               $('#hrefUsingFreh').removeClass('hrefWeatherSelectedUnit').addClass('hrefWeatherUnSelectedUnit'); 
               $('#currentDesc').html(currentWeather["condition"]   +"<br>"+ currentWeather["humidity"] +"<br>"+getKMWindStr(currentWeather["wind_condition"])); 
         
            
            }else{
                $('#hrefUsingCent').removeClass('hrefWeatherSelectedUnit').addClass('hrefWeatherUnSelectedUnit');
                $('#hrefUsingFreh').removeClass('hrefWeatherUnSelectedUnit').addClass('hrefWeatherSelectedUnit'); 
                $('#currentDesc').html(currentWeather["condition"]   +"<br>"+ currentWeather["humidity"] +"<br>"+currentWeather["wind_condition"]); 
         
            }
           showCurrentWeather();            
    }
    
    function getCelsiusFromFahren(tF){
        var farenhite = parseInt(tF);
        var celusis = ((5/9)*(farenhite-32)).toFixed(0);
        return celusis; 
    }
    
    function getKMFromMile(mile){
        var mile=parseInt(mile)
        var km = mile*1.609344;
        return km.toFixed(0); 
    }
    function getKMWindStr(mileStr){
        var tmpStr=mileStr.substring(0, mileStr.lastIndexOf(" "));
        var pos = tmpStr.lastIndexOf(" ");
        var stringWithoutFigure = mileStr.substring(0, pos);
        var mileFigure =  tmpStr.substr(pos);
        return  stringWithoutFigure+" "+  getKMFromMile(mileFigure)+" kph";
    }
    
    function  setWeatherUnit(unit){
        if(CurrentUnit!=unit){
            CurrentUnit=unit;
            $.cookie("weatherUnit", CurrentUnit, { expires: 365 });    
            updateWeatherFigure();
        }
    }
    
    function hideMeWeather(){
        $('#divWeatherWhole').slideUp('slow');       
    }
    
    var bDataNotRetrieved=1;
    
     $(document).ready(function() {       
       // put all your jQuery goodness in here.
      if($.cookie("currentWeather_icon")!=null && $.cookie("locationWeather")!=null && $.cookie("weatherUnit")!=null && $.cookie("currentWeather_temp_c")!=null && $.cookie("currentWeather_temp_f")!=null){    
            showCurrentWeather(); 
            $("#divWeatherHeaderCurrent").show(0); 
       }else if($.cookie("locationWeather")!=null){   
             bDataNotRetrieved=0; 
             getCurrentWeatherFromLocation($.cookie("locationWeather"), $.cookie("weatherUnit"));    
       }else{                                            
             $('#imgWeatherHeaderCurrent').attr("src", "/images/weatherDefault.png");
             $('#tmpWeatherContent').html("Customize<br>Weather");
             $("#divWeatherHeaderCurrent").show(0);  
       }  
      
    }); 
    
    function showCurrentWeather(){
           $('#imgWeatherHeaderCurrent').attr("src", $.cookie("currentWeather_icon")); 
           var pos= $.cookie("locationWeather").indexOf(",");
           var line1="";
           var line2="";
           if(pos>0){
              line1= $.cookie("locationWeather").substr(0,pos); 
              line2= $.cookie("locationWeather").substr(pos+1) +", ";
           }else{
              line1 =  $.cookie("locationWeather");   
           }
           $('#tmpWeatherContent').html(line1+"<br>"+ line2 + ($.cookie("weatherUnit")=='c'?$.cookie("currentWeather_temp_c")+" &deg;"+"C":$.cookie("currentWeather_temp_f")+" &deg;"+"F" )); 
           
           if(bDataNotRetrieved==0){
              bDataNotRetrieved=1;
              $("#divWeatherHeaderCurrent").show("fast");    
           }                                         
    }
    
    function  showWeatherConfig(){
        if($.cookie("locationWeather")!=null){
          getCurrentWeatherFromLocation($.cookie("locationWeather"), $.cookie("weatherUnit"));  
        }   
        $('#divWeatherWhole').slideDown('slow');       
    }
