/**
 * This class represent a web component to rate content for the camsters website
 * Compatible : IE, Firefox, Safari, Opera
  * @author Inoveo technologie inc.
 */
function CM_WEBCOMPONENT_RATING(strName) {

	this.strName = strName;
	this.intFrom = 0;
	this.intTo = 0;
	this.strObjectKey = "";
	this.fltCurrentRating = 0;
	this.blnSmall = false;
	this.objSendDataHttpRequest = new DHL.NET.HTTPREQUEST();
	this.blnSending = false;
	this.blnSendRating = false;
	this.blnRatingAccess = false;
	this.objContainer = null;
	this.blnRated = false;
	this.arrOnRatingResponseReceived = new Array();
	this.arrOnRequireRegsiterEvent = new Array();
	this.arrOnConfirmRegistrationEvent = new Array();

	CM_WEBCOMPONENT_RATING.prototype.__wakeUp = function __wakeUp(aData) {
		this.intFrom = aData['intFrom'];
		this.intTo = aData['intTo'];
		this.fltCurrentRating = aData['currentRate'];
	}

	CM_WEBCOMPONENT_RATING.prototype.getName = function getName() {
		return this.strName;
	}
	CM_WEBCOMPONENT_RATING.prototype.setName = function setName(strName) {
		this.strName = strName;
	}

	CM_WEBCOMPONENT_RATING.prototype.isSendRating = function isSendRating(blnSendRating) {
		this.blnSendRating = blnSendRating;
	}
	CM_WEBCOMPONENT_RATING.prototype.isRatingAccess = function isRatingAccess(blnRatingAccess) {
		this.blnRatingAccess = blnRatingAccess;
	}

	CM_WEBCOMPONENT_RATING.prototype.addOnRequireRegister = function addOnRequireRegister(ptFunction) {
		this.arrOnRequireRegsiterEvent.push(ptFunction);
	}

	CM_WEBCOMPONENT_RATING.prototype.addOnConfirmRegistration = function addOnConfirmRegistration(ptFunction) {
		this.arrOnConfirmRegistrationEvent.push(ptFunction);
	}

	CM_WEBCOMPONENT_RATING.prototype.onRequireRegister = function onRequireRegister() {
		for(var intI=0; intI < this.arrOnRequireRegsiterEvent.length; intI++) {
			this.arrOnRequireRegsiterEvent[intI](CM_WEBCOMPONENT_REGISTER.FORM_LOGIN, null, 'addReminder');
		}
	}
	
	CM_WEBCOMPONENT_RATING.prototype.onConfirmRegistration = function onConfirmRegistration() {
		for(var intI=0; intI < this.arrOnConfirmRegistrationEvent.length; intI++) {
			this.arrOnConfirmRegistrationEvent[intI](CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM);
		}
	}
	
	CM_WEBCOMPONENT_RATING.prototype.isRated = function isRated(blnRated) {
		this.blnRated = blnRated;
	}
	
	CM_WEBCOMPONENT_RATING.prototype.setFrom = function setFrom(intFrom) {
		this.intFrom = intFrom;
	}

	CM_WEBCOMPONENT_RATING.prototype.isSmall = function(blnIsSmall) {
		this.blnSmall = blnIsSmall;
	}

	CM_WEBCOMPONENT_RATING.prototype.setTo = function setFrom(intTo) {
		this.intTo = intTo;
	}

	CM_WEBCOMPONENT_RATING.prototype.addOnRatingReceiveListener = function addOnRatingReceiveListener(ptCallback) {
		this.arrOnRatingResponseReceived.push(ptCallback);
	}

	CM_WEBCOMPONENT_RATING.prototype.getFrom = function getFrom() {
		return this.intFrom;
	}
	
	CM_WEBCOMPONENT_RATING.prototype.getTo = function getTo() {
		return this.intTo;
	}

	CM_WEBCOMPONENT_RATING.prototype.getObjectKey = function getObjectKey() {
		return this.strObjectKey;
	}
	
	CM_WEBCOMPONENT_RATING.prototype.setObjectKey = function setObjectKey(strObjectKey) {
		this.strObjectKey = strObjectKey;
	}

	CM_WEBCOMPONENT_RATING.prototype.setCurrentRating = function setCurrentRating(fltRating) {
		//if(this.blnSendRating && this.blnRatingAccess) {
			this.fltCurrentRating = fltRating;
		//}
	}
	
	CM_WEBCOMPONENT_RATING.prototype.getCurrentRating = function getCurrentRating() {
		return this.fltCurrentRating;
	}

	CM_WEBCOMPONENT_RATING.prototype.equal = function equal(objRating) {
		this.strName = objRating.strName;
		this.intFrom = objRating.intFrom;
		this.intTo = objRating.intTo;
		this.strObjectKey = objRating.strObjectKey;
		this.fltCurrentRating = objRating.fltCurrentRating;
		this.blnSmall = objRating.blnSmall;
		this.objSendDataHttpRequest = objRating.objSendDataHttpRequest;
		this.blnSending = objRating.blnSending;
		this.blnSendRating = objRating.blnSendRating;
		this.objContainer = objRating.objContainer;
		this.blnRated = objRating.blnRated;
		this.arrOnRatingResponseReceived = objRating.arrOnRatingResponseReceived;
		this.arrOnRequireRegsiterEvent = objRating.arrOnRequireRegsiterEvent;
		this.arrOnConfirmRegistrationEvent = objRating.arrOnConfirmRegistrationEvent;
	}
	
	CM_WEBCOMPONENT_RATING.prototype.execute = function execute(objContainer) {
		if(
			typeof objContainer != 'undefined' &&
			this.intTo > 0 
		) {
			this.objContainer = objContainer;
			//Clear the container
			while(this.objContainer.firstChild) {
				this.objContainer.removeChild(objContainer.firstChild);
			}

			this.objContainer.objRating = this;

			for(intI = 1; intI <= this.intTo; intI++) {
				objDiv = document.createElement('div');
				objImage = document.createElement('img');
				objDiv.appendChild(objImage);
				this.objContainer.appendChild(objDiv);
				objImage.id = 'img' + intI;
				objImage.src = "/images/spacer.gif";

				objDiv.id = intI;
				objDiv.objContainer = objContainer;
				
				objDiv.className = (this.blnSmall? 'smallStar': 'largeStar');
			}
		}
	}

	CM_WEBCOMPONENT_RATING.prototype.render = function render(intSelected) {
		if(typeof this.objContainer != 'undefined') {
			fltCurrentRate = (intSelected > 0? intSelected: this.fltCurrentRating);
			for(var intI = 1; intI <= this.intTo; intI++) {
				fltSuffix = CM_WEBCOMPONENT_RATING._getSuffix(fltCurrentRate--);
				arrElement = this.objContainer.getElementsByTagName('*');
				if(objImage = arrElement["img" + intI]) {
					objImage.style.padding = 0;
					objImage.style.margin = 0;
					objImage.src="/images/star_" + (this.blnSmall? "modelbox_": '') + fltSuffix + ".gif";
					if(!this.blnRated) {
						objImage.parentNode.onmouseover = function() {
							this.objContainer.objRating.render(this.id);
						}
						objImage.parentNode.onclick = function() {
							if(this.objContainer.objRating.sendRating(this.id)) {
								this.objContainer.objRating.setCurrentRating(this.id);
							}
						}
						objImage.parentNode.onmouseout = function () {
							this.objContainer.objRating.render();
						}
						objImage.parentNode.style.cursor = "pointer";
					} else {
						objImage.parentNode.onmouseover = null;
						objImage.parentNode.onmouseout = null;
						objImage.parentNode.onclick = null;
						objImage.parentNode.style.cursor = "default";
					}
				}
			}
		}
	}

	CM_WEBCOMPONENT_RATING.prototype.ratingDone = function ratingDone(objRating) {
		if(objRating.objSendDataHttpRequest.getReadyState() == 4) {
			//response has been received
			objRating.blnSending = false;
			if(objRating.objSendDataHttpRequest.getStatus() == 200) {
				objRating.blnRated = true;
				objRating.setCurrentRating(objRating.objSendDataHttpRequest.getResponseText());
				if (document.getElementById('flash-displaybox')) { } else {
					objRating.render();
				}
				if(this.arrOnRatingResponseReceived.length) {
					for(intI = 0; intI < this.arrOnRatingResponseReceived.length; intI++) {
						this.arrOnRatingResponseReceived[intI](objRating);
					}
				}
			}
		}
	}

	CM_WEBCOMPONENT_RATING.prototype.sendRating = function sendRating(intRating) {
		var blnReturn = false;
		if(this.blnSendRating && this.blnRatingAccess) {
			if(!this.blnSending) {
				this.blnSending = true;
				var objUrlToSend = new DHL.NET.URL("/rating_admin.htm");

				strAttributes =
					"strWcName=" + this.strName + "&" + 
					"strObjectKey=" + this.strObjectKey + "&" + 
					"intRating=" + intRating; 
				this.objSendDataHttpRequest.open('POST', objUrlToSend, true);

				this.objSendDataHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				this.objSendDataHttpRequest.setRequestHeader('Content-length', strAttributes.length);
				this.objSendDataHttpRequest.setRequestHeader('Connection', 'close');
				me = this;
				this.objSendDataHttpRequest.onreadystatechange(function() {
					me.ratingDone(me)
				});
				this.objSendDataHttpRequest.objRating = this;
				this.objSendDataHttpRequest.send(strAttributes);
				blnReturn = true;
			}
		}
		else if (this.blnSendRating) {
			this.onConfirmRegistration();
		}
		else {
			this.onRequireRegister();
		}
		return blnReturn;
	}

	// Static members definition {{{
	if (CM_WEBCOMPONENT_RATING.className == undefined) { 

		var arrTemplate = new Object();

		CM_WEBCOMPONENT_RATING.wakeup = function(aData) {
			oRating = new CM_WEBCOMPONENT_RATING();
			oRating.__wakeUp(aData);
			return oRating;
		}

		CM_WEBCOMPONENT_RATING._getSuffix = function _getSuffix(fltResult) {
			strReturn = "0";
			if(fltResult >= 1) {
				strReturn = '1';
			}
			else if(fltResult < 1 && fltResult >= 0.75) {
				strReturn = '0.75';
			}
			else if(fltResult < 0.75 && fltResult >= 0.50) {
				strReturn = '0.50';
			}
			else if(fltResult < 0.50 && fltResult >= 0.25) {
				strReturn = '0.25';
			}
			return strReturn;
		}

		CM_WEBCOMPONENT_RATING.className = 'CM_WEBCOMPONENT_RATING';
		CM_WEBCOMPONENT_RATING.prototype.className = 'CM_WEBCOMPONENT_RATING';
	}
	// }}}
} 