(function($) {

	$.fn.slider = function(options){
	  
		// default configuration properties
		var defaults = {
			'speed': 1,
			'sleep': 8,
			'auto': true,
			'btOverClass': 'current',
			'width':980,
			'height':260,
			'center':true,
			'visibleSleeper':false,
			'visibleButtons':false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {
			/* CACHING THE IMAGES */
			$(this).find('img').each(function(index, obj){ $(obj).css({'visibility':'hidden'}); });
			
			var SLIDER = $(this);
			var ID = SLIDER.attr('id');
			var CONTAINER = $('<div id="'+ID+'-slider-container" class="slider-container"></div>').appendTo(SLIDER);
			var MEDIA = $('<div id="'+ID+'-slider-media" class="slider-media"></div>').appendTo(CONTAINER);
			var BUTTONS = $('<div id="'+ID+'-slider-buttons" class="slider-buttons"></div>').appendTo(CONTAINER);
			var SLEEP_BAR = $('<div id="'+ID+'-slider-sleep-bar" class="slider-sleep-bar"></div>').appendTo(MEDIA);
			
			var S_WIDTH = options.width;
			var S_HEIGHT = options.height;
			var S_TOTAL = SLIDER.find("img").length;
			var S_LIMIT = S_TOTAL-1;
			var S_CURRENT = S_LIMIT;
			
			CONTAINER.css({'text-align':'center'});
			MEDIA.css({'width':S_WIDTH, 'height':S_HEIGHT, 'margin':'0px auto'});
			
			var buttonsVisibility = options.visibleButtons ? 'visible' : 'hidden';
			BUTTONS.css({'margin':'0px auto', 'visibility':buttonsVisibility});
			
			/* Config the SLEEP BAR */
			var sleeperVisibility = options.visibleSleeper ? 'visible' : 'hidden';
			SLEEP_BAR.css({'position':'absolute', 'bottom':0, 'width':S_WIDTH, 'zIndex':9999, 'opacity':0.5, 'visibility':sleeperVisibility});
			
			/* Config the media container */
			MEDIA.css({'position':'relative'});
			
			/* Move all the images into the media container */
			var oCLass,oLink='';
			$.each($('img', SLIDER).toArray(), function(index, obj){
				$(obj).css({'zIndex':S_TOTAL-index}).attr({'id':ID+'-Item'+index}).appendTo(MEDIA);
				if (/.(?:jpeg|jpg|gif|png)/.test($(obj).attr('title'))) {
					oLink='<img src="'+$(obj).attr('title')+'" height="40" />';
					oClass='imageButton';
				} else {
					oLink='<span>'+(index+1)+'</span>';
					oClass='numberButton';
				}
				$('<a id="'+ID+'-Button'+index+'" class="'+oClass+'">'+oLink+'</a>').appendTo(BUTTONS);
			});

			/* Setup css properties for each image */
			$('img', MEDIA).css({'position':'absolute', 'top':0, 'left':0});
						
			/* When a new image is loaded the counter needs to be initialized */
			var startCounter = function(target) {
				SLEEP_BAR.css({'width':1}).animate({'width':S_WIDTH}, options.sleep*1000, function() { updateItem(target); });
			}
			
			/* Update the current media */
			var updateItem = function(target) {
							
				/* Reset the zIndex Of All the Images */
				$.each(MEDIA.find('img').toArray(), function(zIndex, obj){ $(obj).css({'zIndex':zIndex}); });
				
				/* If the slider reaches its end then jump it to the begining otherwise update the current item */
				if (target == S_TOTAL) { target = 0; }
				
				/* Bring to front the new and the last item */
				var oldMedia = $('#'+ID+'-Item'+S_CURRENT);
				oldMedia.css({'zIndex':S_TOTAL});
				
				var newMedia = $('#'+ID+'-Item'+target);
				newMedia.css({'cursor':'pointer', 'zIndex':S_TOTAL+1});
				//newMedia.css({'cursor':'pointer', 'zIndex':S_TOTAL+1}).click(function(){ });

				/* Update the visual state of the old button */
				var oldButton = $('#'+ID+'-Button'+S_CURRENT);
				oldButton.removeClass(options.btOverClass);
				
				/* Select the current button */
				var newButton = $('#'+ID+'-Button'+target);
				newButton.addClass(options.btOverClass);
				
				/* Update the current item */
				S_CURRENT = target;
				
				/* Fade In the clicked media */
				newMedia.css({'opacity':0, 'visibility':'visible'}).animate({'opacity':1}, options.speed*1000, function(){ startCounter(S_CURRENT+1); });
				
			}

			/* BUTTONS */
			$.each(BUTTONS.find('a'), function(index, item) {
				$(this).bind(
					'click',
					{'index':index},
					function(e){
						$('#'+ID+'-Item'+S_CURRENT).stop(true, false);
						SLEEP_BAR.stop(true, false).css({'width':S_WIDTH});
						updateItem(e.data.index);
					}
				);
			});
			
			/* INITIALIZE ALL THE PROCESS */
			updateItem(0);
			
		});
	  
	};

})(jQuery);
