var $current = null;

function select_image($elem) {
	if ($elem.size() == 0) return;
	if (! $elem.is('div.gallery-file-item')) return;
	$current = $elem;
	
	$('#gallery-main img').attr('src', $current.find('a').attr('href'));
	
	$.getJSON(ROOT + 'galleryfile/ajax_get_comments/' + $current.attr('data-id'), function(data) {
		var idx;
		var html = '';
		
		for (idx in data) {
			html += '<div class="comment">'
			html += '<p><b>' + data[idx].name + '</b> <i>' + data[idx].date_added + '</i></p>';
			html += '<p>' + data[idx].text + '</p>';
		}
		
		$('#gallery-comments').html(html);
	});
	
	if ($current.data('rating') == 1) {
		$('#gallery-rate-message').html('<p class="rating-like">You like this!</p>');
		$('#gallery-rate-buttons').hide();
		
	} else if ($current.data('rating') == -1) {
		$('#gallery-rate-message').html('<p class="rating-dislike">You don\'t like this!</p>');
		$('#gallery-rate-buttons').hide();
		
	} else {
		$('#gallery-rate-message').html('');
		$('#gallery-rate-buttons').show();
	}
	
	$('#gallery-miniform-add-comment').show();
	$('#gallery-form-add-comment').hide();
}

$(document).ready(function() {
	$('#gallery-file-list a').click(function() {
		select_image($(this).closest('div.gallery-file-item'));
		return false;
	});
	
	select_image($('#gallery-file-list div.gallery-file-item:first'));
	
	$('#gallery-btn-prev').click(function() {
		select_image($current.prev());
		return false;
	});
	
	$('#gallery-btn-next').click(function() {
		select_image($current.next());
		return false;
	});
	
	$('#gallery-btn-rate-up').click(function() {
		$.post(ROOT + 'galleryfile/ajax_post_rating/' + $current.attr('data-id'), {"rating" : 1}, function(data) {
			if (data.success == 0) {
				alert('Error: ' + data.message);
				return;
			}
			$('#gallery-rate-message').html('<p class="rating-like">You like this!</p>');
			$('#gallery-rate-message').append('<p class="rating-score">' + data.likes + ' likes and ' + data.dislikes + ' dislikes</p>');
			$('#gallery-rate-buttons').hide();
			$current.data('rating', 1);
		}, 'json');
		return false;
	});
	
	$('#gallery-btn-rate-down').click(function() {
		$.post(ROOT + 'galleryfile/ajax_post_rating/' + $current.attr('data-id'), {"rating" : -1}, function(data) {
			if (data.success == 0) {
				alert('Error: ' + data.message);
				return;
			}
			$('#gallery-rate-message').html('<p class="rating-dislike">You don\'t like this!</p>');
			$('#gallery-rate-message').append('<p class="rating-score">' + data.likes + ' likes and ' + data.dislikes + ' dislikes</p>');
			$('#gallery-rate-buttons').hide();
			$current.data('rating', -1);
		}, 'json');
		return false;
	});
	
	$('#gallery-form-add-comment').hide();
	$('#gallery-miniform-add-comment').focus(function() {
		$(this).hide();
		$('#gallery-form-add-comment').show();
		
		var $first = $('#gallery-form-add-comment').find('[name=name]');
		if ($first.val() == '') {
			$first.focus();
		} else {
			$('#gallery-form-add-comment').find('[name=text]').focus();
		}
	});
	
	$('#gallery-form-add-comment').submit(function() {
		$.post(ROOT + 'galleryfile/ajax_post_comment/' + $current.attr('data-id'), $(this).serialize(), function(data) {
			if (data.success == 0) {
				alert('Error: ' + data.message);
				return;
			}
			
			$('#gallery-form-add-comment').slideUp();
			$('#gallery-form-add-comment').find('[name=text]').val('');
		}, 'json');
		return false;
	});
	
	$(document).keypress(function(e) {
		switch(e.keyCode) {
			case 37:  // left 
			select_image($current.prev());
			break;
			
			case 39:  // right
			select_image($current.next());
			break;
		}
	});
});

