/*
	131 ScrollClass
	Creator: MxD
	http://www.131.com
	Updated 2009-12-23.
*/

//兼容性补丁
if(!window.ActiveXObject){
	window.HTMLElement.prototype.__defineGetter__("children",function(){
		var c=[];
		for(var i=0,cs=this.childNodes;i<cs.length;i++){
			if(cs[i].nodeType==1)
				c.push(cs[i]);}
		return c;});
	window.HTMLElement.prototype.__defineSetter__("innerText",function(s){
		return this.textContent=s;});
	window.HTMLElement.prototype.__defineGetter__("innerText",function(){
		return this.textContent;});
}

var $=function(e){
	var o=typeof(e)=="string"?document.getElementById(e):e;
	compatibleWrap(o);
	return o;
}

var compatibleWrap=function(o){
	o.addClass=function(name){
		if(!this.className)this.className=name;
		else if(this.hasClass(name))return;
		this.className+=" "+name;
	};

	o.hasClass=function(name){
		return new RegExp("\\b"+name+"\\b").test(this.className);
	};

	o.removeClass=function(name){
		if(!this.className)return;
		this.className=this.className.replace(new RegExp("\\b"+name+"\\b","ig"),"").replace(/(^ +| +$)/ig,"");
	};

	return o;
}

var setOpacity=function(o,num){
	/*@cc_on @*/
	/*@
	o.style.filter=num?"alpha(opacity="+num+")":"";
	//alert(num?"alpha(opacity="+num+")":"")
	return;
	@*/
	o.style.opacity=num?num*0.01:"";
		//	filter:alpha(opacity=50);
		//	-moz-opacity:0.5;
		//	-kHTML-opacity: 0.5;
		//	opacity: 0.5;
}

//幻灯基类
var Scroll=function(args){
	args=args||{};
	this.index=0;       //当前帧
	this.max=args.max||0;         //最大帧数
	this.pause=false;   //暂停 true 运行 false
	this.handle=null;   //定时器句柄
	this.mode=args.mode||1;     //模式 循环 1 最大后回退 0
	this.direct=args.direct==false?false:true;   //方向
	this.locking=false; //锁定
	this.interval=args.interval||3300; //间隔
	this.turnTo=null;   //跳转
	this.turnFrom=null; //跳转

	//尝试锁定
	//返回值 成功true 失败false
	this.lock=function(){
		if(this.locking)return false;
		return this.locking=true;
	}

	//释放锁
	this.releaseLock=function(){
		this.locking=false;
		this.turnTo=null;
		this.turnFrom=null;
	}
	
	//跳到下一帧
	this.next=function(){
		if(!this.lock())return;
		this.reverse();
		this.turn();
		this.setIndex(this.getNextIndex());
	}

	//跳到前一帧
	this.previous=function(){
		this.select(this.getPreviousIndex());
	}

	//跳到指定帧
	//参数 index=帧号
	this.select=function(index){
		if(index==this.index)return;
		if(!this.lock())return;
		this.turnTo=index;
		this.turnFrom=this.index;
		this.turn();
		this.setIndex(index);
	}

	//检查方向，如果到末端则倒转，模式0时有效
	this.reverse=function(){
		if(this.mode)return;
		if(this.direct&&this.index==this.max)this.direct=false;
		else if(!this.direct&&this.index==0)this.direct=true;
	}

	//设置帧号
	//参数 index=帧号
	this.setIndex=function(index){
		this.index=index;
	}

	//获取当前帧号
	this.getIndex=function(){
		return this.index;
	}

	//获取下一帧号
	this.getNextIndex=function(){
		if(this.turnTo!=null)return this.turnTo;
		var rt=this.index+(this.direct?1:-1);
		if(this.direct){
			if(this.mode && rt>this.max)return 0;
			if(rt>this.max)return rt-2;
		}else{
			if(this.mode && rt<0)return this.max;
			if(rt<0)return rt+2;
		}
		return rt;
	}

	//获取前一帧号
	this.getPreviousIndex=function(){
		if(this.turnFrom!=null)return this.turnFrom;
		var rt=this.index+(this.direct?-1:1);
		if(this.direct){
			if(this.mode && rt<0)return this.max;
			if(rt<0)return rt+2;
		}else{
			if(this.mode && rt>this.max)return 0;
			if(rt>this.max)return rt-2;
		}
		return rt;
	}

	//开始执行幻灯
	this.start=function(){
		var me=this;
		this.handle=setInterval(function(){me.exec();},this.interval);
	}

	//停止执行幻灯
	this.stop=function(){
		this.clear();
	}

	//清除定时器句柄
	this.clear=function(){
		clearInterval(this.handle);
	}

	//被定时器调用的方法
	this.exec=function(){
		if(this.pause)return;
		//if(this.lock)return;
		this.next();
	}

	//this.turn=null;

}

//供覆写,改变html的接口
Scroll.prototype.turn=function(){
	this.releaseLock();//必须提供此行，解除调用前由基类执行的锁定，否则无法进行下一步
}

