
var xml = null;

function vote_video(sid, id, vote) {
	if(window.XMLHttpRequest || window.ActiveXObject) {		
		if(vote >= 1 && vote <= 5) {
			if(window.XMLHttpRequest) {
				xml = new XMLHttpRequest();
			}
			else if(window.ActiveXObject) {
				xml = new ActiveXObject('Microsoft.XMLHTTP');
			}
		
			xml.onreadystatechange = function() {
				if(xml.readyState == 4 && xml.status == 200) {
					update_votes(vote);
				}
			}
			
			var boundary = 'cityfi69cityfi';
			
			var data  = '';
		
			data += '--'+boundary+'\r\n';
			data += 'Content-Disposition: form-data; name="sid"\r\n\r\n';
			data += escape(sid)+'\r\n';
			data += '--'+boundary+'--\r\n';

			data += '--'+boundary+'\r\n';
			data += 'Content-Disposition: form-data; name="id"\r\n\r\n';
			data += escape(id)+'\r\n';
			data += '--'+boundary+'--\r\n';
			
			data += '--'+boundary+'\r\n';
			data += 'Content-Disposition: form-data; name="vote"\r\n\r\n';
			data += escape(vote)+'\r\n';
			data += '--'+boundary+'--\r\n';
			
			data += '--'+boundary+'\r\n';
			data += 'Content-Disposition: form-data; name="ajax"\r\n\r\n';
			data += escape(1)+'\r\n';
			data += '--'+boundary+'--\r\n';

			xml.open('POST', '/antitv/vote.php');
			xml.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
			xml.send(data);
		}
	}
}

function update_votes(my_vote) {

	var video_vote = document.getElementById('video_vote');	
	var video_votes = document.getElementById('video_votes');	
	var video_votes_avg = document.getElementById('video_votes_avg');
	var video_vote_text = document.getElementById('video_vote_text');
	var keys = xml.responseXML.documentElement.getElementsByTagName('vote');
	
	var stars = '';
	var vote_count = '';
	var vote = '';

	
	video_votes_avg.innerHTML = '';
	video_vote.innerHTML = '';
	
	for(a=0; a < keys.length; a++) {
		
		var votes = keys[a].getAttribute('votes');
		var avg = keys[a].getAttribute('avg');
		
		for(x = 1; x <= avg; x++) {
			stars += '<img src="http://rasia.city.fi/icon/fam/star_red.png" alt="*" width="16" height="16" />';	
			var difference = Math.round((avg - x)*100)/100;
		}
		
		if(difference >= 0.25 && difference <= 0.75) {
			stars += '<img src="http://rasia.city.fi/icon/fam/star_red_half.png" alt="*" width="16" height="16" />';	
		} else if(difference > 0.75) {
			stars += '<img src="http://rasia.city.fi/icon/fam/star_red.png" alt="*" width="16" height="16" />';	
		}
		
		for(x = difference >= 0.25 ? x + 1 : x; x <= 5; x++) {
			stars += '<img src="http://rasia.city.fi/icon/fam/star_empty_disabled.png" alt="*" width="16" height="16" />';	
		}
		
		vote_count =  votes + ' ' + (votes == 1 ? 'arvostelu' : 'arvostelua');
	}
	
	if(stars.length > 1) {
		video_votes_avg.innerHTML = stars;
		video_votes.innerHTML = vote_count;
		
		for(x = 1; x <= 5; x++) {
			if(x <= my_vote) {
				vote += '<img src="http://rasia.city.fi/icon/fam/star.png" alt="*" width="16" height="16" />';
			} else {
				vote += '<img src="http://rasia.city.fi/icon/fam/star_empty_disabled.png" alt="" width="16" height="16" />';
			}
		}
		
		video_vote_text.innerHTML = 'Kiitos arvostelustasi!';
		video_vote.innerHTML = vote;
	}
}
