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;
  createCookie(idName, '1');

    http.open('get', '/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("Sorry... Failed to update rating.");
		}
		else {
			//alert("Thanks for your time.");
		}
       
    displayRating = response.substr(0, 4);
    //document.getElementById(currentId+'_showrating').innerHTML = 'Rating: '+displayRating;
    document.getElementById('RatingText').innerHTML = 'Díky moc !';
    totalRating = Math.ceil(response);   
    var obj = document.getElementById(currentId+'_'+totalRating);
    changedef(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;
		
		if (num%2 == 0) {
			document.getElementById(idName+'_'+num).src = '/img/_even1.jpg';			
		}
		else {
			document.getElementById(idName+'_'+num).src = '/img/_odd1.jpg';
		}
	}
}

function changedef(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;
	
	var last = 0;

	for(i=0; i<id; i++) {
		var num = i+1;
		
		if (num%2 == 0) {
			document.getElementById(idName+'_'+num).src = '/img/__even1.jpg';	
		}
		else {
			document.getElementById(idName+'_'+num).src = '/img/__odd1.jpg';
		}
		
    document.getElementById(idName+'_'+num).onmouseover = null;
    document.getElementById(idName+'_'+num).onmouseout = null;
    document.getElementById(idName+'_'+num).onclick = null;
    
    last = i;
	}
	
	for(x = last; x < 10; x++) {
		var num = x+1;
		if (num%2 == 0) {
			document.getElementById(idName+'_'+num).src = '/img/even1.jpg';	
		}
		else {
			document.getElementById(idName+'_'+num).src = '/img/odd1.jpg';
		}
    document.getElementById(idName+'_'+num).onmouseover = null;
    document.getElementById(idName+'_'+num).onmouseout = null;
    document.getElementById(idName+'_'+num).onclick = null;
  }
	
}

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 = '/img/__even1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = '/img/even1.jpg';			
			}
		}
		else {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = '/img/__odd1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = '/img/odd1.jpg';			
			}
		}
	}
}

function displayStars(rating, idName) {

 // document.getElementById(currentId+'_showrating').innerHTML = 'Thank you';
  if(readCookie(idName) == null) {
  	for(i=0; i < 10; i++ ) {
  		if(i%2 ==0) {
  			if(i < rating) {
  				document.write('<img src="/img/__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="/img/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="/img/__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="/img/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
  			}
  		}
  	}
  } else {
  	
    for(i=0; i < 10; i++ ) {
  		if(i%2 ==0) {
  			if(i < rating) {
  				document.write('<img src="/img/__odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" />');
  			}
  			else {
  				document.write('<img src="/img/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" />');
  			}
  		}
  		else {
  			if(i < rating) {
  				document.write('<img src="/img/__even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" />');
  			}
  			else {
  				document.write('<img src="/img/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" />');
  			}
  		}
  	}
  	document.getElementById('RatingText').innerHTML = 'Hodnoceno !';
  	
  }
  

	if (displayRating == '') {
		document.write('<div class="ratingText" id="'+idName+'_showrating" >'+displayRating+'</div>');
	}
	else {
		document.write('<div class="ratingText" id="'+idName+'_showrating" >'+totalRating+'</div>');
	}

}

/************** test ****************/

function createCookie(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(10*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
