﻿ /**
  * @author [cnhubei]yuyangzhi
  * @version v1.1
  * @Date:2008-11-20
  */
function getElement(_id){
      return (document.getElementById) ? document.getElementById(_id): document.all[_id];
    }
	
    var startScroll={};
    startScroll.scrollObj={};//滚动区
    startScroll.scrollInit=function(){
        for( var i = 0; i < arguments.length; i++ ){

            startScroll.scrollObj[arguments[i]]=getElement(arguments[i]);

            if(startScroll.scrollObj[arguments[i]]==null) continue;

            /*************************************field******************************************************/
            startScroll.scrollObj[arguments[i]]._scrolldelay = Math.max(parseFloat(startScroll.scrollObj[arguments[i]].getAttribute("delay") || 0) * 1000,0);//间隔
            
            //startScroll.scrollObj[arguments[i]]._scrollspeed = Math.max(parseInt(startScroll.scrollObj[arguments[i]].speed || 1),1);//速度

            startScroll.scrollObj[arguments[i]]._orienStr = (startScroll.scrollObj[arguments[i]].getAttribute("orien") || "auto").toUpperCase();//方向
            
            startScroll.scrollObj[arguments[i]]._inTimer = null;                                //滚入定时器

            startScroll.scrollObj[arguments[i]]._outTimer = null;                               //滚出定时器

            startScroll.scrollObj[arguments[i]]._hover = false;                                 //鼠标悬停状态

            startScroll.scrollObj[arguments[i]].scrDivs = new Array();                          //滚动区里所有滚动项

            startScroll.scrollObj[arguments[i]]._nextItemIndex = 0;                             //下一个准备活动的item

            startScroll.scrollObj[arguments[i]]._activeItem;                                    //当前活动的item(未启用)

            startScroll.scrollObj[arguments[i]]._statue = 0;                                  //滚动项状态,0:停止状态,1:滚动状态

			startScroll.scrollObj[arguments[i]]._percent = 10;									//总百分比 不要小于10

			startScroll.scrollObj[arguments[i]]._stepLength = 2 ;								//移动百分比步长,不能为0

			startScroll.scrollObj[arguments[i]]._moveSpeedDelay = 100;							//移动速度时间间隔

			//startScroll.scrollObj[arguments[i]]._diagonalL = Math.sqrt(Math.pow(startScroll.scrollObj[arguments[i]].offsetWidth , 2) 
			//													+ Math.pow(startScroll.scrollObj[arguments[i]].offsetHeight , 2));			// 对角线长度

			
            /*************************************function******************************************************/
            startScroll.scrollObj[arguments[i]].onmouseover = function(){
                //alert("_hover="+this._hover+",_nextItemIndex="+this._nextItemIndex+",_statue="+this._statue+",c_index="+this.scrDivs[this._nextItemIndex]._index);
                this._hover=true;
                

            }
            startScroll.scrollObj[arguments[i]].onmouseout=function(){

                this._hover=false;

            }
            startScroll.scrollObj[arguments[i]].getNextItemIndex=function(offset){
                return (this._nextItemIndex + offset) % this.scrDivs.length ;
            }
            //调整位置为下次滚动做准备
            startScroll.scrollObj[arguments[i]].setOrien = function(){
                if(this._orienStr=="UP"){

                    this._orientation=1;

                }else if(this._orienStr == "RIGHT"){

                    this._orientation=2;

                }else if(this._orienStr == "DOWN"){

                    this._orientation=3;

                }else if(this._orienStr == "LEFT"){

                    this._orientation=4;

				}else if(this._orienStr == "LU"){

                    this._orientation=5;

				}else if(this._orienStr == "RU"){

                    this._orientation=6;

				}else if(this._orienStr == "RD"){

                    this._orientation=7;

				}else if(this._orienStr == "LD"){

                    this._orientation=8;

				}else if(this._orienStr == "AUTO1"){//上下左右随机

                    this._orientation=Math.floor(Math.random()*4) + 1;

				}else if(this._orienStr == "AUTO2"){//对角随机

                    this._orientation=Math.floor(Math.random()*4) + 5;

                }else if(this._orienStr == "AUTO"){

                    this._orientation=Math.floor(Math.random()*8) + 1;

                }else{//全方向随机

                    this._orientation=Math.floor(Math.random()*8) + 1;

                }
            }


            
           
            var divs = startScroll.scrollObj[arguments[i]].getElementsByTagName("div");         //滚动区里的所有div

            var divIndex=0;

            for(var di=0;di<divs.length;di++){
                if(divs[di].className=="scrollItem"){//判断是否是需要的div
                    /*************************************field******************************************************/
                    divs[di]._index = divIndex;
                    //startScroll.scrollObj[arguments[i]].scrDivs._parent=startScroll.scrollObj[arguments[i]];
                    divs[di]._scrolldelay = divs[di].getAttribute("delay") ? (parseFloat(divs[di].getAttribute("delay")) * 1000) : startScroll.scrollObj[arguments[i]]._scrolldelay;
                    divs[di]._top = 0;

                    divs[di]._left = 0;
                    //divs[di].style.top=startScroll.scrollObj[arguments[i]].offsetHeight*2 + "px";//停放位置
                    
                    divs[di].style.visibility = "visible";

                    divs[di]._percentLocal = startScroll.scrollObj[arguments[i]]._percent;//位置百分比;
                    
                    if(divIndex==0){

                        startScroll.scrollObj[arguments[i]]._activeItem = divs[di];               //把第一项设为当前活动项
                        divs[di]._percentLocal = -2;
                        divs[di].style.left = 0;
                        divs[di].style.top = 0;
                       

                    }
                    
                    //divs[di]._diagonalL = Math.sqrt(Math.pow(divs[di].offsetWidth , 2) 
					//							+ Math.pow(divs[di].offsetHeight , 2));			// 对角线长度
					
                    /*************************************function******************************************************/
                    
                    divs[di].setLocal = function(){
                        
                        switch(this.parentNode._orientation){
                            //up
                            case 1 : this.style.left=0;

                                     this.style.top = this.parentNode.offsetHeight + "px" ;

                                     break;
                            //right
                            case 2 : this.style.left = -this.offsetWidth + "px" ;

                                     this.style.top = 0 ;

                                     break;
                            //down
                            case 3 : this.style.left = 0;

                                     this.style.top = -this.offsetHeight + "px";

                                     break;
                            //left
                            case 4 : this.style.left = this.parentNode.offsetWidth + "px" ;

                                     this.style.top = 0;

									 break;
							//left up
							case 5 : this.style.left = this.parentNode.offsetWidth + "px" ;

                                     this.style.top = this.parentNode.offsetHeight + "px" ;

									 break;

							//right up
							case 6 : this.style.left = -this.offsetWidth + "px" ;

                                     this.style.top = this.parentNode.offsetHeight + "px" ;

									 break;
							//right down
                            case 7 : this.style.left = -this.offsetWidth + "px" ;

                                     this.style.top =  -this.offsetHeight + "px";

									 break;


							//left down
                            case 8 : this.style.left = this.parentNode.offsetWidth + "px" ;

                                     this.style.top = -this.offsetHeight + "px";
                        }
                    }
                    
                    //alert(startScroll.scrollObj[arguments[i]]._orientation);

                    divs[di].scrollIN=function(){

                        
                        if(!(this.parentNode._hover && this.parentNode._statue==0)){            //如果鼠标停在上面并且滚动处停止状态时
                            
                            var percent = this.parentNode._percent;
                            
                            //this.parentNode._activeItem=this;
                            if(this._percentLocal >= 0){

                                this.parentNode._statue=1;

								

                                var orien = this.parentNode._orientation;

                                if(orien==1){//up

                                    this.style.top = (this.parentNode.offsetHeight * (this._percentLocal/percent)) + "px";

                                }else if(orien == 2){//right

                                    this.style.left = (this.parentNode.offsetWidth * ((percent -this._percentLocal)/percent)) - this.offsetWidth + "px";
                                
                                }else if(orien == 3){//down

                                    this.style.top = ((this.parentNode.offsetHeight) * ((percent - this._percentLocal)/percent)) - this.offsetHeight + "px";
                                
                                }else if(orien == 4){//left

                                    this.style.left = (this.parentNode.offsetWidth * (this._percentLocal/percent)) + "px";

                                }else if(orien == 5){

									this.style.top = (this.parentNode.offsetHeight * (this._percentLocal/percent)) + "px";

									this.style.left = (this.parentNode.offsetWidth * (this._percentLocal/percent)) + "px";
									
								}else if(orien == 6){

									this.style.top = (this.parentNode.offsetHeight * (this._percentLocal/percent)) + "px";

									this.style.left = (this.parentNode.offsetWidth * ((percent -this._percentLocal)/percent)) - this.offsetWidth + "px";

								}else if(orien == 7){

									this.style.left = (this.parentNode.offsetWidth * ((percent -this._percentLocal)/percent)) - this.offsetWidth + "px";

									this.style.top = ((this.parentNode.offsetHeight) * ((percent - this._percentLocal)/percent)) - this.offsetHeight + "px";


								}else if(orien == 8){

									this.style.top = ((this.parentNode.offsetHeight) * ((percent - this._percentLocal)/percent)) - this.offsetHeight + "px";

									this.style.left = (this.parentNode.offsetWidth * (this._percentLocal/percent)) + "px";

								}

                                this._percentLocal -=  this.parentNode._stepLength;
                            }
                        }
                      
                      
                      //alert(this._percentLocal + "," + this.parentNode._stepLength)
                        if(this._percentLocal > - this.parentNode._stepLength){
                            
                            this.parentNode._inTimer=window.setTimeout("startScroll.scrollObj['"+this.parentNode.id+"'].scrDivs["+this._index+"].scrollIN()",this.parentNode._moveSpeedDelay);  
                        
                        }else{
                                
                                this._percentLocal=0;

                                window.clearTimeout(this.parentNode._inTimer);

                                this.parentNode._statue=0;//停止状态

                                this._top=parseInt(this.style.top) || 0;

                                this._left=parseInt(this.style.left) || 0;

                                
                                this.parentNode._nextItemIndex = this.parentNode.getNextItemIndex(1);

                                this.parentNode.setOrien();

                                this.parentNode.scrDivs[this.parentNode._nextItemIndex].setLocal();

                                if(this.parentNode._nextItemIndex != this._index){

                                    window.setTimeout("startScroll.scrollObj['"+this.parentNode.id+"'].scrDivs["+this._index+"].scrollOUT()",this._scrolldelay);
                                    
                                    window.setTimeout("startScroll.scrollObj['"+this.parentNode.id+"'].scrDivs["+this.parentNode._nextItemIndex+"].scrollIN()",this._scrolldelay);    
                                
                                }

                        }
                        
                  }

                  divs[di].scrollOUT=function(){

                    
                    var percent = this.parentNode._percent;
                    if(!(this.parentNode._hover && this.parentNode._statue==0)){
                    
                        this.parentNode._statue=1;
						
						

                        var orien = this.parentNode._orientation;

                        if(this._percentLocal <= percent){

                            if(orien==1){//up

                                this.style.top=( -(this._percentLocal / percent * (this.offsetHeight + this._top)) -6) + "px";

                            }else if(orien == 2){//right

                                this.style.left = this._left + ( (this._percentLocal / percent * (this.parentNode.offsetWidth - this._left)) + 6) + "px";
                            
                            }else if(orien == 3){//down
                                
                                this.style.top= this._top +  ( (this._percentLocal / percent * (this.parentNode.offsetHeight - this._top)) + 6) + "px";
                            
                            }else if(orien == 4){//left
                                
                                this.style.left = ( -(this._percentLocal / percent * (this.offsetWidth + this._left)) -6) + "px";
                            
                            }else if(orien == 5){

								this.style.top=( -(this._percentLocal / percent * (this.offsetHeight + this._top)) -6) + "px";

								this.style.left = ( -(this._percentLocal / percent * (this.offsetWidth + this._left)) -6) + "px";

							}else if(orien == 6){

								this.style.top=( -(this._percentLocal / percent * (this.offsetHeight + this._top)) -6) + "px";

								this.style.left = this._left + ( (this._percentLocal / percent * (this.parentNode.offsetWidth - this._left)) + 6) + "px";

							}else if(orien == 7){

								this.style.left = this._left + ( (this._percentLocal / percent * (this.parentNode.offsetWidth - this._left)) + 6) + "px";

								this.style.top= this._top +  ( (this._percentLocal / percent * (this.parentNode.offsetHeight - this._top)) + 6) + "px";

							}else if(orien == 8){

								this.style.top= this._top +  ( (this._percentLocal / percent * (this.parentNode.offsetHeight - this._top)) + 6) + "px";

								this.style.left = ( -(this._percentLocal / percent * (this.offsetWidth + this._left)) -6) + "px";

							}

                            this._percentLocal+= this.parentNode._stepLength;
                        }
                    }
                        
                        
                        if(this._percentLocal < (percent + this.parentNode._stepLength)){
                            
                            this.parentNode._outTimer=window.setTimeout("startScroll.scrollObj['"+this.parentNode.id+"'].scrDivs["+this._index+"].scrollOUT()",this.parentNode._moveSpeedDelay);    
                        
                        }else{
                            
                            this._percentLocal=percent;

                            window.clearTimeout(this.parentNode._outTimer);
                        }
                    }
                    startScroll.scrollObj[arguments[i]].scrDivs[divIndex++]=divs[di];

                    //alert(startScroll.scrollObj[arguments[i]]._orientation==null)
                    
                    if(startScroll.scrollObj[arguments[i]]._orientation==null || startScroll.scrollObj[arguments[i]]._orientation==0){
                       
                        divs[di].parentNode.setOrien();
                    
                    }
                    if(di == 0){

                    }else{
                        divs[di].setLocal();
                    }
                    
                }
            }
            
            //run
            if(startScroll.scrollObj[arguments[i]].scrDivs[0] && startScroll.scrollObj[arguments[i]].scrDivs.length > 1){//判断里面的滚动项是否为空
                startScroll.scrollObj[arguments[i]].scrDivs[0].scrollIN();
            }
            
        }
    }
