;(function($){
	$.fn.toSlide = function(objs,options) {
		var $this,
			$childs,
			$font,
			time,
			objCurrent,
			zIndex,
			config,
			loaded = 0;
		
		config = {
			time : 3000,
			fadeTime : 500,
			start : 0,
			finalize : false,
			loadedAll : true,
			desc:false,
			tagName : 'IMG',
			description : '.slide-description',
			descCss : {
				color:'white',
				padding:'5px',
				position:'relative',
				fontSize:'12px',
				zIndex:300000
			},
			pdescCss : {
				height:25,
				top:188
			}
		};
		
		if(objs && objs.constructor == Object) {
			options = objs;
			objs = null;
		}
		
		options = $.extend(config,options);
		
		$this = $(this);
		$childs = $this.children();
		
		if(objs && ( objs.constructor != Array 
			|| ( objs.length == 0 && $childs.length < 2) ) ) {
			
			return $this;
		}
		else if(objs && objs.length > 0) {
			// Caso exista uma imagem default
			$font = $childs.eq(0);
			//loaded++;
			$.each(objs,function(i){
				var obj = $font.clone();
				obj.attr(this)
					.hide();
				$this.append(obj);
			});
			
			$childs = $this.children(':not(:first)');
		}
		
		if($childs.length == 0) {
			return $this;
		}
		
		zIndex = $childs.length;
		$childs
			.load(function(){
				if(!options.loadedAll) {
					startSlide();
				}
			})
			.each(function(){
				var o = $(this);
				
				o.css({
					position : 'absolute',
					zIndex   : zIndex--
				});
			});
		objCurrent = 0;
		
		function setDescription(obj) {
			if(!options.desc) return;
			if(!obj || obj.length == 0) return;
			var desc = obj.attr('description');
			if(!desc)
				desc = obj.attr('title');
			$this.find('.slide-description').remove();
			if(typeof desc == 'string' && desc.length > 0) {
				var $overlay = $('<div class="slide-description" style="position:relative;z-index:50000"></div>').css(options.pdescCss || {});
				$overlay.append('<div class="slide-description-text">' + desc + '</div>');
				$overlay.append('<div>&nbsp;</div>');
				$overlay.find('div').eq(0).css(options.descCss || {});
				$overlay.find('div').eq(1).css({opacity:0.5, backgroundColor:'black', height: '100%', width:'100%', position:'absolute', zIndex:'1'});
				$this.append($overlay);
			}
		}
		
		if(options.desc)
			setDescription($childs.eq(0));
		
		function imageTime() {
			if( $childs.lenght <= 1) return;
			
			setTimeout(function(){
				if(options.finalize) {
					$childs.eq(objCurrent).fadeOut(options.fadeTime,function(){
						objCurrent = objCurrent+1;
	
						if( objCurrent >= $childs.length ) {
							objCurrent = 0;
						}
						var obj = $childs.eq(objCurrent);

						setDescription(obj);
						obj.fadeIn(options.fadeTime,imageTime);
					});
				}
				else {
					if(objCurrent+1 >= $childs.length) {

						var obj = $childs.eq(0);
						setDescription(obj);
						
						obj.fadeIn(options.fadeTime,function(){
							$childs.eq(objCurrent).hide();
							objCurrent = 0;
						});
						setTimeout(imageTime,options.fadeTime);
					}
					else {
						$childs.eq(objCurrent).fadeOut(options.fadeTime);
						
						objCurrent += 1;

						var obj = $childs.eq(objCurrent);

						setDescription(obj);
						
						obj.fadeIn(options.fadeTime,imageTime);
					}
				}
			}, options.time);
		}
		
		function startSlide(start) {
			loaded++;
			if(!start && loaded == $childs.length) {
				if($font) {
					$font.remove();
					$childs.eq(0).show();
				}
				setTimeout(imageTime,options.start);
			}
			else if(start) {
				if($font) {
					$font.remove();
					$childs.eq(0).show();
				}
				setTimeout(imageTime,options.start);
			}
		}
		
		if(options.loadedAll) {
			startSlide(true);
		}
		
		return $this;
	};
})(jQuery);