/**
*	rating.js
*
*	Provide a transparent mechanism for dynamically
*	rating virtually anything. Requires events.js,
*	ajax.js and cookies.js to function.
*
*	@author Dave Liotta
*	@copyright &copy;2006 Leedom and Associates, LLC
*	@created July 6th, 2006
*/

var RATE_SERVICE_URL = "./services/RateArticleByIDXHTML.php";
var RATE_CURRENT_RATE_SERVICE_URL = "./services/GetCurrentRatingByArticleIDXHTML.php";
var RATE_CURRENT_RATE_OBJ_NAME = "currentrating";
var RATE_IMAGE_ROOT_PATH = "./images/ratings/";
var RATE_IMAGE_ITEM_PREFIX = "rateimage";
var RATE_MESSAGE_OBJ_NAME = "ratemessage";
var RATE_BASIC_MESSAGE = "&nbsp;-&nbsp;Click with mouse to select rating.";
var RATE_RECORDED_MESSAGE = "&nbsp;-&nbsp;Rating recorded. Thank you.";
var PREVIOUS_RATE_MESSAGE = "&nbsp;-&nbsp;Previously recorded, thanks.";
var RATE_MESSAGES = new Array(
	"&nbsp;-&nbsp;Would not recommend.",
	"&nbsp;-&nbsp;Somewhat recommend.",
	"&nbsp;-&nbsp;Recommend this article.",
	"&nbsp;-&nbsp;Highly recommend!",
	"&nbsp;-&nbsp;Must read article!" );

/**
*	RatingSystem class
*/
function RatingSystem(Id, Namespace) {

	var RatingId = (Id != null) ? Id : 0;
	var RatingNamespace = ((Namespace != null) && (Namespace.length > 0)) ? Namespace + ":" : "";
	var CurrentRating = 0;
	var ImageItems = null;
	var MessageObj = null;

	this.Initialize = function() {

		LoadImageItems();
		LoadMessageObj();

		if (!HasBeenRated()) {
			ApplyEventListeners();
		}
		else {
			UpdateDisplay();
			MessageObj.innerHTML = PREVIOUS_RATE_MESSAGE;
		}

	}

	this.Destroy = function () {}

	function LoadImageItems() {
		var ItemNumber = 1;
		var ImageObj = document.all ? document.all[RatingNamespace + RATE_IMAGE_ITEM_PREFIX + ItemNumber] : document.getElementById ? document.getElementById(RatingNamespace + RATE_IMAGE_ITEM_PREFIX + ItemNumber) : null;

		ImageItems = new Array();
		while(ImageObj != null) {
			ImageObj.rating = ItemNumber;
			ImageItems[ItemNumber-1] = ImageObj;
			ItemNumber++;
			ImageObj = document.all ? document.all[RatingNamespace + RATE_IMAGE_ITEM_PREFIX + ItemNumber] : document.getElementById ? document.getElementById(RatingNamespace + RATE_IMAGE_ITEM_PREFIX + ItemNumber) : null;
		}
	}

	function LoadMessageObj() {
		MessageObj = document.all ? document.all[RatingNamespace + RATE_MESSAGE_OBJ_NAME] : document.getElementById ? document.getElementById(RatingNamespace + RATE_MESSAGE_OBJ_NAME) : null;
	}

	function HasBeenRated() {
		CurrentRating = getCookie(RatingId);
		CurrentRating = CurrentRating ? CurrentRating : 0;
		return (CurrentRating > 0);
	}

	function ApplyEventListeners() {
		if ( ImageItems == null ) LoadImageItems();
		for (var i=0; i < ImageItems.length; i++) {
			attachEventListener(ImageItems[i], "mouseover", ImageItemOver, false);
			attachEventListener(ImageItems[i], "mouseout", ImageItemOut, false);
			attachEventListener(ImageItems[i], "click", ImageItemClicked, false);
		}
	}

	function RemoveEventListeners() {
		if ( ImageItems != null ) {
			for (var i=0; i < ImageItems.length; i++) {
				detachEventListener(ImageItems[i], "mouseover", ImageItemOver, false);
				detachEventListener(ImageItems[i], "mouseout", ImageItemOut, false);
				detachEventListener(ImageItems[i], "click", ImageItemClicked, false);
			}
		}
	}

	function ImageItemOver(event) {
		CurrentRating = queryEventTarget(event).rating;
		UpdateDisplay();
	}

	function ImageItemOut(event) {
		CurrentRating = 0;
		UpdateDisplay();
	}

	function ImageItemClicked(event) {
		CurrentRating = queryEventTarget(event).rating;
		UpdateDisplay;
		RemoveEventListeners();

		function CallBack(AResponse) {
			if ( MessageObj != null ) MessageObj.innerHTML = RATE_RECORDED_MESSAGE;

			// Update the cookie
			var	expires = new Date();
			expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000); // expires one year
			setCookie(RatingId, CurrentRating, expires);

			UpdateCurrentRatingDisplay();
		}

		var PostData = "id=" + encodeURIComponent(RatingId) + "&r=" + CurrentRating;
		AJAXQuery(RATE_SERVICE_URL, PostData, CallBack);
	}

	function UpdateCurrentRatingDisplay(event) {
		function CallBack(AResponse) {
			var CurrentRatingObj = document.all ? document.all[RatingNamespace + RATE_CURRENT_RATE_OBJ_NAME] : document.getElementById ? document.getElementById(RatingNamespace + RATE_CURRENT_RATE_OBJ_NAME) : null;
			if (CurrentRatingObj) CurrentRatingObj.innerHTML = AResponse.responseText;
		}

		AJAXQuery(RATE_CURRENT_RATE_SERVICE_URL + "?id=" + encodeURIComponent(RatingId), null, CallBack);
	}

	function UpdateDisplay() {
		if ( ImageItems != null ) {
			for (var i=0; i < ImageItems.length; i++) {
				ImageItems[i].src = i < CurrentRating ? RATE_IMAGE_ROOT_PATH + "full.gif" : RATE_IMAGE_ROOT_PATH + "empty.gif";
			}
		}

		if ((MessageObj != null) &&
			(RATE_MESSAGES != null) &&
			(CurrentRating <= RATE_MESSAGES.length) &&
			(CurrentRating > 0) ) {
			MessageObj.innerHTML = RATE_MESSAGES[CurrentRating-1];
		}
		else if (MessageObj != null) {
			MessageObj.innerHTML = RATE_BASIC_MESSAGE;
		}
	}

	return true;

}
