var hideSetting =false;
var City='';
City= Ylmf.Cookie.get('city');
var showMoreweather=false;
if(Ylmf.Cookie.get("clear")==null){
	clear ();
	Ylmf.Cookie.set("clear",1)
}

function clear(){
			Ylmf.Cookie.clear("layout");
			Ylmf.Cookie.clear("style");
			Ylmf.Cookie.clear("font");
			Ylmf.Cookie.clear("bg");
}
$(window).on("load",function(){
						 
	skinSelecter("layout-setting");
	skinSelecter("style-setting");
	skinSelecter("style-quick");
	skinSelecter("font-setting");
	skinSelecter("bg-setting");
	
	tabMenu("search-menu-show","search-box",function(box){
		if(box.id){
			$("search-more").hide();
			var num = Number(box.id.replace("search-box",''));
			var input = box.getElementsByTagName("input")[0];
			input.style.background = "#FFFCEC";
			input.focus();
			if(box.id=="search-box6"){
				$("localsearch").show();
			}else{
				$("localsearch").hide();
			}
			
		}
		
	});//search-menu
	
	tabMenu("board-menu","board-box",function(box){
											  
		$("search-more").hide();
		
		var datatxt = "soft.txt";
		var html2width  ='588';
		if(Ylmf.Cookie.get("layout")=="2"&&Ylmf.screenWidth>=1440){
			datatxt = "soft_w.txt"
			html2width  = "100%";
			
		}
		if(box.id=="board-box1"&&box.innerHTML==""){
			box.innerHTML='正在加载中，请稍候…'
			Ylmf.Ajax(datatxt,function(xhr){
				var result = xhr.responseText;
				box.innerHTML = result;
			})
		}
		if(box.id == "board-box2"&&box.innerHTML==""){
			
			box.innerHTML ='<iframe src="game.html" width="'+html2width+'" height="310" frameborder="0"></iframe>';
			board2=true;
		}
		if(box.id == "board-box3"&&box.innerHTML==""){
			box.innerHTML ='<iframe src="#" width="588" height="310" frameborder="0"></iframe>';
			board3=true;
		}
		if(box.id == "board-box4"&&box.innerHTML==""){
			box.innerHTML ='<iframe src="#" width="588" height="310" frameborder="0"></iframe>';
			board4=true;
		}
		
		
	});
	


	
	$(document.body).on("click",function(e){
		var obj = e.srcElement || e.target
		var id = obj.id;
		History.save(obj);
		
		if(id=="delHistory"){
			History.clear();
		}
		
		if(id!=="smore"){
			$("search-more").hide();
		}
		
		if(id=="setting"||id=="setting2"){
			$('weather-box').hide();
			$("setting-box").toggle();
		}else if(hideSetting==true||id=="setting-close"){
			$("setting-box").hide();
		}
		if(id=="setting-reset"){
			setStyle(0);
			Ylmf.Cookie.clear("layout");
			Ylmf.Cookie.clear("style");
			Ylmf.Cookie.clear("font");
			Ylmf.Cookie.clear("bg");
			window.location.reload(); 
		}


		if(id=="simple_w"){
			$('weather-box').toggle();
		}//天气显示
		if(id=="weather-close"){
			$('weather-box').hide();
		}
		
		
		if(id=="change-city"){
		var city = document.getElementById("city");
			if(Ylmf.getStyle(city,"display")=="none"){
				$("weather-setting").hide();
				$("city").show();
			}else{
				$("weather-setting").show();
				$("city").hide();
			}
		}
		if(id=="city-button"){
			$("weather-setting").hide();
			$("city").show();
			Ylmf.Cookie.set("province",document.getElementById("w_pro").value);
			Ylmf.Cookie.set("city",document.getElementById("w_city").value);
			var cityBox = document.getElementById("w_city");
			settingCity(cityBox.options[cityBox.selectedIndex].innerHTML,cityBox.value);
		}

		if(id=="cslayout"){
			
			if(obj.className == ""){
				obj.className = "alignleft";
				Ylmf.Cookie.set("cs",1);
				document.getElementById("list").className ="left";
			}else{
				obj.className = "";
				Ylmf.Cookie.clear("cs");
				document.getElementById("list").className ="";
			}
			
		}
		
		
		
		$("suggest").hide();
		
		if(id=="setHomepage"){
			Ylmf.setHomepage(obj);
		}
		
		
		
		
	});//body click



	$("setting-box").on("mouseover",function(){
		 hideSetting =false;
	})
	$("setting-box").on("mouseout",function(){
		 hideSetting =true;
	})	
	Hover(Dom.get("setting"),function(){
		$("setting-box").show();
		
		
	});
	
	History.show();
	
	showColl();
	
	if(Ylmf.Browser.ie6){
		document.execCommand("BackgroundImageCache", false, true);  
		var lis = Dom.get("coolsites").getElementsByTagName("dl");
		for(var i=0,len = lis.length; i<len;++i){
			lis[i].onmouseover=function(){
				this.tmpClass = this.className;
				this.className ="iehover";
			}
			lis[i].onmouseout=function(){
				this.className = this.tmpClass;
			}
		}
	} //coolsites li:hover for ie6
	
	
	
	
if(City){
	loadWeather(City);
}
else{
	Ylmf.JsLoader('http://pfpip.sina.com/ip.js','ipdata',function(){
		
		var cityName = location2.replace("市","");
		var proName = location1.replace("省","");
		var id = getCityId(cityName);
		var proId = getProId(proName);
		Ylmf.Cookie.set("province",proId);
		Ylmf.Cookie.set("city",id)
		loadWeather(id);
		showCity("w_pro","w_city",Ylmf.Cookie.get("province"),Ylmf.Cookie.get("city"));
	},"gb2312");
}

initProvince("w_pro","w_city");

Dom.get("date").innerHTML = clock();



if(Ylmf.Cookie.get("layout")=="2"&&Ylmf.screenWidth>=1680){
	Dom.get("b1").innerHTML += '<iframe style="margin-left:5px;" src="#" width="470" width="470" height="62" frameborder="0"></iframe>';				  
}
if(Ylmf.screenWidth<1440){
	$("full").hide();
}
	
});//window onload
var getBgimgstyle = false;
function skinSelecter(box) {
	var Items = Dom.get(box).getElementsByTagName("a");
	var type = box.split("-")[0];

	for( var i = 0,len=Items.length;i<len;++i){
		
/*		if(type=="bg"&& getBgimgstyle == false){
			var obj = Items[i];
			var img = obj.getAttribute("rel");
			getBgimg(obj,img);
			getBgimgstyle = true;
		}*/
		
		$(Items[i]).on('click',function(e){
			var obj = e.srcElement || e.target;
			var value = obj.getAttribute("rel");
			switch(type){
				case "layout":
					//setLayout(value);
					Ylmf.Cookie.set("layout",value);
					window.location.reload(); 
					break;
				case "style":
					setStyle(value);
					break;
				case "font":
					//if(value=="default"){ return;}
					setFont(value);
					break;
				case "bg":
					setBg(value);
					iscurBg(obj);
					break;
			}
				
		})
	}
	
}


