var current_photo = 0;
var current_page = 1;
var total_pages = 0;
var total_photos = 0;
var play_status = true;
var thumbs_mode =  true;
var slide_show_timeout = 10000;

$('document').ready(function() {

	$('#thumbs_mode').click(function(){
		setThumbsMode();
	});

	$('#photo_previous').click(function(){

		if (thumbs_mode && current_page == 1) {
		//	return false;
			current_page = total_pages+1;
		}
		else if (!thumbs_mode && current_photo == 0) {
		//	return false;
			current_photo = total_photos;
		}

		current_page -= 1;
		current_photo -= 1;

		if (thumbs_mode) {
			loadThumbs (4, gallery_id, current_page, 'previous');
		} 
		else {
			play_status = false;
			$('#nav_button').attr('src', webroot +'img/ensaios_img_proxima.jpg');
			loadPhoto (gallery_id, current_photo, 'previous');
		}
	});

	$('#photo_next').click(function(){

		if (thumbs_mode && current_page == total_pages) {
		//	return false;
			current_page = 0;
		}
		else if (!thumbs_mode && (current_photo+1) == total_photos) {
		//	return false;
			current_photo = -1;
		}

		current_page += 1;
		current_photo += 1;

		if (thumbs_mode) {
			loadThumbs (4, gallery_id, current_page, 'next');
		} 
		else {
			play_status = false;
			$('#nav_button').attr('src', webroot +'img/ensaios_img_proxima.jpg');
			loadPhoto (gallery_id, current_photo, 'next');
		}

	});

	$('#photo_play').click(function(){

		if (play_status) {
			play_status = false;
			$('#nav_button').attr('src', webroot +'img/ensaios_img_proxima.jpg');
		}
		else 
		{
			play_status = true;
			$('#nav_button').attr('src', webroot +'img/pause_button.jpg');
			
			if (thumbs_mode) {
				current_photo = 0;
			}
			
			showPhoto(current_photo);
		}
		
	});

});

function setThumbsMode ()
{
	setGalleryInfo();

	thumbs_mode = true;
	play_status = false;

	$('#txt_ensaios_thumbs').show();
	$('#fotos_detalhes').hide();
	$('#nav_button').attr('src', webroot +'img/ensaios_img_proxima.jpg');

	current_page = 1;
	loadThumbs (4, gallery_id, current_page);
}

function slideShow ()
{
	if (play_status) 
	{
		if (current_photo+1 == total_photos) {
		//	return false;
			current_photo = -1;
		}

		current_photo += 1;
		
		loadPhoto(gallery_id, current_photo);
		setTimeout('slideShow()', slide_show_timeout);
	}
}

function loadThumbs (limit, gallery_id, page, direction)
{
	var thumbs_url = webroot + 'photos/thumbs/'+ limit;
	var thumbs_container_id = '#fotos_thumbs';

	if (gallery_id != undefined) 
	{
		thumbs_url += '/'+ gallery_id;
		thumbs_container_id = '#fotos_thumbs_baixa';
	}
	
	if (page != undefined) {
		thumbs_url += '/'+ page;
	}
	if (direction != undefined) {
		thumbs_url += '/'+ direction;
	}

	$('#nav_current').html(current_page);

	$.get(thumbs_url, function(data){

		$(thumbs_container_id).fadeOut(500, function(){
			$('#ul_fotos_thumbs').html(data);
			$('#fotos_thumbs_baixa').show();
			$(thumbs_container_id).fadeIn(1500);
		});
		
	});
}

function loadPhoto (gallery_id, position, direction)
{
	var url = webroot +'photos/gallery/'+ gallery_id;

	if (position != undefined) {
		url += '/'+ position;
	}

	if (direction != undefined) {
		url += '/'+ direction;
	}

	$('#nav_current').html(current_photo+1);

	$.getJSON(url, function(data){

		if (data.Photo == undefined) {
			return false;
		}

		$('#fotos_detalhes').fadeOut(500, function(){

		//	$('#foto_detalhe').attr('src', webroot +'img/loading.gif');
			$('#foto_detalhe').attr('src', data.Photo.image_url);
			$('#foto_detalhe').attr('width', data.Photo.width);
			$('#foto_detalhe').attr('height', data.Photo.height);
			$('#fotos_detalhes').fadeIn(2000);
			
		});

	});
}

function showPhoto (position)
{
	current_photo = position;
	setGalleryInfo();

	$('#fotos_thumbs_baixa').hide();
	$('#txt_ensaios_thumbs').hide();

	$('#foto_detalhe').attr('src', webroot +'img/pixel.gif');
	$('#fotos_detalhes').show();

	$('#nav_button').attr('src', webroot +'img/pause_button.jpg');

	thumbs_mode = false;
	play_status = true;

	loadPhoto(gallery_id, current_photo);
	setTimeout('slideShow()', slide_show_timeout);
}

function setGalleryInfo ()
{
	var info_url = webroot + 'galleries/info/'+ gallery_id;

	$.getJSON(info_url, function(data){
		
		if (data.Gallery.picture_count != undefined) 
		{
			total_photos = parseInt(data.Gallery.picture_count);
			total_pages = parseInt(parseInt(data.Gallery.picture_count) / 4);
			
			if (total_photos %4 != 0) {
				total_pages++;
			}
			

			if (total_pages == 0) {
				total_pages = 1;
			}

			if (thumbs_mode) {
				$('#nav_total').html(total_pages);
			} else {
				$('#nav_total').html(total_photos);
			}
		}

	});
}


