/* =========================================================

// jquery.smoothtransitiongallery.js

// Date: 29/01/2008
// Autor: Leandro D. Soares
// Mail: leandrodsoares@gmail.com

// based on the work of Torsten Baldes http://medienfreunde.com/lab/innerfade/

// ========================================================= */


(function(jQuery) {

jQuery.fn.innerfade = function(options) {

	this.each(function(){ 	
		
		var settings = {
			animationtype: 'fade',
			speed: 'normal',
			timeout: 2000,
			type: 'sequence',
			containerheight: 'auto',
			runningclass: 'innerfade'
		};
		
		if(options)
			jQuery.extend(settings, options);
		
		var elements = jQuery(this).children();
	
		if (elements.length > 1) {
		
			jQuery(this).css('position', 'relative');
	
			jQuery(this).css('height', settings.containerheight);
			jQuery(this).addClass(settings.runningclass);

//			alert(jQuery('img', elements[0]).attr('alt'));
			
			for ( var i = 0; i < elements.length; i++ ) {
				jQuery(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				jQuery('a', elements[i]).prepend('<div id=cc'+[i]+' class=textoSmoothTransitionGallery><div class=tituloSmoothTransitionGallery>'+jQuery('img', elements[i]).attr('alt')+'<div class=descricaoSmoothTransitionGallery>'+jQuery('img', elements[i]).attr('rel')+'</div></div></div>');
				jQuery('#cc'+[i]).hide();
			};
		
			if ( settings.type == 'sequence' ) {
				timer = setTimeout((function(){jQuery.innerfade.hide(settings, 0);}), settings.timeout-500);
				setTimeout(function(){
					jQuery.innerfade.next(elements, settings, 1, 0);
				}, settings.timeout);
				jQuery(elements[0]).show();
				jQuery('#cc'+[0]).animate({
					height: 'show',
					opacity: 'show'
				}, settings.speed+200);
				} else if ( settings.type == 'random' ) {
				timer = setTimeout((function(){jQuery.innerfade.hide(0);}), settings.timeout-500);
				setTimeout(function(){
					do { current = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( current == 0 )
					jQuery.innerfade.next(elements, settings, current, 0);
				}, settings.timeout);
				jQuery(elements[0]).show();
				jQuery('#cc'+[0]).animate({
					height: 'show',
					opacity: 'show'
				}, settings.speed+200);
			}	else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			
		}
		
	});
};


jQuery.innerfade = function() {}
jQuery.innerfade.next = function (elements, settings, current, last) {

	if ( settings.animationtype == 'slide' ) {
		jQuery(elements[last]).slideUp(settings.speed, jQuery(elements[current]).slideDown(settings.speed));
	} else if ( settings.animationtype == 'fade' ) {
		jQuery(elements[last]).fadeOut(settings.speed);
		jQuery(elements[current]).fadeIn(settings.speed);
		jQuery('#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(){jQuery.innerfade.hide(settings, last);}), settings.timeout-500);
	setTimeout((function(){jQuery.innerfade.next(elements, settings, current, last);}), settings.timeout);
};

jQuery.innerfade.hide = function (settings, last) {
	jQuery('#cc'+[last]).animate({
		height: 'hide',
		opacity: 'hide'
	}, settings.speed+100);
};

})(jQuery);
