/*--------------------------------------------------------------------------
This script depends on './incConfig.js' and the jQuery library.

As long as we're using Prototype, don't use the '$' shortcut.

- This script assumes that the rater is enclosed in an HTML element marked 
with class='rater'.  (i.e. <span> or <div>)
And each star is an <img> tag, and they are the first elements in the 
'rater'.   

Include this code to launch the rater after the jQuery library has loaded:

oRater.init();
--------------------------------------------------------------------------*/


//--------------------------------------------------------------------------
// Rater object
//--------------------------------------------------------------------------

var oRater = {};
oRater.currentRating = CURRENT_RATING_FIELD;
oRater.rating = RATING_FIELD;
oRater.rating.value = CURRENT_RATING_FIELD ? CURRENT_RATING_FIELD.value : 0;
oRater.hasRated = false;
oRater.ratingMsgs = RATING_MSGS;
oRater.empty = EMPTY_STAR;
oRater.full = FULL_STAR;
oRater.half = HALF_STAR;
oRater.rateURL = HANDLER_URL;

// Release '$' back to Prototype.
jQuery.noConflict();

oRater.init = function() {
	jQuery('.rater').each(function() {
		jQuery(this).children('img').click(oRater.setRating).mouseenter(oRater.highlight).css('cursor', 'pointer');
		jQuery(this).mouseleave(oRater.dehighlight);
	});
};

oRater.render = function(pClicked, pRating) {
	// Render all stars up to and including the clicked star to be full.
	// Render the rest as empty stars.
	if (pRating > 0) {
		jQuery(pClicked).attr('src', oRater.full);
		jQuery(pClicked).prevAll('img').each(function() {
			jQuery(this).attr('src', oRater.full);
		});
		jQuery(pClicked).nextAll('img').each(function() {
			jQuery(this).attr('src', oRater.empty);
		});
	} else {
		jQuery(pClicked).attr('src', oRater.empty);
		jQuery(pClicked).siblings('img').attr('src', oRater.empty);
	}
};

oRater.highlight = function(pEvent){
	if (oRater.hasVoted) {
		return;
	}
	var ratingValue = jQuery(this).prevAll().length + 1;
	if (isNaN(ratingValue)) {
		return;
	}
	oRater.render(jQuery(this), ratingValue);
	oRater.setExtraMsg(jQuery(this).parent(), '');
	oRater.setMsg(jQuery(this).parent(), oRater.ratingMsgs[ratingValue - 1]);
};

oRater.dehighlight = function(pEvent){
	var index = 0;
	if (oRater.rating.value > 0) {
		index = oRater.rating.value - 1;
	}
	var star = jQuery(this).children('img').eq(index);
	oRater.render(star, oRater.rating.value);
	
	if (!oRater.hasVoted) {
		oRater.setMsg(jQuery(this), '');
	} else {
		oRater.hasVoted = false;
	}
};

oRater.setRating = function(pEvent){
	oRater.rating.value = jQuery(this).prevAll().length + 1;
	if (isNaN(oRater.rating.value)) {
		return;
	}
	var parent = jQuery(this).parent('.rater');
	
	var callBack = function(pXml, pStatus) {
		// Set all raters for logged in user to newly selected rating.
		oRater.currentRating.value = oRater.rating.value;
		oRater.resetAll();
		oRater.setMsg(parent, SUCCESS_MSG);

		// Render new average rating.
		// IE reads responseXML, while Firefox reads responseText.
		var oAvgRating = jQuery(pXml.responseXML).find('avgRating:first');
		if (oAvgRating.length) {
			// empty
		} else if ((oAvgRating = jQuery(pXml.responseText).find('avgRating:first')).length) {
			// empty
		} else {
			oAvgRating = null;
		}
		if (oAvgRating) {
			oRater.renderAvgRating(parseFloat(oAvgRating.text()));
		}
	};
	
	if (!parent.hasClass('requireComment')) {
		if (oRater.rating.value != oRater.currentRating.value) {
			jQuery.ajax({
				type: 'POST',
				url: oRater.rateURL, 
				data: jQuery('form').serialize(), 
				dataType: 'xml',
				complete: callBack
			});
		}
	} else {
		oRater.setMsg(parent, '');
		oRater.setExtraMsg(parent, WARNING_MSG);
	}
	oRater.hasVoted = true;
};

oRater.resetAll = function() {
	var index = 0;
	if (oRater.rating.value > 0) {
		index = oRater.rating.value - 1;
	}
	jQuery('.rater').each(function() {
		jQuery(this).children('img').eq(index).each(function() {
			oRater.render(jQuery(this), oRater.rating.value);
		});
	});
};

oRater.setMsg = function(pRater, pMsg) {
	jQuery('.error', pRater).remove();
	
	if (jQuery('.raterMsg', pRater).length) {
		jQuery('.raterMsg', pRater).html(pMsg);
	} else {
		jQuery(pRater).append('<span class="raterMsg">' + pMsg + '</span>');
		jQuery('.raterMsg', pRater).css('color', 'gray');
	}
};

oRater.setExtraMsg = function(pRater, pMsg) {
	if (pMsg) {
		if (jQuery('span.extraRaterMsg', pRater).length) {
			jQuery('span.extraRaterMsg', pRater).html(pMsg);
		} else {
			jQuery(pRater).append('<span class="extraRaterMsg">' + pMsg + '</span>');
			jQuery('span.extraRaterMsg', pRater).css('color', 'gray');
		}
	} else {
		jQuery('.extraRaterMsg', pRater).remove();
	}
};

oRater.renderAvgRating = function(pRating) {
	if (!pRating || pRating <= 0) {
		return;
	}
	var starArr = jQuery('#avgRating').children('img');
	var i = 0;
	for (; i < Math.floor(pRating); ++i) {
		starArr[i].src = oRater.full;
	}
	if ((pRating % 1) > 0) {
		starArr[i++].src = oRater.half;
	}
	for (; i < starArr.length; ++i) {
		starArr[i].src = oRater.empty;
	}
};
