//Copyright (c) 2008 Lewis Linn White Jr.
//Author: Lewis Linn White Jr.

function jVote(parent, locked, settings)
{
	this.locked = locked;
	this.images = [];
	this.settings = settings;
	this.parent = parent;
	this.init();
}

jVote.prototype.init = function()
{
	var that = this;
	var div = document.getElementById( 'vote' );
	var counter = document.createElement("div");
	counter.setAttribute("class", "numbers");
	var count = 0;
	for(var i = 0, e = this.settings.max; i < e; i++)
	{
		count = i+1;
		var image = document.createElement('img');
		var span = document.createElement('span');
		this.images[i] = image;
//		image.value = this.settings.labels[i];
		image.value = count;
		image.alt = this.settings.labels[i];
		image.title = this.settings.title[i];
		image.style.cursor = 'pointer';
		span.innerHTML = count;
		image.onmouseover = function()
		{
			if(that.locked)
			{
				return;
			}
			that.set( this , true );
		};
		image.onmouseout = function ()
		{
			that.reset( current );
		};
		image.onclick = function(evnt)
		{
			if(that.locked)
			{
				return;
			}
			var eEvent = evnt || window.event;
			if(that.settings.click)
			{
				that.settings.click(eEvent, this.value);
			}
		};
		document.getElementById(this.parent).appendChild(image);
		counter.appendChild( span );
	}
	document.getElementById(this.parent).appendChild(counter);
	this.set( this.images[this.settings.min-1] , false );
};

jVote.prototype.set = function(domImage,mouseover)
{
	if( domImage.value == 1 && !mouseover ) {
		domImage.src = url + 'images/grayStar.gif';
	} else {
		domImage.src = url + 'images/greenStar.gif';
	}
	var next = domImage.nextSibling;
	while(next)
	{
		next.off = true;
		next.src = url + 'images/grayStar.gif';
		next = next.nextSibling;
	}
	var prev = domImage.previousSibling;
	while(prev)
	{
		prev.off = false;
		prev.src = url + 'images/greenStar.gif';
		prev = prev.previousSibling;
	}
};

jVote.prototype.reset = function(num)
{
	if(this.locked)
	{
		return;
	}
	var index = (num) ? num : this.settings.min;
	this.set(this.images[index-1]);
};

jVote.prototype.lock = function()
{
	this.locked = true;
};

jVote.prototype.unLock = function()
{
	this.locked = false;
};