﻿var Class={create:function(){
    return function(){
      this.init.apply(this,arguments);
    };
  }},Tween={effect:function(B,A,D,C){
    if((B/=C/2)<1){
      return D/2*B*B*B+A;
    }
    return D/2*((B-=2)*B*B+2)+A;
  }},aEvent=EventUtil.addEventHandler,rEvent=EventUtil.removeEventHandler,ie=Public.Browser.IE,productViewer=Class.create(),userAgent=navigator.userAgent.toLowerCase(),ieVersion=ie?/msie 8.0/.test(userAgent)?"8.0":/msie 7.0/.test(userAgent)?"7.0":/msie 5.5/.test(userAgent)?"5.5":"6.0":false;
var imgLoader=Class.create();
imgLoader.prototype={
	init:function(p){
		var t=this;
		t.objArr=p.objList;
		t.urlArr=p.urlList;
		t.icount=p.icount;
		t.reload=p.reload;
		t.pageSize=Math.floor(t.objArr.length/t.icount);
		t.loadedP=new Array(t.pageSize);
		},
	load:function(page){
		if(this.reload){
			this.loadFun(page);
			this.loadFun(page-1);
			this.loadFun(page+1);
			}else{
			this.loadFun(page);
			}
		},
	loadFun:function(page){
		var t=this;
		if(page<0 || page>t.pageSize)return;
		if(t.loadedP[page])return;
		t.loadedP[page]=true;
		for(var i=page*t.icount;i<(page+1)*t.icount && i<t.objArr.length;i++){
			t.objArr[i].src=t.urlArr[i];
			}
		}
	};
