(function($) {

$.fn.innerfade = function(options) {

	this.each(function(){ 	
		
		var settings = {
			animationtype: 'fade',
			speed: 'normal',
			timeout: 2000,
			type: 'sequence',
			containerheight: 'auto',
			runningclass: 'innerfade'
		};
		
		if(options)
			$.extend(settings, options);
		
		var elements = $(this).children();
	
		if (elements.length > 1) {
		
			$(this).css('position', 'relative');
	
			$(this).css('height', settings.containerheight);
			$(this).addClass(settings.runningclass);

//			alert($('img', elements[0]).attr('alt'));
			
			for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				/*if($('img', elements[i]).attr('alt') == "") {
				        $('a', elements[i]).prepend('<div id=cc'+[i]+' class=textoSmoothTransitionGallery style=height:0px;padding:0px ><div class=tituloSmoothTransitionGallery><b></b><div class=descricaoSmoothTransitionGallery></div></div></div>');
				} else {
				$('a', elements[i]).prepend('<div id=cc'+[i]+' class=textoSmoothTransitionGallery><div class=tituloSmoothTransitionGallery><b>'+$('img', elements[i]).attr('alt')+'</b><div class=descricaoSmoothTransitionGallery>'+$('img', elements[i]).attr('rel')+'</div></div></div>');
			        }*/
				$('#cc'+[i]).hide();
			};
		
			if ( settings.type == 'sequence' ) {
				timer = setTimeout((function(){$.innerfade.hide(settings, 0);}), settings.timeout-500);
				setTimeout(function(){
					$.innerfade.next(elements, settings, 1, 0);
				}, settings.timeout);
				$(elements[0]).show();
				$('#cc'+[0]).animate({
					height: 'show',
					opacity: 'show'
				}, settings.speed+200);
				} else if ( settings.type == 'random' ) {
				timer = setTimeout((function(){$.innerfade.hide(0);}), settings.timeout-500);
				setTimeout(function(){
					do { current = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( current == 0 )
					$.innerfade.next(elements, settings, current, 0);
				}, settings.timeout);
				$(elements[0]).show();
				$('#cc'+[0]).animate({
					height: 'show',
					opacity: 'show'
				}, settings.speed+200);
			}	else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			
		}
		
	});
};


$.innerfade = function() {}
$.innerfade.next = function (elements, settings, current, last) {

	if ( settings.animationtype == 'slide' ) {
		$(elements[last]).slideUp(settings.speed, $(elements[current]).slideDown(settings.speed));
	} else if ( settings.animationtype == 'fade' ) {
		$(elements[last]).fadeOut(settings.speed);
		$(elements[current]).fadeIn(settings.speed);
		$('#cc'+[current]).animate({
			height: 'show',
			opacity: 'show'
		}, settings.speed+200);
	} else {
		alert('animationtype must either be \'slide\' or \'fade\'');
	};
	
	if ( settings.type == 'sequence' ) {
		if ( ( current + 1 ) < elements.length ) {
			current = current + 1;
			last = current - 1;
		} else {
			current = 0;
			last = elements.length - 1;
		};
	}	else if ( settings.type == 'random' ) {
		last = current;
		while (	current == last ) {
			current = Math.floor ( Math.random ( ) * ( elements.length ) );
		};
	}	else {
		alert('type must either be \'sequence\' or \'random\'');
	};
	timer = setTimeout((function(){$.innerfade.hide(settings, last);}), settings.timeout-500);
	setTimeout((function(){$.innerfade.next(elements, settings, current, last);}), settings.timeout);
};

$.innerfade.hide = function (settings, last) {
	$('#cc'+[last]).animate({
		height: 'hide',
		opacity: 'hide'
	}, settings.speed+100);
};

})(jQuery);
