/** 获得天气情况 */
var weather_getWeather=function(){
	$.ajax({"type":"POST","url":"/a/main/getWeather.html","cache" :false,"data":null,"success":function(data,status){
		var tempStr=data.split("|");
		if(tempStr.length!=2){
			weather_callback(null);
			return;
		}
		if(tempStr[1].indexOf("forecast_information")==-1){
			weather_callback(null);
			return;
		}
		var xmlDoc=loadXML(false,tempStr[1]);
		var jsonStr="({'city':'"+tempStr[0]+"',";
		$(xmlDoc).find("weather").each(function(){
			$(this).children("forecast_information").each(function(){
				if(isNull($(this).children("forecast_date").attr("data"))){
					weather_callback(null);
					return;
				}
				jsonStr+="'date':"+"'"+$(this).children("forecast_date").attr("data")+"',";
			});
			var flag=false;
			$(this).children("current_conditions").each(function(){
				if(!isNull($(this).children("condition").attr("data"))&&!isNull($(this).children("temp_c").attr("data"))&&!isNull($(this).children("temp_f").attr("data"))&&!isNull($(this).children("humidity").attr("data"))&&!isNull($(this).children("icon").attr("data"))&&!isNull($(this).children("wind_condition").attr("data"))){
					jsonStr+="'currentDay':{'condition':'"+$(this).children("condition").attr("data")+"','tempC':'"+$(this).children("temp_c").attr("data")+"','tempF':'"+$(this).children("temp_f").attr("data")+"','humidity':'"+$(this).children("humidity").attr("data")+"','icon':'"+weather_changeIcon($(this).children("icon").attr("data"))+"','windCondition':'"+$(this).children("wind_condition").attr("data")+"'},";
					flag=true;
				}
			});
			var cnt=0;
			$(this).children("forecast_conditions").each(function(){
				if(!isNull($(this).children("condition").attr("data"))&&!isNull($(this).children("low").attr("data"))&&!isNull($(this).children("high").attr("data"))&&!isNull($(this).children("day_of_week").attr("data"))&&!isNull($(this).children("icon").attr("data"))){
					if(!flag){
						var tempC=parseInt((parseInt($(this).children("low").attr("data"))+parseInt($(this).children("high").attr("data")))/2);
						var tempF=parseInt(tempC*9/5+32);
						jsonStr+="'currentDay':{'condition':'"+$(this).children("condition").attr("data")+"','tempC':'"+tempC+"','tempF':'"+tempF+"','humidity':'湿度：未知','icon':'"+weather_changeIcon($(this).children("icon").attr("data"))+"','windCondition':'风向：未知'},";
						flag=true;
					}
					if(cnt==0){
						jsonStr+="'forecastDay':[";
						cnt=1;
					}
					jsonStr+="{'condition':'"+$(this).children("condition").attr("data")+"','low':'"+$(this).children("low").attr("data")+"','high':'"+$(this).children("high").attr("data")+"','dayOfWeek':'"+$(this).children("day_of_week").attr("data")+"','icon':'"+weather_changeIcon($(this).children("icon").attr("data"))+"'},";
				}
			});
			jsonStr=jsonStr.substring(0, jsonStr.length-1)+"]";
		});
		jsonStr+="})";
		try{
			weather_callback(eval(jsonStr));
			return;
		}catch(e){
			weather_callback(null);
			return;
		}
	},"error":function(){
		weather_callback(null);
		return;
	}});
}
/**天气图片地址转换*/
var weather_changeIcon=function(oldIconPath){
	var date = new Date();
	if(date.getHours()>7&&date.getHours()<19){
		return "/pages/other/images/common/"+oldIconPath.substring(11,oldIconPath.lastIndexOf("."))+".jpg";
	}else{
		return "/pages/other/images/common/"+oldIconPath.substring(11,oldIconPath.lastIndexOf("."))+"_night.jpg";
	}
}
/**判断是否为空*/
function isNull(obj){
	if(obj==null){
		return true;
	}
	if(obj==""){
		return true;
	}
	return false;
}
/** 转换成XML */
function loadXML(flag,xml){
	var xmlDoc;
	/** 针对IE浏览器 */
	if(window.ActiveXObject){
		var aVersions = ["MSXML2.DOMDocument.6.0","MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XmlDom"];
		for (var i = 0; i < aVersions.length; i++) {
			try {
				/** 建立xml对象 */
				xmlDoc = new ActiveXObject(aVersions[i]);
				break;
			} catch (oError) {
			}
		}
		if(xmlDoc != null){
			/** 同步方式加载XML数据 */
			xmlDoc.async = false;
			/** 根据XML文档名称装载 */
			if(flag == true){
		        xmlDoc.load(xml);
			} else{
		        /** 根据表示XML文档的字符串装载 */
		        xmlDoc.loadXML(xml);
			}
			/** 返回XML文档的根元素节点 */
			return xmlDoc.documentElement;
		}
	}else{
		/** 针对非IE浏览器 */
	    if(document.implementation && document.implementation.createDocument){
			/**
			 * 第一个参数表示XML文档使用的namespace的URL地址 第二个参数表示要被建立的XML文档的根节点名称
			 * 第三个参数是一个DOCTYPE类型对象，表示的是要建立的XML文档中DOCTYPE部分的定义，通常我们直接使用null
			 * 这里我们要装载一个已有的XML文档，所以首先建立一个空文档，因此使用下面的方式
			 */
    		xmlDoc = document.implementation.createDocument("","",null);
	      	if(xmlDoc != null){
		    	  /** 根据XML文档名称装载 */
		    	  if(flag == true){
		        	/** 同步方式加载XML数据 */
		        	xmlDoc.async = false;
		        	xmlDoc.load(xml);
		    	  } else{
		    		  /** 根据表示XML文档的字符串装载 */
		    		  var oParser = new DOMParser();
		    		  xmlDoc = oParser.parseFromString(xml,"text/xml");
		    	  }
					  /** 返回XML文档的根元素节点 */
		    	  	  return xmlDoc.documentElement;
	      		  }
	       }
	}
	return null;
}
