if(jQuery) (function($){

	/**
	 * @param current jQuery object containing visible li#img.. element
	 * @param next jQuery object containing li#img.. element to be shown
	 */
	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);
		}
		$(next).show();
		$(current).fadeOut('slow', function() {
			$(next).css('z-index', zIndex);
		});
	}
	
	/**
	 * @param current jQuery object containing active a element
	 * @param next jQuery object containing a element to be shown
	 * @param content HTML contents of current element
	 * @return String containing HTML contents of selected element
	 */
	function makeActive(current, next, content)
	{
		
		if ($(current).hasClass('first'))
		{}//	$(current).css('opacity', 0.01);
		else
			$(current).css({
				//opacity : 0.01,
				height : function(index, value) {
					return parseFloat(value)-1;
				}
			});
		
		$(current).html(content);
		$(current).addClass('unactiv').removeClass('activ');
		/*$(current).animate({opacity : 1}, 400, 'linear', function() {
			$(this).css('opacity', '');
		});*/

		content = $(next).html();
		$(next).removeClass('unactiv').addClass('activ');
		$(next).html('');
		
		if ($(next).hasClass('first'))
			$(next).css('opacity','');
		else
			$(next).css({
				opacity : '',
				height : function(index, value) {
					return parseFloat(value)+1;
				}
			});
		
		/*$(next).animate({opacity : 0.01}, 400, 'linear', function() {
			$(next).removeClass('unactiv').addClass('activ');
			$(next).html('');
			
			if ($(next).hasClass('first'))
				$(next).css('opacity','');
			else
				$(next).css({
					opacity : 1,
					height : function(index, value) {
						return parseFloat(value)+1;
					}
				});
			});*/
		
		return content;
	}
	
	$.extend($.fn, {
		bapp: function(callback) {
			var images = $(this).find("#bappImgHolder li");
			var menu = $(this).find("#bappMenu li");
			var prevControl = $(this).find(".bappP");
			var nextControl = $(this).find(".bappN");
			var currentIndex = 0;
			var activeContent = $(menu).first().children('a').html();
			
			// Setting the height of menu elements
			$(menu).each(function() {
				$(this).children('a').css('height', (401-$(menu).length) / $(menu).length);
			});
			
			$(images).not(':first').hide();
			$(menu).first().children('a').addClass('activ').removeClass('unactiv').html('');
			
			// Binding events
			$(menu).each(function() {
				$(this).children('a').click(function() {
					var current = $(images).eq(currentIndex);
					var next = $(images).filter($(this).attr('href'));
					
					var nextIndex = $(images).index(next);
					if (nextIndex == currentIndex) return false;
					
					currentIndex = nextIndex;
					showElement(current, next);

					var currentA = $(menu).has('a.activ').children('a');
					activeContent = makeActive(currentA, $(this), activeContent);
					return false;
				});
				$(this).children('a').hover(function() {
					
					var next = $(images).filter($(this).attr('href'));
					var nextIndex = $(images).index(next);
					var background = next.css('background-image');
					var bgposition = parseFloat($(this).css('height')) * nextIndex;
					$(this).css('background-image', background);
					$(this).css('background-position', '0 -' + bgposition + 'px');
					
				}, function() {
					$(this).css('background-image', '');
					$(this).css('background-position', 'left top');
				});
			});
			$(prevControl).add(nextControl).click(function() {
				var nextIndex;
				if ($(this).attr('href') == 'p')
				{
					nextIndex = (currentIndex == 0) ? $(images).length-1 : currentIndex-1; 
				}
				else
				{
					nextIndex = (currentIndex == $(images).length-1) ? 0 : currentIndex+1;
				}
				$(menu).eq(nextIndex).children('a').click();
				return false;
			});
		}
	});
})(jQuery);
