/* =========================================================

// jquery.keyvisualfader.js

// Datum: 2011-11-09
// Firma: kreisform GmbH
// Author: Stephan Günther Romhart
// Mail: s.romhart@kreisform.de
// Web: http://www.kreisform.de

*
*
*
*  

// ========================================================= */


(function($)
{
	var current_id = 0;
	var next_id = 0;
	var next_manual_id = -1;
	var timer_id;
	var fade_out_flag = 0;
	
	
	var settings = {
		'fadeOutTime':500,
		'keyvisualStopTime':3000,
		'centerPager':true
	};
	
	
	var methods = {
	
	
		init:function(options)
		{
			// Settings holen
			if (options)
			{ 
				$.extend(settings,options);
			}
			
			
			//
			var container = this;
			var number_children = container.find('ul li').length;
			
			
			// Keyvisuals-Init
			container.find('ul li').each(function(index)
			{
				$(this).addClass('image-'+index);
				$(this).css('position','absolute');
				$(this).css('z-index',1000 + number_children - index);
				$(this).css('left',0);
				$(this).css('top',0);
				if (index > 1)
				{
					$(this).css('display','none');
				}
			});
			
			
			// Pager-Init
			$(this).append('<ol></ol>');
			for(var i=0; i<number_children; i++)
			{
				$(this).find('ol').append('<li class="pager-'+i+'"><a href="#"><span>'+i+'</span></a></li>');
			}
			$(this).find('ol li a').each(function(index)
			{
				if (index == 0)
				{
					$(this).addClass('active');
				}
				var temp_next = $(this).parent().attr('class').split('-');
				var temp_next_manual_id = parseInt(temp_next[1]);
				$(this).click(function()
				{
					if ((next_id != temp_next_manual_id) && (fade_out_flag == 0))
					{
						next_manual_id = temp_next_manual_id;
						current_id = next_id;
						clearTimeout(timer_id);
						methods.next(container);
					}
					return false;
				});
			});
			
			
			// Pager zentrieren
			if (settings.centerPager)
			{
				var temp_pager_marginLeft = parseInt($(this).find('ol li').css('marginLeft'));
				var temp_pager_left = $(this).width()-(number_children*($(this).find('ol li').width()+temp_pager_marginLeft)-temp_pager_marginLeft);
			}
			
			
			$(this).find('ol').css('left',temp_pager_left/2);
			timer_id = window.setTimeout(function()
			{
				methods.next(container);
			}, settings.keyvisualStopTime);
		},
		
		
		
		
		next:function(container)
		{
			var number_children = container.find('ul li').length;
			
			
			// next_id definieren
			if(next_manual_id > -1)
			{
				next_id = next_manual_id;
				next_manual_id = -1;
			}
			else
			{
				if (current_id == (number_children-1))
				{
					next_id = 0;
				}
				else
				{
					next_id = parseInt(current_id)+1;
				}
			}
			
			
			// Bilder positionieren
			var counter = 0;
			container.find('ul li').each(function(index)
			{
				// Das aktuellen Bild ganz nach oben schiessen, damit er wegfaden kann
				if (index == current_id)
				{
					$(this).css('display','block');
					$(this).css('z-index',1000 + number_children);
				}
				// Den Next direkt dahinter schiessen
				else if (index == next_id)
				{
					$(this).css('display','block');
					$(this).css('z-index',1000 + number_children - 1);
				}
				// alle anderen unsichtbar machen
				else
				{
					$(this).css('z-index',1000 + number_children - counter - 2);
					$(this).css('display','none');
				}
				counter++;
			});
			
			
			// Den Pager-Active steuern
			container.find('ol li a').each(function(index)
			{
				if (index == next_id)
				{
					$(this).addClass('active');
				}
				else
				{
					$(this).removeClass('active');
				}
			});
			
			
			// Den FadeOut zuweisen
			container.find('ul li').each(function(index)
			{
				if (index == current_id)
				{
					fade_out_flag = 1;
					$(this).fadeOut(settings.fadeOutTime,function()
					{
						fade_out_flag = 0;
						timer_id = window.setTimeout(function()
						{
							current_id = next_id;
							methods.next(container);
						}, settings.keyvisualStopTime);
					});
					return false;
				}
			});
		}
	};
	
	
	
	
	$.fn.keyvisualfader = function(method)
	{
		if (methods[method])
		{
			return methods[ method ].apply(this, Array.prototype.slice.call(arguments,1));
		}
		else if(typeof method === 'object' || ! method)
		{
			return methods.init.apply(this,arguments);
		}
		else
		{
			$.error('Method '+ method+' does not exist on jQuery.keyvisualfader');
		}    
	};
})(jQuery);
