//global timer variable, holds the times instance
var timer;
//global play status; 'on' means currently playing, 'off' means not playing
var status;
//the 'current' element in the sequence. This is from the view of the timer who will fire next time, turning off the 'last' element and showing the 'current' element; so actually 'current' is the
//next element to show!
var current;
var last;

(function($) {

$.fn.innerfade = function(options) {

	this.each(function(){ 	
		
		var settings = {
			animationtype: 'slide',
			speed: 1000,
			timeout: 2000,
			runningclass: 'innerfade',
			containerheight: '400px'
		};
		
		if (options.animationtype)
			settings.animationtype = options.animationtype;
		if (options.timeout)
			settings.timeout = options.timeout;
		if (options.speed)
			settings.speed = options.speed;
		if (options.containerheight)
			settings.containerheight = options.containerheight;
				
		var elements = $(this).children();
	
		if (elements.length > 1) {
		
			$(this).css('position', 'relative');
	
			$(this).css('height', settings.containerheight);
			$(this).addClass(settings.runningclass);
			
			for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				$(elements[i]).hide();
			};
		
			//for ( var i = elements.length - 1; i >= 0; i-- ) {
			//	//add individual navigation numbers
			//	$('#previousButton').after('<a href="#" class="navigation">' + (i + 1) + '</a>&nbsp;|&nbsp;');
			//	if (i == 0){
			//		$('#previousButton').after('&nbsp;|&nbsp;');
			//	}
			//}
			
			//always sequence
			current = 1;
			last = 0;
			timer = setTimeout(function(){$.innerfade.next(elements, settings);}, settings.timeout);
			$(elements[0]).show();
			
			status = 'on';
			
			//play/pause button click functionality
			$('#pausePlayButton').bind('click.innerfade', function(){
				if (status == 'on'){
					clearTimeout(timer);
					status = 'off';
					$('#pausePlayButton').html('Play');
				}
				else {
					timer = setTimeout(function(){$.innerfade.next(elements, settings);}, settings.timeout);
					status = 'on';
					$('#pausePlayButton').html('Pause');
				}
				return false;
			});

			//prev button click functionality
			$('#previousButton').bind('click.innerfade', function(){
				//pause automatically!
				clearTimeout(timer);
				status = 'off';
				$('#pausePlayButton').html('Play');
				//set the new current/last values
				current = last;
				if ( ( last - 1 ) < 0 ) {
					last = elements.length - 1;
				} else {
					last = last - 1;
				};
				
				//show the correct image
				$(elements[current]).hide();
				$(elements[last]).show();
				return false;
			});


			//next button click functionality
			$('#nextButton').bind('click.innerfade', function(){
				//pause automatically!
				clearTimeout(timer);
				status = 'off';
				$('#pausePlayButton').html('Play');
				
				//hide the current image
				$(elements[last]).hide();
				$(elements[current]).show();
				
				//set the new current/last values
				last = current;
				if ( ( current + 1 ) < elements.length ) {
					current = current + 1;
				} else {
					current = 0;
				};
				return false;
			});

			//individual/direct button click functionality
			$('a.navigation').bind('click.innerfade', function(){
				//pause automatically!
				clearTimeout(timer);
				status = 'off';
				$('#pausePlayButton').html('Play');

				//show the correct image
				$(elements[last]).hide();

				//the new last is the selected value
				last = $(this).html() - 1;
				
				//set the new current value
				if ( ( last + 1 ) < elements.length ) {
					current = last + 1;
				} else {
					current = 0;
				};
				
				//show the correct image
				$(elements[last]).show();
			});
		} else {
			$('#previousButton').hide();
			$('#nextButton').hide();
		}
		
	});
};


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

	if ( settings.animationtype == 'slide' ) {
		$(elements[last]).hide("slide", { direction: "left" }, settings.speed);
		$(elements[current]).show("slide", { direction: "right" }, settings.speed);
	} else if ( settings.animationtype == 'fade' ) {
		$(elements[last]).hide("fade", { direction: "left" }, settings.speed);
		$(elements[current]).show("fade", { direction: "right" }, settings.speed);
	} else if (settings.animationtype == 'custom'){
		type = $(elements[last]).attr('animationtype');
		if (type == 'fade'){
			$(elements[last]).hide("fade", { direction: "left" }, settings.speed);
			$(elements[current]).show("fade", { direction: "right" }, settings.speed);
		} else {
			//default to slide
			$(elements[last]).hide("slide", { direction: "left" }, settings.speed);
			$(elements[current]).show("slide", { direction: "right" }, settings.speed);
		}
	}
	
	//always sequence
	if ( ( current + 1 ) < elements.length ) {
		current = current + 1;
		last = current - 1;
	} else {
		current = 0;
		last = elements.length - 1;
	};

	timer = setTimeout((function(){$.innerfade.next(elements, settings);}), settings.timeout);
	
};
})(jQuery);

