var timeOut = 1000;
var timeOutFd = 500;			
var timeOutShow = 14000;
var timeOutLongShow = 5000;
var timeOutRun = 500;

var imagesList = null;			
var timeoutobject = null;	
var prevIndex = null;			
var currentIndex = 0;
var item = null;


$(function() {					
	
	
	imagesList = $('#mineslider .imageDiv');		
	if(imagesList.length < 1)return;	
	rotate();	

	
	$('#switchImages li a').click( function()
	{
		item = $(this);	
		
		if(item[0].id!="pause"){clearCurrentClass();}
		
		item.parent().addClass('current');
				
		switch(item[0].id)
		{
			case 'pause':
				pause();
			break;	

			case 'play':
				play();
			break;	
			
			case 'play':
				play();
			break;	
			
			case 'prev':
				displayPrev();
			break;	
			
			case 'next':
				displayNext();
			break;							
			
			default:						
				var gotoIndex  =  item[0].id.match(/^button([0-9]+)$/);
				gotoIndex = parseInt(gotoIndex[1])-1;	
				displayFrame(gotoIndex);						
			break;
			
		}				
	
	});

	
});


function rotate(  )
{				
	prevIndex = setPrevIndex();							
	if(timeoutobject != null){	fadeImageOut(prevIndex);	}	
	fadeImageIn( timeOutShow );
}

function displayPrev()
{
	clearTimeOut();									
	if(prevIndex == null)
	{						
		prevIndex = 0;
		currentIndex = imagesList.length-1;
	}
	else
	{					
		prevIndex = currentIndex-1;
		currentIndex = prevIndex-1;
		
		if( currentIndex < 0)
		{
			currentIndex = imagesList.length-1;
		}						
	}
	
	fadeImageOut(prevIndex);	
	fadeImageIn(timeOutLongShow);			
	
}

function displayNext()
{
	clearTimeOut();									
	if(prevIndex == null)
	{						
		prevIndex = currentIndex;
		currentIndex = 1;
	}
	else
	{					
		prevIndex = currentIndex-1;										
		if( currentIndex >= imagesList.length)
		{
			currentIndex = 0;
		}						
	}
	
	fadeImageOut(prevIndex);	
	fadeImageIn(timeOutLongShow);			
}

function displayFrame( number )
{					
	clearTimeOut();					
	var fadeout = true;
	if((currentIndex-1)==number)
	{					
		fadeout = false;
	}				
	prevIndex = currentIndex-1;
	currentIndex=number;				
	if(prevIndex>=0 && fadeout)
	{				
		fadeImageOut(prevIndex);					
	}				
	fadeImageIn( timeOutLongShow );
	
}

function setPrevIndex()
{
	var prevIndex = 0;
	if(!imagesList[currentIndex]){				
		prevIndex = currentIndex-1;
		currentIndex=0;					
	}
	else {	prevIndex = (!imagesList[currentIndex-1])?prevIndex:currentIndex-1; }
	return prevIndex;
}

function fadeImageIn( timeOutSet )
{
	$('#'+imagesList[currentIndex].id).fadeIn(timeOutFd);
	currentIndex++;	
	clearCurrentClass();		
	$('#pos'+(currentIndex)).addClass('current');		
	timeoutobject = setTimeout( rotate, timeOutSet );		
}

function fadeImageOut(prevIndex)
{				
	
	if(imagesList[prevIndex]!=undefined)
	{					
		$('#'+imagesList[prevIndex].id).fadeOut(timeOutFd);						
	}
	else
	{
		$('#'+imagesList[prevIndex-1].id).fadeOut(timeOutFd);	
	}
}

function pause()
{				
	clearTimeOut();
}

function play()
{					
	clearTimeOut();
	setTimeout(rotate,timeOutRun);
}

function clearCurrentClass()
{
	var liels = $('#switchImages li');
	for (var a=0; a<liels.length; a++){
		$('#'+liels[a].id).removeClass('current');
	}
}

function clearTimeOut()
{
	if(timeoutobject!=null)
	{				
		clearTimeout(timeoutobject);					
	}			
}