/************************************************
*   mooquee v.01                                 *
*   Http: WwW.developer.ps/moo/mooquee          *
*   Dirar Abu Kteish dirar@zanstudio.com        *
/***********************************************/

var mooquee = new Class({
    initialize: function(element, options) {
		this.setOptions({
			marHeight: 12,
			marWidth: 400, //550 or 100%...
			marSpacing: 10,
			speed: 10,
			direction: 'left',
			marLeft: 0,
			pauseOnOver: true
	    }, options);
	    this.timer = null;
	    this.textElement = null;
	    this.mooqueeElement = element;	    	    
	    this.constructMooquee();
	},
	constructMooquee: function() {
		var content = this.mooqueeElement;
	    
		this.textElement = new Element('div',{
		    'class' : 'mooquee-text'
		    ,'id' : 'mooquee-text'
		}).setHTML(content.innerHTML);
		content.setHTML('');
		
		
		var buttons = new Element('div').addClass('buttons').injectInside(content);
		var btnStop = new Element('a').addClass('stop').addEvent(
		'click',
		function () {/* stop animacion*/
		this.clearTimer();
		}.bind(this)).injectInside(buttons);
			    
		var btnPlay = new Element('a').addClass('play').addEvent(
		'click',
		function () {/* play animacion*/
		this.timer = this.startMooquee.delay(this.options.speed, this);
		}.bind(this)).injectInside(buttons);
		
		if (BrowserDetect.browser == "Explorer") 
        {
            this.options.marLeft = 0;
            this.options.marWidth = this.options.marWidth - 2; 
		}
		var divElement = new Element('div',{
		    'class' : 'mooquee'
		    ,'id' : 'mooquee1'
		}).setStyles({
		    'width' : this.options.marWidth
		    ,'height' : this.options.marHeight
		    ,'margin-left': this.options.marLeft
		}).injectInside(content);
		
		
		this.textElement.injectInside(divElement);
		this.textElement = $('mooquee-text');
		
		(this.options.direction == 'left') ?  this.textElement.setStyle('left', ( -1 * this.textElement.getCoordinates().width.toInt())) : this.textElement.setStyle('left', el.getCoordinates().width.toInt());
		if(this.options.pauseOnOver){this.addMouseEvents();}
		//start marquee
		this.timer = this.startMooquee.delay(this.options.speed, this);
		content.setStyle('display','block');
	},
	addMouseEvents : function(){
	    this.textElement.addEvents({
	        'mouseenter' : function(me){
	            this.clearTimer();
	        }.bind(this),
	        'mouseleave' : function(me){
	            this.timer = this.startMooquee.delay(this.options.speed, this);
	        }.bind(this)
	    });
	},
    startMooquee: function(){
        var pos = this.textElement.getStyle('left').toInt();
        this.textElement.setStyle('left', ( pos + ((this.options.direction == 'left') ? -1 : 1)) + 'px');
        this.checkEnd(pos);
        this.timer = this.startMooquee.delay(this.options.speed, this);        
    },
    resumeMooquee: function(){
        this.stopMooquee();
        if(this.options.pauseOnOver){this.addMouseEvents();}
        this.timer = this.startMooquee.delay(this.options.speed, this);        
    },
    stopMooquee: function(){
        this.clearTimer();        
        this.textElement.removeEvents();        
    },
    clearTimer: function(){
        $clear(this.timer);
    },
    checkEnd: function(pos){
        if(this.options.direction == 'left'){
            if(pos < -1 * (this.textElement.getCoordinates().width.toInt())){
                this.textElement.setStyle('left', this.mooqueeElement.getCoordinates().width);
            }
        }
        else{
            if(pos > this.mooqueeElement.getCoordinates().width.toInt()){
                this.textElement.setStyle('left', -1 * (this.textElement.getCoordinates().width.toInt()) );                
            }
        }        
    },
    setDirection: function(dir){
        this.options.direction = dir;
    }
});
mooquee.implement(new Options);