// ============================================================================
// Ratings
// ============================================================================
function Ratings(id, grpid, imgOn, imgOff, imgVoted, xsl)
{
	this._id		= id;
	this._grpid		= grpid;
	this._imgOn		= imgOn;
	this._imgOff	= imgOff;
	this._imgVoted	= imgVoted;
	this._xsl		= xsl;
	this._userid	= $('ctl00_hdnUserID').value;
	
	this._optButtons = new Array();
		
	this._articleid = getQueryVariable("id");
}

Ratings.prototype._articleid;
Ratings.prototype._id;
Ratings.prototype._grpid;
Ratings.prototype._imgOn;
Ratings.prototype._imgOff;
Ratings.prototype._imgVoted;
Ratings.prototype._userid;
Ratings.prototype._xsl;
Ratings.prototype._optButtons;

Ratings.prototype.init = function()
{
	var url = AjaxURL + '/RatingsSelectResults';
	var inParameters = Object.toQueryString(
						{
							'iArticleID': this._articleid,
							'iRatingGroupID': this._grpid, 
							'iUserID': this._userid,
							'sControlID': this._id,
							'sXSLTemplate': this._xsl
						});
	
	var ajax = new Ajax(url, 
	{
		update: $(this._id + '_div'),
		method: 'post', 
		data: inParameters, 
		onComplete: this.beginUpdate
	});
	
	ajax.request();
};

Ratings.prototype.rate = function(opt, val)
{
	//alert(val + ' ' + opt);

	var url = AjaxURL + '/RatingsAddRating';
	var inParameters = Object.toQueryString(
						{
							'iArticleID': this._articleid,
							'iRatingGroupID': this._grpid, 
							'iRatingOptionID': opt,
							'iRating': val,
							'iUserID': this._userid,
							'sControlID': this._id,
							'sXSLTemplate': this._xsl
						});
	
	var ajax = new Ajax(url, 
	{
		update: $(this._id + '_div'),
		method: 'post', 
		data: inParameters, 
		onComplete: this.beginUpdate
	});
	
	ajax.request();
};

Ratings.prototype.beginUpdate = function(request)
{
	//alert(request);

	Ratings.prototype.evalResponse(request);
};


// exec javascript since mootools function is broken
Ratings.prototype.evalResponse = function(request)
{
	var script, scripts;
	
	scripts = [];
	var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
	
	while ((script = regexp.exec(request))) 
		scripts.push(script[1]);
	
	scripts = scripts.join('\n');
		
	//alert(scripts);
			
	if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
};

// ============================================================================
// RatingButton
// ============================================================================
function RatingButton(rating, optid, val, rated, parentElement)
{
	this._rating	= rating
	this._optid		= optid;
	this._val		= val; //3;
	this._rated		= rated;
	this._parent	= $(parentElement);
	
	this._stars		= new Array();
	
	this._rating._optButtons[this._rating._optButtons.length] = this;
}

RatingButton.prototype.addImage = function(img)
{
	img.src = this._rating._imgOff;

	if (this._optid != 0 && this._rating._userid != 0)
	{
		
	}
	else
	{
		img.removeEvents();
	}
	
	this._stars[this._stars.length] = img;
	
	if (this._stars.length == 5)
		this.rollOut(5);
};

RatingButton.prototype._rating;
RatingButton.prototype._optid;
RatingButton.prototype._val;
RatingButton.prototype._rated;
RatingButton.prototype._imgCt			
RatingButton.prototype._stars;

RatingButton.prototype.rollOn = function(val)
{
	switch(val)
	{
		case 1:
			this._stars[0].src = this._rating._imgOn;
			break;
		case 2:
			this._stars[0].src = this._rating._imgOn;
			this._stars[1].src = this._rating._imgOn;
			break;
		case 3:
			this._stars[0].src = this._rating._imgOn;
			this._stars[1].src = this._rating._imgOn;
			this._stars[2].src = this._rating._imgOn;

			break;
		case 4:
			this._stars[0].src = this._rating._imgOn;
			this._stars[1].src = this._rating._imgOn;
			this._stars[2].src = this._rating._imgOn;
			this._stars[3].src = this._rating._imgOn;
			break;
		case 5:
			this._stars[0].src = this._rating._imgOn;
			this._stars[1].src = this._rating._imgOn;
			this._stars[2].src = this._rating._imgOn;
			this._stars[3].src = this._rating._imgOn;
			this._stars[4].src = this._rating._imgOn;
			break;
	}
}

RatingButton.prototype.rollOut = function(val)
{								
	this._stars[0].src = this._rating._imgOff;
	this._stars[1].src = this._rating._imgOff;
	this._stars[2].src = this._rating._imgOff;
	this._stars[3].src = this._rating._imgOff;
	this._stars[4].src = this._rating._imgOff;
	
	if (this._rated != 0)
	{
		if (this._rated == 1)
		{
			this._stars[0].src = this._rating._imgVoted;
		}
		else if (this._rated == 2)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
		}
		else if (this._rated == 3)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
		}
		else if (this._rated == 4)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
			this._stars[3].src = this._rating._imgVoted;
		}
		else if (this._rated == 5)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
			this._stars[3].src = this._rating._imgVoted;
			this._stars[4].src = this._rating._imgVoted;
		}
	}
	else
	{
		if (this._val == 0)
		{
		}
		else if (this._val <= 1)
		{
			this._stars[0].src = this._rating._imgVoted;
		}
		else if (this._val <= 2)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
		}
		else if (this._val <= 3)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
		}
		else if (this._val <= 4)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
			this._stars[3].src = this._rating._imgVoted;
		}
		else if (this._val == 5)
		{
			this._stars[0].src = this._rating._imgVoted;
			this._stars[1].src = this._rating._imgVoted;
			this._stars[2].src = this._rating._imgVoted;
			this._stars[3].src = this._rating._imgVoted;
			this._stars[4].src = this._rating._imgVoted;
		}
	}
}

