
var DarkStar = 'http://thebot.de/star-white16.png';
var NormalStar = 'http://thebot.de/star-gold16.png';
var HoverStar = 'http://thebot.de/star-blue16.png';

function jVote(parent, status, settings)
{
    // make images cached by browser (preload)
    tmp = new Image();
    tmp.src = DarkStar;
    tmp.src = NormalStar;
    tmp.src = HoverStar;

	this.parent = parent;
	this.status = status;
	this.settings = settings;

	this.locked = false;
	this.images = [];
	this.init();
	if (this.settings.locked)
	    this.lock();
}


jVote.prototype.init = function()
{
	var voteobj = this;

    voteobj.value = 0;
    if (voteobj.settings.num_votes > 0)
        voteobj.value = Math.round(voteobj.settings.sum_votes / voteobj.settings.num_votes);

	for (var i = 0, e = voteobj.settings.max; i < e; i++)
	{
		var image = document.createElement('img');
		voteobj.images[i] = image;
		image.label = voteobj.settings.labels[i];
		image.value = i + 1;
		image.alt = voteobj.settings.labels[i];
		image.style.cursor = 'pointer';

		image.onmouseover = function()
		{
			if (voteobj.locked)
				return;

			voteobj.hover(this);
			document.getElementById(voteobj.status).innerHTML = this.label;
		};

		image.onmouseout = function()
		{
			voteobj.paint();
		};

		image.onclick = function(evnt)
		{
			if (voteobj.locked)
				return;

			voteobj.settings.sum_votes += this.value;
			voteobj.settings.num_votes++;
            voteobj.value = Math.round(voteobj.settings.sum_votes / voteobj.settings.num_votes);
			voteobj.lock();

			var eEvent = evnt || window.event;
			if (voteobj.settings.click)
				voteobj.settings.click(eEvent, this.value);
		};

		document.getElementById(voteobj.parent).appendChild(image);
	}

	voteobj.paint();
}


jVote.prototype.hover = function(domImage)
{
    hover_pos = domImage.value;
    
	for (var i = 0; i < this.settings.max; i++)
	{
        if (this.images[i].value <= hover_pos)
            this.images[i].src = HoverStar;
        else if (this.images[i].value <= this.value)
            this.images[i].src = NormalStar;
        else
            this.images[i].src = DarkStar;
    }
}


jVote.prototype.paint = function()
{
	for (var i = 0; i < this.value; i++)
        this.images[i].src = NormalStar;

	for (var i = this.value; i < this.settings.max; i++)
        this.images[i].src = DarkStar;

    s = this.settings.num_votes + " Votes";
    if (this.locked)
        s += " (du hast abgestimmt)";

    document.getElementById(this.status).innerHTML = s;
}


jVote.prototype.lock = function()
{
	this.locked = true;
	this.paint();

	for (var i = 0; i < this.settings.max; i++)
        this.images[i].style.cursor = 'default';
}


jVote.prototype.unlock = function()
{
	this.locked = false;
	this.paint();

	for (var i = 0; i < this.settings.max; i++)
        this.images[i].style.cursor = 'pointer';
}


function loadXMLDoc(url)
{
    var req = false;
    
    // für Mozilla etc.
    if (window.XMLHttpRequest)
    {
        try
        {
            req = new XMLHttpRequest();
        }
        catch (e)
        {
            req = false;
        }
    }
    else if (window.ActiveXObject)
    {
        try
        {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                req = false;
            }
        }
    }
    
    if (req)
    {
        // falls Objekt erzeugt werden konnte…
        // req.onreadystatechange = handleReqChange;
        req.open("GET", url, true);
        req.send(null);
    }
}
