﻿var Weather = {
    
    worldCityList: null,
    
    isPopupInitialized: false,
    
    tabsClickInit: function() {
        $(".Tab").click(function () {
          Weather.tabToggle();
        });
    },
    
    tabToggle : function(){
		 $(".Tab").toggleClass("UnSelectedTab");
         $(".Tab").toggleClass("SelectedTab");
         $(".CityContainer").toggle();
    },
    
    getCityIdCookie : function(){
		var cityId =  (new Cookie("CityID")).getCookie();
		cityId = (typeof(cityId) != "undefined")?cityId:"38"; /*default is Tel-Aviv 38*/
		return (cityId);
    },
    
    setCityIdCookie : function(cityId){
		var oCookie = new Cookie("CityID");	
		oCookie.setCookie(cityId, 60*24, "nana10.co.il");	
    },
    
    saveCityClickInit: function() {
        $(".SaveButton").click(function () {
            var tabId = $(".SelectedTab")[0].id;
            var cityID = "";
            if(tabId == "TabLocalCity") {
                cityID = $("#LocalCity").val();
                if(cityID > 0 && $("#LocalCity")[0].selectedIndex > -1) {
                    if(document.all) {
                        $("#PopupCity")[0].innerText = $("#LocalCity")[0].options[$("#LocalCity")[0].selectedIndex].text;
                    }
                    else {
                        $("#PopupCity")[0].textContent = $("#LocalCity")[0].options[$("#LocalCity")[0].selectedIndex].text;
                    }
                }
                else {
                    if(document.all) {
                        $("#PopupCity")[0].innerText = " ";    
                    }
                    else {
                        $("#PopupCity")[0].textContent = " ";    
                    }
                }    
            }    
            else if(tabId == "TabWorldCity") {
                cityID = $("#WorldCity").val(); 
                if(cityID > 0) {
                    if(document.all) {
                        $("#PopupCity")[0].innerText = $("#WorldCityText").val();  
                    }
                    else {
                        $("#PopupCity")[0].textContent = $("#WorldCityText").val();  
                    }
                }    
                else {
                    if(document.all) {
                        $("#PopupCity")[0].innerText = " ";       
                    }    
                    else {
                        $("#PopupCity")[0].textContent = " ";       
                    }
                }    
            }    
            else {
                cityID = 0;  
                if(document.all) {
                    $("#PopupCity")[0].innerText = " ";  
                }
                else {
                    $("#PopupCity")[0].textContent = " ";       
                }
            }    
            Weather.setCityIdCookie(cityID);
            parent.$.nyroModalRemove();
        });
    },
    
    closePopup: function() {
        //http://common" + sEnvironment + ".nana10.co.il/Multiplayer/Get.ashx?ServiceID="+MP.ServiceID        
        var cityID = Weather.getCityIdCookie();
        $.getJSON("http://common" + sEnvironment + ".nana10.co.il/PersonalBar/Widget/Weather/Get.ashx?CityID="+cityID + "&callback=?", function(data){
            Weather.weatherDataReceived(data, "", "");
        });	
    },
    
    weatherDataReceived: function(data, userContext, methodName) {
	    if(data != null)
	    {
		    if (data.ActionSucceeded != false) {
		        var CityCode = data.Code;
                if (CityCode == 0)
                    CityCode = 1220;
                    
		        $("#WeatherWidget_WeatherImg")[0].src = "http://f"+ sEnvironment +".nau.co.il/partner48/Common/Images/PersonalBar/Weather/" + CityCode + ".gif";  
		        if(document.all) {
			        $("#WeatherWidget_CityName")[0].innerText = data.CityName;  
			        $("#WeatherWidget_TemperatureMin")[0].innerText = data.TemperatureMin;  
			        $("#WeatherWidget_TemperatureMax")[0].innerText = data.TemperatureMax;  
			    }
			    else {
			        $("#WeatherWidget_CityName")[0].textContent = data.CityName;  
			        $("#WeatherWidget_TemperatureMin")[0].textContent = data.TemperatureMin;  
			        $("#WeatherWidget_TemperatureMax")[0].textContent = data.TemperatureMax;  
			    }
		    }
	    }
	},
	
	worldCityListReceived: function(data) {
	    if(data != null) {
		        Weather.worldCityList = data;
		    
	          $("#WorldCityText").autocomplete(Weather.worldCityList, {
	            minChars: 0,
	            width: 158,
	            matchContains: true,
	            autoFill: true,
	            formatItem: function(row, i, max) {
		            return row.country + ", " + row.city;
	            },
	            formatMatch: function(row, i, max) {
		            return row.country + " " + row.city;
	            },
	            formatResult: function(row) {
		            return row.country + ", " + row.city;
	            }
            });

             $("#WorldCityText").result(function(event, data, formatted) {
                $("#WorldCity").val(data.id);
             });
	  }
	},
    
    onPopupEndShowContent: function() {
        //alert("onPopupEndShowContent");
        //debugger;
        if(!parent.Weather.worldCityList) {
            $.getJSON("http://common" + sEnvironment + ".nana10.co.il/PersonalBar/Widget/Weather/WorldCityList.js?callback=?");	
        }
        else {
            Weather.worldCityListReceived(parent.Weather.worldCityList);
        }
        
        Weather.tabsClickInit();
        Weather.saveCityClickInit();
        
        var cityID = Weather.getCityIdCookie();
        if(cityID > 0 && cityID < 1000 && $("#LocalCity")[0].selectedIndex > -1) {
            $("#LocalCity").val(cityID);
            if(document.all) {
                $("#PopupCity")[0].innerText = $("#LocalCity")[0].options[$("#LocalCity")[0].selectedIndex].text;
            }
            else {
                $("#PopupCity")[0].textContent = $("#LocalCity")[0].options[$("#LocalCity")[0].selectedIndex].text;
            }
        }
        else if(cityID > 0 && cityID >= 1000) {
            $("#WorldCity").val(cityID);
            /*shoud to find CityID for display city name - TBD*/
            Weather.tabToggle();
        }
    },
    
    popupOpen: function() {
        $.nyroModalManual({
	        bgColor: "transparent",
	        url: "http://common" + sEnvironment + ".nana10.co.il/PersonalBar/Widget/Weather/WeatherPopup.aspx",
	        ltr: false,
	        modal: true,
	        resizeable: true,
	        autoSizable: true,
	        width: 215,
	        height: 280,
	        minWidth: 215,
	        minHeight: 280,
	        padding: 0,
	        endRemove: Weather.closePopup,
	        contentLoading: "<div id='AjaxLoading'><img src='http://f" + sEnvironment + ".nau.co.il/partner48/Common/Images/PersonalBar/ajax/ajax-loader.gif'>טוען... נא המתן</div>"
	        //endShowContent: Weather.onPopupEndShowContent
        });	
    }
};

