//start by guoshuang 2008-02-27
var nowcity = "";
var nowcitycode = "";
var cncity = 1; //默认中国时间 ＝ CST+14

/*天气术语中文翻译数组*/

cnTianqi = new Array();
cnTianqi[0] = "龙卷风";
cnTianqi[1] = "热带风暴";
cnTianqi[2] = "飓风";
cnTianqi[3] = "严厉雷暴";
cnTianqi[4] = "雷暴";
cnTianqi[5] = "混杂的雨和雪";
cnTianqi[6] = "混杂的雨和雨夹雪";
cnTianqi[7] = "混杂的雪和雨夹雪";
cnTianqi[8] = "结冰的毛毛雨";
cnTianqi[9] = "毛毛雨";
cnTianqi[10] = "冻雨";
cnTianqi[11] = "阵雨";
cnTianqi[12] = "阵雨";
cnTianqi[13] = "雪阵";
cnTianqi[14] = "小雪阵雨";
cnTianqi[15] = "暴风雪";
cnTianqi[16] = "雪";
cnTianqi[17] = "冰雹";
cnTianqi[18] = "雨夹雪";
cnTianqi[19] = "扬沙";
cnTianqi[20] = "有雾";
cnTianqi[21] = "阴霾";
cnTianqi[22] = "烟雾";
cnTianqi[23] = "刮大风";
cnTianqi[24] = "有风";
cnTianqi[25] = "冷";
cnTianqi[26] = "多云";
cnTianqi[27] = "主要多云(夜间)";
cnTianqi[28] = "主要多云(白天)";
cnTianqi[29] = "部分多云(夜间)";
cnTianqi[30] = "部分多云(白天)";
cnTianqi[31] = "晴(夜)";
cnTianqi[32] = "晴朗";
cnTianqi[33] = "晴(夜)";
cnTianqi[34] = "晴(天)";
cnTianqi[35] = "混杂的雨和冰雹";
cnTianqi[36] = "炎热";
cnTianqi[37] = "雷暴";
cnTianqi[38] = "疏散雷暴";
cnTianqi[39] = "疏散雷暴";
cnTianqi[40] = "大面积阵雨";
cnTianqi[41] = "大雪";
cnTianqi[42] = "疏散阵雪";
cnTianqi[43] = "大雪";
cnTianqi[44] = "部分多云";
cnTianqi[45] = "雷阵雨";
cnTianqi[46] = "阵雪";
cnTianqi[47] = "被隔绝的雷阵雨";
cnTianqi[3200] = "无效";

enWeek = new Array("Mon","Tur","Wed","Thu","Fri","Sat","Sun");
cnWeek =new Array("周一","周二","周三","周四","周五","周六","周日");

enTianqi = new Array();
enTianqi[0] = "tornado";
enTianqi[1] = "tropical storm";
enTianqi[2] = "hurricane";
enTianqi[3] = "severe thunderstorms";
enTianqi[4] = "thunderstorms";
enTianqi[5] = "mixed rain and snow";
enTianqi[6] = "mixed rain and sleet";
enTianqi[7] = "mixed snow and sleet";
enTianqi[8] = "freezing drizzle";
enTianqi[9] = "drizzle";
enTianqi[10] = "freezing rain";
enTianqi[11] = "showers";
enTianqi[12] = "showers";
enTianqi[13] = "snow flurries";
enTianqi[14] = "light snow showers";
enTianqi[15] = "blowing snow";
enTianqi[16] = "snow";
enTianqi[17] = "hail";
enTianqi[18] = "sleet";
enTianqi[19] = "dust";
enTianqi[20] = "foggy";
enTianqi[21] = "haze";
enTianqi[22] = "smoky";
enTianqi[23] = "blustery";
enTianqi[24] = "windy";
enTianqi[25] = "cold";
enTianqi[26] = "cloudy";
enTianqi[27] = "mostly cloudy (night)";
enTianqi[28] = "mostly cloudy (day)";
enTianqi[29] = "partly cloudy (night)";
enTianqi[30] = "partly cloudy (day)";
enTianqi[31] = "clear (night)";
enTianqi[32] = "sunny";
enTianqi[33] = "fair (night)";
enTianqi[34] = "fair (day)";
enTianqi[35] = "mixed rain and hail";
enTianqi[36] = "hot";
enTianqi[37] = "isolated thunderstorms";
enTianqi[38] = "scattered thunderstorms";
enTianqi[39] = "scattered thunderstorms";
enTianqi[40] = "scattered showers";
enTianqi[41] = "heavy snow";
enTianqi[42] = "scattered snow showers";
enTianqi[43] = "heavy snow";
enTianqi[44] = "partly cloudy";
enTianqi[45] = "thundershowers";
enTianqi[46] = "snow showers";
enTianqi[47] = "isolated thundershowers";
//enTianqi[3200] = "not available";

