$(document).ready(function(){ 
	goFromStart();
});

function goFromStart() {	
	id = $("#voteObjectId").attr("value");
	type = $("#voteType").attr("value");
	getVote( type, "rateRez", id );
}

function justCantVote() {
	$("#rateRez").html("");
	removeStars( "#starContainer" );
}

function goFromMiddle() {
	$("#rateRez").html("");
	removeStars( "#starContainer" );
	id = $("#voteObjectId").attr("value");
	type = $("#voteType").attr("value");
	getVote( type, "rateRez", id );
}

function getVote ( type, into, id ) {	
	$.post("/act/getVote.php", { type : type, id : id }, 
		function(data, textStatus) {
			if (textStatus=='success') {				
				$("#"+into).html(data['title']);
				drawStars( "#starContainer", data['avg'] );				
				launchVotingTech();			
			}
			
		}, "json");
	return false;
}

function launchVotingTech() {
	$('.auto-submit-star').rating({ 
		cancel: 'Panaikinti balsą',
		callback: function(value, link) {
			$("#rateRez").html("");
			removeStars( "#starContainer" );
			idas = $("#voteObjectId").attr("value");
			type = $("#voteType").attr("value");
			$("#rateRez").load("/act/newVote.php", { valueInt : value, id : idas, type : type } );
			getVote( type, "rateRez", idas );
		}
	});
}

function removeStars(id) {	
	$(id).html("");
}

function addStarLine(value, current) {
	action = $("#voteAction").attr("value");
	if (!action){
		qs = "<input style=\"display: none\" class=\"auto-submit-star\" name=\"rateSak\" type=\"radio\" value=\""+value+"\" ";
	} else {
		qs = "<input style=\"display: none\" class=\"auto-submit-star\" name=\"rateSak\" type=\"radio\" disabled=\"disabled\" value=\""+value+"\" ";		
	}
	if (current==value) {
		qs += "checked=\"checked\" ";
	}
	qs += "/>";
	return qs;
}

function drawStars(id,current) {
	if (!current) {
		current = 0;
	}

	qs = "";
	for (a=1;a<6;a++) {
		qs += addStarLine(a, current);
	}

	$(id).html(qs);
}
