/*********************************************************************************/
/**			copyright (c) PlanetCalc.com, 2007  			**/
/**		    	Artefact Rater		 				 */
/*********************************************************************************/

function ArtefactRater(url, contentid, artefactid) {
	this.ArtefactID = artefactid;
	this.ContentID = contentid;
	this.URL = url;
	var me = this;
	this.Forbid = false;
	this.Score = 0;
	this.Votes = 0;
	var rated_on_img = "/img/rated_on.gif";
	var rated_off_img = "/img/rated_off.gif";
	var rate_on_img = "/img/rate_on.gif";
	var rate_off_img = "/img/rate_off.gif";

	this.setInfo = function(score, votes) {
		this.Score = score;
		this.Votes = votes;
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}

		var loginDiv = FindChild(elem, "rating_login_id");
		if (null != loginDiv) 
			loginDiv.style.display = "none";

        var ratingScore = FindChild(elem, "rating_score_id");
		if (null != ratingScore)
			ratingScore.innerHTML = "(" + (new FormatterNumber(2)).Format(score) + ")";

        var ratingVotes = FindChild(elem, "rating_votes_id");
		if (null != ratingVotes)
			ratingVotes.innerHTML = votes;

		RefreshImages(elem, score, rated_on_img , rated_off_img );	
	}

	this.Disable = function() {
		this.Forbid = true;
	}

	this.setRate = function(score) {
		if (!this.Forbid)
			BSMakePOSTRequest( this.URL, this, {"id":this.ArtefactID, "mark" : score } );
	}

	this.setNormal = function() {
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}
		if (!this.Forbid)
			RefreshImages(elem, this.Score, rated_on_img, rated_off_img);	
	}

	this.setHighlight = function(score) {
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}
		if (!this.Forbid)
			RefreshImages(elem, score, rate_on_img, rate_off_img );	
		else {
			var loginDiv = FindChild(elem, "rating_login_id");
			if ( null != loginDiv ) {
				loginDiv.style.display = "inline";			
				loginDiv.style.position = "absolute";			
			}
		}
	}

	this.OnResponse = function( obj ) {
		me.setInfo( obj.score, obj.votes );
	}

	function RefreshImages(elem, score, setimage, resetimage) {
		for(var i=1; i<=5; ++i) {
			var rateimg = FindChild(elem, "rate" + i);
			if (null != rateimg)
				rateimg.src = (score >= i) ? setimage : resetimage;
		}						
	}

	function FindChild(parent, id) {
		var children = parent.childNodes;
		for( var i=0;i<children.length;++i ) {
			var elem = children[i];
			var attrs = elem.attributes;
			if (null != attrs) {
				var attr = elem.attributes.getNamedItem("id");
				if (null != attr) {
					if (attr.value == id)
						return elem;
				}
			}
			var out = FindChild(elem, id);
			if (null != out)
				return out;				
		}           	
		return null;		
	}
}

