// JavaScript Document


/*
 *		我的JS工具集
 *		[最后编辑在2008-4-25]
 *
 *		$(objID) :用ID取元素, 返回元素
 *
 *		string.trim() :去掉字串左右的空格, 只去掉一边的空格可以用trimL()或trimR()
 *
 *		date.add(ms) :返回加上了毫秒后的时间对象
 *			扩张: addSecond, addMinute, addHour, addDay, addMonth, addYear, addWeak
 *
 *		ui :页面参数
 *			ui.getPageWidth() :整个页面的宽度
 *			ui.getPageHeight() :整个页面的高度
 *			ui.getScrollTop() :页面的滚动高度
 *
 *		mouse :鼠标指针
 *			mouse.x :鼠标的x轴坐标
 *			mouse.y :鼠标的y轴坐标
 *
 *		masker :遮罩层
 *			masker.show() :显示遮罩
 *			masker.hide() :隐藏遮罩
 *
 *		getLeft(obj) :取元素对页面的左边距, 返回numeric
 *
 *		getTop(obj) :取元素对页面的上边距, 返回numeric
 *
 *		hasAttribute(elem, aname) :元素elem是否含有aname的属性, 返回boolean
 *
 *		getElementsByAttribute(aname) :取所有含有aname属性的元素, 返回元素的集合
 *
 *		runOnReady(func) :在文档装入完成后运行, func是函数, 不是字串
 *
 *		checkMail(str) :检查是否为电邮地址, 返回boolean
 *
 *		readCookie(key) :读出键为key的cookie的值, 返回字串
 *
 *
 *	=============================================================================
 *		Rel扩张属性
 *
 *		rel="marquee" :滚动内容
 *			speed="2" :速度, 轴滚动每次+2px, 值越大越快, 必选值
 *			step="100" :延迟, 每100毫秒轴滚动1次, 值越大越慢, 必选值
 *			direct="left" :方向, left为左, up为上, 默认为up
 *			rev="rev" :反向滚动
 *			space="200" :插入200px的空白
 *
 *		rel="dropmenu" :显示下拉菜单
 *			child="childMenuId" :子菜单的ID, 必选值
 *
 *		rel="thumb" :图片自动缩小, 只对img元素作用
 *			maxwidth="200" :最大宽度, 超过这个宽度会被缩小, 必选值
 *			maxheight="80" :最大高度, 超过这个高度会被缩小
 *			noresume="noresume" :被缩小时不添加链向大图的链接
 *			noscale="noscale" :不按原图的比例缩放
 *
 *		rel="tips" :显示提示
 *			msg="这是一个提示" :提示的内容为"这是一个提示", 必选值
 *			tipstyle="border:none" :自定义提示的样式
 *			tipclass="bold" :自定义提示的类
 *
 *		rel="wind" :浮动广告条
 *			origin="100" :原点, 必选值
 *			shift="500" :从水平中间线移动开的距离, 必选值
 *			side="left" :浮动在左或右
 */

var $ = function(objID){ return (document.getElementById(objID)) };

String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "") };
String.prototype.trimL = function(){ return this.replace(/(^\s*)/g, "") };
String.prototype.trimR = function(){ return this.replace(/(\s*$)/g, "") };

Date.prototype.add = function(ms){ return ( new Date(this.getTime() +ms) ) };
Date.prototype.addSecond = function(s){ return this.add(1000*s) };
Date.prototype.addMinute = function(m){ return this.add(1000*60*m) };
Date.prototype.addHour = function(h){ return this.add(1000*60*60*h) };
Date.prototype.addDay = function(d){ return this.addHour(24*d) };
Date.prototype.addWeak = function(w){ return this.addDay(7*w) };
Date.prototype.addMonth = function(m){ return (new Date( new Date().setUTCMonth(this.getUTCMonth() +m) )) };
Date.prototype.addYear = function(y){ return this.addMonth(12*y) };


var getLeft = function(obj){
	var objLeft = obj.offsetLeft;
	if(obj.offsetParent) objLeft += getLeft(obj.offsetParent);
	return (objLeft);
}

var getTop = function(obj){
	var objTop = obj.offsetTop;
	if(obj.offsetParent) objTop += getTop(obj.offsetParent);
	return (objTop);
}

var hasAttribute = function(elem, aname){
	if( elem.nodeName == "#text" ) return(false);
	if( elem.nodeName == "#comment" ) return(false);
	return ( elem.getAttribute(aname) != "" && elem.getAttribute(aname) != null ) 
}

var getElementsByAttribute = function(aname){
	var Elements = [];
	SearchElem( document.body );
	return (Elements);
	
	function SearchElem(elem){
		if(!elem) return;
		if( hasAttribute(elem, aname) ) Elements.push(elem);
		SearchElem(elem.firstChild);
		SearchElem(elem.nextSibling);
	}
}

