var SR = {}

//模型
SR.SearchMode = function(obj){
	var _cache = obj;
	var _self = this;
	var _tabEle = _cache.Tab;
	_tabEle.onclick = (function(){
		if(_cache.ClickHandler){
			_cache.ClickHandler(_self);
		}
	})
	
	this.Show = function(){
		if(_cache.Content){
			_cache.Content.style.display = "";
		}
		_cache.Tab.className = _cache.AbledClass;
		var inputs = _cache.Content.getElementsByTagName("input");
		for(var i = 0,len = inputs.length; i < len; i++){
			if(inputs[i].getAttribute("type","text")){
				//inputs[i].focus();
				if(document.all){
					Yl.getFocus(inputs[i]); //fix ie
				}else{
					inputs[i].focus();
				}
				break;
			}
		}
	}
	
	this.Hide = function(){
		if(_cache.Content){
			_cache.Content.style.display = "none";
		}
		_cache.Tab.className = _cache.DisabledClass;
	}
}

//关联对象
SR.SearchRate = (function(){
	var _cache = {
		Data: []
	};
	
	var instance = function(obj){
		return new SR.SearchMode(obj);
	}
	
	//点击Tab方法
	var clickFun = function(mod){
		for(var k in _cache.Data){
			if(_cache.Data[k].Hide){
				_cache.Data[k].Hide();
			}
		}
		mod.Show();
	}
	
	return {
		Add: function(arr,defaultKey){
			if(arr.length){
				for(var i = 0,len = arr.length; i < len; i++){
					var obj = arr[i];
					obj.ClickHandler = function(r){
						clickFun(r);
					}
					var mod = instance(obj);
					_cache.Data.push(mod);
				}
				if(defaultKey == undefined){
					defaultKey = 0;
				}
				clickFun(_cache.Data[defaultKey]);
			}
		},
		Init: function(tabTagName,abledClass,disabledClass){
			abledClass = abledClass || "";
			disabledClass = disabledClass || "";
			
			var tabList = document.getElementById(tabTagName).getElementsByTagName('li');
			var arr = [];
			var defaultKey = 0;
			
			for(var i = 0,len = tabList.length; i < len; i++){
				var item = tabList[i];
				var attr = item.getAttribute("s_tab");
				
				if(attr != null){
					var content = document.getElementById(attr);
					
					var obj = {
						Tab: item,
						Content: content,
						AbledClass:abledClass,
						DisabledClass:disabledClass
					}
					var ind = arr.push(obj);
					if(item.getAttribute("default")){
						defaultKey = ind - 1;
					}
					new SR.RadioMod(content);
				}
			}
			SR.SearchRate.Add(arr,defaultKey);
		}
	}
})();

SR.RadioMod = function(box){
	var _cache = {radios:[]}
	var l = box.getElementsByTagName("input");
	for(var i = 0,len = l.length; i < len; i++){
		
		var item = l[i];
		var type = item.getAttribute("rad");
		if(type != null){
			_cache.radios.push(item);
		}
	}
	
	var bind = function(){
		for(var i = 0,len = _cache.radios.length; i < len; i++){
			var ele = _cache.radios[i];
			ele.onclick = (function(){
				var radio = this;
				SR.RadioMod.ClickRadio(radio);
			})
		}
	}
	
	if(_cache.radios.length){
		bind();
	}
}

SR.RadioMod.ClickRadio = function(radio){
	var type = radio.getAttribute("rad");
	var mod = SR.SearchData[type];
	if(mod){
		var form = radio.form;
		var inputs = form.getElementsByTagName("input");
		var delInd = [];
		for(var j = 0,jlen = inputs.length; j < jlen; j++){
			var item = inputs[j];
			var rel = item.getAttribute("rel");
			if(item.getAttribute("type") == "hidden"){
				delInd.push(item);
			}
			if(rel){
				switch(rel){
					case "kw":
						item.setAttribute("name",mod.name);
						break;
					case "btn":
						item.value = mod.btn;
						break;
				}
			}
		}
		var img = form.getElementsByTagName("img");
		for(var j = 0,jlen = img.length; j < jlen; j++){
			img[j].setAttribute("src", 'link/'+mod.img[1]);
			img[j].setAttribute("title",mod.img[0]);
		}
		var a = form.getElementsByTagName("a");
		for(var j = 0,jlen = a.length; j < jlen; j++){
			if(a[j].getAttribute("rel") == "lk"){
				a[j].setAttribute("href", mod.url);
			}
		}
		for(var j = 0,jlen = delInd.length; j < jlen; j++){
			form.removeChild(delInd[j]);
		}
		form.setAttribute("action",mod.action);
		if(mod.params){
			for(var k in mod.params){
				var hidden = document.createElement('input');
				hidden.setAttribute("type","hidden");
				hidden.setAttribute("name",k);
				hidden.value = mod.params[k];
				//hidden.value = mod.params[k];
				form.appendChild(hidden);
				//hidden.setAttribute("value",mod.params[k]);
				
				
			}
		}
	}
}

