/*
Project: 		Dynamic Rotator
Author: 		Niń0 Morales
Company: 		ONIN IT Solutions
URL: 			http://www.oninitsolutions.com/
Date Created: 	May 02, 2010
*/

(function($){ $.fn.dynamicRotator = function(options){
	var defaults = {
		interval: 2000,
		transition: "fading",
		width:100,
		height:100,
		callBack:null
	};
	
	var options = $.extend(defaults, options);
	return this.each(function(index) {
		var s_item;				  
		var items = [];
		var cnt = $(this).children().size();
		var	ctr = 0;
		var _parentM = $(this);
		$(this).children().each(function(index,item){
			items[ctr] = $(item);
			ctr++;
		});
		
		$(this).css({
			'width' : options.width,
			'height' : options.height,
			'overflow' : "hidden"
		});		
		var cssObj = {
		  'width' : options.width,
		  'height' : options.height,
		  'position' : 'absolute',
		  'display' : 'none'
		};
		
		if(options.transition == "slideLeft"){
			ctr = 1;
			cssObj = {
			  'width' : options.width,
			  'height' : options.height,
			  'float' : 'left'
			};
			$(this).children().css(cssObj);	
			
			$(this).wrapInner('<div id="wrapper" />');
			var cssWrapper = {
				'width' : (options.width * cnt) + 50,
				'height' : options.height
			};
			$("#wrapper", this).css(cssWrapper);	
			s_item = $("#wrapper", this);
		}else if(options.transition == "slideTop"){
			ctr = 1;
			cssObj = {
			  'width' : options.width,
			  'height' : options.height
			};
			$(this).children().css(cssObj);	
			
			$(this).wrapInner('<div id="wrapper" />');
			var cssWrapper = {
				'width' : options.width,
				'height' : (options.height * cnt) + 50
			};
			$("#wrapper", this).css(cssWrapper);	
			s_item = $("#wrapper", this);
		}else{
			$(this).children().css(cssObj);	
			items[0].css('display','block');	
			ctr = 0;
		}
		
		var fading = function(){
			_parentM.children().each(function(index,item){
				if($(item).is(":visible")){
					$(item).fadeOut();		
				}
			});
			
			if((ctr + 1) < cnt){
				items[ctr + 1].fadeIn("slow");
				ctr = ctr + 1;
			}else{
				items[0].fadeIn("slow");
				ctr = 0;
			}
			if(options.callBack != null){
				eval(options.callBack)(items[ctr]);	
			}
		};
		
		var slideLeft = function(){
			
			if(ctr==cnt){
				s_item.animate({"marginLeft": "+=" + (((cnt - 1) * options.width) + 100) + "px"}, 400);
				s_item.animate({"marginLeft": "-=100px"}, 500);
				ctr = 0;
			}else{
				s_item.animate({"marginLeft": "-=" + (options.width + 100) + "px"}, 400);
				s_item.animate({"marginLeft": "+=100px"}, 500);
			}
			if(options.callBack != null){
				eval(options.callBack)(s_item);	
			}
			ctr++;
		};
		var slideTop = function(){
			if(ctr==cnt){
				s_item.animate({"marginTop": "+=" + (((cnt - 1) * options.height) + 100) + "px"}, 400);
				s_item.animate({"marginTop": "-=100px"}, 500);
				ctr = 0;
			}else{
				s_item.animate({"marginTop": "-=" + (options.height + 100) + "px"}, 400);
				s_item.animate({"marginTop": "+=100px"}, 500);
			}
			if(options.callBack != null){
				eval(options.callBack)(s_item);	
			}
			ctr++;
		};
		var enlarge = function(){
			if(ctr==cnt){
				s_item.animate({"marginTop": "+=" + (((cnt - 1) * options.height) + 100) + "px"}, 400);
				s_item.animate({"marginTop": "-=100px"}, 500);
				ctr = 0;
			}else{
				s_item.animate({"marginTop": "-=" + (options.height + 100) + "px"}, 400);
				s_item.animate({"marginTop": "+=100px"}, 500);
			}
			if(options.callBack != null){
				eval(options.callBack)(s_item);	
			}
			ctr++;
		};
		setInterval(
			function(){
				eval(options.transition)();
			}, 
			options.interval
		);
	});
}})(jQuery);
