var weather_info;
var weather_num = Math.floor(Math.random() * 10) + 1;
var weather_st;
var weather_error;

function weatherView() {
	$.ajax({
		url:"/svc/wsdata/data/weather.xml",
		dataType:"xml",
		success:function(resultXML){
			var areaList = $(resultXML).find("short > area");
			var arealen = areaList.length;

			var img_str = "";
			var span_text = "";

			weather_info = new Array();

			for(var i=0; i<arealen; i++){
				var codeElement = $(areaList[i]).find("code");
				if(codeElement != null)
					var code = $(codeElement).text();

				var name = "";
				if(code=="11B10101"){
					name = "ソウル";
				}else if(code=="11D10301"){
					name = "春川";
				}else if(code=="11D20501"){
					name = "江陵";
				}else if(code=="11E00101"){
					name = "鬱陵";
				}else if(code=="11C20401"){
					name = "大田";
				}else if(code=="11C10301"){
					name = "淸州";
				}else if(code=="11H10701"){
					name = "大邱";
				}else if(code=="11F20501"){
					name = "光州";
				}else if(code=="11F10201"){
					name = "全州";
				}else if(code=="11H20201"){
					name = "釜山";
				}else if(code=="11G00201"){
					name = "濟州";
				}

				var codeElement = $(areaList[i]).find("weather_code");
				if(codeElement != null)
					var code = $(codeElement).text();
				var temperatureElement = $(areaList[i]).find("temperature");
				if(temperatureElement != null)
					var temperature = $(temperatureElement).text();

				weather_info[i] = new Array();
				weather_info[i]["name"]=name;
				weather_info[i]["temperature"]=temperature;
				weather_info[i]["code"]=code;
			}

			var weather_view = weather_info[0]["name"]+' '+weather_info[0]["temperature"]+'℃ <img src="http://www.chosunonline.com/japanese/svc/wsdata/icon/'+weather_info[0]["code"]+'.png" align="absmiddle" />';
			if (document.getElementById("weather_text") != "undefined" && document.getElementById("weather_text") != null)
			document.getElementById("weather_text").innerHTML = weather_view;

			clearTimeout(weather_st);
			clearTimeout(weather_error);
			weather_roll();
		},
		error: function(xhr, status, error) {
			if(weather_info.length==0){
				weather_error = setTimeout("weatherView()",1000*10);
			}
		}
	});
}

function weatherLoad(){
	weatherView();
	setTimeout("weatherLoad()",1000*60*10);
}

function weather_roll() {
	weather_num++;

	if(weather_num > 10){
		weather_num = 0;
	}

	var weather_view = weather_info[weather_num]["name"]+' '+weather_info[weather_num]["temperature"]+'℃ <img src="http://www.chosunonline.com/japanese/svc/wsdata/icon/'+weather_info[weather_num]["code"]+'.png" align="absmiddle" />';

	if (document.getElementById("weather_text") != "undefined" && document.getElementById("weather_text") != null)
	document.getElementById("weather_text").innerHTML = weather_view;
	weather_st = setTimeout("weather_roll()", 5000);
}

weatherLoad();

