/**
 * 特定事件中添加执行Js,必须在body标签后，才能完成使命
 * 示例 _AddJsToEventForFront('window.onload',my_test1,true)
 * @param {Object} EvtId
 * @param {Object} ExecJs
 * @param {Object} BeforeInsert
 * @param {ExecJsParams} ...
 */
function _AddJsToEventForFront() {
   if(arguments.length<2) return;
   
   var EvtId=arguments[0];
   if(typeof(EvtId)!='string') return;
   var EvtHandler=eval(EvtId)/*_Eval(EvtId)*/;
   if(typeof(EvtHandler)!='function' && typeof(EvtHandler)!='object') return;
         
   var ExecJs=arguments[1];
   var BeforeInsert=true;
   var execParams=[];
   if(arguments.length>2)
   {
	  BeforeInsert=arguments[2];
	  if('function'==typeof(ExecJs)) {	//only ExecJs is fun, params is validate
	  	for(var i=3;i<arguments.length;i++) execParams[i-3]=arguments[i];
	  }
   }
	
   try {
      var NewEvt=function() {
		 if(BeforeInsert) {
			try {
			   if(typeof(ExecJs)=='function') ExecJs.apply(null,execParams);
			   else eval(ExecJs);
			}
			catch(ex1) 
			{
			}
		 }
		 if(EvtHandler!=null) {
		    try {
		      var onfun=eval(EvtHandler);
			  if(typeof(onfun)=='function') onfun.apply(null);
			}
			catch(ex2) 
			{
			}
		 }
		 if(!BeforeInsert) {
			try {
			   if(typeof(ExecJs)=='function') ExecJs.apply(null,execParams);
			   else eval(ExecJs);
			}
			catch(ex3) 
			{
			}
		 }
	  }
	  eval(EvtId+'='+NewEvt);
   }
   catch(ex) 
   {
   }
}
/**
 * 取控件位置
 * @param {Object} element 控件对象或ID
 */
function _GetLocation(element) {
	if (!element) 
		return {
			x: 0,
			y: 0
		};
	
	if('object'!=typeof(element)) {
		element=document.getElementById(element);
	}
	if(!element) 
		return {
			x: 0,
			y: 0
		};

	
    var clientRects = element.getClientRects();
    if (!clientRects || !clientRects.length) {
        return [0,0];
    }

    var w = element.ownerDocument.parentWindow;
    var offsetL = w.screenLeft - top.screenLeft - top.document.documentElement.scrollLeft + 2;
    var offsetT = w.screenTop - top.screenTop - top.document.documentElement.scrollTop + 2;

    var f = w.frameElement || null;
    if (f) {
        var fstyle = f.currentStyle;
        offsetL += (f.frameBorder || 1) * 2 +
                    (parseInt(fstyle.paddingLeft) || 0) +
                    (parseInt(fstyle.borderLeftWidth) || 0) -
                    element.ownerDocument.documentElement.scrollLeft;
        offsetT += (f.frameBorder || 1) * 2 +
                    (parseInt(fstyle.paddingTop) || 0) +
                    (parseInt(fstyle.borderTopWidth) || 0) -
                    element.ownerDocument.documentElement.scrollTop;
    }

    var clientRect = clientRects[0];

    return {
		x: clientRect.left - offsetL,
		y: clientRect.top - offsetT
	};
}
/**
 * 轮显层
 * 调用示例 
 * (new _LoopDisplayDiv('LoopDisplayDiv',2000,5,70,50,25,5,'dis1','btn1','lbtn1','dis2','btn2','lbtn2','dis3','btn3','lbtn3')).Start();
 * @param {Object} MasterDiv 主显示层
 * @param {Object} Delay 轮播延时
 * @param {Object} X	按纽左边位置
 * @param {Object} Y	按纽上边位置
 * @param {Object} W	按纽宽度
 * @param {Object} H	按纽高度
 * @param {Object} Space	按纽间距
 * @param {Object} DivObjs ...	   [轮播层、按纽层(正常)、按纽层(激活)]
 */
