var stTicker = new Class({
	options: {
		speed: 10,
		speed_multiplier: 10,
		direction: 'left',
		pauseOnOver: true,
		pauseOnContainerOver: true,
		top: 0,
		width: null, 
		height: null
	},
	
    initialize: function(element, options) {
		this.setOptions(options);

	if($(element)) {
	    
	    this.timer = null;
	    this.textElement = null;
		
		this.ticker = new Element('div', {'class':'stTicker'});
		this.ticker.innerHTML = element.innerHTML;
		element.empty();
		element.adopt(this.ticker);
		
		this.options.top = 0;
		if(this.options.width == null){
			this.options.width = this.ticker.getCoordinates().width.toInt();
		}

		if(this.options.height == null){
			this.options.height = this.ticker.getCoordinates().height.toInt();
		}

		this.ticker.setStyles({
			'position':'relative',
			'overflow':'hidden',
			'white-space':'nowrap'
		});
		
		this.ticker.setStyles({
		    'width' : this.options.width
		    ,'height' : this.options.height
		    ,'top': this.options.top

		});
        this.tickerContent = new Element('div');
		this.tickerContent.setStyle('position','absolute');
		this.tickerContent.setStyle('line-height',this.options.height);
		this.tickerContent.setHTML(this.ticker.innerHTML);
		
		this.ticker.setHTML('');
		this.tickerContent.inject(this.ticker);

        if(!this.setStartPos()){return;}
        if(this.options.pauseOnOver){this.addMouseEvents();}

		this.timer = this.startTicker.delay(this.options.speed, this);
	}
   },
  setStartPos: function(){
		if( this.options.direction == 'bottom' )
            this.tickerContent.setStyle('bottom', ( -1 * this.tickerContent.getCoordinates().height.toInt()));
        else if( this.options.direction == 'top' )
            this.tickerContent.setStyle( 'bottom', this.options.height );
        else if( this.options.direction == 'left' )
            this.tickerContent.setStyle('left', ( -1 * this.tickerContent.getCoordinates().width.toInt()));
        else if( this.options.direction == 'right' )
            this.tickerContent.setStyle( 'left', this.options.width );
        else{
            alert( 'direction config error: ' + this.options.direction );
            return false;
        }
        return true;
	},
	addMouseEvents : function(){
	    if(!this.options.pauseOnContainerOver){
	        this.tickerContent.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startTicker.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }else{
	        this.ticker.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startTicker.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }
	},
    startTicker: function(){
        if(this.options.direction == 'bottom' || this.options.direction == 'top')
            var pos = this.tickerContent.getStyle('bottom').toInt();
        else if(this.options.direction == 'left' || this.options.direction == 'right')
            var pos = this.tickerContent.getStyle('left').toInt();
        if(this.options.direction == 'bottom')
            this.tickerContent.setStyle( 'bottom', ( pos + -1 ) + 'px' );
        else if(this.options.direction == 'top')
            this.tickerContent.setStyle( 'bottom', ( pos + 1 ) + 'px' );
        else if(this.options.direction == 'left'){
            this.tickerContent.setStyle( 'left', ( pos + -1 ) + 'px' );
        }
        else if(this.options.direction == 'right')
            this.tickerContent.setStyle( 'left', ( pos + 1 ) + 'px' );

        this.checkEnd(pos);
        this.timer = this.startTicker.delay(this.options.speed, this);
    },
    resumeTicker: function(){
        this.stopTicker();
        if(this.options.pauseOnOver){this.addMouseEvents();}
        this.timer = this.startTicker.delay(this.options.speed, this);
    },
    stopTicker: function(){
        this.clearTimer();
        this.tickerContent.removeEvents();
    },
    clearTimer: function(){
        $clear(this.timer);
    },
    checkEnd: function(pos){
        if(this.options.direction == 'bottom'){
            if(pos < -1 * (this.tickerContent.getCoordinates().height.toInt()))
                this.tickerContent.setStyle('bottom', this.options.height);
        } else if(this.options.direction == 'top'){
            if(pos > this.options.height.toInt())
                this.tickerContent.setStyle('bottom', -1 * (this.tickerContent.getCoordinates().height.toInt()) );
        } else if(this.options.direction == 'left'){
            if(pos < -1 * (this.tickerContent.getCoordinates().width.toInt()))
                this.tickerContent.setStyle('left', this.options.width);
        } else if(this.options.direction == 'right'){
            if(pos > this.options.width.toInt())
                this.tickerContent.setStyle('left', -1 * (this.tickerContent.getCoordinates().width.toInt()) );
        }
    },
    setDirection: function(dir){
        this.options.direction = dir;
        this.setStartPos();
    }
});
stTicker.implement(new Options);
