/**
 * multibox plugin
 * by Rafał Broll
 * for quicknet.pl
 * 6.03.2010
 */

(function($)
{

	$.fn.multibox= function()
	{ 
      $(this).each(function() {
	    	  
    	  //ustawienie maksymalnej wysokości	
    	  		$(this).find('.box-switchable-page-container .box-switchable-page *').each(function(){
			  			container=$(this).parents('.box-switchable-page-container');
			      	  if (container.height()<$(this).height()) container.height($(this).height());
			    		});	
    	  		
    	  		container=$(this).find('.box-switchable-page-container');
    	  		pages=$(this).find('.box-switchable-page-container .box-switchable-page');
    	  		pages.width(container.width());
    	  		pages.height(container.height());
    	  
    	  	
		        for (i=0; i<$(this).find('.box-switchable-page-container .box-switchable-page').size(); i++)
		    			{
		    				prototype = $(this).find('.box-switchable-control .box-switchable-control-button:last');
		    				clone=prototype.clone().insertAfter( prototype );
		    				clone.find('.box-switchable-control-button-label').text((i+1));
		    			}
		  	
		        $(this).find('.box-switchable-control .box-switchable-control-button:first').remove();
		  	  	
		        $(this).find('.box-switchable-control .box-switchable-control-button:first').addClass('akt');
		    	 
		        $(this).find('.box-switchable-page-container .box-switchable-page').css('z-index',0).hide();
		
		        $(this).find('.box-switchable-page-container .box-switchable-page:first').addClass('box-current');
		        $(this).setBoxOnTop(0);
		  	
		        $(this).find('.box-switchable-control .box-switchable-control-button .box-switchable-control-button-label').click(function(){
		  	  					button = $(this).parents('.box-switchable-control-button');
		  	  					index = $(this).parents('.box-switchable').find('.box-switchable-control .box-switchable-control-button').index(button);
		  	  					$(this).parents('.box-switchable').setBoxOnTop( index ).multiboxAutoscrollReset();
		  	  					return false;
		  	  			});
		  	  	
		  	  		
		  	  	
		        $(this).find('.box-switchable-control-nav-next').click(function()
		  						{
		      	  				$(this).parents('.box-switchable').setNextBoxOnTop().multiboxAutoscrollReset();
		  						return false;
		  						});
		  	  	
		        $(this).find('.box-switchable-control-nav-prev').click(function()
		  						{
		      	  				$(this).parents('.box-switchable').setPrevBoxOnTop().multiboxAutoscrollReset();
		  						return false;
		  						});	
		  	  	

		        $('.box-switchable').hover(
		  				function() { 
		  					if ($(this).hasClass('box-switchable')) $(this).removeClass('box-autoscroll');
		  					else $(this).parents('.box-switchable').removeClass('box-autoscroll');
		  					},
		  				function() {
		  						if ($(this).hasClass('box-switchable')) $(this).addClass('box-autoscroll');
		  						else $(this).parents('.box-switchable').addClass('box-autoscroll');
		  						}
		  				);
			
			
			$(this).animate({top:0},5000,null,$.fn.multiboxAutoscroll);
			
      });
		
	}
	
	$.fn.multiboxAutoscroll=function() { 
			box=$(this); if (box.hasClass('box-autoscroll')) 
			box.setNextBoxOnTop();
			box.animate({top:0},5000,null,$.fn.multiboxAutoscroll);
			} 
	
	$.fn.multiboxAutoscrollReset=function() {
			$(this).stop(true,true);
			}
			
	$.fn.setBoxOnTop=function(id)
	{
		$(this).each(function() {

			currentPage=$(this).find('.box-switchable-page-container .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-container .box-switchable-page').index(currentPage);
	
			pageToShow=$(this).find('.box-switchable-page-container .box-switchable-page:eq('+id+')');
			
			if ($(this).hasClass('animating')) 
				{
				currentPage.stop(true,true);
				}
			
			$(this).find('.box-switchable-page-container .box-switchable-page:not(.box-current,:eq('+id+'))').css('z-index',0);
			
			$(this).find('.box-switchable-control .box-switchable-control-button:eq('+id+')').addClass('akt');
			$(this).find('.box-switchable-control .box-switchable-control-button:not(:eq('+id+'))').removeClass('akt');
	

			if (current!=id)
			{
				pageToShow.css('z-index',2);
				
				$(this).addClass('animating');
				
				(function(currentPage) //wykonanie poniższego kodu w kontekście currentPage
				{
				pageToShow.fadeIn(1000, function()  {
															currentPage.css('z-index',0).hide();
															
															$(this).css('z-index',1);
															$(this).parents('.box-switchable').removeClass('animating'); //zdjęcie locka
														});
				})(currentPage);
			}
			else
			{
				pageToShow.css('z-index',1).show();
			}
			$(this).find('.box-switchable-page-container .box-switchable-page').removeClass('box-current');
			pageToShow.addClass('box-current');
		});
		
		return $(this);
	}
	
	$.fn.setNextBoxOnTop=function()
	{
		$(this).each(function() {
			currentPage=$(this).find('.box-switchable-page-container .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-container .box-switchable-page').index(currentPage);
			count=$(this).find('.box-switchable-page-container .box-switchable-page').size();
			
			if (current==count-1) id=0;
			else
			id= (current+1) % count;
			$(this).setBoxOnTop(id);
		});
		
		return $(this);
	}
	
	$.fn.setPrevBoxOnTop = function()
	{
		$(this).each(function() {
			currentPage=$(this).find('.box-switchable-page-container .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-container .box-switchable-page').index(currentPage);
			count=$(this).find('.box-switchable-page-container .box-switchable-page').size();
			
			if (current==0) id=count-1;
			else
			id = (current-1) % count;
			
			$(this).setBoxOnTop(id);
		});

		return $(this);
	}	
	
	  	
	  	
	
})(jQuery);

(function ($){ 
	$(window).load( function(){ $('.box-switchable').multibox(); } ); 
})(jQuery);