var addEvent = function(evstr, func, cbubble){
	if(document.addEventListener){
		document.addEventListener( evstr, func, cbubble );	//FF, Safari, Opera
	}else if(document.attachEvent){
		document.attachEvent( "on"+ evstr, func );	//IE
	}
}

var runOnReady = function(func){
	if(window.addEventListener){
		window.addEventListener( "load", func, false );	//FF, Safari, Opera
	}else if(window.attachEvent){
		window.attachEvent( "onload", func );	//IE
	}
}

var checkMail = function(str){
	var e = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
	return str.match(e);
}

var readCookie = function(key){
	var cookies = document.cookie;
	cookies = cookies.split("; ");
	for(var i = 0; i < cookies.length; i++){
		var cookie = cookies[i].split("=");
		if(cookie[0] == key) return cookie[1];
	}
	return (null);
}

var ui = {
	getPageWidth:function(){return(document.documentElement.scrollWidth)},
	getPageHeight:function(){return(document.documentElement.scrollHeight)},
	getScrollTop:function(){return(document.documentElement.scrollTop)}
};

var mouse = {};

var initMousePos = function(ev){
	ev = ev || window.event;
	if(ev.pageX || ev.pageY){
		mouse = { x:ev.pageX, y:ev.pageY };
	}else{
		mouse = {
			x:ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
			y:ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
		};
	}
}

addEvent("mousemove", initMousePos, true );


var masker = {
	show:function(){
		var maskdiv = $("maskdiv");
		if(!maskdiv){
			maskdiv = document.createElement("div");
			with(maskdiv){
				id = "maskdiv";
				style.width = "100%";
				style.height = ui.getPageHeight() +"px";
				style.position = "absolute";
				style.zIndex = "1";
				style.top = "0";
				style.left = "0";
				style.backgroundColor = "black";
				style.opacity = "0.2";
				style.filter = "alpha(opacity=20)";
			}
			document.body.appendChild(maskdiv);
		}else{
			maskdiv.style.display = "";
		}
	},
	hide:function(){
		var maskdiv = $("maskdiv");
		if(maskdiv) maskdiv.style.display = "none";
	}
}


/* ======================== 装入Rel ========================= */


var initalizeRel = function(){
	var rels = getElementsByAttribute("rel");
	for( var i=0; i<rels.length; i++ ){
		if(rels[i].getAttribute("rel") == "dropmenu") initalizeDropmenu( rels[i] );
		if(rels[i].getAttribute("rel") == "marquee") initalizeMarquee( rels[i] );
		if(rels[i].getAttribute("rel") == "thumb") initalizeThumb( rels[i] );
		if(rels[i].getAttribute("rel") == "tips") initalizeTips( rels[i] );
		if(rels[i].getAttribute("rel") == "wind") initalizeWind( rels[i] );
	}
}

runOnReady(initalizeRel);

var initalizeDropmenu = function(elem){
	if(!hasAttribute(elem, "child")) return;
	elem.onmouseover = new Function("dropChild(this,\""+ elem.getAttribute("child") +"\")");
}

var initalizeMarquee = function(elem){
	elem.style.overflow = "hidden";
	scrollElem( elem, parseInt(elem.getAttribute("speed")), parseInt(elem.getAttribute("step")), elem.getAttribute("direct"), elem.getAttribute("rev"), elem.getAttribute("space") );
}

var initalizeThumb = function(elem){
	if( elem.nodeName.toLowerCase() != "img" ) return;
	thumbImg( elem, parseInt(elem.getAttribute("maxwidth")), parseInt(elem.getAttribute("maxheight")), elem.getAttribute("noresume"), elem.getAttribute("noscale") );
}

var initalizeTips = function(elem){
	if( !hasAttribute(elem, "msg") ) return;
	elem.onmouseover = new Function("displayTips(this, this.getAttribute(\"msg\"), this.getAttribute(\"tipstyle\"), this.getAttribute(\"tipclass\"))");
}

var initalizeWind = function(elem){
	if( !hasAttribute(elem, "origin") ) return;
	if( !hasAttribute(elem, "shift") ) return;
	scrollWind( elem, parseInt(elem.getAttribute("origin")), parseInt(elem.getAttribute("shift")), elem.getAttribute("side") );
}

/* Rel有关 */

var dropChild = function(p, childID){
	if(p.t) clearInterval(p.t);
	var c = $(childID);
	
	with(c){
		style.display = "";
		if( style.position != "absolute" ){
			style.position = "absolute";
			style.left = getLeft(p) +"px";
			style.top = (getTop(p) + p.offsetHeight) +"px";
		}
	}
	
	c.onmouseover = function(){ clearInterval(p.t) }
	
	p.onmouseout = c.onmouseout = function(){
		p.t = setTimeout(function(){
					 c.style.display = "none";
				 },50);
	}
}


