/* 以前旧版js Start */
function ajaxurl(adDiv){
	var baseurl = "http://xyq.163.com/" + (adDiv || "");
	return baseurl;	
	}
function getajax(){
	var xmlhttp = false;
	if(window.ActiveXObject){
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
	}
function sendRequest(url,func,posts){
	ajax = getajax();
	ajax.onreadystatechange = func;
	if(posts==null){ajax.open("GET", url, true);}
	else{
	ajax.open("POST", url, true);
	ajax.setRequestHeader("Content-Length",posts.length); 
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send(posts);	
}

/*------------Try函数-------------*/
var Try = {
  these: function() {
    var returnValue;

    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }
    return returnValue;
  }
}

function tryCollectGarbage(){
    return Try.these(
        function() {return CollectGarbage();},
        function() {return false;}
        );
}
function getResult(adDiv,num){
	if (ajax.readyState < 4) {
		$("loading").style.visibility = "visible";
	} 
	else if (ajax.readyState == 4) {
		$("loading").style.visibility = "hidden";
		if (ajax.status == 200){
		var result = ajax.responseText;
		if(adDiv=="mhjl"){
			var re = /\#(\d{2})/g;
			result = result.replace(re,"<img src='"+ajaxurl()+"images/emote/$1.gif' alt='' />");
			var re = /\#(\d{1})/g;
			result = result.replace(re,"<img src='"+ajaxurl()+"images/emote/0$1.gif' alt='' />");
						
			$('mhjl_answer').innerHTML = result;
		}
		else if(adDiv=="getrss"){
			$('sub'+num).innerHTML = result;
			}
	}
		else {
			alert("异步加载错误!");
			delete ajax;
			tryCollectGarbage();
		}
	}
}	
function mhjl()//定义收到服务器的响应后需要执行的JavaScript函数
{
	var q = $("mhjl_ask").value;
	if(q==""){alert("您好，问题不能为空！");return false;}
	//alert(q);
	$('mhjl_answer').innerHTML = "正在查询中，请稍候...";
	var url = ajaxurl("php/mhjl.php");
	var posts = "q="+q;
	function func(){
    	getResult("mhjl");
	};
	sendRequest(url,func,posts);
}
/* 以前旧版js End */


// change menu
//参数(menu前字符,content前字符,总数,当前,打开class,关闭class,是否处理open状态)
function change_menu(menu_str,total_num,now_num,open_className,close_className,not_chk_open){
	not_chk_open=(not_chk_open==null)?true:false;
	var menu_className=$(menu_str+now_num).className;
	if(menu_className==open_className && not_chk_open){
		$(menu_str+now_num).className=close_className;
	}
	else if(menu_className==close_className){
		for(var i=1; i<=total_num;i++){
			$(menu_str+i).className=close_className;
		}
		$(menu_str+now_num).className=open_className;
	}
}
// 滚动
var Scroller=function(id){
		
	//初始化函数 START>>>
	this.init=function(){
		
		this.ele.style.position="absolute";
		this.ele.style.width=(this.options.width*2+8)+"px";
		this.ele.style.left="0";
		this.ele.style.top="0";

		var HTML=this.ele.innerHTML;
		this.ele.innerHTML+=HTML;
		
		var __scroller=this;/*最好少用这种方式*/
		
		this.ele.onmouseover=function(){
			__scroller.stop();
		}
		
		this.ele.onmouseout=function(){
			__scroller.run(this.normal);
		}
		
	}

	this.setOptions=function(ops){
		for (var desc in ops) {
			this.options[desc] = ops[desc];
		}
	}
	
	this.run=function(ops){
		this.stop();
		this.setOptions(ops||{});
		var __scroller=this;
		this.mover=setInterval(function(){__scroller.move();},this.options.speed);
		
	}
	
	this.stop=function(){
		if(this.mover!=null){
			clearInterval(this.mover);
		}		
	}
	
	this.move=function(){
		var pos=parseInt(this.ele.style.left);
		if(this.options.orientation=="left"){
			
			if(Math.abs(pos)-this.options.width>=0){
				this.ele.style.left="-"+this.options.step*2+"px";
			}else{
				this.ele.style.left=(pos-this.options.step)+"px";
			}
			
		}else{
		
			if(pos>=0){
				this.ele.style.left="-"+(this.options.width-this.options.step*2)+"px";
			}else{
				this.ele.style.left=(pos+this.options.step)+"px";
			}
		
		}
	}
	
	//定义基本属性；
	this.options={
		orientation:"left",
		speed:50,
		step:1,
		width:100
	}
	
	this.mover=null;
	this.ele=document.getElementById(id);

	//初始化 START>>>
	this.setOptions(arguments[1]||{});
	this.init();
	this.run();
}
//内页左边导航打开，关闭
function change_obj(obj_id,img_id){
	if($(obj_id).style.display =="block"){
		$(obj_id).style.display ="none";
		$(img_id).src = "http://res.nie.netease.com/xyq/gw/08v2/images/"+img_id+".gif";
	}
	else{
		$(obj_id).style.display = "block";
		$(img_id).src = "http://res.nie.netease.com/xyq/gw/08v2/images/"+img_id+"s.gif";
	}
}
//搜索
function onSave(){
	var keywords = document.getElementById("searchKeyWords").value;
	if (keywords.length == 0){
		alert("请输入搜索关键字");
	return false;
	}
  	var checkValue;
  	var r = document.getElementsByName("sradio"); 
  	for(var i=0;i<r.length;i++){ 
      if(r[i].checked==true){
          checkValue = r[i].value;
      } 
  	}
  if(checkValue==1){
	document.getElementById("keyfrom").value = "xyq.163.com";
	document.getElementById("form_search").action = "http://www.yodao.com/search?ue=utf8&";
  }else if(checkValue==2){
	document.getElementById("keyfrom").value = "xyq.163.com/news/";
	document.getElementById("form_search").action = "http://www.yodao.com/search?ue=utf8&";
  }
  else if(checkValue==3){
	document.getElementById("keyfrom").value = "xyq.163.com/introduce/";
	document.getElementById("form_search").action = "http://www.yodao.com/search?ue=utf8&";
  }
  else if(checkValue==4){
	document.getElementById("keyfrom").value = "xyq.163.com/fans/";
	document.getElementById("form_search").action = "http://www.yodao.com/search?ue=utf8&";
  }
  else if(checkValue==5){
	document.getElementById("form_search").action = "http://xyq.zhidao.163.com/htdocs/search/search.html#q=" + encodeURIComponent(keywords);;
  }
  document.getElementById("form_search").submit();
}
// 根据标记名 返回子节点集
// fat: 父级 如果字符串 ? $(fat).childNodes : fat.childNodes;
// childTagName: 子节点标记名
function getChild(fat,childTagName){
	var child = (typeof fat == "object") ? fat.childNodes : document.getElementById(fat).childNodes;
	var obj = new Array();
	if(typeof childTagName != "undefined"){
	  childTagName = childTagName.toUpperCase();
	  for(var i=0;i<child.length;i++){
		  if(child[i].tagName == childTagName) obj.push(child[i]);
	  }
	}
	else{
	  for(var i=0;i<child.length;i++){
		  if(typeof child[i] != "undefined") obj.push(child[i]);
	  }
	}
	return obj;
}
function menuOnload(){	
	var ul = document.getElementById("menuTree").getElementsByTagName("ul");
	for(var i=0;i<ul.length;i++){
		var fat = ul[i].parentNode;		
		if(ul[i].className!="menuTree-sub"){			
			var a = getChild(fat,"a")[0];		
			a.className=" hasSub";
		}		
		(
			function(){				
				var _self = ul[i];
				var status;
				fat.onmouseover = function(){
					status = "open";
					var _this = this;
					setTimeout(
						function(){
							if(status=="open") {								
								_self.style.display = "block";
							}
						}
						,180
					);
				}
				fat.onmouseout = function(){
					status = "close";
					setTimeout(
						function(){
							if(status=="close") _self.style.display = "none";
						}
						,200
					);
				}
			}
		)();
	}
	
}
//首页 列表 栏目背景色处理
function type_onload(fatID){
	var type = $(fatID).getElementsByTagName("span");												
	for(var i=0;i<type.length;i++){
		if(type[i].getAttribute("name")=="type" && type[i].innerHTML!=""){
			if(i%2==0) type[i].className = "typeBg-1";
			else type[i].className = "typeBg-2";
		}
	}
}
//iframe高度自适应
function dyniframesize(iframeObj) {
	if (iframeObj && !window.opera) {
		var leftHeight = 573;
		iframeObj.style.display = "block";
		if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight) {
			if (iframeObj.id=="mainFrame"&& iframeObj.contentDocument.body.offsetHeight < leftHeight) {
				iframeObj.height = leftHeight;
			} else {
				iframeObj.height = iframeObj.contentDocument.body.offsetHeight;
			}
		} else if (iframeObj.Document && iframeObj.Document.body.scrollHeight) {
			if (iframeObj.id=="mainFrame" && iframeObj.Document.body.scrollHeight < leftHeight) {
				iframeObj.height = leftHeight;
			} else {
				iframeObj.height = iframeObj.Document.body.scrollHeight;
			}
		}
	}
}
//首页知道 处理
function index_zhidao(){
	var q = $("q_keywords");
	q.value = "请输入关键字";
	q.className ="default";
	q.onclick = function(){
		this.value = "";
		this.className ="";
	}
	
}
	   // 返回最终样式函数，兼容IE和DOM，设置参数：元素对象、样式特性
function getDefaultStyle(obj,attribute){ 
    return obj.currentStyle?obj.currentStyle[attribute]:document.defaultView.getComputedStyle(obj,false)[attribute];
}
	// right bar 
function navBtn(fatID){
	var plus = 410;
	var timeOut = 80;
	var minHeight = 400;	
    var h = document.documentElement.clientHeight;
	if(getDefaultStyle($(fatID),"position")=="absolute" && h>minHeight+plus){
		$(fatID).style.top = (document.body.scrollTop?document.body.scrollTop :document.documentElement.scrollTop)+plus+"px";				
	}
	setTimeout('navBtn("'+fatID+'")',timeOut);
}
