/**
 * multibox plugin
 * by Rafał Broll
 * for quicknet.pl
 * 30.08.2009
 */

(function($)
{

	$.fn.multibox= function()
	{ 
      $(this).each(function() {
	    	  
    	  //ustawienie maksymalnej wysokości	
    	  		$(this).find('.box-switchable-page-containter .box-switchable-page *').each(function(){
			  			container=$(this).parents('.box-switchable-page-containter');
			      	  if (container.height()<$(this).height()) container.height($(this).height());
			    		});	
    	  		
    	  		container=$(this).find('.box-switchable-page-containter');
    	  		pages=$(this).find('.box-switchable-page-containter .box-switchable-page');
    	  		pages.width(container.width());
    	  		pages.height(container.height());
    	  
    	  	
		        for (i=0; i<$(this).find('.box-switchable-page-containter .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-containter .box-switchable-page').css('z-index',0);
		
		        $(this).find('.box-switchable-page-containter .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 );
		  	  					return false;
		  	  			});
		  	  	
		  	  		
		  	  	
		        $(this).find('.box-switchable-control-nav-next').click(function()
		  						{
		      	  				$(this).parents('.box-switchable').setNextBoxOnTop();
		  						return false;
		  						});
		  	  	
		        $(this).find('.box-switchable-control-nav-prev').click(function()
		  						{
		      	  				$(this).parents('.box-switchable').setPrevBoxOnTop();
		  						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');
		  						}
		  				);
 	
      });
		
	}

	$.fn.setBoxOnTop=function(id)
	{
		$(this).each(function() {
			currentPage=$(this).find('.box-switchable-page-containter .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-containter .box-switchable-page').index(currentPage);
	
			pageToShow=$(this).find('.box-switchable-page-containter .box-switchable-page:eq('+id+')');
			
			$(this).find('.box-switchable-page-containter .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');
	
			currentPage.css('z-index',2);
			
			pageToShow.css('z-index',1);
			
			if (current!=id)
			currentPage.fadeOut(1000, function()  {
														$(this).css('z-index',0);
														$(this).show()
													});
			
			$(this).find('.box-switchable-page-containter .box-switchable-page').removeClass('box-current');
			pageToShow.addClass('box-current');
		});
	}
	
	$.fn.setNextBoxOnTop=function()
	{
		$(this).each(function() {
			currentPage=$(this).find('.box-switchable-page-containter .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-containter .box-switchable-page').index(currentPage);
			count=$(this).find('.box-switchable-page-containter .box-switchable-page').size();
			
			if (current==count-1) id=0;
			else
			id= (current+1) % count;
			$(this).setBoxOnTop(id);
		});
	}
	
	$.fn.setPrevBoxOnTop = function()
	{
		$(this).each(function() {
			currentPage=$(this).find('.box-switchable-page-containter .box-switchable-page.box-current');
			current=$(this).find('.box-switchable-page-containter .box-switchable-page').index(currentPage);
			count=$(this).find('.box-switchable-page-containter .box-switchable-page').size();
			
			if (current==0) id=count-1;
			else
			id = (current-1) % count;
			
			$(this).setBoxOnTop(id);
		});	
	}	
	
	  	
	  	setInterval(function() { $('.box-switchable.box-autoscroll').setNextBoxOnTop();} , 5000);
	
})(jQuery);

(function ($){ 
	$(window).load( function(){ $('.box-switchable').multibox(); } ); 
})(jQuery);
