// Array with messages, this array will also set the number of images to show
ltg_ratings.Msgs = new Array
(
   "Awful", "Poor", "Average", "Good", "Excellent"
)

// By default we show these images
ltg_ratings.Imgs = new Array
(
   "a00.gif", "a100.gif"
);

// These are the begincap and endcap images
ltg_ratings.bcec = new Array
(
   "", ""
);

// Default message when the defaultValue is set to -1
ltg_ratings.defaultMsg = "&nbsp;";

// Path to thhe images (relative or absolute), this is the best guess at local
ltg_ratings.imgPath = "/typo3conf/ext/ltg_rating/img/";

/*
 * /brief
 * param	MD5 (can we calculate this?:)		MD5 is culculate from md5(prefixID+workingTable+workingUID+ratingType);
 * param	prefixID					Name of the hidden field that this class will create.
 * param	workingTable					eg: pages
 * param	workingUid					eg:123
 * param	ratingType					eg: pagerating
 * param	defaultValue					default Value (0-10 or -1) (-1 means nothing will be set...)
*/

//ltg_ratings("pages", 10001, "pageRating", -1);
function ltg_ratings(MD5, prefixID, workingTable, workingUid, ratingType, defaultValue) {
	this.workingTable = workingTable;
	this.workingUid = workingUid;
	this.ratingType = ratingType;
	// Check for invalids.....
	if ((defaultValue < 0 || defaultValue > 10) && (defaultValue != -1)) { 
		defaultValue = 0; 
	}
	this.currentValue = defaultValue;
	this.prefixID = prefixID;
	this.MD5 = MD5;

	this.obj = workingTable + '_' + workingUid + '_' + ratingType;
	this.Imgs = ltg_ratings.Imgs;
	this.bcec = ltg_ratings.bcec;
	this.imgPath = ltg_ratings.imgPath;
	this.Msgs = ltg_ratings.Msgs;
	this.defaultMsg = ltg_ratings.defaultMsg;


	// Reference to the images
	this.msg = document.getElementById(this.obj+"_msg");
	this.img = document.getElementById(this.obj+"_images");

	// Start building the images
	content = "<span id=\"" + this.obj+"_innerimg" + "\" ></span>";
	innerHTML(this.img, content);
	this.innerimg = document.getElementById(this.obj+"_innerimg");
	this.innerimg.obj = this.obj;

	//
	var starStep = 10.0 / (this.Msgs.length - 1);
	var onStars = Math.round (this.currentValue / starStep);
	var iconNum = 0;
	var content = '';

	// Draw the stars
	content = "";
	if (this.bcec[0]) content += "<img src=\""+this.imgPath+this.bcec[0]+"\" />";
	content += "<input type=\"hidden\" name=\"" + this.prefixID +"\" value=\"-1\" />";
	for (sc=0; sc <= onStars; sc++) {
		h1 = 'onMouseOver="return ltg_ratings_mouseOver('+this.obj+', ' + (iconNum * starStep) + ');"';
		h2 = 'onClick="return ltg_ratings_click(' + this.obj+','+(iconNum * starStep) + ');"';
		content += '<span class="unit" ' + h1 + ' ' + h2 + ' >';
		content += "<img id=\"" + this.obj +"_img" + iconNum + "\" src=\""+this.imgPath+this.Imgs[1]+"\" />";
		content += '</span>';
		iconNum++;
	}

	for (sc=onStars+1; sc < this.Msgs.length; sc++) {
		h1 = 'onMouseOver="return ltg_ratings_mouseOver('+this.obj+', ' + (iconNum * starStep) + ');"';
		h2 = 'onClick="return ltg_ratings_click(' + this.obj+','+(iconNum * starStep) + ');"';
		content += '<span class="unit" ' + h1 + ' ' + h2 + ' >';
		content += "<img id=\"" + this.obj +"_img" + iconNum + "\" src=\""+this.imgPath+this.Imgs[0]+"\" />";
		content += '</span>';
		iconNum++;
	}

	if (this.bcec[1]) content += "<img src=\""+this.imgPath+this.bcec[1]+"\" />";
	innerHTML(this.innerimg, content);
	this.update (this.currentValue);

	this.img.onmouseout = ltg_ratings_mouseout;
	this.img.obj = this;

	// Set this default right away
	if (this.currentValue > 0) {
		var form = document.forms[this.MD5];
		form[this.prefixID ].value = this.currentValue;
	}

	return true;

}

