$(document).ready(function(){ 


	$('#topAppGripes #application').uniform();


// GRIPE CONTOLS
/******************************************************/

	// agree
	$("a.agree").click(function(){
		if( $(this).hasClass('disabled') == false ){
			$("#action").val("agree");
			$("#rate_gripe").submit();		
		} else {
			return false;
		}
	});
	
	
	// disagree
	$("a.disagree").click(function(){
		if( $(this).hasClass('disabled') == false ){
			$("#action").val("disagree");
			$("#rate_gripe").submit();
		} else {
			return false;
		}
	});
	
	
	
	// 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;
		}
	});		
	
	
	// 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;
		}
	});			
		
	
		
// CATEGORY FILTER FOR GRIPES
/******************************************************/
	
	// Set the current category on the filter 
	currentApp = $("#current_category").val();
	
	// default page state will have nothing selected
	// so we feed it the 'All apps' option
	if(currentApp == "" || currentApp == "all") {
		currentApp = "all-apps";
		$('#app_browser a.all-apps').addClass('active');
	} else {
		$('#app_browser li.'+currentApp+ ' a').addClass('active');
	}
	
		
	
	
	
	
// RESPONSES
/******************************************************/
	// show/hide responses sections
	$("h2.response-title").click(function(){
		var thisID = $(this).attr("id");
		$("h2 + ol."+thisID).slideToggle();


		if( $(this).hasClass('response-active') == true ){
			$(this).removeClass('response-active');
		} else {
			$(this).addClass('response-active');
		}

	});







// GRIPES SUGGESTION FORM CONTROLS
/******************************************************/

	// uniform select menus in gripe submission box
	$('#category, #version').uniform();


	// if it's an inner page, the user is reading something most likely,
	// so hide the submission form by default
	if($("body").hasClass("inner-page"))
	{
		$("#toggle").addClass("collapsed");
	}


	// show/hide submission form
	$("#toggle").click(function(){
	
		if($(this).hasClass("collapsed"))
		{
			// show the gripe form
			$("#suggest").animate({bottom:0}, 500);
			$(this).removeClass("collapsed");
			
		} else {
			// hide the gripe form
			$("#suggest").animate({bottom:-130}, 500);
			$(this).addClass("collapsed");
		}		
	});
	
	
	// 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;
		}
	});	


	// 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');
 		});
	});


});
