/****************************************************
*	mooquee v.2.2phd								*
*	Http: WwW.developer.ps/moo/mooquee				*
*	Dirar Abu Kteish dirar@zanstudio.com			*
*	2009-01-30										*
*****************************************************
*	Extend By www.Sod.hu							*
*	new directions: top, bottom						*
*	2008-04-30										*
/****************************************************
*	Extend By www.phdcon.com						*
*	uses Fx.Tween, class vars limit DOM calls		*
*	added: start position, fps						*
*	changed: speed to wait, steps to speed			*
*	speed is based on rough char/sec estimate		*
*	2009-06-30 (0.2)								*
*	Added opt. center start for left/right			*
*	2009-07-08 (0.2.1)								*
*	Added started param to stop FX errors on init	*
*	2010-03-11 (0.2.2)								*
/***************************************************/
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */ 
    
    
var mooquee = new Class({
	initialize: function(element, options) {
		this.setOptions({
			marHeight: 40,
			marWidth: 550,
			speed: 1,
			wait: 40,
			direction: 'bottom',
			pauseOnOver: true,
			pauseOnContainerOver: true,
			startPos: false,
			fps: 40
		 }, options);
		 this.timer = null;
		 this.textElement = null;
		 this.mooqueeElement = element;
		 this.contentWidth = 0;
		 this.contentHeight = 0;
		 this.currentPos = 0;	// Current position -- set to this.options.startPos or this.restartPos
		 this.limitPos = 0;		// End position of the marquee element
		 this.restartPos = 0;	// Position to return to when re-starting the marquee
		 this.styleDirection = 'bottom';
		 this.scroller = 0;		// FX Variable Placeholder
		 this.started = false;
		 this.constructMooquee();
	},
	constructMooquee: function() {
		var el = this.mooqueeElement;
		el.setStyles({
			  'width' : this.options.marWidth
			  ,'height' : this.options.marHeight			  
		});
		this.textElement = new Element('div',{
			  'class' : 'mooquee-text'
			  ,'id' : 'mooquee-text'
		}).set('html', el.innerHTML);
		el.set('html', '');//clear mooqueeElement inner html
		this.textElement.inject(el);
		//this.textElement = $('mooquee-text');
		if(!this.setStartPos()){return;}
		if(this.options.pauseOnOver){this.addMouseEvents();}
		//start marquee
		this.startMooquee.delay(this.options.wait, this);
	},
	setStartPos: function(){
		/* phdcon.com -- put all positions into class variables to limit DOM calls */
		switch (this.options.direction)
		{
		case 'bottom':
			this.restartPos = this.options.marHeight;
			if (this.options.startPos == 'center' && ( this.options.marHeight.toInt() > this.textElement.getCoordinates().height.toInt())) {
				this.currentPos = ( (this.options.marHeight >>> 1) - (this.textElement.getCoordinates().height.toInt() >>> 1)).toInt();
			} else { this.currentPos = ( this.options.startPos == 0 ? this.restartPos : this.options.startPos != 'center' ? this.options.startPos : 0 ); }
			this.limitPos = -1 * this.textElement.getCoordinates().height.toInt();
			this.textElement.setStyle('bottom', this.currentPos);
			break;
		case 'top':
			this.restartPos = -1 * this.textElement.getCoordinates().height.toInt();
			if (this.options.startPos == 'center' && ( this.options.marHeight.toInt() > this.textElement.getCoordinates().height.toInt())) {
				this.currentPos = ( (this.options.marHeight >>> 1) - (this.textElement.getCoordinates().height.toInt() >>> 1)).toInt();
			} else { this.currentPos = ( this.options.startPos == 0 ? this.restartPos : this.options.startPos != 'center' ? this.options.startPos : 0 ); }
			this.limitPos = this.options.marHeight.toInt();
			this.textElement.setStyle( 'bottom', this.currentPos );
			break;
		case 'left':
			this.restartPos = this.options.marWidth;
			if (this.options.startPos == 'center' && ( this.options.marWidth.toInt() > this.textElement.getCoordinates().width.toInt())) {
				this.currentPos = ( (this.options.marWidth >>> 1) - (this.textElement.getCoordinates().width.toInt() >>> 1)).toInt();
			} else { this.currentPos = ( this.options.startPos == 0 ? this.restartPos : this.options.startPos != 'center' ? this.options.startPos : 0 ); }
			this.limitPos = -1 * this.textElement.getCoordinates().width.toInt();
			this.textElement.setStyle('left', this.currentPos);
			this.styleDirection = 'left';
			break;
		case 'right':
			this.restartPos = -1 * this.textElement.getCoordinates().width.toInt();
			if (this.options.startPos == 'center' && ( this.options.marWidth.toInt() > this.textElement.getCoordinates().width.toInt())) {
				this.currentPos = ( (this.options.marWidth >>> 1) - (this.textElement.getCoordinates().width.toInt() >>> 1)).toInt();
			} else { this.currentPos = ( this.options.startPos == 0 ? this.restartPos : this.options.startPos != 'center' ? this.options.startPos : 0 ); }
			this.limitPos = this.options.marWidth.toInt();
			this.textElement.setStyle('left', this.currentPos);
			this.styleDirection = 'left';
			break;
		default:
			alert( 'direction config error: ' + this.options.direction );
			return false;
		}
		return true;
	},
	addMouseEvents : function(){
		 if(this.options.pauseOnContainerOver){
			 this.mooqueeElement.addEvents({
				 'mouseenter' : function(me){
				 	if (this.started){ this.scroller.pause(); }
				 }.bind(this),
				 'mouseleave' : function(me){
					if (this.started){ this.scroller.resume(); }
				 }.bind(this)
			 });
		 }else{
			 this.textElement.addEvents({
				 'mouseenter' : function(me){
					 if (this.started){ this.scroller.pause(); }
				 }.bind(this),
				 'mouseleave' : function(me){
					 if (this.started){ this.scroller.resume(); }
				 }.bind(this)
			 });
		 }
	},
	startMooquee: function(){
		this.scroller = new Fx.Tween(this.textElement, { 
			duration: ((Math.abs(this.limitPos)+Math.abs(this.currentPos))/(12*this.options.speed)*1000).toInt(), 
			transition: Fx.Transitions.linear, 
			fps: this.options.fps,
			onComplete: function(){
				this.textElement.setStyle( this.styleDirection,this.restartPos );
				this.currentPos = this.restartPos;
				(function(){ this.startMooquee()}.bind(this)).delay(this.options.wait);
			}.bind(this)
		});
		(function(){ this.scroller.start(this.styleDirection,this.currentPos,this.limitPos) }.bind(this)).delay(this.options.wait);
		//wait before pause works to make sure the marquee is running.
		(function(){ this.started = true; }.bind(this)).delay(this.options.wait);
	},
    resumeMooquee: function(){
        this.scroller.resume();
        if(this.options.pauseOnOver){this.addMouseEvents();}
    },
    stopMooquee: function(){
        this.scroller.pause();
        this.textElement.removeEvents();
        this.mooqueeElement.removeEvents();
    },

    setDirection: function(dir){
    	this.scroller.cancel();
        this.options.direction = dir;
        this.setStartPos();
        this.startMooquee();
    }
});
mooquee.implement(new Options);
