// JavaScript Document
/*栏目播放*/
var scrollNo=1;
var SetTime=5*1000;
var SetTimer;
// clip：标签ID，box：版面ID，n：当前滑动项，m：滑动总数
function hideAllClips(clip,box,n,m) {
	for (i=1; i<(m+1); i++){
		var allClips=box.toString()+i;
		var clipNum=clip.toString()+i;
		//alert("隐藏"+allClips);
		//if(i=n) break;
		$(allClips).style.display="none";
		$(clipNum).className="topnavoff";
		}
	}

function clip_Switch(clip,box,n,m) {
	var curClip=box.toString()+n;
	var curClipNum=clip.toString()+n;
	//alert("当前"+curClip);
	hideAllClips(clip,box,n,m);
	$(curClip).style.display="block";
	$(curClipNum).className="topnavon";
	scrollNo=n;
	}

function fwdScroll(clip,box,m) {
	stopScroll();
	clip_Switch(clip,box,scrollNo,m);
	scrollNo+=1;
	if (scrollNo==m+1){
		scrollNo=1;
		}
	SetTimer=setTimeout("fwdScroll('"+clip+"','"+box+"',"+m+")",SetTime);
	}

function stopScroll() {
	clearTimeout(SetTimer);
	}

function $(id){return document.getElementById(id)}

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}

var Scroller = Class.create();
Scroller.prototype = {
  initialize: function(idScroller, idScrollMid, idUl, options) {
	if (!$(idScroller) || !$(idScrollMid) || !$(idUl)) return;
    var oScroll = this, oScroller = $(idScroller), oScrollMid = $(idScrollMid), ulHeight = $(idUl).offsetHeight;
    
    this.heightScroller = oScroller.offsetHeight;
	if (oScrollMid.offsetHeight < ulHeight) {
		oScrollMid.style.height = ulHeight + 'px';
	}
    this.heightList = oScrollMid.offsetHeight;
    
	if (this.heightList <= this.heightScroller) return;
    
	var oUl = $(idUl);
	var lis = oUl.getElementsByTagName('li');
	var lisNum = lis.length;
	for (var i=0; i<lisNum; i++) {
		var li = lis[i].cloneNode(true);
		oUl.appendChild(li);
	}
    
    this.oScroller = oScroller;    
    this.timer = null;
    
    this.SetOptions(options);
    
    oScrollMid.onmouseover = function() { oScroll.Stop(); };
    oScrollMid.onmouseout = function() { oScroll.Start(); };
    
    if(this.options.PauseStep <= 0 || this.options.PauseHeight <= 0) this.options.PauseStep = this.options.PauseHeight = 0;
    this.Pause = 0;
    
    this.Start();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
      Step:            1,//每次变化的px量
      Time:            10,//速度(越大越慢)
      PauseHeight:    0,//隔多高停一次
      PauseStep:    2000//停顿时间(PauseHeight大于0该参数才有效)
    };
    Object.extend(this.options, options || {});
  },
  //滚动
  Scroll: function() {
    var iScroll = this.oScroller.scrollTop, iHeight = this.heightList, time = this.options.Time, oScroll = this, iStep = this.options.Step;
    
	if(iScroll >= (iHeight * 2 - this.heightScroller)){ iScroll -= iHeight; }
    
    if(this.options.PauseHeight > 0){
        if(this.Pause >= this.options.PauseHeight){
            time = this.options.PauseStep;
            this.Pause = 0;
        } else {
            this.Pause += iStep;
            this.oScroller.scrollTop = iScroll + iStep;
        }
    } else { this.oScroller.scrollTop = iScroll + iStep; }
    
    this.timer = window.setTimeout(function(){ oScroll.Scroll(); }, time);
  },
  //开始
  Start: function() {
    this.Scroll();
  },
  //停止
  Stop: function() {
    clearTimeout(this.timer);
  }
};
scrollerArray = new Array();	//滚动对象数组

