/************************************************
*   mooquee v.1.0                               *
*   Http: WwW.developer.ps/moo/mooquee          *
*   Dirar Abu Kteish dirar@zanstudio.com        *
*************************************************
*   Extend By www.Sod.hu                        *
*   new directions: top, bottom                 *
*   2008-04-30                                  *
/***********************************************/

var mooquee = new Class({
    initialize: function(element, options) {
        this.setOptions({
            marHeight: 20,
            marWidth: 550,
            marSpacing: 10,
            speed: 10,
            direction: 'top',
            pauseOnOver: true
        }, options);
        this.timer = null;
        this.textElement = null;
        this.mooqueeElement = element;
        this.constructMooquee();
    },
    constructMooquee: function() {
        //var el = this.mooqueeElement;
        this.mooqueeElement.setStyles({
            'width' : this.options.marWidth
            ,'height' : this.options.marHeight
        });
        var textElementName = 'mooquee-text-' + this.mooqueeElement.id;
        this.textElement = new Element('div',{
            'class' : 'mooquee-text'
            ,'id' : textElementName
        }).set('html',this.mooqueeElement.innerHTML);
        this.mooqueeElement.set('html','');
        this.textElement.injectInside(this.mooqueeElement);

        this.textElement = $(textElementName);

        /* sod.hu Ext */
        if( this.options.direction == 'bottom' )
            this.textElement.setStyle('bottom', ( -1 * this.textElement.getCoordinates().height.toInt()));
        else if( this.options.direction == 'top' )
            this.textElement.setStyle( 'bottom', this.options.marHeight );
        else 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 );

        /* sod.hu Ext end
        // Old version:
        (this.options.direction == 'left') ?  this.textElement.setStyle('left', ( -1 * this.textElement.getCoordinates().width.toInt())) : this.textElement.setStyle('left', this.options.marWidth);
        */

        if(this.options.pauseOnOver){this.addMouseEvents();}
        
        //start marquee
        this.timer = this.startMooquee.delay(this.options.speed, this);
    },
    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(){

        /* sod.hu Ext */
        if(this.options.direction == 'bottom' || this.options.direction == 'top')
            var pos = this.textElement.getStyle('bottom').toInt();
        else if(this.options.direction == 'left' || this.options.direction == 'right')
            var pos = this.textElement.getStyle('left').toInt();

        if(this.options.direction == 'bottom')
            this.textElement.setStyle( 'bottom', ( pos + -1 ) + 'px' );
        else if(this.options.direction == 'top')
            this.textElement.setStyle( 'bottom', ( pos + 1 ) + 'px' );
        else 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' );
        
        /* sod.hu Ext end
        // Old version:
        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){

        /* sod.hu Ext */
        if(this.options.direction == 'bottom'){
            if(pos < -1 * (this.textElement.getCoordinates().height.toInt()))
                this.textElement.setStyle('bottom', this.options.marHeight);
        } else if(this.options.direction == 'top'){
            if(pos > this.options.marHeight.toInt())
                this.textElement.setStyle('bottom', -1 * (this.textElement.getCoordinates().height.toInt()) );
        } else 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()) );
        }
        /* sod.hu Ext end
        // Old version:
        if(this.options.direction == 'left'){
            if(pos < -1 * (this.textElement.getCoordinates().width.toInt())){
                this.textElement.setStyle('left', this.options.marWidth);
            }
        }
        else{
            if(pos > this.options.marWidth.toInt()){
                this.textElement.setStyle('left', -1 * (this.textElement.getCoordinates().width.toInt()) );                
            }
        } 
        */
    },
    setDirection: function(dir){
        this.options.direction = dir;
    }
});
mooquee.implement(new Options);