function ltg_ratings_show(workingTable, workingUid, ratingType, defaultValue) {
	this.workingTable = workingTable;
	this.workingUid = workingUid;
	this.ratingType = ratingType;
	// Check for invalids.....
	if ((defaultValue < 0 || defaultValue > 10) && (defaultValue != -1)) { defaultValue = 0; }
	this.currentValue = defaultValue;

	this.obj = workingTable + '_' + workingUid + '_' + ratingType;
	this.Imgs = ltg_ratings.Imgs;
	this.bcec = ltg_ratings.bcec;
	this.imgPath = ltg_ratings.imgPath;
	this.Msgs = ltg_ratings.Msgs;
	this.defaultMsg = ltg_ratings.defaultMsg;


	// Reference to the images
	this.msg = document.getElementById(this.obj+"_msg");
	this.img = document.getElementById(this.obj+"_images");

	// Start building the images
	content = "<span id=\"" + this.obj+"_innerimg" + "\" style=\"border:4px;\" ></span>";
	innerHTML(this.img, content);
	this.innerimg = document.getElementById(this.obj+"_innerimg");
	this.innerimg.obj = this.obj;

	//
	var starStep = 10.0 / (this.Msgs.length - 1);
	var onStars = Math.round (this.currentValue / starStep);
	var iconNum = 0;
	var content = '';

	// Draw the stars
	content = "";
	if (this.bcec[0]) content += "<img src=\""+this.imgPath+this.bcec[0]+"\" />";
	content += "<input type=\"hidden\" name=\"" + this.prefixID +"\" value=\"-1\" />";
	for (sc=0; sc <= onStars; sc++) {
		content += '<span class="unit" >';
		content += "<img id=\"" + this.obj +"_img" + iconNum + "\" src=\""+this.imgPath+this.Imgs[1]+"\" />";
		content += '</span>';
		iconNum++;
	}

	for (sc=onStars+1; sc < this.Msgs.length; sc++) {
		content += '<span class="unit" >';
		content += "<img id=\"" + this.obj +"_img" + iconNum + "\" src=\""+this.imgPath+this.Imgs[0]+"\" />";
		content += '</span>';
		iconNum++;
	}

	if (this.bcec[1]) content += "<img src=\""+this.imgPath+this.bcec[1]+"\" />";
	innerHTML(this.innerimg, content);
	this.update (this.currentValue);

	return true;

}


function innerHTML(object, msg) {
	object.innerHTML = msg;
}

function ltg_ratings_update(currentValue) 
{
	var starStep = 10.0 / (this.Msgs.length - 1);
	var onStars = Math.round (currentValue / starStep);
	var iconNum = 0;
	
	if (currentValue == -1) {
		this.msg.innerHTML = this.defaultMsg;
		onStars = -1;
	} else {
		content = this.Msgs[onStars];
		this.msg.innerHTML = content;
	}
	// Draw the Message
	   
	// Update Images
	for (sc=0; sc <= onStars; sc++) {
		var img = document.getElementById(this.obj +"_img" + iconNum);
		img.src = this.imgPath+this.Imgs[1];
		iconNum++;
	}
	for (sc=onStars+1; sc < this.Msgs.length; sc++) {
		var img = document.getElementById(this.obj +"_img" + iconNum);
		img.src = this.imgPath+this.Imgs[0];
		iconNum++;
	}
	return true;
}

// Update form
function ltg_ratings_mouseOver(obj, n)
{
   obj.update(n);
   return true;
}

function ltg_ratings_click(obj, n)
{
	// Show and set a new current Value
	obj.currentValue = n;
	
	// Set value to the form
	var form = document.forms[obj.MD5];
	form[obj.prefixID].value = n;
	return true;
}

function ltg_ratings_mouseout(e)
{
	this.obj.update(this.obj.currentValue);
	return true;
}

ltg_ratings_show.prototype.update = ltg_ratings_update;
ltg_ratings.prototype.update = ltg_ratings_update;
ltg_ratings.prototype.click = ltg_ratings_click;

