var qindQuee = new Class({
    initialize: function(element, options) {
		this.setOptions({
			marSpacing: 10,
			speed: 10,
			direction: 'left',
			pauseOnOver: true
	    }, options);
	    this.timer = null;
	    this.textElement = null;
	    this.qindQueeElement = element;	    	    
	    this.constructqindQuee();
	},
	constructqindQuee: function() {
		var el = this.qindQueeElement;

		var strTextEl = el.id+'_content';
		this.textElement = new Element('div',{
		    'class' : 'qindQuee_content'
		    ,'id' : strTextEl
		}).setHTML(el.innerHTML);
		
		el.setHTML('');
		
		if (this.options.direction == 'up' || this.options.direction == 'down')
		{
			this.textElement.setStyle('width', el.getStyle('width'));
		}
		else if (this.options.direction == 'left' || this.options.direction == 'right')
		{
			this.textElement.setStyle('height', el.getStyle('height'));
		}
		
		this.textElement.injectInside(el);
		this.textElement = $(strTextEl);
		
		if (this.options.direction == 'left')
		{
			this.textElement.setStyle('left', this.qindQueeElement.getCoordinates().width.toInt());
		}
		else if (this.options.direction == 'right')
		{
			this.textElement.setStyle('left', -(this.textElement.getCoordinates().width.toInt()));
		}
		else if (this.options.direction == 'up')
		{
			this.textElement.setStyle('top', this.qindQueeElement.getCoordinates().height.toInt());
		}
		else if (this.options.direction == 'down')
		{
			this.textElement.setStyle('top', -(this.textElement.getCoordinates().height.toInt()));
		}
		
		if(this.options.pauseOnOver){this.addMouseEvents();}
		//start qindQuee
		this.timer = this.startqindQuee.delay(this.options.speed, this);
	},
	addMouseEvents : function(){
	    this.qindQueeElement.addEvents({
	        'mouseenter' : function(me){
	            this.clearTimer();
	        }.bind(this),
	        'mouseleave' : function(me){
	            this.timer = this.startqindQuee.delay(this.options.speed, this);
	        }.bind(this)
	    });
	},
    startqindQuee: function(){
        if (this.options.direction == 'left')
		{
			var pos = this.textElement.getStyle('left').toInt();
        	this.textElement.setStyle('left', (pos - 1) + 'px');
        }
        else if (this.options.direction == 'right')
		{
			var pos = this.textElement.getStyle('left').toInt();
        	this.textElement.setStyle('left', (pos + 1) + 'px');			
		}
        else if (this.options.direction == 'up')
		{
			var pos = this.textElement.getStyle('top').toInt();
        	this.textElement.setStyle('top', (pos - 1) + 'px');			
		}
        else if (this.options.direction == 'down')
		{
			var pos = this.textElement.getStyle('top').toInt();
        	this.textElement.setStyle('top', (pos + 1) + 'px');			
		}
        
        this.checkEnd(pos);
        this.timer = this.startqindQuee.delay(this.options.speed, this);        
    },
    resumeqindQuee: function(){
        this.stopqindQuee();
        if(this.options.pauseOnOver){this.addMouseEvents();}
        this.timer = this.startqindQuee.delay(this.options.speed, this);        
    },
    stopqindQuee: function(){
        this.clearTimer();        
        this.textElement.removeEvents();        
    },
    clearTimer: function(){
        $clear(this.timer);
    },
    checkEnd: function(pos){
        if (this.options.direction == 'left')
		{
            if (pos < -(this.textElement.getCoordinates().width.toInt()))
			{
                this.textElement.setStyle('left', this.qindQueeElement.getCoordinates().width);
            }
        }
        else if (this.options.direction == 'right')
		{
            if (pos > this.qindQueeElement.getCoordinates().width.toInt())
			{
				this.textElement.setStyle('left', -(this.textElement.getCoordinates().width.toInt()));
            }
        }
        else if (this.options.direction == 'up')
		{
            if (pos < -(this.textElement.getCoordinates().height.toInt()))
			{
                this.textElement.setStyle('top', this.qindQueeElement.getCoordinates().height.toInt());                
            }
        }
        else if (this.options.direction == 'down')
		{
            if (pos > this.qindQueeElement.getCoordinates().height.toInt())
			{
                this.textElement.setStyle('top', -(this.textElement.getCoordinates().height.toInt()));  
            }
        }                
    },
    setDirection: function(dir){
        this.options.direction = dir;
    }
});
qindQuee.implement(new Options);