/****************************
*   Version 1.0.0
*            功能：DIV 包含元素的之下而上滚动
*/
/*************
*****结构如下，即可实现定时滚动效果********
*<div id="">
*   <ul><li>...</li><li>...</li></ul>     
*</div>
*
*     //调用如下：
*      var slide = new ScollSlide({
*         id : 'news',
*	      speed : 40,//滚动速度
*		  duration : 3000,//停顿时间
*		  height : 150,//高度
*		  width : 390//宽度
*      });
*      slide.play();
*      
*/


function ScollSlide(config){

    if(!config||!config.id)
	    return;
		
    var self = this;
    var $ = this.$ = function(id){
	     return document.getElementById(id);
	};
	
	
	$(config.id).onmouseover=function() {
	    clearInterval(self.timer);
    }
    $(config.id).onmouseout=function(){   
	   self.timer = setInterval(self.active,config.speed);
	}

	
	var oHead = document.getElementsByTagName("head")[0];
    var oStyle = document.createElement("style");
    oStyle.type="text/css";
    oHead.appendChild(oStyle);
    var oCss = document.styleSheets[0];
    oCss.cssRules ? (oCss.insertRule("#"+config.id+"{overflow:hidden; width: "+config.width+"px;height: "+config.height+"px;}",oCss.cssRules.length )) : oCss.addRule("#"+config.id, "{overflow:hidden; width: "+config.width+"px;height: "+config.height+"px;}");
	
	var news01 = document.createElement("div");
	news01.id = config.id+"01";
	news01.innerHTML = $(config.id).innerHTML;
	$(config.id).innerHTML = "";
	$(config.id).appendChild(news01);
	
	var news01_hidden = document.createElement("div");
	news01_hidden.id = config.id+"01_hidden";
	$(config.id).appendChild(news01_hidden);
    news01_hidden.innerHTML = news01.innerHTML;
	
	
    this.active = function(){
	     if($(config.id+"01_hidden").offsetHeight-$(config.id).scrollTop<=0){
           $(config.id).scrollTop-=$(config.id+"01").offsetHeight;
		   clearInterval(self.timer);
           self.timer = setTimeout(self.temp,config.duration);
		}
        else{

           $(config.id).scrollTop++;
		   
           if($(config.id).scrollTop%config.scollHeight==0){

           clearInterval(self.timer);
           self.timer = setTimeout(self.temp,config.duration);

         }
       }
	}
	this.temp = function(){
	   self.play();
	}
	
	this.play = function(){
	    this.timer = setInterval(self.active,config.speed);
	}
	
	this.stop = function(){
	    //alert("停止播放");
		clearTimeout(self.timer);
	};
	
	this.config = config;
}
/*
function test(){

      var slide = new ScollSlide({
          id : 'news',
	      speed : 1,
		  duration : 5000,
		  height : 150,
		  width : 390
       });
       slide.play();
}*/
