/*
 * feedback.js
 * Brown University
 * CIS Template
 * John Pennypacker
 * John_Pennypacker@brown.edu
 * 2008-06-02
 */



/*
 * The URL that receives all the feedback
 */
var webFeedbackURL = "/web/ownership/feedback-receive";


/*
 * A little snippet of jQuery to kick things off.
 */
$(document).ready(function(){

	$("a[href*=web/feedback]").bind("click", function() { showFeedback("#content"); return false; });
	$("a[href*=web/feedback]").html("Send Feedback");
	

});


function checkMessageLength() {

	if($("#web-feedback-container").find("#too-long-alert")) {
		// reset the message and styles
		$("#too-long-alert").remove();
		$("#web-feedback").css({border:"1px solid #666"});
	}

	if($("#web-feedback").val().length > 255) {
		$("#web-feedback").css({border:"2px solid red"});

		$("#web-feedback").after("<p class='alert' id='too-long-alert' style='color: red; padding: 1em;'><strong>The maximum length for messages sent using this form is 255 characters.  Your message is "+ $("#web-feedback").val().length +" characters long.  Please reduce the length of your message.</strong></p>")

	} 
}



/*
 * Show the feedback form, and change the onclick triggers on relevant links
 *
 * @param	id	The id of the element that the feedback form is appended to.
 */
function showFeedback(id) {

	$("a[href*=web/feedback]").unbind("click");
	$("a[href*=web/feedback]").bind("click", function() { hideFeedback(); return false; });


	var ask = "<div id='web-feedback-container'><h5>Send Instant Feedback about this page</h5><p id='limit-notice'>(255 character limit) For more options, you may also use the <a href='http://www.brown.edu/web/feedback/'>advanced contact form</a>.</p><textarea id='web-feedback' name='web-feedback' rows='5' cols='10'></textarea> <input type='button' id='web-feedback-submit' value='Submit' /> <input type='button' class='cancel' value='Cancel' /></div>";

	if(!document.getElementById("web-feedback-container")) {
		$(id).append(ask);
		$("#web-feedback-container .cancel").bind("click", function() { hideFeedback(); });
		$("#web-feedback-submit").bind("click", sendFeedback);
		$("#web-feedback-container").hide();
	}

	$("#web-feedback-container").slideDown();

	$("#web-feedback").bind("keyup", function() {
		checkMessageLength();
	});

	return false;
}

/*
 * Hide the feedback form, and change onclick triggers back to show form.
 */

function hideFeedback() {
	$("a[href*=web/feedback]").unbind("click");
	$("#web-feedback-container").slideUp();
	$("a[href*=web/feedback]").bind("click", function() { showFeedback("#content"); return false; });
}

/*
 * Fire off an ajax request with the user's feedback
 */

function sendFeedback() {
	var fb = ($("#web-feedback").val());
	
	showFeedbackLoader();

	// do ajax with fb
	$.ajax({
		type: "post",
		url: webFeedbackURL,
		data: { url: window.location.toString(), message: fb, submit: true },
		success: function(txt) { feedbackSuccess(txt) }, 
		error: function(XMLHttpRequest, textStatus, errorThrown) { feedbackFail(textStatus) },
		dataType: "json"
	});
}

/*
 * what to do with the success message
 */
function feedbackSuccess(txt) {
	$("#web-feedback-loader").remove();
	$("#web-feedback-container").append("<p>"+ txt + " <strong>click to dismiss</strong></p>");
	$("#web-feedback-container p").bind("click", function() {
		$("#web-feedback-container").slideUp(500, function() { $("#web-feedback-container").remove(); } );
	});
}

/*
 * something went wrong.
 */
function feedbackFail(txt) {
	$("#web-feedback-loader").remove();
	$("#web-feedback-container").append("<p>"+ txt + " <strong>An error has occurred.  Please use the <a href='/web/feedback'>advanced contact form</a>.  Click to dismiss this message</strong></p>");
	$("#web-feedback-container p").bind("click", function() {
		$("#web-feedback-container").slideUp(500, function() { $("#web-feedback-container").remove(); } );
	});
}

/*
 * show the loader screen (spinner gif)
 */
function showFeedbackLoader() {
	$("#web-feedback-container input").hide();
	$("#web-feedback-container #limit-notice").hide();
	$("#web-feedback-container textarea").hide();
	$("#web-feedback-container").append("<img src='http://www.brown.edu/web/ownership/i/loader.gif' id='web-feedback-loader' alt='' />");
}