/**
 * @author zhangyi1
 * 20090410 调整为允许多个实例 以及 自动滚动开关
 */
/*
 * Speed_1 速度(毫秒)
 * Space_1 每次移动(px)
 * PageWidth_1 翻页宽度px
 * interval_1 翻页间隔(毫秒)
 * fill_1      0
 * MoveLock_1  false
 * MoveWay_1   默认方向,向右
 * Comp_1      0
 * AutoPlayObj_1 null
 */
/** 
					<div class="topStartLTcot_OutKK" id="star.topCot.scrollCot">
						<div class="topStartLTcotKK">
							<div class="topStartLTcot" id="star.topCot"><!--置顶明星-->
								...
							</div>
						</div>
					</div>
			Speed_1 	  :  options["speed"] || 10,
			Space_1 	  :  options["step"]  || 20,
			PageWidth_1   :  options["width"] || 124*7,
			interval_1    :  options["timer"] || 7000,
			mode		  :  options["mode"] || "x",
.topStartLTcot{overflow:hidden; height:204px; float:left; display:inline}
.topStartLTcotKK{ width:324324px; height:100%;}
.topStartLTcot_OutKK{width:695px;overflow:hidden; height:204px;}

new XN.HScroll("star.topCot","","star.topCot.scrollCot",{"width":234*3, "autoScroll":false});
 */
try {XN} catch(ex) {XN={}};

