//Contains common Javascript stuff. For Fig Hunter dot com, by Pseudolonewolf, 2011.

//-----------------COMMENT SECTION-----------------\\

minLimit=50;
maxLimit=10000;

function trueLength(str){
	return str.replace(/(<.*?(>|$)|[\s\n]{2,})/g,"").length;
}

function caAdd(box){
	var len=trueLength(box.value);
	if(len<minLimit) alert("That comment is too short! Please elaborate on what you have to say, or don't post at all. Thank you.\n\n(Length: "+len+")");
	else if(len>maxLimit) alert("That comment is too long, and people tend to dislike reading such monstrous posts. Consider saving some of your points for responses?\n\n(Length: "+len+")");
	else return true;
	return false;
}

function caUpdate(box){
	var len=trueLength(box.value);
	$('#'+box.id+'_chars').find('span').removeClass('red').removeClass('green').addClass(len>maxLimit||len<minLimit?'red':'green').html(len);
}

function editPost(cid){
	var cbody=$('#cbody-'+cid);
	var w=parseInt(cbody.css('width'));
	cbody.hide();
	$('#edit-'+cid).show().children('textarea').width(w).height(cbody.height()).focus();
}

function finishEditing(txt,cid){
	var txt=$('#edit-'+cid).children('textarea').val();
	
	if(trueLength(txt)<minLimit){ alert("That's too short!"); return; }
	if(trueLength(txt)>maxLimit){ alert("That's too long!"); return; }

	$('#edit-'+cid).children('textarea').attr('disabled',true);
	$.ajax({
		url:"ajax/edit_comment.php",
		data:{cid:cid, txt:txt},
		type:"POST",
		success:function(msg){
			$('#comment-core-'+cid).replaceWith(msg);
		},
		error:function(jqXHR, textStatus, errorThrown){
			alert("Damn, an error: "+textStatus+", "+errorThrown);
			$('#edit-'+cid).children('textarea').attr('disabled',false);
		}
	});
}

function showReplyBox(event,cid){
	$('#cc_'+cid).slideToggle('normal');
	(event || window.event).stopPropagation();
}

function examineRatings(post_id){
	$('#popup').show().children('.centrer1').children('.centrer2').html("Loading...").load("ajax/examine_ratings.php",{post_id:post_id});
}

function markAsRead(post_id){	
	$('#mar-'+post_id).html("Working...");
	$.ajax({
		url:"ajax/mark_as_read.php",
		data:{post_id:post_id},
		type:"POST",
		success:function(msg){
			$('#comment-'+post_id).fadeOut();
			$('#spacer-'+post_id).fadeOut();
		},
		error:function(msg){
			$('#mar-'+post_id).html("Ugh, error! Please refresh to retry!");
		}
	});
}

function markAllAsRead(){
	if(confirm("Are you sure you want to clear all messages from your inbox? This cannot be undone!")){
		window.location='actions/clear_inbox.php';
	}
}

//-----------------POLLS-----------------\\

function voteOnPoll(t,v){
	$('#poll').replaceWith("<div class='poll tea' id='poll'>Voting...</div>");
	$.ajax({
		url:"actions/vote_on_poll.php",
		data:{t:t,v:v},
		type:"POST",
		success:function(msg){
			$('#poll').replaceWith(msg);
		},
		error:function(jqXHR, textStatus, errorThrown){
			$('#poll').html("Ugh, error! Please refresh to retry! ("+errorThrown+")");
		}
	});
}

//-----------------RATINGS, ETC-----------------\\

function rateThis(post_id,letter){
	$('#ri_'+post_id).replaceWith("<div class='now-rating' id='nr_"+post_id+"'>Now Rating...</div>");
	$.ajax({
		url:"actions/rate.php",
		data:{letter:letter,post_id:post_id},
		type:"POST",
		success:function(msg){
			$('#nr_'+post_id).replaceWith(msg);
		}
	});
}

function affinityPopup(u){
	$('#popup').show().children('.centrer1').children('.centrer2').html("Loading...").load("ajax/affinity_panel.php",{u:u});
}

function setAffinity(u,a){
	$('#popup').show().children('.centrer1').children('.centrer2').html("Changing affinity...");
	$.ajax({
		url:"ajax/affinity_set.php",
		data:{a:a,u:u},
		type:"POST",
		success:function(msg){
			$('#popup').hide();
		}
	});
}

//-----------------STRUCTURAL-----------------\\

//Used for boxes that serve as links.
function linkTo(url){
	window.location=url;
	(event || window.event).stopPropagation();
}