/*天气加载类开始*/
var Weather = (function(){
					
	//var CityCookie = Cookie.init(Yl.getHost(),"/",new Date(new Date().getTime()+1000*3600*2));
	function getCityId(city){
		var id = [];
		var city = city || this.City;
		
		CityArr.forEach(function(element,index,array){
		
			if(element[0]==city){
				id[0] = element[1];
				id[1] = element[2];
			}
		})
		return id;
	}
	
	
	function getWeather(cityid){
		
		var api = "http://tool.115.com/static/weather/"+cityid+".txt?rd="+(new Date()).getTime();
		var Timeout;
		var weatherHTML = "";
		if(cache.is("WEATHER_TEMP_TITLE") == false && cache.is("WEATHER_TEMP") == false){
			cache.set("WEATHER_TEMP_TITLE", '<a  title="点击查看更多" id="wet" style="background-image:none;" onclick="Weather.more()"><strong>#{city}</strong> #{img} #{title}</a> <a onclick="Weather.set()" href="javascript:void(0)" target="_parent">[定制城市]</a>');
			cache.set("WEATHER_TEMP", '<div class="head"><a title="关闭" id="closeWTBOX" class="close">关闭</a><span style="float:left;">#{postTime}</span></div><h2>#{city} #{more}</h2>#{weather}');
		}
				
		XSAjax.send({
			url:api,
			before:function(){
			_$("#weather").innerHTML = "天气预报正在加载中…";
			
				Timeout = setTimeout(				
				function(){
					if(_$("#weather").el.innerHTML == "天气预报正在加载中…"){
						_$("#weather").el.innerHTML='天气加载超时。<a onclick="Weather.init(1);window.clearTimeout(Timeout)" class="red">[重新加载]</a> <a onclick="Weather.set()" class="red">[手动设置]</a>'
					}else{
						return;
					}
				},
				5000
				)
				
			},
			after:function(){
				var data = weatherJSON;
				if(!data){
					_$("#weather").el.innerHTML = '很抱歉，暂没有该城市的天气数据！ <a onclick="Weather.set()" class="red">[重 设]</a>';
					return;
				}	
				var w = data.weather,
				imgPath = "link/static/images/weather/",
				iconPath = imgPath+'24x20/'+data.weather.today.icon[0].replace(".gif",".png");
				icon = '<img align="absmiddle" onload="pngfix(this)" src="'+iconPath+'" height="10" />';
			
				_$("#weather").el.innerHTML = format(cache.get("WEATHER_TEMP_TITLE"),{
					city:data.city[1],
					title:data.title,
					img:icon
				});
				
				for(var i in w){
					var datatxt = "";
					switch(i){
						case "today":
							datatxt = "今天";
							break;
						case "tomorrow":
							datatxt = "明天";
							break;
						case "after":
							datatxt = "后天";
							break;
					}
				
					weatherHTML+= '<ul><li class="date">'+datatxt+'<br/>'+w[i].temp+'</li>' 
					           +'<li><img src="'+imgPath+"42x30/"+w[i].icon[0]+'"/> <img src="'+imgPath+"42x30/"+w[i].icon[1]+'"/></li>'
							   +'<li class="desc">'+w[i].desc[0]+'</li>'
							   +'<li class="wind">'+w[i].desc[1]+'</li></ul>'
				}
				
				var P = {
					more:'<a href="http://tool.115.com/?ct=live&ac=weather&city='+cityid+'" rel="nr"  title="点击查看详细天气预报" target="_blank">[查看详细]</a>',
					city: data.city[0] + "&nbsp;" + data.city[1],
					postTime: data.public_time,
					weather:weatherHTML
				}
				
				_$("#weatherBox").el.innerHTML = format(cache.get("WEATHER_TEMP"),P);	
				
			}
		})
	}
	

	
	function setCity(){
		_$("#weather").hide();
		_$("#setCity").show();
		CT = cache.get("CITY");
		LoadCity(CT[0],CT[1]);		
	}
	
	function LoadCity(key,cityid){
		_$("#w_city").el.innerHTML="";
	
		CityArr.forEach(function(element,index,array){
			if(element[1]==key){
				_$("#w_city").create("option",
					{
						value:element[2]
					},
					function(el){
						el.innerHTML = element[0];
						this.append(el);
					}
				)
			}
		});
		
		
		if(key){
			_$("#w_pro").el.value = Number(key);
		}
		
		setTimeout(function(){
			if(cityid){
				_$("#w_city").el.value = Number(cityid);
			}else{
				_$("#w_city").el.childNodes[0].selected = true
			}
		},0)
		
	}//加载城市select 二级数据
		
	function tick(){
		_$("#setCity").hide();
		_$("#weather").show();
		var p = _$("#w_pro").el.value;
			c = _$("#w_city").el.value;
		getWeather(c)			
		cache.set("CITY",[p,c]);
		Cookie.set("cityId",p+"|"+c);
	}//确定
	
	function showMore(){
		
		_$("#weatherBox").toggle();
		if(Browser.isIE=="6.0"){
			_$("#mailBox ul").toggle()
		}
		
		_$("#weatherBox").on("mouseover",function(){
			cache.set("WTBOX_HIDESTATE",0)
		})
		_$("#weatherBox").on("mouseout",function(){
			cache.set("WTBOX_HIDESTATE",1)
		})
	}
	function init(n){
		
		if(Cookie.is("cityId") && !n){			
			var id = Yl.trim(Cookie.get("cityId")).split("|"); 
			cache.set("CITY",id);
		
			getWeather(id[1]);
		}else{
		
			XSAjax.send({
				url:"http://api.115.com/ipaddress",
				after:function(){
					try{
						cityName =ILData[3].replace("市","");
						
						if(cityName){
							var City = getCityId(cityName);
							
							LoadCity(City[0],City[1])
							
							getWeather(City[1]);
							cache.set("CITY",City);
							Cookie.set("cityId",City[0]+"|"+City[1]);
							
						}
						
					}catch(e){}
				}
			});//ip2city
			
		}
		
	}
	
	
	return{
		init:init,
		set:setCity,
		loadCity:LoadCity,
		tick:tick,
		more:showMore
	}


})();
/*天气加载类结束*/


