/*
使用方法：
将父节点id设置为MouseEventHandler，对应样式为 active
如有多个该效果， id 依次为MouseEventHandler1, MouseEventHandler2 ... MouseEventHandler9 ， 对应样式为 active1~9
*/
(function (){
var mehName = 'MouseEventHandler';
var activeClassName = 'active';

window.addListener = function(type, fn){
	if (this.addEventListener) this.addEventListener(type, fn, false);
	else this.attachEvent('on' + type, fn);
};

function $(id){
	if ('string' == typeof id)
		id = document.getElementById(id);
	if(id)	id.addListener = window.addListener;
	return id;
}

Function.prototype.bindNode=function(oNode){
   var foo=this,iNodeItem;

   //使用了全局数组__bindNodes，通过局部变量iNodeItem进行跨函数传值，如果直接传送oNode，也将造成闭包
   if(window.__bindNodes==null)
       __bindNodes=[];
   __bindNodes.push(oNode);
   iNodeItem=__bindNodes.length-1;
   oNode=null;
   return function(e){
       foo.call(__bindNodes[iNodeItem],e||event);
   }
}

function bindChildEvent(nodeList, index){
	for(var i=0, j=nodeList.length, node; node=nodeList[i],i<j; i++){
		if('#text' == node.nodeName)
			continue;
		window.__currentActive = window.__currentActive || new Array(10);
		var actCn = activeClassName + (index < 1 ? '' : index);
		if(node.className.indexOf(actCn) > -1)
			__currentActive[index] = node;
		$(node).addListener('mouseover', (function(){
			if(__currentActive[index]){
				__currentActive[index].className = __currentActive[index].className.replace(actCn, '');
			}
			__currentActive[index] = this;
			this.className += (this.className ? ' ' : '') + actCn;
		}).bindNode(node));
	}
}

window.addListener('load', function(){
	var meh = $(mehName);
	if(!meh)	return;
	bindChildEvent(meh.childNodes, 0);
	for(var i=1; i<10; i++){
		meh = $(mehName + i);
		if(!meh)	return;
		bindChildEvent(meh.childNodes, i);
	}
});
})();
