//var g_clear = 'image/pomaranczowa.gif';
//var g_fill = 'image/szara.gif';
var g_clear = 'image/szara.gif';
var g_fill = 'image/pomaranczowa.gif';
var ile = '6';
		
var x = 17;
var y = 14;

function $( id ){
	return document.getElementById( id );
}

function generuj(file, rate)
{
	var rate = Math.round(rate);

	var iden = $( 'ocena' );
	docBody = $( 'ocena' );
	
	for( i = 0; i < ile; i++ )
	{
		element = document.createElement("img");
		element.id = i;
		docBody.appendChild(element);
		
		incre = i + 1;
		//docBody.innerHTML += '<img id="vote' + i + 'Image' + i + '" src="' + g_clear + '" onmouseover="setImages(' + i + ', ' + i + ')" onmouseout="setImages(' + i + ',0)" onclick="rateItem(' + i + ',' + i + ')" class="rateImage" alt="' + i + '" />';
		element.setAttribute("src", g_clear);
		element.setAttribute("width", x);
		element.setAttribute("height", y);
		element.setAttribute("name", incre);
		element.setAttribute("class", "rateImage");

		element.onclick=function () { vote(i, file); };
		element.onmouseover=function () { zmien(this.name); };
		$('ocena').onmouseout=function () { czysc(); };
	}
}

function zmien(nr)
{
	for(i = 0; i < nr; i++)
	{
		$(i).setAttribute("src", g_fill);	
	}
}

function czysc()
{
	for( i = 0; i < ile; i++ )
	{
		$(i).setAttribute("src", g_clear);	
	}
}

function vote(rate, file)
{
	advAJAX.post({
		url: 'rate.php',
		onSuccess: function(obj) {
			var result = obj.responseText.split('|');

			//var btns  = document.getElementById('filesStatus' + file_id);
			if (result[0] != '0')
			{
				var votes = document.getElementById('votes' + file).firstChild;
				votes.nodeValue = result[1]+'/6 ('+result[2]+' votes)';

				alert('Thank you for your vote');
			}
			else
			{
				alert('You have already voted');
			}	
		},
		onError: function(obj) { alert("Error: Nie moglem zmienic statusu"); },
		parameters : {
			file: file,
			rate: i
		}
	});
	
	return false;
}

