var http = createRequestObject();
var displayRating = '';
var currentId = '';

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        ro = new XMLHttpRequest();
    }
    return ro;    
}



function updateRating(obj, rating) {
	var id = obj.title;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
	currentId = idName;

    http.open('get', 'http://www.munna.pl/ajax.php?id='+id+'&idName='+idName);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	if(http.readyState == 4){
        var response = http.responseText;
		if (response.indexOf('GLOS') > 0){
			alert("Już głosowałeś...");
		}
        displayRating = response.substr(0, 4);
        document.getElementById(currentId+'_showrating').innerHTML = 'ok!';
        totalRating = Math.ceil(response);
        var obj = document.getElementById(currentId+'_'+totalRating);
        changeend(obj, totalRating);
		displayStars(rating, currentId);
    }
}

function changeover(obj, rating) {
	
	var imageName = obj.src;
	var id = obj.title;
	var index = imageName.lastIndexOf('/');
	var filename = imageName.substring(index+1);
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;

	for(i=0; i<id; i++) {
		var num = i+1;
			document.getElementById(idName+'_'+num).src = 'http://www.munna.pl/1.gif';			

	}

}
function changeend(obj, rating) {
	
	var imageName = obj.src;
	var id = obj.title;
	var index = imageName.lastIndexOf('/');
	var filename = imageName.substring(index+1);
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
}
function changeout(obj, rating) {

	var imageName = obj.src;
	var id = obj.title;
	var index = imageName.lastIndexOf('/');
	var filename = imageName.substring(index+2);
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
	
	for(i=0; i<id; i++) {
		var num = i+1;
		if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = 'http://www.munna.pl/1.gif';			
			}
			else {
				document.getElementById(idName+'_'+num).src = 'http://www.munna.pl/0.gif';			
			}
	}
}

function displayStars(rating, idName) {
	
	for(i=0; i < 5; i++ ) {
			if(i < rating) {
				document.write('<img src="http://www.munna.pl/1.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
			else {
	document.write('<img src="http://www.munna.pl/0.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
	}


		



}