function setStyle(n) {
	var cssList = ["","green/green.css","pink/pink.css"]; 
	var n = Number(n);
	var css = Dom.get("skin");
		var cssurl = "static/skins/";
		if(cssList[n]!==""){
			cssurl += cssList[n];
			Ylmf.Ajax(cssurl,function(){
				css.href = cssurl;
				switch(n){
				case 1:
				setFont("green");
				break;
				case 2:
				setFont("pink");
				break;
				default:
				setFont("blue");
				}
			})
		}else{
			css.href = "";
			setFont("blue");
		}
		
		
	Ylmf.Cookie.set("style", n);
}

function setLayout(n){
	
	var Layout = ["default","layout-medium","layout-full"];	
	setStylesheet(Layout[n]);
/*	var Layout = ["default","medium","full"];
	document.body.className = Layout[n];*/
	Ylmf.Cookie.set("layout",n);
}

function setFont(css) {
	var font = Dom.get("font");
		var cssurl = "static/css/font/";
		cssurl += css;
		cssurl += ".css";
		font.href = cssurl;
	Ylmf.Cookie.set("font",css);
}
function setBg(img) {
	var styleEl = document.getElementsByTagName("style")[0];
	var csscode = "";
	var num = img.split(".")[0];
	if(img!=="default"){
		csscode+='.wrap { background:none;}'
		csscode+='body { background:url(static/images/bg/'+img+');}'
		csscode+='#bg-setting a.img'+num+'{border:1px solid red;border-bottom-width:2px;}';
	}else{
		csscode+='#bg-setting a.default{border:1px solid red;border-bottom-width:2px;}';
	}
	if (styleEl.styleSheet) { // IE
	   styleEl.styleSheet.cssText = csscode;
	} else { // W3C
	   //styleEl.appendChild(document.createTextNode(csscode));
	   styleEl.innerHTML = csscode;
	}
	Ylmf.Cookie.set("bg",img);;
}