$(function(){

/*ui behavior*/
$("#cities").find("li").hover(function(){$(this).addClass("hover");$(this).attr("title",$(this).attr("enname"));},function(){$(this).removeClass("hover")});

//$("#collapseCNWorld").click(function(){alert($(this).text());if($(this).text().indexOf("国外")!=-1){$("ul.guoneichengshi").slideUp();{$("ul.guowaichengshi").slideDown();}else{}})

$("#collapseCNWorld").click(function(){othis=$(this);if(othis.text().indexOf("国外")!=-1){$("ul.guoneichengshi").slideUp("slow",function(){$("ul.guowaichengshi").slideDown("slow");othis.text("显示国内城市");cncity = 0;});}else{$("ul.guowaichengshi").slideUp("slow",function(){$("ul.guoneichengshi").slideDown();othis.text("显示国外城市");cncity = 1;});}})

//点击开始查询
$("#cities").find("li").click(function(){
$("#divresult").css("min-height","0");
$("#divresult").slideUp();
$("#divresult").css("min-height","200px");
$("#divresult").slideDown("fast",function(){window.scrollTo(0,1999);});

nowcity = $(this).text();
nowcitycode = $(this).attr("ccode");

$.ajax({
type: "GET",
url: "getcity.php",
dataType: "xml",
data: "city="+nowcitycode,
success:
//ajax start
function(data){

oitem = $(data).find("item");

//如果有这个信息的话
if(oitem.find("pubDate").next().attr("code")){
weatherInfo = cnTianqi[oitem.find("pubDate").next().attr("code")];
weatherTemp = oitem.find("pubDate").next().attr("temp")+"&deg;C";
if(!weatherInfo){weatherInfo = oitem.find("pubDate").next().attr("text")}
}else{
weatherInfo = "";
weatherTemp = "";
}

//有的浏览器可以拿到 带冒号的 item 如firefox；有的不行，如 chrome
if($(data).find("yweather\\:astronomy").length!=0){
	oyweatherastronomy = $(data).find("yweather\\:astronomy");
	oyweatheratmosphere = $(data).find("yweather\\:atmosphere");
	oyweatherwind = $(data).find("yweather\\:wind");
	oylong = $(data).find("geo\\:long");
	oylat = $(data).find("geo\\:lat");
}else{
	oyweatherastronomy = $(data).find("astronomy");
	oyweatheratmosphere = $(data).find("atmosphere");
	oyweatherwind = $(data).find("wind");
	oylong = $(data).find("long");
	oylat = $(data).find("lat");
}

/*
//如果ie的话如何拿到 大气和风力，还有纬度等
if ($.browser.msie){

oyweatherastronomy = $(data).find("image").prev();
oyweatheratmosphere = oyweatherastronomy.prev();
oyweatherwind = oyweatheratmosphere.prev();

oylat = oitem.find("title").next();
oylong = oylat.next();

}
*/

// 是否所有天气都是 gif？
//未来天气 有的地方没有

oresult = oitem.find("description").text().match(/\b\w{3}\b\s-[^<]*/g);
weaInfo1 = oresult[0].match(/(\w+)\b\./)[1];
weaHigh1 = oresult[0].match(/High:\s(-?\d+)/)[1];
weaLow1 = oresult[0].match(/Low:\s(-?\d+)/)[1];

weaInfo2 = oresult[1].match(/(\w+)\b\./)[1];
weaHigh2 = oresult[1].match(/High:\s(-?\d+)/)[1];
weaLow2 = oresult[1].match(/Low:\s(-?\d+)/)[1];

str="";
str+="<h2>"+nowcity+"的天气预报</h2>\r";
str+="<div class=\"lastBuildDate\">"+$(data).find("lastBuildDate").text()+"</div>\r";
str+="<div class=\"totaldata\">";

str+="<em>"+weatherInfo+" "+weatherTemp+"</em>\r";
str+="<div>最高气温 "+weaHigh1+"&deg;C | 最低 "+weaLow1+"&deg;C</div>";
str+="<img src=\""+oitem.find("description").text().match(/[^\"]*\.gif/g)+"\" title=\""+weatherInfo+" "+weatherTemp+"\" alt=\""+nowcity+"\" /><ul>\r";
str+="<li>风力： "+oyweatherwind.attr("speed")+"km/h</li>";
str+="<li>风向："+oyweatherwind.attr("direction")+"度 </li>";
str+="<li>湿度： "+oyweatheratmosphere.attr("humidity")+"%</li>";
str+="<li>能见度："+oyweatheratmosphere.attr("visibility")+"米</li>";
str+="<li>气压："+oyweatheratmosphere.attr("pressure")+"mb"; 

//类似安康这样的地方没有风力数据，没有总体温度等

str+="</ul></div>";

//3200 不在内，解决 ie 不能直接 indexOf 数组的问题

var enInfoNum;

//alert(weaInfo2.toLowerCase());
//alert(enTianqi[32])

for(i=0;i<enTianqi.length;i++){
 if(enTianqi[i] == weaInfo2.toLowerCase()){enInfoNum = i;}
}

tomorrowInfo = cnTianqi[enInfoNum];

if(!enInfoNum){tomorrowInfo = "不祥"};

str+="<div class=\"footdata\"><ul>";
str+="<li><strong>明天</strong>：<em>"+tomorrowInfo+"</em><br />最高气温 "+weaHigh2+"&deg;C | 最低 "+weaLow2+"&deg;C</li>";
str+="</ul><div class=\"collapsemore\" title=\"显示更多信息\">&#8595;</div></div>";
str+="<div class=\"moredata\"><ul>";
str+="<li>纬度："+oylat.text()+" 纬度："+oylong.text()+"</li>";
str+="<li>日出(落)："+oyweatherastronomy.attr("sunrise")+"-"+oyweatherastronomy.attr("sunset")+"</li>";
str+="</ul></div>";

//alert($(data).find("atmosphere").text()+"%")

//显示 demo

$("#ubuntu4lifeweather").html(str);

$("#ubuntu4lifeweather .collapsemore").hover(function(){$(this).toggleClass("collapover")},function(){$(this).toggleClass("collapover")})
$("#ubuntu4lifeweather .collapsemore").click(function(){$("#ubuntu4lifeweather .moredata").slideToggle();})

}
//ajax end

});

})

//produce code

$("#getTheCode").click(function(){

if(nowcitycode == ""){alert("请先在上面选择城市！");return;}

ocode1 ='<iframe frameborder="0" width="180" height="260" scrolling="no" src="http://weather.ubuntu4life.com/weather.php?cid='+nowcitycode+'&cname='+encodeURI(nowcity)+'"></iframe>';

if($("#widthuwant").attr("checked")==true){ocode1 ='<iframe frameborder="0" width="160" height="240" scrolling="no" src="http://weather.ubuntu4life.com/weather.php?cid='+nowcitycode+'&cname='+encodeURI(nowcity)+'&width=160"></iframe>';}

$("#code01").val(ocode1);
$("#embedCode").slideDown("slow",function(){window.scrollTo(0,1999);});

})

//end
})

