var currentInterval = 0;

function attachPhotoMenu( button )
{
	var menu = new PopupMenu( PopupMenu.MODE_BUTTON );
	menu.add( translations['jaiPhotosRemove'], function( target ) 
	{ 
		if( confirm( translations['jaiConfirmDeletePhoto'] ) ) 
		{
			sajax_do_call( "ajaxJRemovePhoto", [ $('#selected_album').attr('rel'), target.rel ], removePhotoComplete );
			setTimeout( 'startProgress()', 100 );
		}
	} );
	menu.add( translations['jaiPhotosSetPreview'], function( target ) 
	{ 
		sajax_do_call( "ajaxJSetAlbumPreview", [ $('#selected_album').attr('rel'), target.rel ], setAlbumPreviewComplete );
		setTimeout( 'startProgress()', 100 );
	} );
	menu.bind( button );
}

function removeAlbum( )
{
	if( confirm( translations['jaiConfirmDeleteAlbum'] ) )
		window.location = window.location + '&action=removeAlbum';

	return false;
}

function saveDescriptions()
{
	// album descriptions
	if( $('#paTitle').hasClass( 'changed' ) || $('#paDescription').hasClass( 'changed' ) )
	{
		sajax_do_call( "ajaxJSetAlbumDescription", [ $('#selected_album').attr('rel'), $('#paTitle').val(), $('#paDescription').val() ], setAlbumDescriptionComplete );
		setTimeout( 'startProgress()', 100 );
	}
	
	// photo captions
	var photos = [];
	var descs = [];
	$('.photo_editor input').each( function() {
		var jqt = $(this);
		if( jqt.hasClass('changed') )
		{
			photos.push( jqt.attr( 'rel' ) );
			descs.push( jqt.val() );
		}
	});
	
	if( photos.length )
	{
		sajax_do_call( "ajaxJSetPhotoDescriptions", [ photos.join( '|' ), descs.join( '|' ) ], setPhotoDescriptionsComplete );
		setTimeout( 'startProgress()', 100 );
	}
	
	return false;
}

function setPhotoDescriptionsComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
	{
		var pics = resp.responseText.trim().split( '||' );
		for( var i = 0; i < pics.length; i++ )
		{
			var tokens = pics[i].split( '|' );
			
			$( '#pa_' + tokens[0].replace( ':', '\:' ) ).attr( 'title', tokens[1] );
			$( '#de_' + tokens[0].replace( ':', '\:' ) ).removeClass( 'changed' );
		}
		fb.activateElements();
	}
	endProgress();
}


function setAlbumDescriptionComplete( resp )
{
	if( resp.responseText.trim() == 'OK' )
		$('#paTitle,#paDescription').removeClass('changed');
	
	endProgress();
}

function removePhotoComplete( resp ) 
{
	if( resp.status == 200 && resp.responseText != '-' )
		$( "#" + resp.responseText.trim().fixForSelector() ).parent().remove();

	endProgress();
}

function setAlbumPreviewComplete( resp ) 
{
	var img = $( '#pi_' + resp.responseText.trim().fixForSelector() );
	var preview = $( '#selected_album_thumb' );
	preview.attr( 'src', img.attr('src'));
	preview.width( img.width() );
	preview.height( img.height() );
	endProgress();
}

function addPhoto( id, src, title, fullimg, width, height )
{
	fb.end();
	sajax_do_call( "ajaxJAddPhoto", [ $('#selected_album').attr('rel'), id, src, title, fullimg ], addPhotoComplete );
	setTimeout( 'startProgress()', 500 );
}

function addPhotoComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
	{
		var tokens = resp.responseText.trim().split( '|' );
		addPhotoThumb( tokens[0], tokens[1], tokens[2], tokens[3] );
	}

	endProgress();
}

function addPhotoThumb( id, src, title, fullimg, rev )
{
	var list = $('#photos_list');
	var li = document.createElement( 'li' );
	
	var photo = document.createElement( 'div' );
	photo.id = id;
	photo.className = 'photo inline_block';

	var a = document.createElement( 'a' );
	a.id = 'pa_' + id;
	a.title = title;
	a.href = fullimg;
	a.rel = 'floatbox.1';
	a.rev = rev;
	photo.appendChild( a );
	var a_photo = a;
	
	var img = document.createElement( 'img' );
	img.src = src;
	img.id = 'pi_' + id;
	img.className = 'shadow';
	a.appendChild( img );
	
	var a = document.createElement( 'a' );
	a.href = '#';
	a.className = 'photo_menu tree_control';
	a.rel = id;
	photo.appendChild( a );
	attachPhotoMenu( a );
	li.appendChild( photo );

	var edit = document.createElement( 'div' );
	edit.className = 'photo_editor';
	
	var input = document.createElement( 'input' );
	input.type = 'text';
	input.value = title;
	input.rel = id;
	input.id = 'de_' + id;
	edit.appendChild( input );
	
	li.appendChild( edit );
	list.append( li );

	fb.activateElements();
}

$( function() {
	$('a.photo_menu').each( function() { attachPhotoMenu( this ); } );
	$('.photo_editor input').change( function() { $(this).addClass( 'changed' ) } );
	$('#paTitle, #paDescription').change( function() { $(this).addClass( 'changed' ) } );
} );
