﻿// Used in conjuction with mootools-1.2.3-core-nc.js
// http://mootools.net

var mytimer = null;
var play = 1;
var elButton;
                                            
var Ticker = new Class({
    setOptions: function(options) {
        this.options = Object.extend({
            speed: 1500,
            delay: 5000,
            direction: 'horizontal',
            onComplete: Class.empty,
            onStart: Class.empty
        }, options || {});
    }, //end of Object.extend
    initialize: function(el,options){
        this.setOptions(options);
        this.el = $(el);
        this.items = this.el.getElements('li');
    
        elButton = $('btnPause');
       
        
        var w = 0;
        var h = 0;
        if(this.options.direction.toLowerCase()=='horizontal') {
            h = this.el.getSize().y;
            this.items.each(function(li,index) {
                w += li.getSize().x;
            }); //end of function(li,index)
        } else {
            w = this.el.getSize().x;
            this.items.each(function(li,index) {
                h += li.getSize().y;
            }); //end of function(li,index)
        }
        this.el.setStyles({
            position: 'absolute',
            top: 0,
            left: 0,
            width: w,
            height: h
        }); //end of setStyles
        
        this.fxPrev = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
                var i = (this.current<0)?this.items.length-1:this.current;
                this.items[i].inject(this.el, 'top');
                
                this.el.setStyles({
                    left:0,
                    top:0
                }); //end of setStyles
                
            }.bind(this)
            }//onComplete:function()
        ); //Morph
        
        this.fxNext = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
                var i = (this.current==0)?this.items.length:this.current;
                this.items[i-1].injectInside(this.el);
                
                this.el.setStyles({
                    left:0,
                    top:0
                }); //end of setStyles
                
            }.bind(this)
            }//onComplete:function()
        ); //Morph
        
        this.fx = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
                var i = (this.current==0)?this.items.length:this.current;
                this.items[i-1].injectInside(this.el);
                
                this.el.setStyles({
                    left:0,
                    top:0
                }); //end of setStyles
              
                elButton.disabled = false;
               
            }.bind(this)
            ,onStart:function(){elButton.disabled = true;}
            }//onComplete:function()
        ); //Morph
        
        this.current = this.items.length;
        this.autonext();					                                    					                                    
    }, //end if initialize

    autonext: function() {
            if (play == 1){
   
                this.fxNext.cancel();
                this.fxNext.stopTimer();
                this.fxPrev.cancel();
                this.fxPrev.stopTimer();
                
                this.current++;
                if (this.current >= this.items.length) this.current = 0;
                var pos = this.items[this.current];   
               
               // if(typeof(pos) != "undefined")
               // {
                   this.fx.start({
                        top: -pos.offsetTop,
                        left:  -pos.offsetLeft
                    });
       
                    //mytimer = this.autonext.bind(this).delay(this.options.delay+this.options.speed);
                    this.timeoutId = this.autonext.bind(this).delay(this.options.delay+this.options.speed);
                   
                    
               // }
            }
            
    },  //end of autoNext
    
    next: function() {
            this.fx.cancel();
            this.fx.stopTimer();
            

            this.current++;
            if (this.current >= this.items.length) this.current = 0;
            var pos = this.items[this.current];
            
            if(typeof(pos) != "undefined")
            {
                this.fxNext.start({
                    top: -pos.offsetTop,
                    left:  -pos.offsetLeft
                });
            }
            
            
    },  //end of Next
    
    prev: function() {  
            this.fx.cancel();
            this.fx.stopTimer();
                         
            this.fx.options.delay=10000;
                                          
            this.current--;	
           
            if (this.current < 0) this.current = this.items.length-1;
            var i = this.current;
            var j = (this.current-1<0)?this.items.length-1:this.current-1;
         
            var pos = this.items[i];
            var pos1 = this.items[j];
            
            if(typeof(pos) != "undefined")
            {
                this.fxPrev.start({
                    top: pos.offsetTop,
                    left:  this.items[i].getSize().x //-(pos1.offsetLeft-pos.offsetLeft) //278
                });
            }
            
            
    }, //end of Prev
    
    pause: function() {
        //$clear(mytimer);
        //mytimer = null;
        //play = 0;
        
        /*
        this.fx.pause(); 
        $clear(this.timeoutId);
                                    
        var elStatus = $('newsStatus');
        elStatus.setStyle('visibility', "visible");
        */
    },
    
    pauseIt: function() {  
        //var elButton = $('btnPause');
       
        /*
        this.fxNext.cancel();
        this.fxNext.stopTimer();
        this.fxPrev.cancel();
        this.fxPrev.stopTimer();
        this.fx.cancel(); 
        this.fx.stopTimer(); 
        this.fx.complete();
        */    
        
        if(elButton.disabled == false){
            if(elButton.value == "pause"){
                
                elButton.value = "resume";
                this.fx.pause(); 
                $clear(this.timeoutId);
            }else if(elButton.value == "resume"){
                elButton.value = "pause";
                elButton.disabled = true;
                
                this.fx.resume(); 
                this.autonext();   
                
            }
        }
    },
    
    resume: function() {
        /*
        if (mytimer == null) {
            play = 1;
            this.autonext();
            var elStatus = $('newsStatus');
            elStatus.setStyle('visibility', "hidden");
        }
        */
        
      /*
        this.fx.resume(); 
        this.autonext();
        var elStatus = $('newsStatus');
        elStatus.setStyle('visibility', "hidden");
        */
    }


}); //end of Class