productViewer.prototype = {
    init: function(arg) {
        this.build(arg)
    },
    build: function(p) {
        var _this = this;
        var c = this.c = {
            effect: p.effect || Tween.effect,
            itemSize: p.itemSize,
            itemCount: parseInt(p.itemCount, 10) || 0,
            itemGroup: parseInt(p.itemGroup, 10) || 0,
            step: parseInt(p.step, 10) || 0,
            iDefault: p.iDefault,
            delayRun: parseInt(p.delayRun, 10) || 20,
            etype: p.etype || 'click',
            urlList: p.urlList || [],
            checkImg: p.checkImg || true,
            imgList: null,
            reloadImgNum: 0,
			auto:p.auto,
			urlList:p.urlList
        };
        if (p.scrolls) {
            var s = $(p.scrolls);
			this.wrap=s;
			this.c.scrWidth = c.itemSize[0] * c.step;
			this.rollMax=p.itemSize[0]*(p.itemCount-p.itemGroup);
            this.c.imgList = s.getElementsByTagName('img');
            if (this.c.checkImg) {
                this.c.iDefault = this.getIndex(s.getElementsByTagName('li'), $(this.c.iDefault)) + 1 || 0;
                this.c.reloadImgNum = this.c.iDefault;
            } else {
                this.c.reloadImgNum = this.c.iDefault = 0;
            }
            if (s && s.nodeType == 1) this.s = s; else return;
        } else return;
		if(p.urlList){
			this.imgLoader=new imgLoader({objList:s.getElementsByTagName('img'),urlList:p.urlList,icount:p.itemGroup,reload:true});
			this.imgLoader.load(Math.floor(s.scrollLeft/this.c.scrWidth));
			}
        if (p.btnPreId) {
            var pre = $(p.btnPreId);
            if (pre && pre.nodeType == 1) this.btnPre = pre; else return;
        } else return;

        if (p.btnNextId) {
            var next = $(p.btnNextId);
            if (next && next.nodeType == 1) this.btnNext = next; else return;
        } else return;


        this.thread = true;


        s.getElementsByTagName('ul')[0].style.width = _this.c.itemCount * _this.c.itemSize[0] + 'px';

        try {
            setTimeout(function() {
                if (c.iDefault > 0) {
                    s.scrollLeft = (c.iDefault - 3) * _this.c.itemSize[0];
                }
                _this.checkBtnIco();
            }, 100);
        } catch (e) { }

        this.maxScrWidth = c.itemSize[0] * (c.itemCount - c.itemGroup);
        aEvent(this.btnNext, c.etype, this.display(1));
        aEvent(this.btnPre, c.etype, this.display(3));
	if(this.c.auto){
		var H=this,A=this.c,J=this.c.auto;
		  var D=A.auto.step,F=A.auto.delay,C=A.auto.interval;
		  D=this.c.auto.step=parseInt(D,10)?D:J.step;
		  F=this.c.auto.delay=parseInt(F,10)?F:J.delay;
		  C=this.c.auto.interval=parseInt(C,10)?C:5000;
		  F=ie?parseInt(F*2/3):F;
		  H.start(D,F,C);
		  aEvent(this.wrap.parentNode,"mouseover",function(){
			if(H.autoId){
			  clearTimeout(H.autoId);
			}
		  });
		  aEvent(this.wrap.parentNode,"mousemove",function(){
			if(H.autoId){
			  clearTimeout(H.autoId);
			}
		  });
		  aEvent(this.wrap.parentNode,"mouseout",function(){
			H.start(D,F,C);
		  });
		}
    },

    display: function(direct) {
        var _this = this;
        return function() {
            if (!_this.thread) return;
            _this.toggle(direct);
        }
    },
	start:function(C,B,A){
    var E=this,D=1;
    E.autoId=setInterval(function(){
      if(E.wrap.scrollLeft>=E.rollMax){
        D=-1;
      }
      if(E.wrap.scrollLeft==0){
        D=1;
      }
      E.toggle(D);
    },A);
  },
    toggle: function(direct) {
        this.thread = false;
        var s = this.s, scrWidth = this.c.scrWidth, maxScr = this.maxScrWidth, _this = this, effect = this.c.effect;
        _this.c.reloadImgNum = direct == 1 ? _this.c.reloadImgNum + 5 : _this.c.reloadImgNum - 5;
        var b = s.scrollLeft, c = direct == 1 ? scrWidth : -scrWidth, d = this.c.delayRun, t = 0;
        if (b >= maxScr && c > 0) { this.thread = true; return; }
        (function Run() {
            s.scrollLeft = Math.ceil(effect(t, b, c, d));
            if (t < d) { t++; _this.clearRun = setTimeout(Run, 10) }
            else if (t == d) {
                _this.thread = true;
                _this.checkBtnIco();
				_this.imgLoader.load(Math.floor(s.scrollLeft/scrWidth));
            }
        })()
    },
    checkBtnIco: function() {
        var direct = 1;
        var s = this.s, scrWidth = this.c.scrWidth, maxScr = parseInt(this.s.getElementsByTagName('ul')[0].style.width), _this = this;
        var b = s.scrollLeft, c = direct == 1 ? scrWidth : -scrWidth;
        if (Math.abs(s.scrollLeft - maxScr + scrWidth) < 100 || maxScr < scrWidth) {
            _this.btnNext.className = 'rightNotActive';
        } else {
            _this.btnNext.className = 'right';
        }
        if (s.scrollLeft == 0) {
            _this.btnPre.className = 'leftNotActive';
        } else {
            _this.btnPre.className = 'left';
        }
    },
    getIndex: function(list, child) {
        for (i = 0; i < list.length; i++) {
            if (list[i] == child) return i;
        }
    }
}
function slideUrgent(B,H,F,C,J){
  var I=B,E=H;
  var F=F||1000,C=C||20,J=J||20;
  var D=null,K=false;
  var A=function(){
    D=setInterval(G,C);
  };
  var G=function(){
    if(K){
      return ;
    }
    I.scrollTop+=2;
    if(I.scrollTop%J==0){
      clearInterval(D);
      E.appendChild(I.getElementsByTagName("li")[0]);
      E.appendChild(I.getElementsByTagName("li")[0]);
      E.appendChild(I.getElementsByTagName("li")[0]);
      I.scrollTop=0;
      setTimeout(A,F);
    }
  };
  I.onmouseover=function(){
    K=true;
  };
  I.onmouseout=function(){
    K=false;
  };
  setTimeout(A,F);
}