$(document).ready(
function(){ 
	
	th_roller();
	
});
	


function th_roller()
{
	
	//------------------------------------ CONFIG ----------------------------------------------------------------
	// -> conf elements to select
	var ccn = ".mainRotationContent"; //container content name or id
	var cr  = " .oferPos"; // content rotation
	var blocks   = " .miniBlocks";
	//var ccn = "#mRC"; //container content name
	
	// -> switching display time (default)
	var timeOut = 2000; //hide
	var timeIn  = 1000; //show /1000
	
	// -> 
	var childrenIndexed = $( ccn ).children( cr ).size();
	
	
	// ----------------------------------- ACTIONS ---------------------------------------------------------------
	
	//-> show roller
	$(ccn).show();
	
	//-> create link to content & operation on content
	mkBlocks( ccn, blocks, childrenIndexed );
	// -> change class blockLinks on start
	changeClassBlockLinks( $(blocks).children(), $(blocks+" > :first") );
	
	// -> select view
	$(blocks).children('a.block').click( function()
							{ 
								executeTimerRoller( ccn, cr, blocks, childrenIndexed, $(blocks).children().index(this), 100, timeIn ); 
								//alert( $(blocks).children().index(this)+1 ); 
							});
	
	
	//-> find all childs and hide after first one
	$( ccn+cr+":first" ).nextAll().toggle();
	
	
	//-> run timer
	var myTC = timerContentRoller( ccn, cr, blocks, childrenIndexed, timeOut, timeIn );
	$(ccn).mouseover( function(){ clearInterval( myTC ); } );
	$(ccn).mouseout( function(){  myTC = timerContentRoller( ccn, cr, blocks, childrenIndexed, timeOut, timeIn ); } );

}	
	


function mkBlocks( ccn, blocks, childrenIndexed )
{
	if( childrenIndexed > 1 )
	{
		for( var i = 1; i <= childrenIndexed; i++ )
		{
			$( blocks ).append( '<a class="block">'+mkBlocksName(i)+'</a>' );
		}
	}
}
function mkBlocksName(id){
	
	switch(id){
	
	case	1	:	id2 = 'systemy informatyczne';break;
	case	2	:	id2 = 'pobieranie danych z tachografów, GPS';break;
	case	3	:	id2 = 'elektroniczne systemy zabezpieczeń';break;
	default		:	id2;
	
	
	}
	return id2;
}
function changeClassBlockLinks( allChildren, thisChild )
{
	$(allChildren).removeClass("blockON");
	$(thisChild).addClass("blockON").animate({ opacity: '1' });
	
	$(allChildren).not( $(thisChild) ).animate({
	    
	    opacity: '0.5' });
}

function executeTimerRoller( ccn, cr, blocks, childrenIndexed, indexChild, timeOut, timeIn )
{  
	
//	$( ccn+" > "+cr+":visible").fadeOut( 
//			timeOut, function()
//						{ 	
							
							var myThis = $( ccn+" > "+cr+":visible");
							var myThisNext = $(myThis).next();
							
							
							
							var i = $( ccn+" > "+cr ).index(myThisNext) ;
							
							
							if( indexChild == -1 ) //-> autorotation
							{	
								if( i != -1 )
								{
									$(myThis).next().fadeIn(timeIn);
									var indexBlock = $(blocks+">:eq("+i+")");
								}
								else
								{
									$( ccn+" "+cr+":first").fadeIn(timeIn);
									var indexBlock = $(blocks+">:eq(0)");
								}
								
							}
							else //-> indexChild selected
							{	
								$( ccn+" "+cr+":eq("+indexChild+")").fadeIn(timeIn);
								var indexBlock = $(blocks+">:eq("+indexChild+")");
							}
							
							// -> fadeOut
							if( $( ccn+" > "+cr ).index(myThis) != indexChild )
							{
								$( myThis ).fadeOut();
							}
							
							//-> change class blockLinks
							changeClassBlockLinks( $(blocks).children(), indexBlock );
//					} );
}

function timerContentRoller( ccn, cr, blocks, childrenIndexed, timeOut, timeIn )
{ 
	return setInterval( function(){ executeTimerRoller( ccn, cr, blocks, childrenIndexed, -1, timeOut, timeIn ); } , 5000 );
}



