// Rate this Page script
// Created by: NElliott 30/01/2009

var RatePage = function() {
	
	var _ajaxFormUrl = "ratepage.htm"; // url of ratepage script
	var _currentFormUrl = "";
	var _currentRating = "";
	var _currentPageId = "";
	var _currentRatingText = "";

	//	
	
	var _q1currentRating = "";	
	var _q2currentRating = "";		
	var _q3currentRating = "";	
	
	var _q1currentRatingText = "";
	var _q2currentRatingText = "";
	var _q3currentRatingText = "";

	//
					
	var _pageIsRated = false;
	
	var init = function(){
		_currentRatingText = $("ul li.current-rating").text();
		$("#survey").before("<p class='current-rating'><span>Your rating: </span>" + _currentRatingText + "</p>");
		$("#survey li a").click(function(){
			_currentFormUrl = $(this).attr("href").replace(/ratethispage\.htm/, _ajaxFormUrl);
			_currentPageId = _currentFormUrl.match(/pageid\=([0-9]+)/i)[1];
			_setRating(_currentFormUrl.match(/rating\=([0-9]+)/i)[1]);
			
			_displayForm();
			return false;
		}).mouseover(function(){
			$("#rate_this_page ul.star-rating li a").removeClass("selected");
			$("p.current-rating").html("<span>Your rating: </span>" + $(this).attr("title"));
		}).mouseout(function(){
			$("#rate_this_page ul.star-rating li a").eq(Number(_currentRating)-1).addClass("selected");
			//$("p.current-rating").text(_currentRating);
			$("p.current-rating").html("<span>Your rating: </span>" + _currentRatingText);
		});
		
	};
	
	var _setRating = function(rating) {
		// Update rating info
		
		if(!rating || (rating == "undefined") || (rating == null)) {
			rating = 0;
		}
		_currentRating = Number(rating);
		switch(_currentRating) {
			case 1:
				_currentRatingText = "Poor";
				break;
			case 2:
				_currentRatingText = "Average";
				break;
			case 3:
				_currentRatingText = "Good";
				break;
			default:
				_currentRatingText = "Not yet rated";
				break;
		}
		$("#rate_this_page ul.star-rating li a").removeClass("selected").eq(_currentRating -1).addClass("selected");
		$("#eddc_survey_form ul.star-rating li a").removeClass("selected").eq(_currentRating -1).addClass("selected");

		
		$("p.current-rating").html("<span>Your rating: </span>" + _currentRatingText);
	};
	
	var _setResponse = function(responseId, responseValue) {
		
	};

	
	var _loadContentForRating = function(rating) {
		// Load form content for rating via ajax call
		
		var formUrl = _ajaxFormUrl + "?pageid=" + _currentPageId + "&rating=" + rating + "&t=" + new Date().getTime();

		$.getJSON(formUrl, function(data){
			$("#eddc_survey_form").html(data.responseHTML);
			if(data.surveyResponse == "success") {
				$("#eddc_survey_form ul.star-rating li a").eq(Number(rating)-1).addClass("selected");
				$("#eddc_survey_form button").click(function(){
					_submitForm();
					return false;
				});
				$("#eddc_survey_form ul.star-rating li a").click(function(){
					_setRating($(this).attr("href").match(/rating\=([0-9]+)/i)[1]);
					_loadContentForRating($(this).attr("href").match(/rating\=([0-9]+)/i)[1]);
					return false;
				});
				$("#eddc_survey_form ul.star-rating li a").mouseover(function(){
					$("#eddc_survey_form ul.star-rating li a").removeClass("selected");
					$("span.current-rating").text($(this).attr("title"));
				}).mouseout(function(){
					$("#eddc_survey_form ul.star-rating li a").eq(Number(rating)-1).addClass("selected");
					$("span.current-rating").text(_currentRatingText);
				});
				$("#eddc_survey_form div.survey_header a.close").click(function(){
					_closeForm();
					return false;
				});
								
				$("#eddc_survey_form ul.star-rating-q li a").click(function(){
					var response_id = $(this).parent().parent().attr("id");
					$("#" + response_id + " a").removeClass("selected");
					$(this).addClass("selected");
					var response_value = "0";
					if($(this).hasClass("one-star")) {
						response_value = "1";
					} else if($(this).hasClass("two-stars")) {
						response_value = "2";
					} else if($(this).hasClass("three-stars")) {
						response_value = "3";
					}
					$("input[name=" + response_id + "]").val(response_value);
					$("#" + response_id + "_label").text($(this).attr("title")).data("label", $(this).attr("title"));
					return false;
				}).mouseover(function(){
					var response_id = $(this).parent().parent().attr("id");
					$("#" + response_id + " a").removeClass("selected");
					
					$("#" + response_id + "_label").text($(this).attr("title"));
				}).mouseout(function(){
					var response_id = $(this).parent().parent().attr("id");
					var rating = $("input[name=" + response_id + "]").val();
					if(rating != "") {
						$("#" + response_id + " a").eq(Number(rating)-1).addClass("selected");
					}
					if($("#" + response_id + "_label").data("label")) {
						$("#" + response_id + "_label").text($("#" + response_id + "_label").data("label"));
					} else {
						$("#" + response_id + "_label").text("");
					}
				});
				
				
				
				
				
			} else if(data.surveyResponse == "failure") {
				$("#eddc_survey_form").css("height", "130px");
				$("#eddc_survey_form button").click(function(){
					_closeForm();
				});
				
			} else if(data.surveyResponse == "alreadyrated") {
				$("#eddc_survey_form").css("height", "130px");
				$("#eddc_survey_form button").click(function(){
					_closeForm();
				});
				
			}
		});

	};
	
	var _displayForm = function(){
		// Create form overlay 
		
		$("body").append("<div id='eddc_survey_overlay' style=''></div><div id='eddc_survey_form' style=''></div>");
		$("#eddc_survey_overlay").css("opacity", "0.6");
		var windowHeight = $.browser.msie ? (document.documentElement.clientHeight) : $(window).height();
		var formOffsetLeft = ($(window).width() - $("#eddc_survey_form").width()) / 2;
		var formOffsetTop = (windowHeight - $("#eddc_survey_form").height()) / 2;
		$("#eddc_survey_form").css("left", formOffsetLeft);
		$("#eddc_survey_form").css("top", formOffsetTop);
		$("#eddc_survey_overlay").click(function(){
			_closeForm();
		});
		
		_loadContentForRating(_currentRating);
		
	};
	
	var _validateForm = function(){
		// Validate that we have all the required information
		
		var error = false;
		
		if($("input[name=response_1]").val() == "") {
			if(!$("#response_1_question").hasClass("error")) {
				$("#response_1_question").addClass("error");
			}
			error = true;
		} else {
			if($("#response_1_question").hasClass("error")) {
				$("#response_1_question").removeClass("error");
			}
		}
		if($("input[name=response_2]").val() == "") {
			if(!$("#response_2_question").hasClass("error")) {
				$("#response_2_question").addClass("error");
			}
			error = true;
		} else {
			if($("#response_2_question").hasClass("error")) {
				$("#response_2_question").removeClass("error");
			}
		}
		if($("input[name=response_3]").val() == "") {
			if(!$("#response_3_question").hasClass("error")) {
				$("#response_3_question").addClass("error");
			}
			error = true;
		} else {
			if($("#response_3_question").hasClass("error")) {
				$("#response_3_question").removeClass("error");
			}
		}

		if(error) {
			return false;
		} else {
			return true;
		}
	};
	
	var _submitForm = function(){
		// Attempt to submit form data back to server
		
		if(_validateForm()) {
			// Data is valid
			
			var response_1 = $("input[name=response_1]").val();
			var response_2 = $("input[name=response_2]").val();
			var response_3 = $("input[name=response_3]").val();

			var emailr = $("textarea[name=emailr]").val();						

			var furtherdetails = $("#furtherdetails").val();
		
			$.getJSON(_ajaxFormUrl, {
			
				emailr: emailr,			
				submit: "true",
				rating: _currentRating,
				pageid: _currentPageId,
				response_1: response_1,
				response_2: response_2,
				response_3: response_3,
				furtherdetails: furtherdetails,
				t: new Date().getTime()
			}, function(data){
				var strResponse;
				if(data.surveyResponse) {
					strResponse = data.responseHTML;
					switch(data.surveyResponse) {
						case "success":
							_pageIsRated = true;
							break;
						case "failure":
							break;
						case "alreadyrated":
							break;
						default:
							break;
					}
				}
				$("#eddc_survey_form").html(strResponse);
				$("#eddc_survey_form div.survey_rating span").css("width", "100%").css("text-align", "center");
				$("#eddc_survey_form").animate({height:"130px"}, 200);
				$("#eddc_survey_form div.survey_footer button").click(function(){
					_closeForm();
					return false;
				});
			});
		}
	};
	
	var _closeForm = function(){
		// Close the form and remove the overlay
		
		if(!_pageIsRated) {
			_setRating();
		} else {
			$("#survey li a").unbind().click(function(){
				return false;
			});
		}
		$("#eddc_survey_form").remove();
		$("#eddc_survey_overlay").fadeOut("normal", function(){
			$(this).remove();
		});
	};
	
	return {
		init: init
	}
}();

$(document).ready(function() {
	RatePage.init();
});
