﻿var MZ_weather = {
	type: {
		"0":		["晴","32.gif"],
		"1":		["多云","28.gif"],
		"2":		["阴","26.gif"],
		"3":		["大雨","40.gif"],
		"4":		["雷阵雨","38.gif"],
		"5":		["雷阵雨","38.gif"],
		"6":		["雨夹雪","10.gif"],
		"7":		["小雨","11.gif"],
		"8":		["中雨","12.gif"],
		"9":		["大雨","40.gif"],
		"10":		["中雨","12.gif"],
		"11":		["大雨","40.gif"],
		"12":		["大雨","40.gif"],
		"13":		["大雪","14.gif"],
		"14":		["中雪","16.gif"],
		"15":		["中雪","16.gif"],
		"16":		["大雪","14.gif"],
		"17":		["大雪","14.gif"],
		"18":		["雾","20.gif"],
		"19":		["冰雹","18.gif"],
		"20":		["扬沙","22.gif"],
		"21":		["小雨","11.gif"],
		"22":		["中雨","12.gif"],
		"23":		["大雨","12.gif"],
		"24":		["中雨","16.gif"],
		"25":		["大雨","12.gif"],
		"26":		["小雪","13.gif"],
		"27":		["中雪","16.gif"],
		"28":		["大雪","14.gif"],
		"29":		["浮尘","22.gif"],
		"30":		["沙尘暴","19.gif"]
	},
	
	$: function(id) {
		return document.getElementById(id);
	},
	
	loadJs: function(file, callback) {
		var head = document.getElementsByTagName('head')[0];
		var js = document.createElement('script');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', file);
		head.appendChild(js);
		js.onload = js.onreadystatechange = function() {
			if (js.readyState && js.readyState != 'loaded' && js.readyState != 'complete') return;
			js.onreadystatechange = js.onload = null;
			if (callback) callback();
		}
		return false;
	},
	
	getCookie: function(name) {
		var cookie_start = document.cookie.indexOf(name);
		var cookie_end = document.cookie.indexOf(";", cookie_start);
		return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
	},
	
	setCookie: function(cookieName, cookieValue, seconds, path, domain, secure) {
		var expires = new Date();
		expires.setTime(expires.getTime() + seconds);
		document.cookie = escape(cookieName) + '=' + escape(cookieValue)
			+ (expires ? '; expires=' + expires.toGMTString() : '')
			+ (path ? '; path=' + path : '/')
			+ (domain ? '; domain=' + domain : '')
			+ (secure ? '; secure' : '');
	},
	
	toQueryString: function(data) {
		var arr = [];
		for (var key in data) {
			arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
		}
		return arr.join('&');
	},
	
	toQueryParams: function(str) {
		var data = {};
		var arr = str.split('&');
		for (var i = 0; i < arr.length; i++) {
			var o = arr[i].split('=');
			data[decodeURIComponent(o[0])] = decodeURIComponent(o[1]);
		}
		return data;
	},
	
	show: function(data, type) {
		if (!type) {
			this.setCookie('MZ_data', this.toQueryString(data), 21600000, '/', this.cookieDomain);
			this.setCookie('MZ_cityId', data.cityId, 31536000000, '/', this.cookieDomain);
		}
		data.url = this.weatherUrl + data.url.replace('/', '');
		for (var i = 1; i <= 3; i++) {
			data['txt' + i] = this.type[data['weather' + i]][0];
			data['pic' + i] = this.weatherUrl + 'style/2/s/' + this.type[data['weather' + i]][1];
		}
		this.template = this.template || this.$(this.wetherId).innerHTML
			.replace(/%7B/img, '{').replace(/%7D/img, '}').replace('<!--', '').replace('-->', '');
		this.$(this.wetherId).innerHTML = this.template.replace(/\{.*?\}/img, function(label) {
			label = label.replace(/^\{(.*?)\}$/, '$1');
			return data[label];
		});
		this.$(this.wetherId).style.display = '';
	},
	
	load: function(obj) {
		if (!this.$(obj.templateId)) return;
		this.wetherId = obj.templateId;
		this.weatherUrl = obj.weatherUrl;
		this.cookieDomain = obj.cookieDomain;
		if (this.getCookie('MZ_data')) {
			this.show(this.toQueryParams(this.getCookie('MZ_data')), 'cookie');
		} else {
			obj.cityId = obj.cityId || this.getCookie('MZ_cityId') || 0;
			if (!obj.cityId) {
				this.loadJs('http://search.hao123.com/weather/bin/forecast.php', function() {
					MZ_weather.loadJs(obj.weatherUrl + 'forecast.asp?cityName=' + (HaoForecast ? HaoForecast.Name : ''));
				});
			} else {
				MZ_weather.loadJs(obj.weatherUrl + 'forecast.asp?cityId=' + obj.cityId);
			}
		}
	},
	
	showSet: function() {
		if (!this.setState) {
			this.setState = true;
			this.loadJs(this.weatherUrl + 'js/c.js');
			this.loadJs(this.weatherUrl + 'js/f.js', function() {
				init_weather_set();
			});
		}
		this.$(this.wetherId).style.display = 'none';
		this.$('weather-set').style.display = '';
	},
	
	cancel: function() {
		this.$(this.wetherId).style.display = '';
		this.$('weather-set').style.display = 'none';
	},
	
	setCity: function() {
		var cityId = this.$('city').options[this.$('city').selectedIndex].value;
		this.loadJs(this.weatherUrl + 'forecast.asp?cityId=' + cityId);
		this.cancel();
	}
};
function MZ_show(data) {
	MZ_weather.show(data);
}