var stopped = 1;
var direction = 'next';
var play = '';


$(function() {
	$('.slideshow').bind("click", function(){
		playSlideshow(0);
	});		
});	

function showPhoto(direction)
{	
	$.getJSON(
	  	'/ajax/ajax.php',
	  	{
			action: 'gallery',
         	event: 'show_photo',
         	direction: direction,
			module_name: $('#module_name').val(),
			gal_id: $('#gal_id').val(),
			id_cur_photo: $('#id_cur_photo').val()
	  	},
	  	onAjaxSuccess
	);		
	
	function onAjaxSuccess(data)
	{     
		if (data.photo_id)
		{
			$('#id_cur_photo').val(data.photo_id);
			$('#number_photos').html(data.number);				
		}

		src = '/files/gallery/small/gal_' + data.photo_id + '.jpg'
		
		$('#bigphoto').fadeTo("slow", 0.1, function(){
	  		$('#bigphoto img#mainphoto').attr('src', src);
	  		$('#bigphoto').fadeTo("slow", 1)
		});

		$('#photo_description').fadeTo("slow", 0.1, function(){
	  		$('#photo_description').html(data.photo_description);
	  		$('#photo_description').fadeTo("slow", 1)
		});	
		
		showBanner();
	}
}

function showBanner()
{	
	top.frames['banner_placeholder'].location = '/ajax/ajax.php?action=banners&event=show_banner';
}

function playSlideshow(run)
{	
	stopped = run;
	if (stopped == 0)
	{
		$('.slideshow').html('Stop slideshow');
		$('.slideshow').unbind();
		$('.slideshow').bind("click", function(){
  			stopSlideshow();
		});
		showPhoto(direction);
		play = setTimeout("playSlideshow(0)", 5000);			
	}
	else
	{
		clearTimeout(play);
		stopSlideshow();
	}
}

function stopSlideshow()
{	
	stopped = 1;
	clearTimeout(play);
	$('.slideshow').html('Play slideshow');
	$('.slideshow').unbind();
	$('.slideshow').bind("click", function(){
  		playSlideshow(0);
	});		
}
