﻿var ScrollAd1=new ScrollAd();

function ScrollAd()
{
var self=this;
var AdArea=document.getElementById("AdArea");
var ScrollAd=document.getElementById("ScrollAd");
var AdNo=document.getElementById("AdNo");
var AdNoObj=getChildNodes(AdNo);
var AdHeight=ScrollAd.style.height.replace('px',''); //高度
var AdSum=getChildNodes(AdArea).length; //广告数量
var MoveDist=AdHeight/4; //移动距离
var AddDist=MoveDist;
var ActiveNo=0;
var TargetNo=0;
var AdInterval=null;
var AdTout=null;
var AutoMoveAd=true;


AdInit();

function AdInit()
{
     
     for(i=0;i<AdNoObj.length;i++)
	 {
	      var AdNoWidth=AdNoObj[i].offsetWidth;
		  //alert(left);
	      AdNoObj[i].style.left=(AdArea.offsetWidth-(AdNoWidth+5)*AdSum-5)+(AdNoWidth*i+(i+1)*5)+"px";
	      AdNoObj[i].style.visibility="visible";
	      AdNoObj[i].onmouseover=function()
		  {
		         clearInterval(AdInterval);
				 clearTimeout(AdTout);
				 AutoMoveAd=false;	
		         TargetNo=Math.abs(this.innerHTML)-1;				 
			     setAdInterval();
		  }
		  
		  AdNoObj[i].onmouseout=function()
		  {		  
		      clearTimeout(AdTout);
			  clearInterval(AdInterval);	  
			  AutoMoveAd=true;
			  setAdInterval();
		      //setAdTimeout();
		  }
	 }
	 setAdInterval();
	 
}

function setAdTimeout()
{     	 
     //clearInterval(AdInterval);
     AdTout=setTimeout(function(){setAdInterval();},5000);
}

function setAdInterval()
{
    AdInterval=setInterval(function(){MoveToAdNo();},100);
}

function MoveToAdNo()
{
   var curTop=Math.abs(AdArea.style.top.replace('px',''));
   var TargetTop=TargetNo*AdHeight;    
   //alert(TargetTop);
   var Dist=Math.abs(TargetTop-curTop);   
   var AddDist=Dist/2;   
   
   if(AddDist<10)
   {
       AdArea.style.top=-TargetTop+"px";
	   clearInterval(AdInterval); 
	   ActiveNo=TargetNo;
	   setActiveAdNo();
	   setAdTimeout();
	   if(AutoMoveAd)
	   {         
          TargetNo=TargetNo+1;
	      if(TargetNo>=AdSum) TargetNo=0;	      
	   }
	   
   }
   else
   {
   
      if(curTop>TargetTop)
      {
          curTop=curTop-AddDist;
	      AdArea.style.top=-curTop+"px";
      }
      else
      {
          curTop=curTop+AddDist;
      }
	  AdArea.style.top=-curTop+"px"; 
   }       
}

function setActiveAdNo()
{
   for(i=0;i<AdNoObj.length;i++)
   {      
	  if(ActiveNo==i)
	  {
	      AdNoObj[i].className="AdNoActive";
	  }
	  else
	  {
	     AdNoObj[i].className="AdNo";
	  }
   }
}

/*获取子节点,兼容firefox */
function getChildNodes(Item)
{
     var ChildNodes=new Array();
	 var N=0;
     for(var i=0;i<Item.childNodes.length;i++)
	 {
	    if(Item.childNodes[i].nodeName !="#text")
		{
	        ChildNodes[N]=Item.childNodes[i];
		    N++;
		}
	 }	 
	 return ChildNodes;
}

}