XN.HScroll = function(_child,_oChild,_parent,_options) {
	this.initialize(_child,_oChild,_parent,_options);
};
XN.HScroll.prototype = {
	initialize:function(_child,_oChild,_parent,_options) {
		this._child = document.getElementById(_child);
		this._oChild = _oChild?document.getElementById(_oChild):null;
		this._parent = document.getElementById(_parent);
		_options = _options || {};										
		this.SetOptions(_options);	
		
		var me = this;
		if(!me._oChild) {			
			me._oChild = me._child.cloneNode(true);
			me._oChild.id = me._child.id+"_1";
			me._child.parentNode.appendChild(me._oChild);
		} else {			
			me._oChild.innerHTML = me._child.innerHTML;
		}
		
		me._parent.scrollLeft = me.dom.fill_1>=0?me.dom.fill_1:me._child.scrollWidth-Math.abs(me.dom.fill_1);
		me._parent.scrollTop = me.dom.fill_1>=0?me.dom.fill_1:me._child.scrollHeight-Math.abs(me.dom.fill_1);
		
	
		if (document.attachEvent) {
			me._parent.attachEvent("onmouseover",function parentOv() {
				if(me.dom.AutoPlayObj_1 != null) clearInterval(me.dom.AutoPlayObj_1);
			});
			me._parent.attachEvent("onmouseout",function parentOt() {
				me.AutoPlay_1();
			});
		} else {
			me._parent.addEventListener("mouseover",function parentOv() {
				if(me.dom.AutoPlayObj_1 != null) clearInterval(me.dom.AutoPlayObj_1);
			},false);
			me._parent.addEventListener("mouseout",function parentOt() {
				me.AutoPlay_1();
			},false);
		}
		
		if (this.dom.autoScroll) {
			this.AutoPlay_1();
		}
	},
	SetOptions:function(options) {
		this.dom = {
			//默认值
			Speed_1 	  :  options["speed"] || 10,
			Space_1 	  :  options["step"]  || 20,
			PageWidth_1   :  options["width"] || 124*7,
			interval_1    :  options["timer"] || 7000,
			mode		  :  options["mode"] || "x",
			fill_1  	  :  0,
			MoveLock_1    :  false,
			MoveTimeObj_1 :  null,
			MoveWay_1  	  :  "right",
			Comp_1	   	  :  0,
			AutoPlayObj_1 :  null,
			
			autoScroll    : options["autoScroll"] || false,
			scrollFn      : options["scrollFn"]
		};
	},
	AutoPlay_1:function() {
		if (!this.dom.autoScroll) return;
		
		var me = this;
		clearInterval(me.dom.AutoPlayObj_1);
		me.dom.AutoPlayObj_1=setInterval(function() {
			me.ISL_GoDown_1();
			me.ISL_StopDown_1();
		},me.dom.interval_1)
	},
	ISL_GoUp_1:function() {
		var me = this;
		if(me.dom.MoveLock_1)return;
		clearInterval(me.dom.AutoPlayObj_1);
		me.dom.MoveLock_1=true;
		me.dom.MoveWay_1=(me.dom.mode=="x"?"left":"up");
		me.dom.MoveTimeObj_1=setInterval(function() {
			me.ISL_ScrUp_1();
		},me.dom.Speed_1);
	},
	ISL_StopUp_1:function() {
		var me = this;
		if(me.dom.MoveWay_1 == "right"||me.dom.MoveWay_1 == "down"){return};
		clearInterval(me.dom.MoveTimeObj_1);
		
		if(me.dom.mode=="x"&&((me._parent.scrollLeft-me.dom.fill_1)%me.dom.PageWidth_1!=0)){
			me.dom.Comp_1=me.dom.fill_1-(me._parent.scrollLeft%me.dom.PageWidth_1);
			me.CompScr_1();
		}else if(me.dom.mode!="x"&&((me._parent.scrollTop-me.dom.fill_1)%me.dom.PageWidth_1!=0)){
			me.dom.Comp_1=me.dom.fill_1-(me._parent.scrollTop%me.dom.PageWidth_1);
			me.CompScr_1();
		}else{me.dom.MoveLock_1=false}
		me.AutoPlay_1();
	},
	ISL_ScrUp_1:function() {
		var me = this;
		if (me.dom.mode == "x") {
			if (me._parent.scrollLeft <= 0) {
				me._parent.scrollLeft = me._parent.scrollLeft + me._child.offsetWidth
			}
			me._parent.scrollLeft -= me.dom.Space_1;
		} else {
			if (me._parent.scrollTop <= 0) {
				me._parent.scrollTop = me._parent.scrollTop + me._child.offsetHeight
			}
			me._parent.scrollTop -= me.dom.Space_1;
		}
	},
	ISL_GoDown_1:function() {
		var me = this;
		clearInterval(me.dom.MoveTimeObj_1);						
		if(me.dom.MoveLock_1)return;
		clearInterval(me.dom.AutoPlayObj_1);
		me.dom.MoveLock_1=true;
		me.dom.MoveWay_1=(me.dom.mode=="x"?"right":"down");
		me.ISL_ScrDown_1();
		me.dom.MoveTimeObj_1=setInterval(function() {
			me.ISL_ScrDown_1();	
		},me.dom.Speed_1)
	},
	ISL_StopDown_1:function() {
		var me = this;
		if(me.dom.MoveWay_1 == "left" || me.dom.MoveWay_1 == "up"){return};
		clearInterval(me.dom.MoveTimeObj_1);
		if(me.dom.mode=="x"&&(me._parent.scrollLeft%me.dom.PageWidth_1-(me.dom.fill_1>=0?me.dom.fill_1:me.dom.fill_1+1)!=0)){
			me.dom.Comp_1=me.dom.PageWidth_1-me._parent.scrollLeft%me.dom.PageWidth_1+me.dom.fill_1;
			me.CompScr_1()
		}else if(me.dom.mode!="x"&&(me._parent.scrollTop%me.dom.PageWidth_1-(me.dom.fill_1>=0?me.dom.fill_1:me.dom.fill_1+1)!=0)){
			me.dom.Comp_1=me.dom.PageWidth_1-me._parent.scrollTop%me.dom.PageWidth_1+me.dom.fill_1;
			me.CompScr_1()
		}else{me.dom.MoveLock_1=false}
		me.AutoPlay_1();
		
	},
	ISL_ScrDown_1:function() {
		var me = this;
		if (me.dom.mode == "x") {
			if (me._parent.scrollLeft >= me._child.scrollWidth) {
				me._parent.scrollLeft = me._parent.scrollLeft - me._child.scrollWidth
			}
			me._parent.scrollLeft += me.dom.Space_1;
		} else {
			if (me._parent.scrollTop >= me._child.scrollHeight) {
				me._parent.scrollTop = me._parent.scrollTop - me._child.scrollHeight
			}
			me._parent.scrollTop += me.dom.Space_1;
		}
	},
	CompScr_1:function() {
		var me = this;
		if(me.dom.Comp_1==0){me.dom.MoveLock_1=false;return}
		var num,TempSpeed=me.dom.Speed_1,TempSpace=me.dom.Space_1;
		if(Math.abs(me.dom.Comp_1)<me.dom.PageWidth_1/2){
			TempSpace=Math.round(Math.abs(me.dom.Comp_1/me.dom.Space_1));
			if(TempSpace<1){TempSpace=1}
		}
		if(me.dom.Comp_1<0){
			if(me.dom.Comp_1<-TempSpace){
				me.dom.Comp_1+=TempSpace;
				num=TempSpace
			}else{
				num=-me.dom.Comp_1;
				me.dom.Comp_1=0;
			}
			if (me.dom.mode == "x") {
				me._parent.scrollLeft-=num;
			} else {
				me._parent.scrollTop-=num;
			}
			
			setTimeout(function() {
				me.CompScr_1();	
			},TempSpeed)
		}else{
			if(me.dom.Comp_1>TempSpace){
				me.dom.Comp_1-=TempSpace;num=TempSpace
			}else{
				num=me.dom.Comp_1;
				me.dom.Comp_1=0;
			}
			if (me.dom.mode == "x") {
				me._parent.scrollLeft+=num;
			}else{
				me._parent.scrollTop+=num;
			}
						
			setTimeout(function() {
				me.CompScr_1();	
			},TempSpeed)
		}
	}
}
