var nLastPhotoLoadedId = 0;
$(function()
{
	/* load first photo */
	if(default_photo_id == 0)
		default_photo_id = $('#photos_bar img:first').attr('photoid');
	
	cargarFoto(default_photo_id);
	$('#a_view_all_comments').click(function(e)
	{
		e.preventDefault();
		cargarComentarios();
	});
	/* bind minilinks events */
	$('#photos_bar img').click(function(e)
	{
		cargarFoto($(this).attr('photoid'));
	});
});

/*
 * Handler para cuando se guarda un comentario Ver: leave_comments.js
 */
function comments_save_handler()
{
	cargarComentarios(5);
}

function cargarComentarios(nLimit)
{
	if(nLimit == undefined)
		nLimit = 0;
	$.ajax( {
		url: $.sah.ajaxUrl('Index', 'ajax_getComments'),
		data: {
			item_id: nLastPhotoLoadedId,
			table_name: 'fotos',
			limit: nLimit,
			order: nLimit == 0 ? 'ASC' : 'DESC'
		},
		beforeSend: function()
		{
			$('#loading_comments').show();
			$('#last_comments').empty();
		},
		success: function(aComments, st)
		{
			if(aComments.length == 0)
				$('#last_comments').text('No comments');
			else
			{
				if(nLimit > 0)
					aComments.reverse();
				$.each(aComments, function(i, Comment)
				{
					$('#last_comments').append(
						DivComment =
							$('<div>').attr('id', 'comment' + Comment.id).addClass('comment')
									.append($('<b>').text(Comment.autor + ':')).append(
										$('<div>').text(Comment.comment)).append(
										$('<div>').addClass('album_date').text(
											'On ' + Comment.short_date)));
					/* can edit? using foo ninja var -_- jusha!! */
					if(foo)
						DivComment.append('[').append(
							$('<a>').data('comment_id', Comment.id).attr('href', 'javascript:;')
									.text('Delete').click(
										function(e)
										{
											if(confirm('Delete comment?'))
											{
												nCommentId = $(this).data('comment_id');
												$.ajax( {
													url: $.sah.ajaxUrl('Index',
														'ajax_deleteComment'),
													data: {
														comment_id: nCommentId
													},
													success: function(bOk, st)
													{
														if(bOk)
															$('#comment' + nCommentId).slideUp(
																'fast', function()
																{
																	$(this).remove();
																});
													}
												});
											}
										})).append(']');
				});
			}
			// $('#last_comments').show();
		},
		complete: function(Xhr, st)
		{
			$('#loading_comments').hide();
		}
	});
}

function cargarFoto(nFotoId)
{
	if(nLastPhotoLoadedId != nFotoId)
	{
		nLastPhotoLoadedId = nFotoId;
		/* load photo description */
		$.ajax( {
			url: $.sah.ajaxUrl('Photos', 'ajax_loadPhotoData'),
			data: {
				photo_id: nFotoId
			},
			success: function(aData, st)
			{
				$('#photo_description, #photo_date').empty();
				$('#photo_description').html(aData.descripcion.replace(/\n/g,'<br/>'));
				$('#photo_date').text(aData.fecha);
				$('#comments_count').text(aData.comments_count);
				/* item id para el comentario */
				$('#comment_item_id').val(nFotoId);
				if(IS_SHOP_ALBUM)
					$('#shop_item_name').text(aData.shop_item_name);
			}
		});
		
		/* load photo last comments */
		cargarComentarios(5);
		
		/* update #photo src */
		$('#photo').attr('src',
			SAHIB_APP_INFO_PHOTOS_URL + '/' + $('img[photoid=' + nFotoId + ']').attr('alt'));
	}
}
