/*
 * ZRotator
 * ------------------------------------------------------------------------------------------
 * 
 * Author: David Joly, Zeletron LLC
 * Link: http://www.zeletron.com
 * Date: 05/10/2010
 * version: 1.0
 *
 * Usage
 * ------------------------------------------------------------------------------------------
 * Rotator Options
 * {
 * 	selector : '', 		//Use this to limit the rotating elements to those matching the
 *						//Selector. It defaults to nothing, selecting all first-level
 *                      //child elements.
 *	delay : 8000,		//The time an element stays active. Default is eight (8) seconds.
 *	rotateIn : {
 *	 	type:'none'		//The type of animation to use during 'in' rotation.
 *						//Choose between 'slide' or 'fade'. Defaults to 'none'.
 *		duration:1000	//Specifies the animation's duration. Defaults to one (1) seconds.
 *	},				
 *	rotateOut : {{
 *	 	type:'none'		//The type of animation to use during 'out' rotation.
 *						//Choose between 'slide' or 'fade'. Defaults to 'none'.
 *		duration:1000	//Specifies the animation's duration. Defaults to one (1) seconds.
 *	}
 * }	
 * 
 * Known Bugs
 * ------------------------------------------------------------------------------------------
 * When using a combination of slide and fade, display and timing problems appear.
 * Suggested usage is to use either both slide, or both fade effects. Also, be sure
 * the delay option is greater than the rotateIn.duration and rotateOut.duration options.
 */
if(jQuery) (function($){

	function rotate(e,current,targets,delay,rotateIn,rotateOut){
			if(targets.length > 1){
				var previous;
				if(current < targets.length){
					previous = current;
					current++;
				} else {
					previous = current;
					current = 1;
				}
				
				switch(rotateOut.type){
					case 'slide':
						e.children('.t_'+previous).slideUp(rotateOut.duration);
						break;
					case 'fade':
						e.children('.t_'+previous).fadeOut(rotateOut.duration).hide();
						break;
					default:
						e.children('.t_'+previous).hide();
						break;
				}
				switch(rotateIn.type){
				case 'slide':
					e.children('.t_'+current).slideDown(rotateIn.duration);
					break;
				case 'fade':
					e.children('.t_'+current).fadeIn(rotateIn.duration);
					break;
				default:
					e.children('.t_'+current).show();
					break;
			}
				setTimeout(function(){
					rotate(e,current,targets,delay,rotateIn,rotateOut);
				},delay,"JavaScript");
			}
		}
		
	$.extend($.fn, {

		ZRotator : function(options) {
		
			//Set options
			if( !options ) options = {};
			if( options.selector == undefined ) options.selector = '';
			if( options.delay == undefined ) options.delay = 8000;
			else {options.delay = parseInt(options.delay);}
			
			if( options.rotateIn == undefined ) options.rotateIn = {type:'none',duration:1000};
			if( options.rotateIn.type == undefined || (options.rotateIn.type != 'slide' && options.rotateIn.type != 'fade'))
				options.rotateIn.type = 'none';
			if( options.rotateIn.duration == undefined )
				options.rotateIn.duration = 1000;
			else options.rotateIn.duration = parseInt(options.rotateIn.duration);

			if( options.rotateOut == undefined ) options.rotateOut = {type:'none',duration:1000};
			if( options.rotateOut.type == undefined || (options.rotateOut.type != 'slide' && options.rotateOut.type != 'fade'))
				options.rotateOut.type = 'none';
			if( options.rotateOut.duration == undefined )
				options.rotateOut.duration = 1000;
			else options.rotateOut.duration = parseInt(options.rotateOut.duration);
	
			//Initialize elements to be rotated
			return $(this).each( function() {
				var e = $(this);
				var targets = e.children(options.selector);
				var t = 1;
				//Hide all but first element to be rotated
				targets.each(function(){
					t > 1 ? $(this).hide() : $(this).show();
					$(this).addClass('t_'+t);
					t++;
				});
				//Show rotator
				e.fadeIn();
				setTimeout(function(){
					rotate(e,1,targets,options.delay,options.rotateIn,options.rotateOut);
				},options.delay,"JavaScript");
			});
		}
		
	});
})(jQuery);
