$(document).ready(function () {
	
});
$(document).keypress(function (evt) {
	var keyCode = null;
	if (evt.which) {
		keyCode = evt.which;
	} else if (evt.keyCode) {
		keyCode = evt.keyCode;
	}
	// 27 = esc
	// 39 = ->
	// 37 = <-

	var inInput = $('input:focus').attr('id');
	var inTextarea = $('textarea:focus').attr('id');

	if (!inInput && !inTextarea) {
		if (keyCode == 27) {
			closeAlbum();
		}
		if (keyCode == 39) {
			if (next_id) {
				showAlbumPic(next_id);
			}
		}
		if (keyCode == 37) {
			if (prev_id) {
				showAlbumPic(prev_id);
			}
		}
	}
});

function closeAlbum() {
	photo_id = 0;
	showMask(1);
	$('#albumClose').fadeOut(300);
	$('#albumOuter').fadeOut(300);
}
var child_id = 0;
var album_id = 0;
var photo_id = 0;
var prev_id = 0;
var next_id = 0;
function showAlbumPic(pic_id) {
	$.getJSON('/album/pic/' + pic_id, function (data) {
		picTitle = data['photo']['name'];
		child_id = data['wall']['child_id'];
		photo_id = data['photo']['id'];
		album_id = (data['album']) ? data['album']['id'] : 0;
		prev_id = data['photo']['prev'];
		next_id = data['photo']['next'];
	
		$('#albumContent').html(data['photo_html'] + '<div class="albumComments">' + data['content'] + '<div>');

		showMask();
		$('#albumClose').fadeIn(300);
		$('#albumOuter').fadeIn(300);

	});
}
var picTitle = '';
function editPhotoTitle(save) {
	if (save == 1) { 
		picTitle = $('#newPicTitle').val();
		var showTitle = (picTitle == '') ? 'Click to add title' : picTitle;
		$('#titlePos').html('<a href="javascript:void(0);" onclick="editPhotoTitle()" title="Click to edit">' + showTitle + '</a>');
		$.post('/profile/' + child_id + '//photos/' + album_id + '/' + photo_id, { 'newtitle': picTitle });
	} else {
		$('#titlePos').html('<form method="post" action="" onsubmit="return editPhotoTitle(1);"><input type="text" id="newPicTitle" value="' + picTitle + '" size="45" maxlength="250" onblur="editPhotoTitle(1);" style="margin-right:5px;" /><a href="javascript:void(0);" onclick="editPhotoTitle(1);">Save</a></form>');
		$('#newPicTitle').focus();
	}
	return false;
}
