/* =========================================================

// moofade.js

// Datum: 2009-06-02
// Author: Marcel Marnet
// Mail: m.marnet@all2e.com

 * Description: mooFade (element-fading) for mootools
 * 
 * ==========================================================
 * 
 * <div id="parentElementID">
 * 		<div>div1</div>
 * 		<div>div2</div>
 * 		<div>div3</div>
 * 		<span>span1</span>
 * 		<span>span2</span>
 *   	<span>span3</span>
 *   	<img src="image.gif" />
 * </div>
 * 
 * var myFader = new mooFade('parentElementID', { params });
 * 
 * params:
 * 
 * timeout: timeout between fading				-> 	time in ms
 * (default: 5000)										example: { timeout: 1000 }
 * 
 * startElement: first showed element			-> 	first, last, random or number of element
 * (default: 'first')									example: { startElement: 'random' }
 * 
 * type: type of the animation 					-> 	sequence or random
 * (default: 'sequence')								example: { type: 'sequence' }
 * 
 * height: height of the parent element 		-> 	height+px
 * (default: '500px')									example: { height: '500px' }
 * 
 * width: width of the parent element 			-> 	width+px
 * (default: '500px')    								example: { width: '300px' }
 * 
 * overflow: overflow of the parent Element 	-> 	auto, visible, scroll, hidden
 * (default: 'hidden')									example: { overflow: 'hidden' }
 * 
 * 
// ========================================================= */

var mooFade = new Class({
	globals:	{
					'current': 		1,
					'ElementCount': 	0
				},
	objElements:{ },
	options:	{
					'timeout': 		      4000,
					'startElement':	      'first',
					'type':			      'sequence',
					'height': 		      '500px',
					'width': 		      '500px',
					'overflow': 	      'hidden',
					'fadeTime':           3000,
					'showFirstElement':   true
				},
	initialize:	function( fadingElements, options){
					this.options = $merge(this.options, options);
					if($(fadingElements))
					{
						this.objElements = $(fadingElements).getChildren();
						this.globals['ElementCount'] = this.objElements.length;
					}
					if(this.globals['ElementCount'] > 1)
					{
						$(fadingElements).setStyles({ 
								position: 'relative',
								height: this.options['height'],
								width: this.options['width'],
								overflow: this.options['overflow']
								});
						$each(this.objElements, function(fadingElement, index){
						  if( ( index == 0 && !this.options['showFirstElement'] ) || index != 0 )
    						{
                            	$(fadingElement).setStyles({
    							    visibility: 'hidden',
    							    opacity: '0',
    							    display: 'block',
    							    position: 'absolute',
    							    'z-index': index+1000
    							});
							}else{
                            	$(fadingElement).setStyles({
    							    visibility: 'visible',
    							    opacity: '1',
    							    display: 'block',
    							    position: 'absolute',
    							    'z-index': index+1000
    							});                            
                            }
						}, this);
						this.options['startElement'] = this.getStartElement(this);
						this.getAnimationType(this);
						this.FadeInElement(this.options['startElement']);
						this.setCurrentElement(this.options['startElement']);
						this.fadingObjects.periodical(this.options['timeout'], { obj: this });
					}
	},
	setCurrentElement: function(currentElement){
		this.globals['current'] = currentElement;
	},		
    fadingObjects: function(obj) {
		if(this.obj.options['type'] == "random")
		{
			this.obj.FadeOutElement(this.obj.globals['current']);
			this.obj.setCurrentElement(Math.ceil(Math.random()*this.obj.globals['ElementCount'])-1);
			this.obj.FadeInElement(this.obj.globals['current']);			
		}else if(this.obj.options['type'] == "sequence"){	
			this.obj.FadeOutElement(this.obj.globals['current']);
			if(this.obj.globals['current'] == this.obj.globals['ElementCount']-1)
			{
				this.obj.setCurrentElement(0);
			}else{
				this.obj.setCurrentElement(this.obj.globals['current']+1);
			}
			this.obj.FadeInElement(this.obj.globals['current']);
		}
	},		
	FadeInElement: function(fadeIn){
	    $(this.objElements[fadeIn]).set('tween', {duration: this.options['fadeTime']});
		$(this.objElements[fadeIn]).fade('in');
	},	
	FadeOutElement: function(fadeOut){
	    $(this.objElements[fadeOut]).set('tween', {duration: this.options['fadeTime']});
		$(this.objElements[fadeOut]).fade('out');
	},		
	getAnimationType: function(obj)
	{
		if(this.options['type'] != "random" && this.options['type'] != "sequence")
		{
			this.options['type'] = "sequence";
		}
	},
	getStartElement: function(obj)
	{
		if(this.options['startElement'] == parseInt(this.options['startElement']))
		{
			if(this.options['startElement'] > obj.globals['ElementCount'])
			{
				return obj.globals['ElementCount']-1;
			}else{
				return this.options['startElement']-1;	
			}
		}else{
			switch(this.options['startElement'])
			{
			default:
			case 'first':
				return 0;
				break;
			case 'last':
				return obj.globals['ElementCount']-1;
				break;
			case 'random':
				return Math.ceil(Math.random()*obj.globals['ElementCount'])-1;
				break;
			}
		}
	}
});
