var $ = jQuery;
var ge_comment_html = '<li class="hidden"><div class="wire-post-metadata"></div><div class="ge-comment-content"></div>';
var ge_comment_loading = false;

$(document).ready(function() {
	// like case solution
	$('.ge-like').click(function() {
		var wire_id = $(this).attr('href');
		
		$(this).parent().fadeOut();
		
		$.post(ge_case_url +'?action=like', {wire_id: wire_id}, function(data, status) {
			if(status != "success" || data == "false") {
				alert('Failed.');
				return false;
			}

			if(data == "1")
				$('#ge-like-'+ wire_id +' p.text').text(data +' person gillar detta.');
			else
				$('#ge-like-'+ wire_id +' p.text').text(data +' personer gillar detta.');
			
			$('#ge-like-'+ wire_id).fadeIn();
		});
		
		return false;
	});
	
	// comments
	$('.ge-comments li:first').addClass('first');
	
	$('.ge-comment').click(function() {
		var wire_id = $(this).attr('href');
		
		$('#ge-comment-'+ wire_id).slideToggle();
		
		return false;
	});
	
	$('.ge-comment-submit').click(function() {
		var textarea = $(this).prev();
		
		var wire_id = $(this).prev().attr('id');
		var text = $(textarea).val();
		
		if(ge_comment_loading === true)
			return false;

		ge_comment_loading = true;
		
		var first_li = $(this).parent().parent().find('li.form');
		var comment = $(ge_comment_html).insertAfter($(first_li));
		
		$.post(ge_case_url +'?action=comment', {wire_id: wire_id, comment: text}, function(data, status) {
			if(status != "success") {
				alert('Failed.');
				return false;
			}
			
			$(comment).find('.wire-post-metadata').text(data.metadata);
			$(comment).find('.ge-comment-content').text(data.ge_comment_text);
			
			$(textarea).val('');
			
			$(comment).show("fast");
			
			
			
			ge_comment_loading = false;
		}, "json");
		
		return false;
	});
});