function getBgimg(obj,img){
/*	if(img=="default"){
		obj.style.background = "#4289C5";
	}else{
		obj.style.background = 'url(static/images/bg/'+img+') -40px -10px';
	}*/
	if(img == Ylmf.Cookie.get("bg")){
		obj.style.border = "1px solid red";
		obj.style.borderBottomWidth = "2px";
	}else{
		obj.style.border = "1px solid #cccccc";
		obj.style.borderBottomWidth = "2px";
	}
}
function iscurBg(obj){
	var Items = document.getElementById("bg-setting").getElementsByTagName("a");
	for(var i = 0,len = Items.length;i<len;++i){
		Items[i].style.border = "1px solid #cccccc";
		Items[i].style.borderBottomWidth = "2px";
	}
		obj.style.border = "1px solid red";
		obj.style.borderBottomWidth = "2px";
}


function Hover(obj,fn){
	var MouseDelayTime=400;
	var waitInterval;
	$(obj).on("mouseover",function(){
		waitInterval = window.setTimeout(fn,MouseDelayTime)
	});
	$(obj).on("mouseout",function(){
		if(waitInterval!=undefined){
			window.clearTimeout(waitInterval);
		}
	});
}

function tabMenu(id,boxId,callback,evt){
	var Items = document.getElementById(id).getElementsByTagName("a");
	var lis = document.getElementById(id).getElementsByTagName("li");
	var MouseDelayTime=400;
	var waitInterval;
	var evt = "click,mouseover";
	for(var i=0,len = Items.length;i<len;i++){

		$(Items[i]).on(evt,function(e){
			var obj = e.srcElement || e.target;
			
			function selectMenu(){
				var bid = obj.getAttribute("rel");
				var	box = document.getElementById(bid);
				
				var cleanstyle =function(){
					for(var j=0,len=lis.length;j<len;j++){
						if(lis[j].className){
							lis[j].className = "";
						}
					}
				}
				cleanstyle();
				obj.parentNode.className = "current";
				if(bid=="more"){
					$("search-more").show();
					return;
				}
				for(var k = 0;contentBox = document.getElementById(boxId+k);k++){
					$(contentBox).hide();
				}
				$(box).show();
				return callback(box);
			}
				waitInterval = window.setTimeout(selectMenu,MouseDelayTime)
		
		})
			
		$(Items[i]).on("mouseout",function(e){
			if(waitInterval!=undefined){
				window.clearTimeout(waitInterval);
			}
		});

		
	}
}//tabMenu



function setStylesheet(title){
	var i, cacheobj, altsheets=[""];
	for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
		if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { 
			cacheobj.disabled = true;
			altsheets.push(cacheobj);
			if(cacheobj.getAttribute("title") == title) {
				
				cacheobj.disabled = false;
			}
		}
	}

}
//setStylesheet("medium");






var History = {
save: function (obj){
	try {
        if (obj.tagName == "A" && obj.href.indexOf("http://") == 0 && obj.href.indexOf("http://" + location.host) != 0 &&obj.getAttribute("rel")!=="sugitem") {
				var wlink = obj.href;
				var wname = obj.innerHTML;
			 if (wname!='undefined'){
				 h=wname+"+"+wlink+"_114la_"; 
				 
				 h+=unescape(Ylmf.Cookie.get("history"));
				 h=escape(h);
				 Ylmf.Cookie.set("history",h); 
				 History.show();
			 }
        }
    } catch(e) {}
    return true;
},
show :function() {
    try {
        var history = Ylmf.Cookie.get("history");
        history = unescape(history);
        var content = '';
        if (history != "null") {
            history_arg = history.split("_114la_");
            i = 0;
            linknum = 0;
            len = history_arg.length;
            for (i = 0; i < len; i++) {
                var wlink = history_arg[i].split("+");
                if (history_arg[i] != "null" && content.indexOf(wlink[0]) == -1 && linknum < 44) {
                    content += "<li><a href=\"" + wlink[1] + "\" title=\"" + wlink[0] + "\">" + wlink[0] + "</a></li>";
                    linknum += 1;
                }
            }
            Dom.get("history").innerHTML = content;
        } else {
            Dom.get("history").innerHTML = '<li class="none">对不起，您没有任何浏览记录</li>';
        }
    } catch(e) {}
},

clear: function() {
	clean = confirm("确定要清除所有的浏览记录？") ;
	if (clean) {
		Ylmf.Cookie.clear("history");
		Dom.get("history").innerHTML = '<li class="none">对不起，您没有任何浏览记录</li>';
	}
}

}


