// JavaScript Document
// Version : 0.7		Date : 2010-03-29
// Author  : Nanyang	Email: nanyang9@gmail.com
function _scroll(scrollid,direction,interval,step){
	this.con = document.getElementById(scrollid);
	this.dir = !direction ? 4 : direction;
	this.int = !interval ? 40 : interval;
	this.stp = !step ? 2 : step;
	var tab,tb,tr;
	this.sw=null;
	this.sw2=null;
	tab=document.createElement("table");
	tab.setAttribute("cellpadding",0);
	tab.setAttribute("cellspacing",0);
	this.node1=document.createElement("td");
	this.node2=document.createElement("td");
	tb=document.createElement("tbody");
	tr=document.createElement("tr");
	tb.appendChild(tr);
	tr.appendChild(this.node1);
	if(this.dir%2==1){
		tr=document.createElement("tr");
		tb.appendChild(tr);
		tab.setAttribute("width","100%");
	}
	tr.appendChild(this.node2);
	tab.appendChild(tb);
	this.node1.innerHTML=this.con.innerHTML;
	this.node2.innerHTML=this.con.innerHTML;
	this.con.innerHTML="";
	this.con.appendChild(tab);
	this.size = this.dir%2==0 ? this.node1.offsetWidth : this.node1.offsetHeight;
	this.con.onmouseover=_scroll.mouseover(this);
	this.con.onmouseout=_scroll.mouseout(this);
}
_scroll.marquee=function(scObj){
	return function(){scObj.move(scObj.stp);};
}
_scroll.marqueeto=function(scObj,ds){
	return function(){
		if(ds<0){
			clearInterval(scObj.sw);
			scObj.sw2=setTimeout(function(){scObj.start();},3000);
			return ;
		}
		var t=Math.floor(ds/8);
		if(t<scObj.stp*5) t=scObj.stp*5;
		scObj.move(t);
		ds-=t;
	};
}
_scroll.mouseover=function(scObj){
	return function(e){
		if(checkHover(e||window.event,scObj.con)&&scObj.sw) clearInterval(scObj.sw)
	};
}
_scroll.mouseout=function(scObj){
	return function(e){
		if(checkHover(e||window.event,scObj.con)) scObj.start();
	};
}
_scroll.prototype.move=function(sp){
	var addr;
	if(!sp) sp=this.stp;
	switch(this.dir){
		case 1 : addr=this.con.scrollTop;
			addr+=sp;
			if(addr>this.size) addr-=this.size;
			this.con.scrollTop=addr;
			break;
		case 2 : addr=this.con.scrollLeft;
			addr-=sp;
			if(addr<0) addr=this.size-sp;
			this.con.scrollLeft=addr;
			break;
		case 3 : addr=this.con.scrollTop;
			addr-=sp;
			if(addr<0) addr=this.size-sp;
			this.con.scrollTop=addr;
			break;
		case 4 : addr=this.con.scrollLeft;
			addr+=sp;
			if(addr>this.size) addr-=this.size;
			this.con.scrollLeft=addr;
			break;
	}
}
_scroll.prototype.start=function(){
	if(this.sw) clearInterval(this.sw);
	if(this.sw2) clearTimeout(this.sw2);
	var mq=_scroll.marquee(this);
	this.sw=setInterval(mq,this.int);
}
_scroll.prototype.moveto=function(distance){
	var mq=_scroll.marqueeto(this,distance);
	if(this.sw) clearInterval(this.sw);
	if(this.sw2) clearTimeout(this.sw2);
	this.sw=setInterval(mq,this.int);
}


