$(document).ready(function() {

	$('a.voteup').click(function() {
		var post = $(this).attr('rel');
		$.get('/js/actions.php', { action : 'voteup', param : post }, function(data)
			   {
					$('h2.message').remove();	   
					$('#confession-' + post).append('<h2 class="message">' + data + '</h2>');
					$('h2.message').fadeOut(2500);
			   });
																		   
		 return false;
	});

	$('a.votedown').click(function() {
		var post = $(this).attr('rel');
		$.get('/js/actions.php', { action : 'votedown', param : post }, function(data)
			   {
					$('h2.message').remove();	   
					$('#confession-' + post).append('<h2 class="message">' + data + '</h2>');
					$('h2.message').fadeOut(2500);
			   });

		return false;
	});
	
	$('#createconfession a#createcancel').click(function() {
		$('div#createconfession').slideUp(1500);
		return false;
	});
	
	$('a#create').click(function() {
		$('div#createconfession').slideDown(1000);
		return false;									 
	});

	$('#searchbar a#searchcancel').click(function() {
		$('div#searchbar').slideUp(1000);
		return false;
	});

	$('a#search').click(function() {
		$('div#searchbar').slideDown(1000);
		return false;									 
	});
	
	$('#searchbutton').click(function() {
		var str = $('#searchbar form input').val();
		$('.ajaxloader').show();
		$('#searchbar h3').remove();		
		
		$.post('/js/actions.php', { action : 'search' , param : str }, function(data) {
			$('.ajaxloader').hide();																						
			if (data.status == 'error') {
				$('#searchbar h3').remove();
				$('#searchbar').append('<h3>' + data.string + '</h3>');			
			} else if (data.status == 'success') {
				$('#searchbar h3').remove();				
				$('#content h2').remove();
				$('div.pagination').hide();
				$('#confessions').html(data.html);
				eval(data.script);
			}
		}, "json");

		return false;
	});
	
	$('#createbutton').click(function() {
		var confession = $('#createconfession form textarea').val();
		var challengestr = $('#recaptcha_challenge_field').val();
		var responsestr = $('#recaptcha_response_field').val();

		$('#createconfession h3').remove();	
		$('#createconfession').append('<h3><img src="/images/ajax-loader.gif" /></h3>');
		
		$.post('/js/actions.php', { action : 'create' , param : confession, challenge : challengestr, response : responsestr }, function(data) {
			if (data.status == 'error') {
				$('#createconfession h3').remove();
				$('#createconfession').append('<h3>' + data.string + '</h3>');
			} else if (data.status == 'success') {
				$('div#createconfession').hide();
				window.location = data.url;
			}
																																		 		}, "json");
																																		
		return false;
	});

	function editfunction() {
		var post = $(this).attr('rel');

		var text = $('div#confession-' + post + ' .confession-body p').html();
		$('div#confession-' + post + ' .confession-body p').html('<textarea cols="87" rows="12">' + br2nl(text) + '</textarea>');

		$(this).text('save');
		$(this).unbind('click');

		$(this).click(function() {
			var newtext = $('div#confession-' + post + ' .confession-body p textarea').val();
			$('div#confession-' + post + ' .confession-body p').html(nl2br(newtext));

			$.post('/js/actions.php', { action : 'edit', param : post, param2 : newtext }, function(data) {
				$('h2.message').remove();          
				$('#confession-' + post).append('<h2 class="message">' + data + '</h2>');
				$('h2.message').fadeOut(2500);
			});


			$(this).unbind('click');
			$(this).text('edit');
			$(this).bind('click', editfunction);
			return false;
		});

		return false;
	};

	$('a.editbutton').click(editfunction);

	function br2nl(str) {
		str = str.replace(/<br\s*\/?>/mg,"");
		return str.replace(/<BR\s*\/?>/mg,"");
	};

	function nl2br(str) {
		return str.replace(/\n/mg,"\n<br />");
	};

});
