/*
 * jQuery Photo Fader Plugin
 * Copyright (c) 2010 Nick Brewer
 * Developer: Nick Brewer	|	nick-brewer.com, geekeemedia.com    |    @brewern	|	brewer.nick@gmail.com
 * Version: 1.0
 * Requires: jQuery v1.3 or later
 
 	DOCUMENTATION & USAGE:
 	This plugin was developed for the purpose of having a selection of photos fade to the next.  Each 
 	photo can have a title which will display as a quoteblock on the image.  You can position the quoteblock
 	anywhere on the image and style accordingly.
 	
 	$('#myID').brewfade({
 		speed: 2000,
		duration: 2000,
		width: '950px',
		height: '313px',
		quotePosTop: 100,
		quotePosRight: 0,
		quotePosBottom: null,
		quotePosLeft: null,
		quoteWidth: '250px'
 	});
 */

;(function($) {
	
	$.fn.brewfade = function(params){
		
		/* --------------------------
		DEFAULT PARAMETERS
		-------------------------- */
		params = $.extend({
			speed: 2000,
			duration: 2000,
			width: '950px',
			height: '313px',
			quotePosTop: null,
			quotePosRight: null,
			quotePosBottom: null,
			quotePosLeft: null,
			quoteWidth: '250px'
		}, params);
		
		return this.each(function() {
			/* --------------------------
			SETUP OBJ VARS AND STAGE
			-------------------------- */
			var fadeTimeout,
				title = [],
				$t = $(this),
				$slides = $t.children();
			$t.addClass('brew-photofader');
			$t.children('div:not(:first)').hide();
			
			/* --------------------------
			SETUP DEFAULT CSS
			-------------------------- */
			$t.css({
				position:	'relative',
				width:		params.width,
				height: 	params.height,
				overflow:	'hidden'
			});
			
			$slides.css({
				position:'absolute',
				left:0,
				top:0
			});
			
			/* -----------------------------
			LOOP THROUGH EACH CHILD [IMAGE]
			----------------------------- */
			$slides.each(function(e){
				title[e] = $slides.find('img').eq(e).attr('title');
				if(title[e]){
					$slides.find('img').eq(e).before('<blockquote>'+title[e]+'</blockquote>');
				}
			});/* END of $slides.each */
			
			/* --------------------------
			BLOCKQUOTE CSS
			-------------------------- */
			if(title){
				$slides.find('blockquote').css({
					position:'absolute',
					top: params.quotePosTop,
					right: params.quotePosRight,
					bottom: params.quotePosBottom,
					left: params.quotePosLeft,
					width: params.quoteWidth
				});
			}
			
			/* ------------------d-------------
			ANIMATION FUNCTION (FADE IMAGES)
			------------------------------- */
			var beginFade = function(){
				fadeTimeout = false;
				$('.brew-photofader div:visible').css('z-index',100);
				$('.brew-photofader div:hidden').css('z-index',99);
				var $active = $('.brew-photofader div:visible'),
					$next = ($('.brew-photofader div:visible').next('div').length > 0) ? $('.brew-photofader div:visible').next('div') : $('.brew-photofader div:first');
				$next.show(); /* DISIBLE NEXT IMAGE FOR VISIBLE BEGINS TO FADE */
				$active.fadeOut(params.speed, function(){
					fadeTimeout = setTimeout(function(){ beginFade() }, params.duration);
				});
			};
			if(!fadeTimeout){
				fadeTimeout = setTimeout(function(){ beginFade() }, params.duration);
			}
			
		}); /* END of EACH */
	}; /* END of BREWFADE FUNCTION */
			
})(jQuery);
