if(jQuery) (function($){

	/**
	 * @param current jQuery object containing visible li#img.. element
	 * @param next jQuery object containing li#img.. element to be shown
	 */
	var anim = false;
	function showElement(current, next)
	{
		if ($(next).css('z-index') >= $(current).css('z-index'))
		{
			var zIndex = $(next).css('z-index');
			$(next).css('z-index', $(current).css('z-index')-1);
		}
		anim = true;
		$(next).show();
		$(current).fadeOut(1500, function() {
			$(next).css('z-index', zIndex);
			anim = false;
		});
	}

	
	
	$.extend($.fn, {
		bapp: function(callback) {
			var images = $(this).find(".home_front");
			var menu = $(this).find("#home_content_buttons li");
			var prevControl = $(this).find(".bappP");
			var nextControl = $(this).find(".bappN");
			var currentIndex = $(menu).size()-1;
			
			$(images).not(':last').hide();
			$(menu).last().addClass('current');
			
			$(images).each(function() {
				$(this).css('z-index', 10 + $(images).index($(this)));
			});
			
			$.loop();
			
			// Binding events
			$(menu).click(function() {
				if (anim) return false;
				var current = $(images).eq(currentIndex);
				var next = $(images).filter("#bapp-" + $(this).attr('value'));
				
				var nextIndex = $(images).index(next);
				if (nextIndex == currentIndex) return false;
				
				currentIndex = nextIndex;
				showElement(current, next);

				$(menu).filter('.current').removeClass('current');
				$(this).addClass('current');
				return false;
			});

			
		}
	});

	$.extend({
		loop: function()
		{
			var curIndex = $("#home_content_buttons li").index($("#home_content_buttons li.current"));
			var modus = $("#home_content_buttons li").size();
			var next = $("#home_content_buttons li").eq((curIndex+1)%modus);
			//alert((curIndex+1)%modus);
			$(next).click();
			setTimeout("$.loop()", 5000);
		}
	});
})(jQuery);
