

var  weather=function(){
	//------------------------------------------------------------------------------------------------------------
	var self=this;
	var data=CityArr,
		refreshTimer = null, //自动刷新定时器
		state = "mini",
		weatherWidget,
		weatherWidget_now_pic,
		weatherWidget_now_temperature,
		weatherWidget_city,
		weatherWidget_time,
		weatherWidget_refresh,
		weatherWidget_moreInfoPanel,
		weatherWidget_selectCityPanel,
		weatherWidget_onlypic,
		weatherWidget_more,
		weatherWidget_detail,
		flag = false,
		selectProvince,
		selectCity,
		weatherWidget_cancelButton,
		weatherWidget_okButton;
		
	
	var _updateWeather = function(data_all){
		
		if(!data_all || data_all.msg!='success'){
			
			
			return ;
		}
		var data=data_all.current;
		var now_temperature = data.temp || 0;
		var city = data.city || "";
		var update_time = data.time || "";
		weatherWidget_now_temperature.innerHTML = now_temperature+'°C';
		weatherWidget_city.innerHTML = city;
		weatherWidget_city.title = city;
		weatherWidget_time.innerHTML = '更新时间 ' + update_time;
		if(jQuery.browser.msie && jQuery.browser.version>0 && jQuery.browser.version<7){
				jQuery(weatherWidget_onlypic).one('load',function(){
					var png = this.src;
					this.src = 'dzz/images/b.gif';
					this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "',sizingMethod='scale')";
					});
		jQuery(weatherWidget_now_pic).one('load',function(){
					var png = this.src;
					this.src = 'dzz/images/b.gif';
					this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "',sizingMethod='scale')";
			});
		}
		
		weatherWidget_now_pic.src=WEATHER_PIC_URL+data.img+'.png';
		weatherWidget_onlypic.src=WEATHER_PIC_URL+data.img+'.png';
		if(data_all.more){
			var data=data_all.more;
			var html='';
			var i=0;
			for(var day in data){
				i++;
				if(i<3)	html+='<div class="detail-item">';
				else html+='<div class="detail-item detail-item-last">';
					html+='<div class="detail-item-left"><img class="detail-item-icon" src="'+WEATHER_PIC_URL+data[day].img+'.png"></div>';
				
					html+='<div class="detail-item-right">';
					html+='<div>'+day+'日'+'<span class="detail-item-temp">'+data[day].temp+'</span></div>';
					html+='<div>'+data[day].weather+' - '+data[day].wind+'</div>';
					html+='</div>';
				html+='</div>';
			}
			
			weatherWidget_detail.innerHTML=html;
		}
	};
	var observer = {
		/*onRun: function(){
			if(flag){
				$D.show(this.window._titleBar);
				$D.show(this.window._bg_container);
				setTimeout(function(){if(packageContext.window._titleBar){$D.hide(packageContext.window._titleBar);$D.hide(packageContext.window._bg_container);}}, 1000);
			}
			flag=true;
		},*/
		onClickCityButton:function(e){
			e.preventDefault();
		
			self.toggleSelectCity();
		
		},
		onClickMoreButton:function(e){
			e.preventDefault();
			
			self.toggleMore();
		},
		onChangeSelectProvince:function(e){
			updateCityList(this.value);
		},
		onClickOkButton:function(e){
			e.preventDefault();
			var city = selectCity.value;
			sendGetWeather(city);
			obj1.setWinSize(215,118);
			self.setToMini();
		},
		onClickCancelButton:function(e){
			e.preventDefault();
			self.setToMini();
			obj1.setWinSize(215,118);
		},
		onClickRefreshButton:function(e){
			e.preventDefault();
			var city=getcookie('weather_id');
			sendGetWeather(city);
		},
		stopPropagation:function(e){
			e.stopPropagation();
		},
		onClickWeatherWidget_onlypic:function(e){
			obj1.setWinSize(215,118);
			jQuery('#control').show();
			self.setToMini();
			jQuery('#onlypic').hide();
			jQuery(top.document).one('mousedown',function(){
				obj1.setWinSize(175,120);
				jQuery('#control').hide();
				jQuery('#onlypic').show();
				return true;
			});
		}
	
	};


	/* 创建城市列表 */
    var updateCityList = function(province){
    	var provinceList = self.getCitys(self.getProId(province));

    	selectCity.options.length = 0;
    	var html='';
    	for(var i=0; i<provinceList.length; i++){
			selectCity.options[selectCity.options.length] = new Option(provinceList[i][0],provinceList[i][2]);
    	}
    };
   
	var sendGetWeather = function(city){
		/*if(city === "请选择城市"){
			alert("请选择城市");
			return;
		}*/
		
		weatherWidget_time.innerHTML = '正在更新天气...!';
		var data={id:city,t:new Date().getTime()};
		setcookie('weather_id',city,60*60*24*365);
	/* 请求数据 */	
	jQuery.ajax({
			url: WEATHER_URL,
			method : "GET",
			dataType :'json',
			data:data,
			success: function(data){
				_updateWeather(data);
				setUpRefreshTimer();
			},
			error : function(data){
				weatherWidget_time.innerHTML = '实时天气 数据加载失败!';
				setUpRefreshTimer();
			}
		});
	};
	var sendGetWeather1 = function(city){
		/*if(city === "请选择城市"){
			alert("请选择城市");
			return;
		}*/
		weatherWidget_time.innerHTML = '正在更新天气...!';
		var data={id:city,t:new Date().getTime()};
		/* 请求数据 */	
		jQuery.ajax({
				url: WEATHER_URL,
				method : "GET",
				dataType :'json',
				data:data,
				success: function(data){
					_updateWeather(data);
					setUpRefreshTimer();
					
				},
				error : function(data){
					weatherWidget_time.innerHTML = '实时天气 数据加载失败!';
					setUpRefreshTimer();
				}
			});
		};
	/* 一个小时请求一次时间 */
	var setUpRefreshTimer = function(){
		if(refreshTimer){
			clearTimeout(refreshTimer);
		}
		refreshTimer = setTimeout(sendGetWeather1, 1000 * 60 * 60); //一个钟更新一次	
	};
	
	/*//实现可带参数的settimeout
	var __sto = window.setTimeout;
	var _setTimeout = function(callback,timeout,param){
	    var args = Array.prototype.slice.call(arguments,2);
	    var _cb = function(){
	        callback.apply(null,args);
	    }
	    return __sto(_cb,timeout);
	};
	*/

		
	var onClickRefreshButton = function(){
			var city = weatherWidget_city.innerHTML;
			sendGetWeather(city);
		}
	var onWindowClose = function(){
			appContext.destroy();
		}
	var onExit = function(){
		//	$E.notifyObservers(window, "closeWindow", window);
		}	

		/*$E.addObserver(window, "close", observer.onWindowClose);
		$E.addObserver(window, "clickRefreshButton", observer.onClickRefreshButton);
		$E.addObserver(this, "exit", observer.onExit);
		*/
	var preventDefault=function(e){
		e.preventDefault();
	};
	
	this.toggleSelectCity = function(){
		if(state === "mini" || state == 'more'){
			self.setToSelectCity();
			obj1.setWinSize(215,220);
		
		}else{
			this.setToMini();
			obj1.setWinSize(215,118);
		}
	};
	this.toggleMore = function(){
		if(state === "mini" || state == 'selectCity'){
			self.setToMore();
			obj1.setWinSize(215,220);
		
		}else{
			this.setToMini();
			obj1.setWinSize(215,118);
		}
	};
	
	
	this.setToSelectCity = function(){
		state = "selectCity";
		jQuery(weatherWidget).removeClass("weatherWidget_miniBox").addClass("weatherWidget_selectCityBox");
		jQuery(weatherWidget_moreInfoPanel).hide();
		jQuery(weatherWidget_detail).hide();
		jQuery(weatherWidget_selectCityPanel).show();
		
	};
	this.setToMore= function(){
		state = "more";
		jQuery(weatherWidget).removeClass("weatherWidget_miniBox").addClass("weatherWidget_selectCityBox");
		jQuery(weatherWidget_moreInfoPanel).hide();
		jQuery(weatherWidget_selectCityPanel).hide();
		jQuery(weatherWidget_detail).show();
		
	};
	this.setToMini = function(){
		state = "mini";
		jQuery(weatherWidget).removeClass("weatherWidget_selectCityBox").addClass("weatherWidget_miniBox");
		jQuery(weatherWidget_moreInfoPanel).show();
		jQuery(weatherWidget_selectCityPanel).hide();
		jQuery(weatherWidget_detail).hide();
	
		
	};
	
	this.init = function(){
		    document.body.style.cursor="move";
			weatherWidget = document.getElementById("weatherWidget");
			weatherWidget_now_pic = document.getElementById("weatherWidget_now_pic");
			weatherWidget_onlypic = document.getElementById("weatherWidget_onlypic");
			weatherWidget_now_temperature = document.getElementById("weatherWidget_now_temperature");
			weatherWidget_city = document.getElementById("weatherWidget_city");
			weatherWidget_more = document.getElementById("weatherWidget_more");
			weatherWidget_time = document.getElementById("weatherWidget_time");
			weatherWidget_refresh = document.getElementById("weatherWidget_refresh");
			
			weatherWidget_moreInfoPanel = document.getElementById("weatherWidget_moreInfoPanel");
			weatherWidget_selectCityPanel = document.getElementById("weatherWidget_selectCityPanel");
			
			weatherWidget_detail = document.getElementById("weatherWidget_detail");
			
			weatherWidget_okButton = document.getElementById("weatherWidget_okButton");
			weatherWidget_cancelButton = document.getElementById("weatherWidget_cancelButton");
			
			selectCity = document.getElementById("weatherWidget_selectCity");
			selectProvince = document.getElementById("weatherWidget_selectProvince");
			
			
			var html = '';
			var pros=self.getPros();
			for(var p in pros){
				selectProvince.options[selectProvince.options.length] = new Option(pros[p][0],pros[p][0]);
			}
			updateCityList('北京');
			jQuery(selectProvince).bind("change", observer.onChangeSelectProvince);
			
			jQuery(weatherWidget_onlypic).bind("click", observer.onClickWeatherWidget_onlypic);
			
			jQuery(selectProvince).bind("mousedown", observer.stopPropagation);
			jQuery(selectCity).bind("mousedown", observer.stopPropagation);
			
			jQuery(weatherWidget_okButton).bind("click", observer.onClickOkButton);
			jQuery(weatherWidget_cancelButton).bind("click", observer.onClickCancelButton);
			jQuery(weatherWidget_city).bind("click", observer.onClickCityButton);
			jQuery(weatherWidget_more).bind("click", observer.onClickMoreButton);
			jQuery(weatherWidget_refresh).bind("click", observer.onClickRefreshButton);
			sendGetWeather1(city);//获取天气信息
			//_updateWeather({"now_temperature":"7","update_time":"14:12","now_pic":"b_a0.png","city":"徐州"});
			flag=false;
			if (jQuery.browser.msid) {
				jQuery(document.body).bind("selectstart", preventDefault);
			}
		}
	this.getPros = function(){
		var Pros = [];
		for (var i = 0, len = CityArr.length; i < len; ++i) {
			if (CityArr[i].length==3 && CityArr[i][1] != "0") {
				Pros.push(CityArr[i]);
			}
		}
		return Pros;
	};
	this.getProId= function(proName) {
		var ProId;
		for (var i = 0, len = CityArr.length; i < len; ++i) {
			if (CityArr[i][0] == proName && parseInt(CityArr[i][2]) <900) {
				ProId = CityArr[i][2];
			}
		}
		return ProId;
	};
	this.getCityId=function(ProId, CityName) {
		if(!ProId) return false;
		var CityId;
		for (var i = 0, len = CityArr.length; i < len; ++i) {
			if (CityArr[i][1] == ProId && CityArr[i][0] == CityName) {
				CityId = CityArr[i][2];
			}
		}
		return CityId;
	};
	this.getCitys = function(ProId){
		if(!ProId) return false;
		var Citys = [];
		for (var i = 0, len = CityArr.length; i < len; ++i) {
			if (CityArr[i][1] == ProId) {
				Citys.push(CityArr[i]);
			}
		}
		return Citys;
	};
};