//首页型幻灯
Scroll.prototype.rootInit=function(){
		var me=this;
		var eNav=$("scrollNav");
		this.max=eNav.children.length-1;
		this.cNav=eNav.children;
		for(var i=0;i<this.cNav.length;i++){
			this.cNav[i].idx=i;
			this.cNav[i].onmouseover=function(){me.pause=true;me.select(this.idx);}
			this.cNav[i].onmouseout=function(){me.pause=false;}
			compatibleWrap(this.cNav[i]);
		}
		var eTag=$("scrollTag");
		this.cTag=eTag.children;
		for(var i=0;i<this.cTag.length;i++){
			this.cTag[i].onmouseover=function(){me.pause=true;}
			this.cTag[i].onmouseout=function(){me.pause=false;}
			this.cTag[i].style.position="absolute";
			this.cTag[i].style.width="360px";
			compatibleWrap(this.cTag[i]);
			if(i==0)continue;
			this.cTag[i].removeClass("disNone");
			this.cTag[i].addClass("gNav-s");
			this.cTag[i].style.display="none";
		}
		this.turn=function(){
			clearTimeout(this.tHandle);
			var me=this;
			var cur=this.getIndex();
			var next=this.getNextIndex();
			this.cTag[cur].style.display="";
			this.cTag[cur].style.zIndex=501;
			this.cTag[cur].children[1].style.display="none";
			this.cTag[cur].children[2].style.display="none";
			for(var i=0;i<this.cTag.length;i++){
				setOpacity(this.cTag[i].getElementsByTagName("img")[0],"");
				if(i==cur)continue;
				this.cTag[i].children[1].style.display="";
				this.cTag[i].children[2].style.display="";
				this.cTag[i].style.zIndex=500;
				this.cTag[i].style.display="none";
			}
			this.cTag[next].style.display="";
			this.tCount=8;
			this.tMax=6;
			this.tDirect=true;
			this.tHandle=setInterval(function(){
				me.transition(cur,next)
					;},50);

			me.turnNav();
			me.releaseLock();
		}

		this.turnNav=function(){
			var cur=this.getIndex();
			var next=this.getNextIndex();
			this.cNav[cur].removeClass("hot");
			this.cNav[cur].addClass("nhot");
			this.cNav[next].removeClass("nhot");
			this.cNav[next].addClass("hot");
			if(cur>0)this.cNav[cur-1].removeClass("nobot");
			if(next>0)this.cNav[next-1].addClass("nobot");
		}

		this.transition=function(cur,next){
			if(this.tDirect&&this.tCount<=0){
				this.cTag[cur].style.display="none";
				setOpacity(this.cTag[cur].getElementsByTagName("img")[0],"");
				this.tDirect=false;
				this.tCount=0;
			}else if(!this.tDirect&&this.tCount>=0){
				this.tDirect=true;
				setOpacity(this.cTag[next].getElementsByTagName("img")[0],"");
				this.tCount=this.tMax;
			}
			else if(this.tDirect){
				setOpacity(this.cTag[cur].getElementsByTagName("img")[0],this.tCount*15);
				this.tCount--;
			}
			else{
				setOpacity(this.cTag[next].getElementsByTagName("img")[0],this.tCount*15);
				this.tCount++;
			}
		}
		/*@cc_on @*/
		/*@
		return;
		@*/
		$("scrollNav").parentNode.style.marginLeft="370px";
}

//例子，在首页上调用。代码加在幻灯的html块下方
//<script>
//	rootScroll=new Scroll({direct:false});
//	rootScroll.rootInit();
//	rootScroll.start();
//</script>

Scroll.prototype.rootInit2=function(){
		var me=this;
		var eNav=$("scrollNav");
		this.max=eNav.children.length-1;
		this.cNav=eNav.children;
		for(var i=0;i<this.cNav.length;i++){
			this.cNav[i].idx=i;
			this.cNav[i].onmouseover=function(){me.pause=true;me.select(this.idx);}
			this.cNav[i].onmouseout=function(){me.pause=false;}
			compatibleWrap(this.cNav[i]);
		}

		var bt=function(){
			
		
		}
		var bt1=document.getElementById("scrollBt2");
		bt1.onclick=function(){me.next();}
		var bt2=document.getElementById("scrollBt1");
		bt2.onclick=function(){me.previous();}
		bt1.onmouseover=bt2.onmouseover=function(){me.pause=true;}
		bt1.onmouseout=bt2.onmouseout=function(){me.pause=false;}
		var eTag=$("scrollTag");
		this.cTag=eTag.children;
		for(var i=0;i<this.cTag.length;i++){
			this.cTag[i].onmouseover=function(){me.pause=true;}
			this.cTag[i].onmouseout=function(){me.pause=false;}
			compatibleWrap(this.cTag[i]);
			if(i==0)continue;
			this.cTag[i].style.display="none";
		}
		this.turn=function(){
			clearTimeout(this.tHandle);
			var me=this;
			var cur=this.getIndex();
			var next=this.getNextIndex();
			this.cTag[cur].style.display="";
			this.cTag[cur].style.zIndex=501;
			this.cTag[cur].children[1].style.display="none";
			this.cTag[next].style.display="";
			this.cTag[next].style.zIndex=500;
			this.cTag[next].children[1].style.display="";
			for(var i=0;i<=this.max;i++){
				setOpacity(this.cTag[i].getElementsByTagName("img")[0],"");
				if(i==cur||i==next)continue;
				this.cTag[i].style.display="none";
			}
			me.turnNav(cur,next);
			this.tCount=8;
			this.tHandle=setInterval(function(){
				me.transition(cur,next)
					;},33);
			me.releaseLock();
		}
		this.turnNav=function(cur,next){
			this.cNav[cur].removeClass("curr");
			this.cNav[next].addClass("curr");
			var a=next-next%4,b=a+3;
			for(var i=0;i<=this.max;i++)
				this.cNav[i].style.display=i>=a&&i<=b?"":"none";
		
		}
		this.transition=function(cur,next){
			if(this.tCount<=0){
				this.cTag[cur].style.display="none";
				clearTimeout(this.tHandle);
				return;
			}
			setOpacity(this.cTag[cur].getElementsByTagName("img")[0],this.tCount*15);
			this.tCount--;
		}
		/*@cc_on @*/
		/*@
		return;
		@*/
}
