/*
 * jQuery image rotator plugin
 * @author jeroen@dhvv.nl - http://www.dhvv.nl
 * @version 1.0.0
 * @date August 11, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 jeroen@dhvv.nl (www.dhvv.nl)
 * @license CC Attribution-No Derivative Works 3.0 - http://creativecommons.org/licenses/by-nd/3.0/
 */

(function($){
	
	$.fn.extend({ 
		
		rotator: function(options){
			
			var defaults = 
			{
				displayTime : 4000,
				transitionSpeed : 500,
				animationIn : 'fade',
				animationOut : 'fade'
			};
		
			options = $.extend(defaults, options);
				
			return this.each( 
				
				function(){
				
					var obj = $(this);
					var t=options.displayTime;
					var s=options.transitionSpeed;
					var ain=options.animationIn;
					var aout=options.animationOut;
					
					var w = $('img:first', obj).width();
					var h = $('img:first', obj).height();
					var types = Array('left','right','top','bottom','fade');
					var kids = $(obj).children();
					var items = kids.length;
					var itemno = 0;
					
					//set styles
					$(obj).css({'width':w+'px','height':h+'px','overflow':'hidden','position':'relative'});
					$(obj).children().css({'width':w+'px','height':h+'px'});
					$(obj).children().css({'position':'absolute','top':'0px','left':w+'px'});
						
					anim(ain,aout);
							
					function anim(ain,aout){
						
						if( itemno == items ){
							itemno = 0;
						}
						var cur = kids[itemno];
						
						if(ain=="random"){
							var a=types[Math.floor(Math.random()*types.length)];
						}else{
							var a=ain;
						}

						//set defaults and animate in
						switch(a){
							case 'left':
								$(cur).css({'top':'0px','left':-w+'px'});
								$(cur).animate({'left':0},{'duration':s,'queue':true});
							break;
							case 'right':
								$(cur).css({'top':'0px','left':w+'px'});
								$(cur).animate({'left':0},{'duration':s,'queue':true});
							break;							
							case 'top':
								$(cur).css({'top':-h+'px','left':'0px'});
								$(cur).animate({'top':0},{'duration':s,'queue':true});
							break;
							case 'bottom':
								$(cur).css({'top':h+'px','left':'0px'});
								$(cur).animate({'top':0},{'duration':s,'queue':true});
							break;
							case 'fade':
								$(cur).css({'opacity':0,'top':'0px','left':'0px'});
								$(cur).animate({'opacity':1},{'duration':s,'queue':true});
							break;
							case 'show':
								$(cur).css({'top':'0px','left':'0px'});
								$(cur).animate({'opacity':1},{'duration':s,'queue':true});
							break;

						}
						
						if(aout=="random"){
							var a=types[Math.floor(Math.random()*types.length)];
						}else{
							var a=aout;
						}

						//wait
						$(cur).animate({'opacity':1},{'duration':t,'queue':true});
						
						//animate out
						switch(a){
							case 'left':
								$(cur).animate({'left':-w},s,function(){anim(ain,aout);});
							break;
							case 'right':
								$(cur).animate({'left':w},s,function(){anim(ain,aout);});
							break;							
							case 'top':
								$(cur).animate({'top':-h},s,function(){anim(ain,aout);});
							break;
							case 'bottom':
								$(cur).animate({'top':h},s,function(){anim(ain,aout);});
							break;
							case 'fade':
								$(cur).animate({'opacity':0},s,function(){$(cur).css({'left':w,'opacity':1});anim(ain,aout);});
							break;
							case 'hide':
								$(cur).animate({'opacity':1},s,function(){$(cur).css({'left':w,'opacity':1});anim(ain,aout);});
							break;
						}
						
						itemno++;
						
					}
		  	}
		 );
   }
	});
})(jQuery);
$(document).ready(function() {
	$('.rotator').rotator({});
});
$(document).ready(function() {
	$('.banners').rotator({'animationIn':'right','animationOut':'left','displayTime':5000});
});