function _LoopDisplayDiv() {
 	this.MasterDiv=null;
 	if (arguments.length > 0) {
		if(arguments[0]) this.MasterDiv = document.getElementById(arguments[0]);
	}
	if(!this.MasterDiv) return;
	this.Delay=5000; //间隔时间
	if(arguments.length>1) {
		if('number'==typeof(arguments[1])) this.Delay=arguments[1];
	}
	this.X=5;  //按纽位置
	if(arguments.length>2) {
		if('number'==typeof(arguments[2])) this.X=arguments[2];
	}
	this.Y=5; //按纽位置
	if(arguments.length>3){
		if('number'==typeof(arguments[3])) this.Y=arguments[3];
	}
	this.W=25; //按纽宽度
	if(arguments.length>4) {
		if('number'==typeof(arguments[4])) this.W=arguments[4];
	}
	this.H=25; //按纽高度
	if(arguments.length>5){
		if('number'==typeof(arguments[5])) this.H=arguments[5];
	}
	this.Space=5; //按纽间距
	if(arguments.length>6){
		if('number'==arguments[6]) this.H=arguments[6];
	}

	this.DisplayDivs=[];
	this.LostBtns=[];	
	this.FocusBtns=[];
	for(var i=7;i<arguments.length;i+=3){
		if(arguments[i] && arguments[i+1] && arguments[i+2]) {
			this.DisplayDivs[this.DisplayDivs.length]=document.getElementById(arguments[i]);
			this.LostBtns[this.LostBtns.length]=document.getElementById(arguments[i+1]);
			this.FocusBtns[this.FocusBtns.length]=document.getElementById(arguments[i+2]);
		}
	}
	
	var me=this;

	this.Start=function() {
		if(this.DisplayDivs.length<2) return;

		me.Play();
		me.Go();
	}

	//if(this.DisplayDivs.length<2) return;
	this.Timer=null;  //定时器
	
	
	this.Idx=0; //播放索引
	this.Btns=[]; //按纽层	
	for(var i=0;i<this.DisplayDivs.length;i++){
		var btn=document.createElement('DIV');
		document.body.appendChild(btn);
		this.Btns[this.Btns.length]=btn;
		
		btn.style.position='absolute';
		btn.style.left=_GetLocation(this.MasterDiv).x+this.X+(this.W+this.Space)*i;
		btn.style.top=_GetLocation(this.MasterDiv).y+this.Y;
		btn.style.width=this.W;
		btn.style.height=this.H;
		btn.style.border='1 dashed';
		btn.style.cursor='hand';
		btn.Idx=i;
		btn.onmouseover=function() {
			me.Pause();
			me.Focus(this.Idx);
		}
		btn.onmouseout=function() {
			this.innerHTML=me.LostBtns[this.Idx].innerHTML;
			me.Go();
			me.Idx++;
		}
	}	
	this.Focus=function(CurIdx) {
		me.Idx=CurIdx;
		me.MasterDiv.innerHTML=me.DisplayDivs[CurIdx].innerHTML;
		me.Btns[CurIdx].innerHTML=me.FocusBtns[CurIdx].innerHTML;
		for(var i=0;i<me.Btns.length;i++) {
			if(i!=CurIdx) {
				me.Btns[i].innerHTML=me.LostBtns[i].innerHTML;	
			}
		}
	}
 	this.Play=function() {
		if(me.Idx>me.DisplayDivs.length-1) me.Idx=0;		
		me.Focus(me.Idx);
		
		me.Idx++;
	}
	this.Pause=function() {
		if(me.Timer) {
			window.clearInterval(me.Timer);
			me.Timer=null;
		}
	}
	this.Go=function() {
		if(!me.Timer) {
			me.Timer=window.setInterval(me.Play,me.Delay)	
		}
	}
}	
/**
 * 为指定层分屏显示
 * @param {Object} MasterDiv		主层
 * @param {Object} DisplayRowNum	显示的行数
 * @param {Object} RowHeight		行高
 * @param {Object} PauseTime		一屏完成后暂停时间
 * @param {Object} Speed			滚动速度
 */	
