jQuery.noConflict();

var g_index = 1;
var g_timerCounter = 0;
var g_intervalId = null;

var INTERVAL_MILLISECONDS = 2000;
var IMG_FILENAME_RUNNING_PLAY   = '/img/control_play.png';
var IMG_FILENAME_RUNNING_STOP   = '/img/control_stop_blue.png';
var IMG_FILENAME_RUNNING_REWIND = '/img/control_rewind_blue.png';
var IMG_FILENAME_RUNNING_FFWD   = '/img/control_fastforward_blue.png';
var IMG_FILENAME_STOPPED_PLAY   = '/img/control_play_blue.png';
var IMG_FILENAME_STOPPED_STOP   = '/img/control_stop.png';
var IMG_FILENAME_STOPPED_REWIND = '/img/control_rewind.png';
var IMG_FILENAME_STOPPED_FFWD   = '/img/control_fastforward.png';


function slideshowUpdateImage()
{
	slideshowTimerImageReplacement();

 
	if (0 == g_timerCounter)
	{
		if (count <= (++g_index)) { g_index = 1; }
		
		slideshowMakeAjaxUpdateRequest(g_index);
	}
}


function slideshowMakeAjaxUpdateRequest(index)
{
	g_timeCounter = 0;
	new Ajax.Updater('slideshow','/main/index/'+(index),{asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'slideshow'], onComplete:slideshowTimerImageReplacement});
}


function slideshowImageReplacement()
{
	var slideshow_control_stop = $('slideshow_control_stop');
	var slideshow_control_play = $('slideshow_control_play');
	
	
	if (slideshowIsRunning())
	{
		slideshow_control_stop.src = IMG_FILENAME_RUNNING_STOP;
		slideshow_control_play.src =  IMG_FILENAME_RUNNING_PLAY;
	}
	else
	{
		slideshow_control_stop.src = IMG_FILENAME_STOPPED_STOP;
		slideshow_control_play.src =  IMG_FILENAME_STOPPED_PLAY;
	}
}


function slideshowTimerImageReplacement()
{
	var timer_status = $('timer_status');
	
	
	if (slideshowIsRunning())
	{
		switch(g_timerCounter)
		{
			case 0:
				timer_status.src = '/img/timer_0percent.png';
				++g_timerCounter;
				break;
			case 1:
				timer_status.src = '/img/timer_25percent.png';
				++g_timerCounter;
				break;
			case 2:
				timer_status.src = '/img/timer_50percent.png';
				++g_timerCounter;
				break;
			case 3:
				timer_status.src = '/img/timer_75percent.png';
				++g_timerCounter;
				break;
			case 4:
				timer_status.src = '/img/timer_100percent.png';
				g_timerCounter = 0;
				break;
		}
	}
	else
	{
		timer_status.src = '/img/timer_0percent.png';
		g_timerCounter = 0;
	}
}


function slideshowIsRunning()
{
	return (null != g_intervalId);
}


function slideshowPlay()
{
	if (!slideshowIsRunning())
	{
		g_intervalId = setInterval(slideshowUpdateImage, INTERVAL_MILLISECONDS);
	}
	
	
	slideshowImageReplacement();
	slideshowTimerImageReplacement();
}


function slideshowStop()
{
	if (slideshowIsRunning())
	{
		clearInterval(g_intervalId);
		
		g_intervalId = null;
	}
	
	
	slideshowImageReplacement();
	slideshowTimerImageReplacement();
}


function slideshowRewind()
{
	var isRunning = slideshowIsRunning();
	
	if (isRunning)
	{
		slideshowStop();
	}

	
	g_index = (g_index-1 <= 0) ? (count - 1) : --g_index;
	
	
	slideshowMakeAjaxUpdateRequest(g_index);
	
	
	if (isRunning)
	{
		slideshowPlay();
	}
}


function slideshowFastForward()
{
	var isRunning = slideshowIsRunning();
	
	if (isRunning)
	{
		slideshowStop();
	}
	
	
	g_index = (g_index+1 >= count) ? 1 : ((++g_index) % count);
	
	
	slideshowMakeAjaxUpdateRequest(g_index);
	
	
	if (isRunning)
	{
		slideshowPlay();
	}
}


jQuery(document).ready(
	function()
	{
		++count;
		
		$('slideshow_control_rewind').onclick = slideshowRewind;
		$('slideshow_control_stop').onclick = slideshowStop;
		$('slideshow_control_play').onclick = slideshowPlay;
		$('slideshow_control_fforward').onclick = slideshowFastForward;
		
		slideshowPlay();
	}
);
