;(function($){
	var datapool = {
			"tqxx" : {
				 "00" :  "晴", "01" : "多云", "02" : "阴","03" : "阵雨","04" : "雷阵雨","05" : "雷阵雨伴有冰雹","06" : "雨夹雪","07" : "小雨","08" : "中雨","09" : "大雨","10" : "暴雨","11" : "大暴雨",
				 "12" : "特大暴雨","13" : "阵雪","14" : "小雪","15" : "中雪","16" : "大雪","17" : "暴雪","18" : "雾","19" : "冻雨","20" : "沙尘暴","21" : "小到中雨","22" : "中到大雨","23" : "大到暴雨",
				 "24" : "暴雨到大暴雨","25" : "大暴雨到特大暴雨","26" : "小到中雪","27" : "中到大雪","28" : "大到暴雪","29" : "浮尘","30" : "扬沙","31" : "强沙尘暴","53" : "霾","99" : "无"
			},
			"xjj" : {
				"0" : "星期天","1" : "星期一","2" : "星期二","3" : "星期三","4" : "星期四","5" : "星期五","6" : "星期六"
			}
	};
	//取对应天气的中文字
	var getChinaeseStr = function(pCulDate){
		return datapool["tqxx"][pCulDate];
	};
	//取得对应的星期几
	var getDayStr = function(pDay){
		return datapool["xjj"][pDay];
	} ;
	//列表式显示，连续几天的天气情况（黑色背景）
	$.fn.tianqi = function(options){
		var tianQiData = $.extend({},$.fn.tianqi.defaults,options);
		//取得results里的days
		var resultsDays = tianQiData.results["f"]["f1"];
		return this.each(function(){
			//var year = tianQiData.beginDate.getFullYear();
			//var month = tianQiData.beginDate.getMonth()+1;
			var date = tianQiData.beginDate.getDate();
			//var day = tianQiData.beginDate.getDay(); 
			var _table = '';
			//var tday = tianQiData.beginDate;
			var addDay = function(dd,inc){
				var ndate=new Date(dd.valueOf());
				ndate.setDate(ndate.getDate() + i);
				if (!dd.getHours() && ndate.getHours()) //shift to yesterday
					ndate.setTime(ndate.getTime() + 60 * 60 * 1000 * (24 - ndate.getHours()));
					return ndate;
			};	
			
			$(this).html(tianQiData.outSideDiv);
			for(var i=0;i<=resultsDays.length-1;i++){
				//对应日期存放的天气数据
			     var culDateData = resultsDays[i];
			     var fa = culDateData["fa"];
			     var fb = culDateData["fb"];
			     var culDate = date+i;
			     //var culDay = day+i;
			    var cd = addDay(tianQiData.beginDate,1);
			     //var addClass1 = "tianQi_innside";
			     //var addClass2 = "tianQi_innside2";
			     if(tianQiData.tqColor!=""){
			    	 _table += '<div class="tianQi_innside_lvse"><table class="tianQi_table">';
			     }else{
			    	 if(i==resultsDays.length-1){
				    	 _table += '<div class="tianQi_innside2"><table class="tianQi_table">';
				     }else{
				    	 _table += '<div class="tianQi_innside"><table class="tianQi_table">';
				     }
			     }
			    
				 if(i==0){
					 _table+='<tr><td>今天&nbsp;&nbsp'+getDayStr(cd.getDay())+'</td></tr>';
				 }else if(i==1){
					 _table+='<tr><td>明天&nbsp;&nbsp'+getDayStr(cd.getDay())+'</td></tr>';
				 }else{
					 _table+='<tr><td>'+culDate+'号&nbsp;&nbsp'+getDayStr(cd.getDay())+'</td></tr>';
				 }
				
				 if(fa==""){
					 _table+='<tr><td><img src="'+tianQiData.ctx+'/images/night/'+fb+'.png"  width="60px" height="65px" alt="图2"/></td></tr>';
					 _table+='<tr style="line-height: 80%;"><td>'+culDateData["fd"]+'℃</td></tr>';
					 _table+='<tr><td>'+getChinaeseStr(fb)+'</td></tr>';
				 }else{
					 _table+='<tr><td><img src="'+tianQiData.ctx+'/images/day/'+fa+'.png"  width="60px" height="65px" alt="图1"/></td></tr>';
					 _table+='<tr style="line-height: 80%;"><td>'+culDateData["fc"]+'&nbsp;～&nbsp;'+culDateData["fd"]+'℃</td></tr>';
					if(getChinaeseStr(fa)==getChinaeseStr(fb)){
						_table+='<tr><td>'+getChinaeseStr(fa)+'</td></tr>';
					}else{
						_table+='<tr><td>'+getChinaeseStr(fa)+'【转】'+getChinaeseStr(fb)+'</td></tr>';
					}
				 }
				
				_table+='</table></div>';
			}
			$(this).find('[data-role="outside"]').append(_table);
		});
	};
	//默认参数
	$.fn.tianqi.defaults = {
			beginDate:new Date(),
			results:"",
			ctx:"",
			tqColor:"",
			outSideDiv:'<div class="tianQi_outside" data-role="outside"></div>'
	};
	
	//单独显示一天的天气情况
	$.fn.tianqiSingle = function(options){
		var tianQiData = $.extend({},$.fn.tianqi.defaults,options);
		//取得results里的days
		var resultsDays = tianQiData.results["f"]["f1"];
		return this.each(function(){
			 var culDateData = resultsDays[0];
		     var fa = culDateData["fa"];
		     var fb = culDateData["fb"];
		     
			 var _html = "<div>";
			 if(fa==""){
				 _html +=' <img src="'+tianQiData.ctx+'/images/day/'+fb+'.png" alt="图1" width="26px" height="26px"/>&nbsp;'+getChinaeseStr(fb) +'&nbsp'+culDateData["fd"]+'℃';
			 }else{
				 _html +=' <img src="'+tianQiData.ctx+'/images/day/'+fa+'.png" alt="图1" width="26px" height="26px"/>&nbsp;'+getChinaeseStr(fa) +'&nbsp'+culDateData["fc"]+'℃';
			 }
		     _html +='</div>';
		     $(this).html(_html);
		});
	};
	
	//天气
})(jQuery);