/* Author: Craig Morey - MindWorks Marketing http://www.mindworks.co.uk */

(function($){
 
 
	$.fn.xVaderFader = function(options){

		var element = this; 

		// options
		var defaults = {
			tag: 'img',
			dwell: 3000,
			duration: 500,
			caption: ''
		};
		var options = $.extend(defaults, options);	
		
		// get a list of photos to rotate through
		var faderAnchors = element.children(options.tag);
		
		// if only one image, exit;
		if (faderAnchors.length < 2) {
			return;
		}
		
		// are we nesting the img inside other
		var nested = false;
		if (options.tag != 'img') {
			nested = true;
		}
		
		
		// set the top image as opacity 1 
		if (Modernizr.csstransitions) {
			faderAnchors.addClass('vaderfade');
			faderAnchors.addClass('vaderfade_zero');
			faderAnchors.first().addClass('vaderfade_hero');
		} else {
			faderAnchors.css('opacity',0);
			faderAnchors.first().animate({'opacity':1} , options.duration);
		}

		go();
		
		function go() {
			

			// loop
			var counter = 0;
			var faderArray = {}
			faderAnchors.each(function() {
			
				// set up the info
				faderArray[counter] = new Array();
				if (nested) {
					faderArray[counter]['url'] = $(this).children('img').first().attr('href');
					faderArray[counter]['title'] = $(this).children('img').first().attr('title');
				} else {
					faderArray[counter]['title'] = $(this).attr('title');
				}
				// set up the z-index
				$(this).css('z-index',faderAnchors.length-counter);
				// counter
				counter++;
			})
					
			// fade up the new caption
			if (options.caption) {
				
				// create a new div
				var innerCaption = options.caption.replace('#','') + '_inner';
				$(options.caption).append('<div id="' +innerCaption + '"></div>');
				// put the text in
				$('#' + innerCaption).html(faderArray[0]['title']);
				// how big is it?
				var fullHeight = $('#' + innerCaption).height();
				// move it back
				$('#' + innerCaption).css({'opacity':0 , 'height':0});
				// animate it
				$('#' + innerCaption).animate({
					'height': fullHeight,
					'opacity': 1
				})
			}
			
			var t = setTimeout ( function() {
			
				// hide the caption
				if (options.caption) {
					$(options.caption + '_inner').first().animate({
						'height': 0,
						'opacity': 0
					});
					
				}
				
				// fade down hero pic, fade up the next pic
				
					// css transform way
					if (Modernizr.csstransitions) {
					
						faderAnchors.first().removeClass('vaderfade_hero');
						faderAnchors.first().next().addClass('vaderfade_hero');
					
					// js way
					} else {		
						
						faderAnchors.first().animate({
							opacity: 0
						} , options.duration);
						faderAnchors.first().next().animate({
							opacity: 1
						} , options.duration);
						
					}
			
				var n = setTimeout ( function() {
					
					// clone the now faded out front pic to the back of the stack
					var oldHero = faderAnchors.first();
					element.append(oldHero);
					
					// reinitialize the fader
					faderAnchors = element.children(options.tag);
				
					$(options.caption + '_inner').remove();
						
					// run again
					go();
					
				},options.duration)
			
			}, options.dwell ); /* time visible */
		}
			
		
	};
 
 
})(jQuery);