var scrollElem = function(obj, speed, step, direct, rev, space){
	direct = (direct == "left")?true:false;
	rev = (rev == "rev")?true:false;
	
	if( space ){
		var spaceDiv = document.createElement("div");
		direct?spaceDiv.style.width:spaceDiv.style.height = space +"px";
		obj.appendChild(spaceDiv);
	}
	
	var currPos = 0,
		scrEnd = direct?obj.scrollWidth:obj.scrollHeight,
		offEnd = direct?obj.offsetWidth:obj.offsetHeight,
		scrPth = scrEnd,
		childSize = obj.childNodes.length,
		pauseScr = false;
	
	if( offEnd == scrEnd ){
		return;
	}
	
	if(rev){
		currPos = scrEnd;
		speed *= -1;
		scrPth = scrEnd * -1;
		scrEnd = 0;
	}
	
	for(var i=0; i<childSize; i++){
		obj.appendChild( obj.childNodes[i].cloneNode(true) );
	}
	
	obj.onmouseover = function(){ pauseScr = true };
	obj.onmouseout = function(){ pauseScr = false };
	
	setInterval(function(){
				if(pauseScr) return;
				currPos += speed;
				if(currPos == scrEnd) currPos -= scrPth;
				direct?obj.scrollLeft:obj.scrollTop = currPos;
			},step);
}

var thumbImg = function(obj, maxWidth, maxHeight, noResume, noScale){
	noResume = (noResume == "noresume")?true:false;
	noScale = (noScale == "noscale")?true:false;
	var img = document.createElement("img"),
		changed = false;
	if(!maxWidth) return;
	
	img.onload = function(){
		if( img.width >= maxWidth ){
			if(!noScale) img.height = img.height * maxWidth / img.width;
			img.width = maxWidth;
			changed = true;
		}
		if( maxHeight && img.height >= maxHeight ){
			if(!noScale) img.width = img.width * maxHeight / img.height;
			img.height = maxHeight;
			changed = true;
		}
		if(changed){
			with(obj){
				width = img.width;
				height = img.height;
				if(!noResume){
					style.cursor = "pointer";
					onclick = function(){window.open(src)};
				}
			}
		}
	}
	img.src = obj.src;
}

var displayTips = function(obj, msg, tipstyle, tipclass){
	var tipLayer = obj.tipLayer;
	if(obj.t){
		clearInterval(obj.t);
	}else{
		tipLayer = document.createElement("div");
		with(tipLayer){
			style.borderWidth = "1px";
			style.borderStyle = "solid";
			style.borderColor = "bisque";		//边框色
			style.backgroundColor = "white";	//背景色
			style.color = "midnightblue";		//前景色
			style.padding = "3px";
			style.position = "absolute";
			style.left = (mouse.x+3) +"px";
			style.top = (mouse.y+3) +"px";
			innerHTML = msg;
		}
		if(tipstyle) tipLayer.style.cssText += tipstyle;
		if(tipclass) tipLayer.className = tipclass;
		obj.tipLayer = tipLayer;
		document.body.appendChild(tipLayer);
	}
	
	obj.onmouseout = function(){ obj.t = setTimeout(function(){
			document.body.removeChild(tipLayer);
			obj.t = null;
		},700) };
}

var scrollWind = function(obj, origin, shif, side, timeout){
	if(!timeout){
		with(obj){
			style.position = "absolute";
			style.top = origin +"px";
			if(side == "right"){
				style.right = (ui.getPageWidth()/2 - shif) +"px";
			}else{
				style.left = (ui.getPageWidth()/2 - shif) +"px";
			}
		}
		timeout = 500;
	}
	setTimeout(function(){
			var currScrollTop = ui.getScrollTop() + origin,
				currWinderTop = parseInt(obj.style.top);
			if(currScrollTop != currWinderTop){
				timeout = 80;
				var newWinderTop = (currScrollTop - currWinderTop) * 0.3;
				obj.style.top = (currWinderTop + newWinderTop) +"px";
			}else{
				timeout = 500;
			}
			scrollWind(obj, origin, shif, side, timeout);
		},timeout);
}

function addBK(){//加入收藏
	var title = document.title;
	var url = "http://" +location.hostname;
	if(window.sidebar){ 
		window.sidebar.addPanel(title,url,""); 
	}else if( document.all ){
		window.external.AddFavorite(url,title);
	}else if( window.opera && window.print ){
		return true;
	}
}

function setHP(){//设为首页
	var url = "http://" +location.hostname;
	var obj = document.getElementsByTagName("a")[0];
	try{
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage(url);
	}catch(e){
		alert("浏览器不支持");
	}
}
