/**
 * 2009-11-29
 */
if (typeof giant === 'undefined')
    var giant = window.giant = {};
if (typeof giant.ui === 'undefined')
    giant.ui = {};
(function($) {
    var intervel = null;
    giant.ui.marquee = function(options) {
        this.opts = $.extend({}, giant.ui.marquee.defaults, options,true);
        this.$li =$("#"+this.opts.id).find("li");
        this.$inner = $("#"+this.opts.id).find(".ui-marqueeInner");
        this.total =this.$li.size();
        this.activeIndex = 0;
        this._init();
        this.isMoving = false;
    };
    giant.ui.marquee.prototype = {
         _init:function(){
            if(this.total>this.opts.number){    //当总数大于显示数时，自动播放，否则不自动播放
              $("#"+this.opts.id).find("ul:first").clone().appendTo(this.$inner);
              this.startAutoMarquee();  
            }
             this._eventBind();
         },
        _eventBind:function(){
            var $this = this;
            $("#btnLeft").bind("click",function(){
                   if(!$this.isMoving&& $this.total>$this.opts.number){
                       $this.clearAutoMarquee();
                        $this.scroll($this.opts.number*(-1));                       
                   }
            });
             $("#btnRight").bind("click",function(){
                   if(!$this.isMoving&& $this.total>$this.opts.number){
                        $this.clearAutoMarquee();
                        $this.scroll($this.opts.number);
                   }
            });  
        },
        scroll:function(num){
            this.activeIndex+=num;
            var $this = this;
            this.isMoving = true;
             $("#textfield").val(this.activeIndex);
            if(this.activeIndex>=this.total+5){
                var left = parseInt(this.$inner.css("left"))+ $("#"+this.opts.id).find("ul").width();
                this.activeIndex -= this.total;
               this.$inner.css("left",left+"px");
            }
             if(this.activeIndex<0) {
                  var left = parseInt(this.$inner.css("left"))- $("#"+this.opts.id).find("ul").width();
                  this.activeIndex += this.total;
                  this.$inner.css("left",left+"px");
            }
            var animateWidth = num>=0?"-="+(num*this.opts.unitWidth)+"px":"+="+(-1*num*this.opts.unitWidth)+"px";
            var speed = 400;
            if(Math.abs(num)>1){
                speed = speed*Math.abs(num)/2;
            }
            this.$inner.animate({left:animateWidth},speed,function(){
                $this.isMoving = false;
            });
        },
        startAutoMarquee:function(){
            var $this = this;
            intervel = window.setInterval(function(){
               $this.scroll(1);
            },3000);
        },
        clearAutoMarquee:function(){
           if(intervel){
               window.clearInterval(intervel);
           }
        }
    };
    giant.ui.marquee.defaults = {
       id:"marquee",
       unitWidth:96,
       number:6,
       isAuto:true
    };
})(jQuery);