//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 = [];
//this helps generating new unique IDs for each instance
var instanceCounter = 1;

(function($) {

$.fn.innerfade = function(options) {

	this.each(function(){ 	
	
		//generate unique ID for this timer
	    	var instanceId = instanceCounter++; 

		var settings = {
			animationtype: 'slide',
			speed: 1000,
			timeout: 2000,
			runningclass: 'innerfade',
			containerheight: '400px',
			pausePlayButton: 'pausePlayButtonDefault',
			individualSelectorLinksClass: 'navigationDefault'
		};
		
		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;
		if (options.pausePlayButton)
			settings.pausePlayButton= options.pausePlayButton;
		if (options.individualSelectorLinksClass)
			settings.individualSelectorLinksClass= options.individualSelectorLinksClass;
				
		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();
			};
		
			if ($('#' + settings.pausePlayButton).size() != 0){
				for ( var i = elements.length - 1; i >= 0; i-- ) {
					//add individual navigation numbers
					if (i == 0) {
						$('#' + settings.pausePlayButton).before('<div class="' + settings.individualSelectorLinksClass + 'On">' + (i + 1) + '</div>');
					} else {
						$('#' + settings.pausePlayButton).before('<div class="' + settings.individualSelectorLinksClass + 'Off">' + (i + 1) + '</div>');
					}
				}
			}
			
			//always sequence
			current[instanceId] = 1;
			last[instanceId] = 0;
			timer[instanceId] = setTimeout(function(){$.innerfade.next(elements, settings, instanceId);}, settings.timeout);
			$(elements[0]).show();
			
			status[instanceId] = 'on';
			
			//play/pause button click functionality
			$('#' + settings.pausePlayButton).bind('click.innerfade', function(){
				if (status[instanceId] == 'on'){
					clearTimeout(timer[instanceId]);
					status[instanceId] = 'off';
					$('#' + settings.pausePlayButton + ' img').attr('src', '/media/47513/bg_carousel_button_play.jpg');
				}
				else {
					timer[instanceId] = setTimeout(function(){$.innerfade.next(elements, settings, instanceId);}, settings.timeout);
					status[instanceId] = 'on';
					$('#' + settings.pausePlayButton + ' img').attr('src', '/media/47477/bg_carousel_button_pause.jpg');
				}
				return false;
			});

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


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

			//individual/direct button click functionality
			$('div.buttons div.' + settings.individualSelectorLinksClass + 'On, div.buttons div.' + settings.individualSelectorLinksClass + 'Off').bind('click.innerfade', function(){
				//pause automatically!
				clearTimeout(timer[instanceId]);
				status[instanceId] = 'off';
				$('#' + settings.pausePlayButton + ' img').attr('src', '/media/47513/bg_carousel_button_play.jpg');

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

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

				//highlight the right button
				$('div.buttons div.' + settings.individualSelectorLinksClass + 'On').removeClass(settings.individualSelectorLinksClass + 'On').addClass(settings.individualSelectorLinksClass + 'Off');
				$(this).removeClass('' + settings.individualSelectorLinksClass + 'Off').addClass(settings.individualSelectorLinksClass + 'On');
				return false;
			});
		}
		
	});
};


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

	if ( settings.animationtype == 'slide' ) {
		$(elements[last[instanceId]]).hide("slide", { direction: "left"}, settings.speed);
		$(elements[current[instanceId]]).show("slide", { direction: "right" }, settings.speed);
	} else if ( settings.animationtype == 'fade' ) {
		$(elements[last[instanceId]]).hide("fade", { direction: "left" }, settings.speed);
		$(elements[current[instanceId]]).show("fade", { direction: "right" }, settings.speed);
	} else if (settings.animationtype == 'custom'){
		type = $(elements[last[instanceId]]).attr('animationtype');
		if (type == 'fade'){
			$(elements[last[instanceId]]).hide("fade", { direction: "left" }, settings.speed);
			$(elements[current[instanceId]]).show("fade", { direction: "right" }, settings.speed);
		} else {
			//default to slide
			$(elements[last[instanceId]]).hide("slide", { direction: "left", distance: "495" }, settings.speed);
			$(elements[current[instanceId]]).show("slide", { direction: "right" }, settings.speed);
		}
	}

	//highlight the right button
	$('div.buttons div.' + settings.individualSelectorLinksClass + 'On').removeClass(settings.individualSelectorLinksClass + 'On').addClass(settings.individualSelectorLinksClass + 'Off');
	$("div.buttons div").filter(function() {return $(this).text() == (current[instanceId] + 1);}).removeClass(settings.individualSelectorLinksClass + 'Off').addClass(settings.individualSelectorLinksClass + 'On');

	
	//always sequence
	if ( ( current[instanceId] + 1 ) < elements.length ) {
		current[instanceId] = current[instanceId] + 1;
		last[instanceId] = current[instanceId] - 1;
	} else {
		current[instanceId] = 0;
		last[instanceId] = elements.length - 1;
	};

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

