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', 'ajaxvote/ajax.php?id='+id+'&idName='+idName);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	if(http.readyState == 4){
        var response = http.responseText;
	
		if (response == 'ERROR'){
			alert("Hiba történt az adatátvitel során..");
		}
		else if (response == 'ALREADY'){
			alert("Már szavaztál!");
		}
		else if (response == 'NOTLOGGEDIN'){
			alert("Először jelentkezz be!");
		}
		else {
			alert("Szavazatodat rögzítettük");
		    
			var vote = Math.ceil(response);
			document.getElementById(currentId+'_szerinted').innerHTML = '';
			document.getElementById(currentId+'_votes').innerHTML = '';
			document.getElementById(currentId+'_resp').innerHTML = 'Köszönjük';
			}
        totalRating = Math.ceil(response);   
        var obj = document.getElementById(currentId+'_'+totalRating);
        changeover(obj, totalRating);
    }
}

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;
		
		if (num%2 == 0) {
			document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/_even1.jpg';			
		}
		else {
			document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/_odd1.jpg';
		}
	}

}

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 (num%2 == 0) {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/__even1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/even1.jpg';			
			}
		}
		else {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/__odd1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = 'http://rocker.hu/ajaxvote/odd1.jpg';			
			}
		}
	}
}

function displayStars(rating, idName,alr,votes) {

	document.write('<em>');
	document.write('<font color="black">Szerintetek ennyire jó: </font><br />');
	for(i=0; i < 10; i++ ) {
		if(i%2 ==0) {
			if(i < rating) {
				document.write('<img src="http://rocker.hu/ajaxvote/__odd1.jpg" 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://rocker.hu/ajaxvote/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
		}
		else {
			if(i < rating) {
				document.write('<img src="http://rocker.hu/ajaxvote/__even1.jpg" 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://rocker.hu/ajaxvote/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
		}
	}
	
	if(votes)
	{    
	    document.write('<br /><em class="ratingText" id="'+idName+'_votes" ></em>');
	    document.getElementById(idName+'_votes').innerHTML = '('+votes+' szavazat)';
	}
	document.write('<br /><em class="ratingText" id="'+idName+'_resp" ></em>');
	if(alr == "1")
	{
	    document.write('<br /><em class="ratingText">Már szavaztál!</em>');
	}
	else
	{
	    document.write('<br /><em class="ratingText" id="'+idName+'_szerinted" ></em>');
	    document.getElementById(idName+'_szerinted').innerHTML = '<font style="color:black;font-weight:bold;">Szerinted?!<font>';
	}
	
	
}