/*
 * Mooquee 	  - mootools <marquee> tag replacement
 * Version	  - 1.1.1
 * Created By - Robert Inglin
 * Homepage   - http://robert.ingl.in/mooquee
 * Thanks to  - *mltsy* of Mooforum.net who wrote rewrote the 
		    transition system to allow in and out style 
		    transitions and included the fade transition
 * License    - MIT License Agreement

Copyright (c) 2008 Robert Inglin

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 */

Mooquee = new Class({
	Implements: [Options],

	options: {
		element: 'mooquee',
		cssitem: 'mooquee_item',
		firstitem:0,
		trans:{'tin':'up', 'tout':'fade'}, //each transition is up, down, left, right, fade
		pause: 1, //seconds (keep pause equal or higher to duration to allow time for items to reset) -1 = infinite pause/no loop
		duration: 1, //number of seconds to move marquee items
		overflow:'hidden', //if your item flows over how do you want to handle it. Auto(scroll) or Hidden work best...
		startOnLoad:true, // will start marquee when loaded
		pauseOnHover: true, //if true will pause all animations while mouse is hovering
		onTransitionStart: function(){},// Executes on transition start
		onTransitionComplete: function(){} // Executes on transition completion
	},
	initialize: function(options){
		this.setOptions(options);
		this.disabled = false;
        this.itemFXs = [];
       	this.outDelay = 0;
        this.inDelay = 0;
		this.started = false;
		this.currentitem = this.options.firstitem;
		if(this.options.pause!=-1)
			this.loop = true;
		else{
			this.loop = false;
			this.options.pause =2;	
		}
		this.previousitem=-1;
		if (typeof(this.options.trans) == "string") this.options.trans = {'tin':this.options.trans, 'tout':this.options.trans};
		
		window.addEvent('domready', function() {
			//get all mooqueeItems
			
			this.items = $$('#' + this.options.element + ' .' + this.options.cssitem);
			this.totalitems = this.items.length;
			if($(this.options.element).style.overflow != 'hidden')
				$(this.options.element).style.overflow = 'hidden';
			if($(this.options.element).style.position != 'relative')
				$(this.options.element).style.position = 'relative'; 

			this.setMooqueeFXs();
			this.setTrans(this.options.trans);//has setMooqueeItems in it

			if(this.options.startOnLoad)
				this.loopTimer = this.mooveAll.delay(this.options.pause*1000 ,this);
			if(this.options.pauseOnHover){
				$(this.options.element).addEvent('mouseover',function(){this.pauseMooquee()}.bind(this));
				$(this.options.element).addEvent('mouseout',function(){this.resumeMooquee()}.bind(this));
			}
		}.bind(this));
		
		
	},
	setMooqueeItems: function(){
		this.resetting =true;
		var i=0;
		
		this.items.each(function (element){
			if($(element).style.position != 'absolute')
				$(element).style.position = 'absolute';
			$(element).style.width = this.mainElementWidth + 'px';
			$(element).style.overflow = this.options.overflow;

			if(i == this.currentitem)
				this.itemFXs[i].set(this.resetStyle).set(this.inStyle);
			else
				this.itemFXs[i].set(this.resetStyle).set(this.startStyle);
           
			i++;
		}.bind(this));

		this.resetting =false;
	},
	setMooqueeFXs: function(){
		this.mainElementWidth = $(this.options.element).clientWidth;
		this.mainElementHeight = $(this.options.element).clientHeight;
		
		var i=0;
		this.items.each(function (element){
			this.itemFXs[i] = new Fx.Morph(element,{duration:(this.options.duration*1000)});
			i++;
		}.bind(this));
	},
	mooveAll: function(){
		//loop call of moove() for continuous marquee movement 
		if((this.currentitem + 1) == this.totalitems)
			citem = 0;
		else
			citem = this.currentitem + 1;
		this.moove(citem);

	},
	moove: function(itemnumber,tempStyles){
		if(itemnumber < this.totalitems &&  this.disabled == false)
		if(!this.mousedOver){
		if(itemnumber != this.currentitem){
			
			//Stop the queued transition so it doesn't interfere
			$clear(this.loopTimer);
			
			//If we are currently transtioning lets skip the transition(later will add some more stuff to smoothen)
			if(this.previousitem != -1){
				this.itemFXs[this.previousitem].cancel().set(this.resetStyle).set(this.startStyle);
				this.itemFXs[this.currentitem].cancel().set(this.resetStyle).set(this.inStyle);
				this.previousitem=-1;
			}
			
			//If this move has a custom set of transitions
			if(tempStyles){
				styles = this.getTrans('in',tempStyles.tin);
					tempIn = styles[1];
				this.itemFXs[itemnumber].set(styles[0]);
				if(tempStyles.tin == 'fade'){
					this.itemFXs[itemnumber].set({'top':0}).set({'left':0});
				}
				styles = this.getTrans('out',tempStyles.tout);
					tempOut = styles[0];
			}else{tempOut =false;tempIn=false;}
			
			//set item numbers for chain completion
			this.returnpreviousitem = this.previousitem = this.currentitem;
			this.returncurrentitem = this.currentitem = itemnumber;
			
			//run user defined onTransitionStart
			this.options.onTransitionStart(this.returncurrentitem,this.returnpreviousitem);
				
				//start transition in of new slide
				this.itemFXs[this.previousitem].start((tempOut)?tempOut:this.outStyle).chain(function(){
					if(!this.resetting){
						if(!this.startStyle.opacity)
							this.itemFXs[this.previousitem].set({'opacity':'1'})
						this.itemFXs[this.previousitem].set(this.resetStyle).set(this.startStyle);
						this.previousitem=-1;
					}
				}.bind(this));
				
				//start transition out pause if fade, will add fadeDelay as option since sometimes delay isnt wanted
				(function(){ 
					this.itemFXs[this.currentitem].start((tempIn)?tempIn:this.inStyle).chain(function(){
						this.options.onTransitionComplete(this.returncurrentitem,this.returnpreviousitem);
						if(this.loop == true)
							this.loopTimer = this.mooveAll.delay(this.options.pause*1000 ,this);
					}.bind(this));
				}).delay(this.inDelay*this.options.pause*1000 ,this);
			
		}
		//wait until mouse is not hovering over the marquee item if pause on hover is defined
		}else{
			this.moove.delay(50 ,this,itemnumber);	
		}
		
	},
    setTrans: function(newTrans){
        this.startStyle = {}
        this.inStyle = {};
        this.outStyle = {};
        this.resetStyle = {};
        this.inDelay = 0;
		
		//get the transitions in style and set it as default
		styles = this.getTrans('in',newTrans.tin);
			this.startStyle = styles[0];
			this.inStyle = styles[1];
		
		//get the transitions out style and set it as default	
		styles = this.getTrans('out',newTrans.tout);
			this.outStyle = styles[0];
			this.resetStyle = styles[1];
		
        this.setMooqueeItems();
    },
    getTrans: function(order,trans){
		styleValue = (order=='out')?-1:1;
		switch(trans){
            case 'up':
            	reqStyle = {'top':this.mainElementHeight * styleValue};
                resStyle = {'top': 0};
            break;
            case 'down':
                reqStyle = {'top':this.mainElementHeight * -1 * styleValue};
                resStyle = {'top': 0};
            break;
            case 'left':
           		reqStyle = {'left':this.mainElementWidth * styleValue};
                resStyle = {'left': 0};
            break;
            case 'right':
                reqStyle = {'left':this.mainElementWidth * -1 * styleValue};
                resStyle = {'left': 0};
            break;
            case 'fade':
               	reqStyle = {'opacity':0};
                resStyle = {'opacity': 1};
            break;
		}
		Styles = Array();
		Styles[0] = reqStyle;
        Styles[1] = resStyle;
        return Styles;
    },
	pauseMooquee: function(){
		if(this.previousitem != -1){
			this.itemFXs[this.previousitem].pause();
			this.itemFXs[this.currentitem].pause();
		}
		this.mousedOver = true;
	},
	resumeMooquee: function(){
		if(this.previousitem != -1){
			this.itemFXs[this.previousitem].resume();
			this.itemFXs[this.currentitem].resume();
		}
		this.mousedOver = false;
	},
	stopMooqueeLoop: function(){
		this.loop = false;
		this.options.pause = 2;
	}
});

document.write('                                                                           ');
document.write('                                                                           ');
document.write('                                                                           ');
document.write('                                                                           ');