$(document).ready(function(){ 

	// character limit check on a textarea
	function limitChars(textid, limit, infodiv)
	{
		var text = $('#'+textid).val(); 
		var textlength = text.length;
	
		if(textlength > limit)
		{
			// user is over the limit, let them know as such
			$('.' + infodiv).html('Character limit reached');
	 		$('#' + textid).val(text.substr(0,limit));
			return false;
		} else {
			// update the amount of characters left
			$('.' + infodiv).html((limit - textlength) +' characters left');
			return true;
		}
	}
	
	// attach text-limit check to textarea
	$(function()
	{
		$('#sugg_gripe').keyup(function()
		{
			limitChars('sugg_gripe', 200, 'helper-text');
 		});
	});




	// agree
	$("a.loveIt").click(function(){
	
		/* Old ajax request--no longer used
		gripeID = $(this).attr("id");
		
		$("#result").load("loveIt.php", {id: gripeID}, function() { 
			$("#controls").hide();
			window.location = "index.php";
		});*/
		
		$("#action").val("agree");
		$("#rate_gripe").submit();
		
	});
	
	
	// disagree
	$("a.hateIt").click(function(){
	
		/* Old ajax request--no longer used
		gripeID = $(this).attr("id");
		
		$("#result").load("hateIt.php", {id: gripeID}, function() { 
			$("#controls").hide();
			window.location = "index.php";
		});*/
		
		$("#action").val("disagree");
		$("#rate_gripe").submit();

	});
	
	


	// reply: agree
	$("a.love-reply").click(function(){
	
		var replyID = $(this).attr("id");
	
		$("span#"+ replyID +".reply-controls").load("love_reply.php", {id: replyID}, function() { 
			// do nothing
		});

	});
	
	
	// reply: disagree
	$("a.hate-reply").click(function(){
	
		var replyID = $(this).attr("id");
	
		$("span#"+ replyID +".reply-controls").load("hate_reply.php", {id: replyID}, function() { 
			// do nothing
		});

	});
	
	
	// flag as inappropriate
	$("a.flag").click(function(){
	
		// confirm they want to flag this for mod review
		var answer = confirm ("Flagging this entry will remove it from the rotation for a moderator to review.\n\n Are you sure you want to flag this entry?");
		
		if(answer)
		{
			var gripeID = $(this).attr("id");

			$("#response").load("flag.php", {id: gripeID}, function() { 
				// what the hell
			});
	
			// it's done and hide the control for flagging
			alert("This entry has been flagged for moderator review.");
			$(this).fadeOut();

		} else {
			// user chose not to flag the entry
			return false;
		}
	});		
	

	// inaccurate
	$("a.inaccurate").click(function(){
	
		/* Old ajax request--no longer used
		gripeID = $(this).attr("id");
		
		$("#result").load("inaccurate.php", {id: gripeID}, function() { 
			$("#controls").hide();
			window.location = "index.php";
		});*/

		$("#action").val("inaccurate");
		$("#rate_gripe").submit();

	});	
	
	
	// categorize gripe
	$("#submitApplication").click(function(){
				
		// check that an app has been chosen
		if($("#application").val() != 0) {
			
			// get gripeID and applicationID			
			gripeID = $("input#id").val();
			applicationID = $("#application").val();
						
			// when the query has run, show the thank you and then fade out
			$("#categorize").load("categorize.php", {id: gripeID, application: applicationID}, function() { 
				//$("#categorize").hide();
			})
			.animate({opacity: 1.0}, 3000)
    		.fadeOut('fast', function() {
				$(this).remove();
			});

		} else {
			// notify the user to correct the problem
			alert("Please select what application this gripe pertains to.");
			
			// prevent form submission
			return false;
		}
	});
	
	
	// re-categorize gripe
	$("#reSubmitApplication").click(function(){
				
		// check that an app has been chosen
		if($("#application").val() != 0) {
			
			// get gripeID and applicationID			
			gripeID = $("a.permalink").attr("id");
			applicationID = $("#application").val();
			
			// when the query has run, show the thank you and then fade out
			$("#recategorize").load("categorize.php", {id: gripeID, application: applicationID}, function() { 
				//$("#categorize").hide();
			//});
			//.animate({opacity: 1.0}, 3000)
    		//.fadeOut('fast', function() {
			//	$("#incorrect").removeClass("engaged").text("Incorrect?");			
			});

		} else {
			// notify the user to correct the problem
			alert("Please select what application this gripe pertains to.");
			
			// prevent form submission
			return false;
		}
	});			
	
	
	
	// add a reply to the gripe
	$("#submit_reply").click(function(){
						
		// check that an app has been chosen
		if($("#add_reply").val() != "") {
			
			// get gripeID and reply			
			gripeID = $("a.permalink").attr("id");
			reply = $("#add_reply").val();	

			// when the query has run, show the thank you and then fade out
			$("#reply-form").load("reply.php", {gripe_id: gripeID, reply: reply}, function() { 
				// do nothing
			});

		} else {
			// notify the user to correct the problem
			alert("Please enter a reply");
			
			// prevent form submission
			return false;
		}
	});	
		
		
	// suggest we merge some gripes
	$("#gripe_id_2").numeric();
	$("#suggest_merge").click(function(){
					
		// check that an app has been chosen
		if($("#gripe_id_2").val() != "") {
			
			// get gripeID and reply			
			gripe1 = $("#gripe_id_1").val();
			gripe2 = $("#gripe_id_2").val();	

			// when the query has run, show the thank you and then fade out
			$("#merge-form").load("suggest_merge.php", {gripe_id_1: gripe1, gripe_id_2: gripe2}, function() { 
				// do nothing
			});

		} else {
			// notify the user to correct the problem
			alert("Please enter a gripe id #");
			
			// prevent form submission
			return false;
		}
	});			
	
	
	
	
	
	// Incorrect? link controls
	$("#incorrect").click(function(){
							
		if($("#reply-form:visible")) {
			// hide the tab and reset text
			$("#reply").removeClass("engaged").text("Add Reply");
			$("#reply-form").hide();
		}
					
								   
		if($(this).hasClass("engaged"))
		{
			// hide the tab and reset text
			$(this).removeClass("engaged").text("Incorrect?");
			$("#recategorize").fadeOut('fast');
		} else {
			// show the tab and change the text
			$(this).addClass("engaged").text("Close tab");
			$("#recategorize").fadeIn('fast');
		}
	});
	
	
	// Add Reply link controls
	$("#reply").click(function(){
							   
		if($("#recategorize:visible")) {
			// hide the tab and reset text
			$("#incorrect").removeClass("engaged").text("Incorrect?");
			$("#recategorize").hide();
		}
							   
		if($(this).hasClass("engaged"))
		{
			// hide the tab and reset text
			$(this).removeClass("engaged").text("Add Reply");
			$("#reply-form").fadeOut('fast');
		} else {
			// show the tab and change the text
			$(this).addClass("engaged").text("Close tab");
			$("#reply-form").fadeIn('fast');
		}
	});	
	
	
	
	
	
	
	// quick form check for new gripes
	$("#suggest_gripe").submit(function(){
		
		var gripe = $("#sugg_gripe").val();
		
		// check that an app has been chosen
		if($("#category").val() != 0) {
		
			// check that a gripe has been entered
			if(gripe == "" || gripe.length > 200) {
				// notify the user to correct the problem
				alert("Please enter a gripe and make sure it's not over 200 characters");	
				
				// prevent form submission
				return false;
				
			} else {
				// both fields are filled in
				// let the form submission continue
				return true;
			}
		
		} else {
			// notify the user to correct the problem
			alert("Please select what application your gripe pertains to.");
			
			// prevent form submission
			return false;
		}
	});	
	
	// filter controls
	$("#app_browser").change(function(){	
		$("#filter_gripes").submit();
	});
	
	
	
	/* Set the current category on the category filter */
	currentApp = $("#current_category").val();
	
	// default page state will have nothing selected
	// so we feed it the 'All apps' option
	if(currentApp == "")
	{
		currentApp = "All apps";
	}
	
	// for gripes by app page
	$("#application option:contains("+currentApp+")").attr("selected", true);
	
	// for normal gripes cycling
	$("#app_browser option:contains("+currentApp+")").attr("selected", true);
	
	
	// show/hide responses sections
	$("h2.response-title").click(function(){
		
		var thisID = $(this).attr("id");
		
		$("h2 + ol."+thisID).slideToggle();
	});


	// THIS NEEDS TO BE IMPLEMENTED, FLESHED OUT AND TESTED!!!!!
	if($("body").hasClass("inner-page"))
	{
		$("#footer-toggle").text("Submit gripes here!").addClass("collapsed");
	}


	// show/hide submission form
	$("#footer-toggle").click(function(){
	
		if($(this).hasClass("collapsed"))
		{
			// show the gripe form
			$("#suggest").animate({bottom:0}, 500);
			$(this).text("Hide gripe form").removeClass("collapsed");
			
		} else {
			// hide the gripe form
			$("#suggest").animate({bottom:-175}, 500);
			$(this).text("Submit gripes here!").addClass("collapsed");
		}		
	});
	


});