window.onload = (function(){
	Weather.init();
	
	document.getElementById('date').innerHTML=Calendar.day();
	SR.SearchRate.Init("sm_tab","active","");
	var sbBox = document.getElementById('sb');
	var sbForms = sbBox.getElementsByTagName('form');
	/*for(var i = 0,len = sbForms.length; i < len; i++){
		sbForms[i].reset();
	}*/
	document.onclick = function(e){
    var e = e || window.event, obj = e.srcElement || e.target, tid = obj.id;

	if(tid!=="showSetting"){
		if(_$("#settingBox").el){
			_$("#settingBox").hide();
		}
	}
	if(tid=="closeWTBOX" || cache.get("WTBOX_HIDESTATE")&& tid!=="wet" || (tid!='weatherBox' &&obj.parentNode.id!='weatherBox'&&obj.parentNode.parentNode.id!='weatherBox'&& obj.parentNode.parentNode.parentNode.id!='weatherBox')&& tid!=="wet" ){
		if(document.getElementById('weatherBox')) document.getElementById('weatherBox').style.display='none';
	}
	
    if (
		(obj.tagName && obj.tagName.toUpperCase()== "A") || 
		(obj.parentNode.tagName && obj.parentNode.tagName.toUpperCase() == "A") || 
		(obj.parentNode.parentNode.tagName && obj.parentNode.parentNode.tagName.toUpperCase() == "A")){
		
		
		if(obj.rel && obj.rel=='nr'){ return;}
		
		var L,T;
		if(obj.parentNode.tagName.toUpperCase() == "A" && obj.tagName.toUpperCase() =="IMG"){
			L = obj.parentNode.href,T = obj.alt;
		}else if(obj.parentNode.parentNode.tagName.toUpperCase() == "A"){
			L = obj.parentNode.parentNode.href,
			T = obj.innerHTML;
		}else{
			L = obj.href , T = obj.innerHTML;
			if(Yl.trim(L)=="javascript:void(0);" || Yl.trim(L)=="javascript:void(0)"){
				L = T;
			}
			if(obj.getAttribute("rel")){
				L=T = obj.innerHTML;
			}
		}
		
		
		UserTrack.add(obj);
    }
	
};
	
})



/*历史记录*/
var UserTrack = (function(){
	function add(o){
		try{
			if(o.tagName.toUpperCase() ==("A") && o.href.indexOf("http://") == 0 && o.href.indexOf("http://" + Yl.getHost())!= 0 ){
				if(o.rel && o.rel=="nr"){
					return;
				}
				var Track ={
					url: o.href,
					content: o.innerHTML
				},
				data = Track.url+"+"+Track.content+"_[YLMF]_",
				oldData = Cookie.get("history");
				if(oldData){
					if(oldData.indexOf(data)>-1){
						oldData = oldData.replace(data,"");
					}				
					data += oldData;
				}
				//Cookie.set("history",data,null,null,'114la.com');
				Cookie.set("history",data);
				var Hbox;
				if( document.getElementById('bb1')){
					Hbox = document.getElementById('bb1').getElementsByTagName("iframe");
				}
				if(Hbox && Hbox.length){
					Hbox[0].contentWindow.History.show();
				}
			}
			
		}catch(e){}
	
	};
	
	return{
		add: add
	}
	
})(); 
function KeywordCount(keyword, Counturl){
  
	
}