function _LoopMoreScreenForDiv() {
 	this.MasterDiv=null;
 	if (arguments.length > 0) {
		if(arguments[0]) this.MasterDiv = document.getElementById(arguments[0]);
	}
	if(!this.MasterDiv) return;
	this.DisplayRowNum=3; //显示的行数
	if(arguments.length>1) {
		if('number'==typeof(arguments[1])) this.DisplayRowNum=arguments[1];
	}
	this.RowHeight=20;  //行高
	if(arguments.length>2) {
		if('number'==typeof(arguments[2])) this.RowHeight=arguments[2];
	}
	this.PauseTime=5000; //一屏完成后暂停时间
	if(arguments.length>3){
		if('number'==typeof(arguments[3])) this.PauseTime=arguments[3];
	}
	this.Speed=20; //移动速度
	if(arguments.length>4){
		if('number'==typeof(arguments[4])) this.Speed=arguments[4];
	}
		
	//主显示区行高
	this.MasterDiv.style.height=this.DisplayRowNum*this.RowHeight;
	this.MasterDiv.style.display='block';
	//填充到显示行整数
	var j=0;
	while(0!=this.MasterDiv.children.length%this.DisplayRowNum) {
		if(this.MasterDiv.children.length<this.DisplayRowNum) 
			this.MasterDiv.appendChild(document.createElement('DIV'));
		else {
			var newDiv=document.createElement('DIV');
			this.MasterDiv.appendChild(newDiv);
			newDiv.innerHTML=this.MasterDiv.children[j].innerHTML;
			newDiv.style.cssText=this.MasterDiv.children[j].style.cssText;
			
			j++;
		}						
	}
	//再加一个显示块用于头行显示
	for(var i=0;i<this.DisplayRowNum;i++) {
		var newDiv=document.createElement('DIV');
		this.MasterDiv.appendChild(newDiv);
		newDiv.innerHTML=this.MasterDiv.children[i].innerHTML;
		newDiv.style.cssText=this.MasterDiv.children[i].style.cssText;
	}
	
	//行元素
	for(var i=0;i<this.MasterDiv.children.length;i++) {
		this.MasterDiv.children[i].style.height=this.RowHeight;
		this.MasterDiv.children[i].style.lineHeight=this.RowHeight+'px';
	}
	//主参数
	this.CurrentTop=0;
	this.Timer=null;
	this.DelayTimer=null;
	
	var me=this;

	//播放
	this.Scroll=function() {
		me.CurrentTop++;
		me.MasterDiv.scrollTop++;
		
		if(0==(me.CurrentTop%(me.DisplayRowNum*me.RowHeight))) {
			//完成一屏
			me.Pause();
			
			//暂停
			if(!me.DelayTimer) {
				me.DelayTimer=window.setTimeout(me.Delay,me.PauseTime);
			}
		}
	}
	//一屏完成后暂停
	this.Delay=function() {
		//是最后一屏完成
		if((me.MasterDiv.children.length-me.DisplayRowNum)*me.RowHeight==me.CurrentTop) {
			//alert('over');
			me.MasterDiv.scrollTop=0;
			me.CurrentTop=0;
		}		
		
		//下一屏开始
		me.Go();
		
		//清除暂停
		if(me.DelayTimer) {
			window.clearTimeout(me.DelayTimer);
			me.DelayTimer=null;
		}		
	}
		
	//暂停
	this.Pause=function() {
		if(me.Timer) {
			window.clearInterval(me.Timer);
			me.Timer=null;
		}
	}
	//开始
	this.Go=function() {
		if(!me.Timer) {
			me.Timer=window.setInterval(me.Scroll,me.Speed)	
		}
	}
}
/**
 * 为指定层包含内容跑马灯显示
 * @param {Object} MasterDiv		主层
 * @param {Object} IsHor			水平滚动
 * @param {Object} Speed			滚动速度
 */	
function _MarqueeForDiv() {
 	this.MasterDiv=null;
 	if (arguments.length > 0) {
		if(arguments[0]) this.MasterDiv = document.getElementById(arguments[0]);
	}
	if(!this.MasterDiv) return;
	this.IsHor=true;
	if(arguments.length>1) {
		this.IsHor=arguments[1];
	}
	this.Speed=20; 
	if(arguments.length>2){
		if('number'==typeof(arguments[2])) this.Speed=arguments[2];
	}
		
	//主参数
	this.CurrentPos=0;
	this.ScrollMax=0;
	this.Timer=null;
	this.Scrollable=false;
	
	var me=this;

	if(me.MasterDiv.children.length>0) {
		var html=me.MasterDiv.innerHTML;
		var tb=document.createElement('TABLE');
		tb.cellSpacing=0;
		tb.cellPadding=0;
		tb.border=0;

		if(me.IsHor) { //水平
			if(me.MasterDiv.offsetWidth<me.MasterDiv.scrollWidth) {
				me.MasterDiv.innerHTML='';
				this.Scrollable=true;
				me.MasterDiv.appendChild(tb);

				var myNewRow = tb.insertRow();
				var myFirstCell=myNewRow.insertCell();
				myFirstCell.innerHTML=html;

				me.ScrollMax=me.MasterDiv.scrollWidth;
				for(;;) {
					if(me.MasterDiv.scrollWidth-me.MasterDiv.offsetWidth>me.ScrollMax) break;

					var myNewCell=myNewRow.insertCell();
					myNewCell.innerHTML=html;
				}

				me.MasterDiv.onmouseover=function() {
				    me.Pause();
				}
				me.MasterDiv.onmouseout=function() {
				    me.Go();
				}
			}
		}
		else { //垂直

		}
	}

	//播放
	this.Scroll=function() {
		if(me.IsHor) { //水平
			me.CurrentPos++;
			me.MasterDiv.scrollLeft++;

			if(me.CurrentPos>me.ScrollMax) {
				me.MasterDiv.scrollLeft=0;
				me.CurrentPos=0;
			}
		}
		else { //垂直

		}
	}		
	//暂停
	this.Pause=function() {
		if(me.Scrollable && me.Timer) {
			window.clearInterval(me.Timer);
			me.Timer=null;
		}
	}
	//开始
	this.Go=function() {
		if(me.Scrollable && !me.Timer) {
			me.Timer=window.setInterval(me.Scroll,me.Speed)	
		}
	}
}
