//==================================================
// system
if (!system){var system={};};
(function($,$$){
//==================================================
// document load

$(function(){
	//--------------------------------------------------
	// bbcode editor
	
	$('.input-bbcode').markItUp(mySettings);
	
	//--------------------------------------------------
	// like a post
	
	$('a.likes').click(function(){
		var postid = $(this).attr('rel');
		var data = {post:postid};
		// run action
		system.action('like_forum_post',data,function(d){
			if (d.success){
				var likebox = $('#post-'+postid).find('a.likes').eq(0);
				var dislikebox = $('#post-'+postid).find('a.dislikes').eq(0);
				likebox.text(parseInt(likebox.text())+1);
				if (d.result=='disliked'){
					dislikebox.text(parseInt(dislikebox.text())-1);
					}
				var likevalue = parseInt(likebox.text());
				var dislikevalue = parseInt(dislikebox.text());
				likebox.removeClass('highest');
				dislikebox.removeClass('highest');
				if (likevalue>dislikevalue){likebox.addClass('highest');}
				else if (dislikevalue>likevalue){dislikebox.addClass('highest');}
				}
			else{
				var message = d.errors.join('\n');
				alert(message);
				}
			});
		return false;
		});
	
	//--------------------------------------------------
	// dislike a post
	
	$('a.dislikes').click(function(){
		var postid = $(this).attr('rel');
		var data = {post:postid};
		// run action
		system.action('dislike_forum_post',data,function(d){
			if (d.success){
				var dislikebox = $('#post-'+postid).find('a.dislikes').eq(0);
				var likebox = $('#post-'+postid).find('a.likes').eq(0);
				dislikebox.text(parseInt(dislikebox.text())+1);
				if (d.result=='liked'){
					likebox.text(parseInt(likebox.text())-1);
					}
				var likevalue = parseInt(likebox.text());
				var dislikevalue = parseInt(dislikebox.text());
				likebox.removeClass('highest');
				dislikebox.removeClass('highest');
				if (likevalue>dislikevalue){likebox.addClass('highest');}
				else if (dislikevalue>likevalue){dislikebox.addClass('highest');}
				}
			else{
				var message = d.errors.join('\n');
				alert(message);
				}
			});
		return false;
		});
	
	//--------------------------------------------------
	// quote a post
	
	$('a.button-quote').click(function(){
		var post = $(this).closest('tr.post');
		var content = post.find('.post-bbcode').eq(0).text();
		var author = post.find('.author-name').eq(0).text();
		$('textarea.input-bbcode').focus();
		$.markItUp({replaceWith:'[quote='+author+']'+content+'[/quote]'});
		});
	
	//--------------------------------------------------
	});

//==================================================
}(jQuery,system));;
//==================================================

