// Get Visitor ID from Cookies
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Interface
$(document).ready(function(){
	$('#gaFeedbackComment').hide();
	$('#gaFeedback').show('slow'); // Cool little animation to grab visitors' attention
	var gaLP = document.location.pathname; // The path of the document for the feedback
	var gaFL = 0; // Binary answer to whether or not the visitor liked the page

	$('#gaFeedbackLike').children('input').click(function() {
		var gaFeedbackLike = $('input:radio[name=gaFeedbackLike]:checked').val(); // Get the response for "Like this page?"
		if (gaFeedbackLike = "Yes") {gaFL = 1;} // Set the binary answer for GA based on response
		pageTracker._trackEvent('Did You like it?',gaFeedbackLike,gaLP,gaFL); // Submit response & custom variable through an event
		$('#gaFeedbackComment').show('fast');
		$('#gaFeedbackLike').html('');
	})
	
	$('#gaFeedbackComment').children('button').click(function() {
		var gaFeedbackComment = escape($('input:text[name=gaFeedbackComment]').val()); // Get feedback comment and escape it for transmission
		var gaID = encodeURI(readCookie('__utma')); // Get GA visitor identifier

		$.get('/gaPostFeedback.php', {id : gaID, feedback : gaFeedbackComment, location : escape(document.location.pathname + document.location.search), liked : gaFL}, function(data){
			$('#gaFeedback').html(data); // Show success message
			$('#gaFeedback').delay(800).fadeOut('slow');
			// Record feedback along with unique visitor ID and session
			pageTracker._setCustomVar(4,'VisitorID',gaID.toString(),2);
			pageTracker._trackPageview('/virtual/feedback-submit');
			return true;
		});

	})

})