function searchlog(num){}
function logad(){}


function favoFuninput(o, n) {
    if (n == 1) {
        o.className = "int n";
        if (o.value == "站名" || o.value == "网址") {
            if (o.value == "网址") o.value = "http://";
            else o.value = ""
        }
    } else {
        if (!o.value || o.value == "http://") {
            o.className = "int u";
            if (o.name == "urlName") o.value = "站名";
            if (o.name == "Url") o.value = "网址";
        }
    }
}

function addColl(o) {
    favoname = o.urlName;
    favourl = o.Url;

    err = 0;
    if (favoname.value == "" || favoname.value == "站名") {
        favoname.className = "int e";
        favoname.value = "站名";
        err = 1
    }
    if (favourl.value == "" || favourl.value == "http://" || favourl.value == "网址") {
        favourl.className = "int e";
        favourl.value = "网址";
        return false;
        err = 1
    }
	if(favourl.value.indexOf("http://") !== 0 && favourl.value.indexOf("https://")!==0 ){

			alert('网址格式不正确！必须以 "http://" 或 "https://"开头！');
			err = 1;

	}
	
    if (err == 1){ return false }
	saveColl(favoname.value,favourl.value);
	
	Dom.get('addColl').style.display='none';
	Dom.get('addCollmsg').style.display = "block";
    Dom.get('addCollmsg').innerHTML = "成功添加 [<a onclick=\"Dom.get('addColl').style.display='block';Dom.get('addCollmsg').style.display = 'none';\">返回继续添加</a>]";
	o.reset();
	
    return false;
}

function delColl(o) {
	var obj = o.getElementsByTagName("a")[0];
	var name = obj.innerHTML;
	var url=obj.href;
	url=url.replace(/^(.*?)(\/)?$/,'$1');
	var coll = name+"+"+url+"■";
	var sitename = Ylmf.Cookie.get("cl");

		sitename = unescape(sitename);
		newColl = sitename.replace(coll, "");
		if(newColl==""){
			Ylmf.Cookie.clear("cl");
		}else{
			Ylmf.Cookie.set("cl",newColl);	
		}
	showColl();
}

function saveColl(name,url){
	url = url.replace(/^(.*?)(\/)?$/,'$1');
	var coll =name+"+"+url+"■";

	if(coll){
		if(Ylmf.Cookie.get("cl")!==null&&unescape(Ylmf.Cookie.get("cl")).indexOf(coll)!==0){
			coll+=unescape(Ylmf.Cookie.get("cl"));
		}
		
		coll=escape(coll);
		Ylmf.Cookie.set("cl",coll);
		showColl();
	}
	
}

function showColl(){

    try {
        var customsite = Ylmf.Cookie.get("cl");
		 customsite = unescape(customsite);
        var content = '';
		var img = '<img src="static/images/del.gif" title="删除此网址" onclick="delColl(this.parentNode)" align="absmiddle" />';
        if (customsite !== "null") {
            customsite_arg = customsite.split("■");
            i = 0;
            linknum = 0;
            len = customsite_arg.length;
            for (i = 0; i < len; i++) {
                var slink = customsite_arg[len - i-1].split("+");
                if (customsite[i] !== "null" && content.indexOf(slink[0]) == -1 && linknum < 35) {
                    content += '<li>'+img+' <a href="' + slink[1] + '" title="' + slink[0] + '">' + slink[0] + '</a></li>';
                    linknum += 1;
                }
            }
            Dom.get("Collbox").innerHTML = content;
        } else {
            Dom.get("Collbox").innerHTML = '<li class="none">您还没有任何已添加的网址收藏。</li>';
        }
    } catch(e) {
		
	}
}