onLoadFunctionList = new Array();
function performOnLoadFunctions()
{
	for (var i in onLoadFunctionList)
	{
		onLoadFunctionList[i]();
	}
}


var UT_RATING_IMG = 'http://taayaa.com/wp-content/plugins/votesakid/images/starvs.gif';
var UT_RATING_IMG_HOVER = 'http://taayaa.com/wp-content/plugins/votesakid/images/starvs.gif';
var UT_RATING_IMG_HALF = 'http://taayaa.com/wp-content/plugins/votesakid/images/star05.gif';
var UT_RATING_IMG_BG = 'http://taayaa.com/wp-content/plugins/votesakid/images/starv.gif';
var UT_RATING_IMG_REMOVED = 'http://taayaa.com/wp-content/plugins/votesakid/images/star0.gif';
var UT_RATING_IMG_LOADING = 'http://taayaa.com/wp-content/plugins/votesakid/images/working.gif';

function UTRating(ratingElementId, starsElementId, maxStars, objectName, formName, ratingMessageId, componentSuffix, size)
{
	this.ratingElementId = ratingElementId;
	this.starsElementId = starsElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId
	this.componentSuffix = componentSuffix

	this.starTimer = null;
	this.starCount = 0;

	if(size=='S') {
		UT_RATING_IMG      = 'http://taayaa.com/wp-content/plugins/votesakid/images/star_sm.gif'
		UT_RATING_IMG_HALF = 'http://taayaa.com/wp-content/plugins/votesakid/images/star_sm_half.gif'
		UT_RATING_IMG_BG   = 'http://taayaa.com/wp-content/plugins/votesakid/images/star_sm_bg.gif'
	}
	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;
	(new Image()).src = UT_RATING_IMG_HOVER;
	(new Image()).src = UT_RATING_IMG_REMOVED;
	(new Image()).src = UT_RATING_IMG_LOADING; 

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum);
	}

	function setMessage(starNum) {
		messages = new Array("โหวดหน่อย", "ห่วย", "ธรรมดา", "พอได้", "แจ๋ว", "สุดยอด!");
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star_'  + this.componentSuffix + "_" + (i+1)).src = UT_RATING_IMG;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount)
				document.getElementById('star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG; // UT_RATING_IMG_REMOVED;
			else
				document.getElementById('star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
	}

	function setStars(starNum) {
		this.starCount = starNum;
		this.drawStars(starNum);
		document.forms[this.formName]['rating'].value = this.starCount;
		var ratingElementId = this.ratingElementId;
		
		/* Add temporary feedback that the request has been sent */ 
		document.getElementById(starsElementId).innerHTML = 'รอแป๊ปนะ...';
		var loadingImg = document.createElement('img');
      	loadingImg.src = UT_RATING_IMG_LOADING;
      	document.getElementById(starsElementId).appendChild(loadingImg);
		
		postForm(this.formName, true, function (req) { replaceDivContents(req, ratingElementId); });
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
	}

	function resetStars() {
		this.clearStarTimer();
		if (this.starCount)
			this.drawStars(this.starCount);
		else
			this.greyStars();
		this.setMessage(0);
	}

	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}

	this.clearStars = clearStars;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.setStars = setStars;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;

}


// JavaScript Document