/************************************************
*   mooquee v.1.1                               *
*   Http: WwW.developer.ps/moo/mooquee          *
*   Dirar Abu Kteish dirar@zanstudio.com        *
*   2008-10-20                                  *
*************************************************
*   Extend By www.Sod.hu                        *
*   new directions: top, bottom                 *
*   2008-04-30                                  *
/************************************************
*   v.1.1 Simplyfied by Arian Stolwijk          *
*   Deleted directions: top, bottom             *
*   2009-01-28                                  *
/***********************************************/

var Mooquee = new Class({
    
	Implements: Options,
	
	initialize: function(element, options) {
		this.setOptions({
			marHeight: element.getCoordinates().height,
			marWidth: element.getCoordinates().width,
			steps: 1,
			speed: 15,
			direction: 'left',
			pauseOnOver: true
	    }, options);
	    this.timer = null;
	    this.textElement = null;
	    this.mooqueeElement = element;
	    this.constructMooquee();
	},
	constructMooquee: function() {
		var el = this.mooqueeElement;
		el.setStyles({
		    'width' : this.options.marWidth,
		    'height' : this.options.marHeight,
			'position': 'absolute',
			'overflow': 'hidden',
			'white-space': 'nowrap'	    
		});
        this.textElement = new Element('div',{
		    'styles': {
				'position': 'absolute'
			}
		}).set('html', el.innerHTML);
		el.set('html', '');//clear mooqueeElement inner html
		this.textElement.inject(el);
        if(!this.setStartPos()){return;}
        if(this.options.pauseOnOver){this.addMouseEvents();}
		//start marquee
		this.timer = this.startMooquee.delay(this.options.speed, this);
	},
	setStartPos: function(){
		if( this.options.direction == 'left' )
            this.textElement.setStyle('left', ( -1 * this.textElement.getCoordinates().width.toInt()));
        else if( this.options.direction == 'right' )
            this.textElement.setStyle( 'left', this.options.marWidth );
        else{
            alert( 'direction config error: ' + this.options.direction );
            return false;
        }
        return true;
	},
	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(){
		if(this.options.direction == 'left' || this.options.direction == 'right')
            var pos = this.textElement.getStyle('left').toInt();
		if(this.options.direction == 'left'){
            this.textElement.setStyle( 'left', ( pos + -1 ) + 'px' );
        }else if(this.options.direction == 'right')
            this.textElement.setStyle( 'left', ( pos + 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.options.marWidth);
        } else if(this.options.direction == 'right'){
            if(pos > this.options.marWidth.toInt())
                this.textElement.setStyle('left', -1 * (this.textElement.getCoordinates().width.toInt()) );
        }
    },
    setDirection: function(dir){
        this.options.direction = dir;
        this.setStartPos();
    }
});
Element.implement({
	mooquee: function(options){
		this.store('mooquee',new Mooquee(this,options));
		return this;
	}
});

