var noCaptionsText		= '';
var defaultPhotoDialog	= '';
var removePhotoDialog	= '';

function initGallery()
{
	// Set current index
	photoid = getPhotoID_fromURL();
	
	if (photoid==undefined)
	{
		jsPhotos.current = 0;
	} else {
		jsPhotos.current = getIndex_fromPhotoID(photoid);
	}

	// Position all gallery elements
	var w = parseInt(jQuery('#jsGallery .viewport').width());
	var h = w / 16 * 12;
	
	jQuery('#jsGallery .photoDisplay').height(h);
	jQuery('#jsGallery .container').height(h);
	jQuery('#jsGallery .overlay').css('margin-top', h*-1);
	jQuery('#jsGallery .photoBtn').css('margin-top', h/2-30);
	jQuery('#jsGallery .photoLoad').css('top', h*0.5-9).css('left', w*0.5-27);	

	// Display photo
	displayPhoto(jsPhotos.current);
	
	// Bind photo hover
	jQuery('#jsGallery .photoDisplay').hover(
		function(){
			jQuery('#jsGallery .overlay').fadeIn('fast');
		}, 
		function(){
			jQuery('#jsGallery .overlay').fadeOut('normal');
		});

	// Bind button click
	jQuery('#jsGallery .photoBtn.prev').click(function(){
			displayPhoto(getPrevPhotoIndex());
		});
	
	jQuery('#jsGallery .photoBtn.next').click(function(){
			displayPhoto(getNextPhotoIndex());
		});	
}

function displayPhoto(i)
{	
	// Update current index
	jsPhotos.current = i;
	
	if( !jsPhotos.entry[i] )
		return;

	// Update caption
	var caption = (jsPhotos.entry[i].caption!='') ? jsPhotos.entry[i].caption : noCaptionsText;
	jQuery('#jsGallery .photoCaption h3').text(caption);

	// Show loading indicator;
	jQuery('#jsGallery .photoLoad').show();

	var imagePath = jsPhotos.entry[i].fullsize 
		+ '&maxW='+ jQuery('#jsGallery .container').width() 
		+ '&maxH='+ jQuery('#jsGallery .container').height();

	// Update image
	var img = new Image();
	jQuery(img).load(function () {
		jQuery('#jsGallery .photoImg').fadeOut('fast',
			function() {
				// Load image
				jQuery('#jsGallery .photoImg').attr('src', img.src);
				
				// V-align image // if (iH < cH)
				var iH = img.height; 
				var cH = jQuery('#jsGallery .container').height();
				jQuery('#jsGallery .photoImg').css('top', parseInt((cH-iH)/2));					
				
				// H-align image // if (iW < cW)
				var iW = img.width; 
				var cW = jQuery('#jsGallery .container').width();
				jQuery('#jsGallery .photoImg').css('left', parseInt((cW-iW)/2));
				
				// Display image
				jQuery('#jsGallery .photoImg').fadeIn('fast');
				
				// Hide loading indicator
				jQuery('#jsGallery .photoLoad').hide();
		});
	}).attr('src', imagePath);
	// Set image alt text
	jQuery('#jsGallery .photoImg').attr( 'alt' , caption );	
		
	// Update wall
	var photoid = jsPhotos.entry[i].photoid;	
	switchPhotoTrigger(photoid);
	
	// Update URL anchor
	setPhotoID_toURL(jsPhotos.entry[i].photoid);

	// Update thumbnails
	jQuery('#jsGallery .photoBtn.next img').attr('src', jsPhotos.entry[getNextPhotoIndex()].thumbnail);
	jQuery('#jsGallery .photoBtn.prev img').attr('src', jsPhotos.entry[getPrevPhotoIndex()].thumbnail);
}


function getNextPhotoIndex()
{
	i = parseInt(jsPhotos.current)+1;
	if (i >= jsPhotos.entry.length)
		i = 0;

	return i;
}

function getPrevPhotoIndex()
{
	i = parseInt(jsPhotos.current)-1;
	if (i < 0)
		i = jsPhotos.entry.length-1;

	return i;
}

function setPhotoID_toURL(photoid)
{
	document.location = document.location.href.split('#')[0] + '#photoid=' + photoid;
}

function getPhotoID_fromURL()
{
	var url = document.location.href;
	if (url.match('#') && url.split('#')[1].match('photoid='))
	{
		url = url.split('#')[1];
		if (url.match('&'))
		{
			url = url.split('&')[0];
		}
		return url.split('=')[1];
	}
}

function getIndex_fromPhotoID(photoid)
{
	for (var i=0; i<jsPhotos.entry.length; i++)
	{
		if (jsPhotos.entry[i].photoid == photoid)
			return i;	
	}	
}

function switchPhotoTrigger( photoid )
{
	jax.call( 'community' , 'photos,ajaxSwitchPhotoTrigger' , photoid );
}


function editPhotoCaption()
{
	var caption = jQuery.trim(jQuery('#jsGallery .photoCaption h3').text());
	jQuery('#jsGallery .photoCaption_EditMode input').val(caption);
	
	jQuery('#jsGallery .photoCaption').hide();
	jQuery('#jsGallery .photoCaption_EditMode').show();
}

function cancelPhotoCaption()
{
	jQuery('#jsGallery .photoCaption_EditMode').hide();
	jQuery('#jsGallery .photoCaption').show();
	jQuery('#jsGallery .photoCaption_EditMode input').val('');	
}

function savePhotoCaption()
{
	var caption = jQuery.trim(jQuery('#jsGallery .photoCaption h3').text());
	var newcaption = jQuery.trim(jQuery('#jsGallery .photoCaption_EditMode input').val());

	if (newcaption=='' || newcaption == caption)
	{
		cancelPhotoCaption();
	} else {
		jax.call('community', 'photos,ajaxSaveCaption', jsPhotos.entry[jsPhotos.current].photoid, newcaption);
	}
}

function updatePhotoCaption(photoid, caption)
{
	// Update photo caption
	jQuery('#jsGallery .photoCaption h3').text(caption);
	
	// Update JSON's caption
	jsPhotos.entry[getIndex_fromPhotoID(photoid)].caption = caption;
	
	cancelPhotoCaption();
}


function removePhoto()
{
	if(confirm(removePhotoDialog))
	{
		var i = jsPhotos.current;
		
		var photoid = jsPhotos.entry[i].photoid;
		
		// Remove in DB
		jax.call('community', 'photos,ajaxRemovePhoto', photoid);
		
		// Remove in JSON
		jsPhotos.entry.splice(i,1);
		
		// Check if there's any photos left
		if (jsPhotos.entry.length==0)
		{
			window.location.reload(); /* PHP to display the no photo message */
		} else {
			if(i >= jsPhotos.entry.length )
				i = 0;
				
			displayPhoto(i);
		}
	}
}



function setPhotoAsDefault()
{
	// Get the vars
	var albumid	= jsPhotos.album;
	var photoid = jsPhotos.entry[jsPhotos.current].photoid;

	if(confirm(defaultPhotoDialog))
	{
		jax.call('community', 'photos,ajaxSetDefaultPhoto', albumid, photoid);
	}
}
	
