// JavaScript Document
$(document).ready(function(){
	/*					   						   
		//......程序正常运行....................		
		if( getCookie("placeName") != null && getCookie("weatherConditions") != null && getCookie("airTemperature") != null   ){
                     var placeName = getCookie("placeName");
                     var weatherConditions = getCookie("weatherConditions");
                     var airTemperature = getCookie("airTemperature");
					 $('div.cen2').empty();
					 wea(weatherConditions);
					 $('div.cen2').append("  "+placeName+" "+airTemperature);
		}//if
		*/
		//else{		
	 	   $.ajax({
                 url: 'http://news.bangkaow.com/templets/v3/weather/index.php',
				 //url: 'index.php',
                 type: 'POST',
                 dataType: 'HTML',
                 error: function(){
                     alert('Error');
                 },
                 success: function(data){
                     var weather = explose(data,"+");
                     var placeName = weather[0];
                     var weatherConditions = weather[1];
                     var airTemperature = weather[2];
					 setCookie("placeName",placeName,1);
					 setCookie("weatherConditions",weatherConditions,1);
					 setCookie("airTemperature",airTemperature,1);	
					 $('div.cen2').empty();
					 wea(weatherConditions);
					 $('div.cen2').append("  "+placeName+" "+airTemperature);
                 }//success
            });//ajax end
		//}
		
});

function wea(weatherConditions){
var wea = new Array()
wea[0] = "晴";
wea[1] = "多云";
wea[2] = "阴";
wea[3] = "阵雨";
wea[4] = "雷阵雨";
wea[5] = "雷阵雨并伴有冰雹";
wea[6] = "雨加雪";
wea[7] = "小雨";
wea[8] = "中雨";
wea[9] = "大雨";
wea[10] = "暴雨";
wea[11] = "大暴雨";
wea[12] = "特大暴雨";
wea[13] = "阵雪";
wea[14] = "小雪";
wea[15] = "中雪";
wea[16] = "大雪";
wea[17] = "暴雪";
wea[18] = "雾";
wea[19] = "冻雨";
wea[20] = "沙尘暴";
wea[21] = "小雨-中雨";
wea[22] = "中雨-大雨";
wea[23] = "大雨-暴雨";
wea[24] = "暴雨-大暴雨";
wea[25] = "大暴雨-特大暴雨";
wea[26] = "小雪-中雪";
wea[27] = "中雪-大雪";
wea[28] = "大雪-暴雪";       
wea[29] = "浮尘";                         
wea[30] = "扬沙";   
wea[31] = "强沙尘暴";
  for (i=0;i<wea.length;i++){
	  var temp = 0;
	  if(weatherConditions == wea[i]){
		temp = i;
		break;
	  }//if
   }//for
        var src = "http://news.bangkaow.com/templets/v3/weather/weatherimg/"+temp+".gif";
	   //var src = "./weatherimg/"+temp+".gif";
	$('div.cen2').append("<img src="+src+" alt="+wea[temp]+">");
		//$('div.cen2').find('img').attr('src',src); 
}//end func

function explose(str,divide){
	    var tempFirst = 0;
		var tempLast = 0;
		var temp = 0;
		var k = 0;
		var dataArray = new Array();
		while( (temp = str.indexOf(divide,temp+divide.length)) != -1 ){
		         tempLast = temp;
				  dataArray[k] = str.substring(tempFirst,tempLast);
				    k++;
				 tempFirst = tempLast+divide.length; 
		}//while
     return dataArray;
}




//读写cookie函数
function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1)
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			if (c_end == -1)
			{
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null
}
function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString()
}
