(function($) {

	var opt 		= new Array;
	var played		= new Array;
	var currentpos	= new Array;
	var autoInterval = new Array;
	 
	$.fn.WIPslider= $.fn.WIPslider = function(options){
		
		init = function(tg){
			opt[tg.id] = $.extend({}, $.fn.WIPslider.defaults, options);
			played[tg.id]		= new Array();
			currentpos[tg.id]	= 0;
			
			$('#'+tg.id+'').css('background-image', 'none');//remove the preloader image

			$.each($('#'+tg.id+' img'), function(i,item){ //grab the data for each image.
				played[tg.id][i]	= $(item);
			});
			
			played[tg.id][0].addClass('onPlay');
			
			if( $('#'+tg.id+' img').length > 1 ) //only do the animation if length more than 1.
				$.transition(tg);

		};
		
		// transitions
		$.transition = function(tg){

			autoInterval[tg.id] = setInterval(function() { $.tp(tg) }, opt[tg.id].delay);		
		};
		
		$.tp = function(tg){
				
				currentpos[tg.id]++;
				if(currentpos[tg.id] == played[tg.id].length ) {
					currentpos[tg.id] = 0;
				}	

				if(currentpos[tg.id] == -1){
					currentpos[tg.id] = played[tg.id].length-1;
				}
				
				$('.onPlay').fadeOut(opt[tg.id].animateSpeed).removeClass('onPlay');
				played[tg.id][currentpos[tg.id]].fadeIn(opt[tg.id].animateSpeed,
					function(){
						played[tg.id][currentpos[tg.id]].addClass('onPlay');
					}
				);
				
		};

		this.each (
			function(){ init(this); }
		);


	};
	
	// default values
	$.fn.WIPslider.defaults = {	
		delay : 6000, // delay between div/object
		animateSpeed : 1000 //speed on amimation per div/object
	};	
	
})(jQuery);