_scroll.marqueeto2=function(scObj,ds,ds2){
	return function(){
		if(ds<=0){
			clearInterval(scObj.sw);
			scObj.sw2=setTimeout(function(){scObj.next(ds2);},3000);
			return ;
		}
		var t=Math.floor(ds/6);
		if(t<scObj.stp*4) t=scObj.stp*4;
		if(t>ds) t=ds;
		scObj.move(t);
		ds-=t;
	};
}
_scroll.prototype.start2=function(distance){
	if(this.sw) clearInterval(this.sw);
	if(this.sw2) clearTimeout(this.sw2);
	this.con.onmouseover=function(scObj){
		return function(e){
			if(scObj.sw2) clearTimeout(scObj.sw2);
		}
	}(this);
	this.con.onmouseout=function(scObj,dis){
		return function(e){
			if(scObj.sw2) clearTimeout(scObj.sw2);
			scObj.sw2=setTimeout(function(){scObj.next(dis);},500);
		}
	}(this,distance);
	this.next(distance);
}
_scroll.prototype.next=function(distance){
	var mq=_scroll.marqueeto2(this,distance,distance);
	if(this.sw) clearInterval(this.sw);
	if(this.sw2) clearTimeout(this.sw2);
	this.sw=setInterval(mq,this.int);
}


// tools function
function checkHover(e,handler){
	if(e.type!='mouseout' && e.type!='mouseover') return false;
	var reltg = e.relatedTarget ? e.relatedTarget : e.type=='mouseout'?e.toElement:e.fromElement;
	if(reltg==handler) return false;
	//return handler.contains ? !handler.contains(reltg) : !!(handler.compareDocumentPosition(reltg)&16);
	while(reltg && reltg!=handler) reltg=reltg.parentNode;
	return (reltg != handler);
}
function nyAddEvent(_obj,eventName,fun){
	if(_obj.addEventListener) _obj.addEventListener(eventName,fun,false);
	else _obj.attachEvent("on"+eventName,fun);
}


function AddFavorite(sURL,sTitle){
	try{window.external.addFavorite(sURL,sTitle);}
	catch(e){
		try{window.sidebar.addPanel(sTitle,sURL,"");}
        catch(e){alert("加入收藏失败，请使用Ctrl+D进行添加");}
	}
}
function SetHome(obj,vrl){
	try{obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);}
	catch(e){
		if(window.netscape){
			try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}
			catch(e){alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");}
			var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',vrl);
		}
	}
}





function getPos(nodeObj){
	var x=0;
	var y=0;
	while(nodeObj){
		x+=nodeObj.offsetLeft;
		y+=nodeObj.offsetTop;
		nodeObj=nodeObj.offsetParent;
	}
	return [x,y];
}
function talk(main_id,aid_id,t_top){
	var mobj=document.getElementById(main_id);
	var aobj=document.getElementById(aid_id);
	var aid_w=aobj.scrollWidth;
	var dom;
	var ctop=t_top;
	mobj.style.top=ctop+"px";
	mobj.style.left=(getPos(aobj)[0]+aid_w)+"px";
	mobj.style.display="block";
	return function(){
		var p,t
		if(!dom){
			t=document.documentElement.scrollTop-document.body.scrollTop;
			if(t>0) dom=document.documentElement;
			else if(t<0) dom=document.body;
		}
		t = (!!dom) ? dom.scrollTop+t_top : t_top;
		if(t!=ctop){
			t-=ctop;
			p=t/5;
			p=Math.abs( p>0 ? Math.ceil(p) : Math.floor(p) );
			if(p<3) p=3;
			if(p>Math.abs(t)) p=Math.abs(t);
			ctop+=( t<0? -1*p : p );
			mobj.style.top=ctop+"px";
		}
		t=getPos(aobj)[0]+aid_w;
		mobj.style.left=t+"px";
	};
}

if(location.href.lastIndexOf("ask.asp")<0)
	nyAddEvent(window,"load",function(){setInterval(talk("online","top",160),60);});



function setHeight(ids,dis){
	var obj1=document.getElementById(ids[0]);
	var obj2=document.getElementById(ids[1]);
	if(!obj1||!obj2) return;
	if(obj1.scrollHeight>obj2.scrollHeight+dis[2]) obj2.style.height=(obj1.scrollHeight-dis[1]-dis[2])+"px";
	else obj1.style.height=(obj2.scrollHeight+dis[2]-dis[0])+"px";
}
nyAddEvent(window,"load",function(){setHeight(["lefter","info"],[15,40,50+10+87]);})
