/* rating */

// StringBuffer object
/*function StringBuffer() {
	this.buffer = "";
	this.append = new Function("str", "this.buffer += str;");
	this.getString = new Function("return this.buffer");
}*/

var yourRating = 0;
var postRatedCount = 0;
var ratingContainer = "";

/**
 * volá se při loadu stránky, zjistí, zda uživatel může hlasovat a podle toho připraví hlasovací odkazy
 **/
function onRatingStatusResult(ratingStatus) {
	if (typeof(ratingStatus) != "object") return;

	var canVote = ratingStatus.allowed;
	var postRating;
	for (var i = 0; i < ratingStatus.ratingResults.length; i++) {
		var result = ratingStatus.ratingResults[i];
		postRating = result.rating;
		postRatedCount = result.totalVotes;
	}
	$(document).ready(function() {
		writeRatingControls(canVote, postRating, postRatedCount);
	});
}

/**
 * samotné zahlasování
 **/
function rate(rating, category, idArticle) {
	// započtení hlasu k článku
	yourRating = rating;
	//$("#"+ratingContainer).html("");
	$("#"+ratingContainer).css( { 'background-position' : '0px -84px' } );
	var contents = [{name:category, identifier:idArticle}];
	RatingService.rate(contents, rating, onRatingResult);
	$(".rate").prepend("<strong>Děkujeme za hodnocení</strong>");
}

/**
 * zavolá se po odhlasování (poté, co proběhne funkce rate(). Zpracování výsledků, zákaz dalšího hlasování
 **/
function onRatingResult(ratings){
	if (typeof(ratings) != "object") return;
	var canVote = false;
	var postRating;

	for (var i = 0; i < ratings.length; i++) {
		var rating = ratings[i];
		var postRating = rating.rating;
		var postRatedCount = rating.totalVotes;
		var postId = rating.ratedType.identifier;
	}
	$(".rate img").animate({
				width: postRating*20 + "%"
	}, 500);
	$("#podcastitem-"+postId+" a[rel]").attr("rel", parseInt(postRating*20));
	writeRatingControls(canVote, postRating, postRatedCount);
	$("#"+ratingContainer).css( { 'background-position' : '0px 0px' } );
//	$("#rateResult").addClass("infos");
//	$("#rateResult").text("Děkujeme za hlas. Vaše hodnocení je "+yourRating+", celkový průměr je "+Math.round(postRating)+".");
//	$("#rateResult").show();
}

function initRating(strIdentificator, idArticle) {
	//$("#multimedia-content .fr").css( { "visibility" : "hidden"} );
	var sb = new StringBuffer();

	//sb.append('<div class="rating">');
	//sb.append('	<h3>Hodnocení</h3>');
	//sb.append('	<p>Líbí se Vám tento projekt? Máte možnost ocenit jeho kvalitu.</p>');
	//sb.append('	<strong>Ohodnoť</strong>');
	sb.append('	<div class="rate">');
	sb.append('		<img width="0" height="14" src="/img/u/spacer.gif" alt=""/>');
	sb.append('		<a href="#1" class="rate1" title="Udělit 1 hvězdičku" onclick="rate(1, \''+strIdentificator+'\', \''+idArticle+'\'); return false;"><span>1/5</span></a><span> | </span>');
	sb.append('		<a href="#2" class="rate2" title="Udělit 2 hvězdičky" onclick="rate(2, \''+strIdentificator+'\', \''+idArticle+'\'); return false;"><span>2/5</span></a><span> | </span>');
	sb.append('		<a href="#3" class="rate3" title="Udělit 3 hvězdičky" onclick="rate(3, \''+strIdentificator+'\', \''+idArticle+'\'); return false;"><span>3/5</span></a><span> | </span>');
	sb.append('		<a href="#4" class="rate4" title="Udělit 4 hvězdičky" onclick="rate(4, \''+strIdentificator+'\', \''+idArticle+'\'); return false;"><span>4/5</span></a><span> | </span>');
	sb.append('		<a href="#5" class="rate5" title="Udělit 5 hvězdiček" onclick="rate(5, \''+strIdentificator+'\', \''+idArticle+'\'); return false;"><span>5/5</span></a>');
	sb.append('	</div>');
	//sb.append('	<p id="rateResult"></p>');
	//sb.append('</div>');
	$(document).ready(function() {
		//$("#content #left").append(sb.getString());
		$(".rate").html(sb.getString());
		ratingContainer = "rate";
		$("."+ratingContainer+" a").each( function() {
			$(this).hover(
				function() { var no = this.className.substr(4); $("."+ratingContainer).css( { 'background-position' : '0px ' + (-14*no) + 'px' } ); $("#"+ratingContainer+" img").hide() },
				function() { $("#"+ratingContainer).css( { 'background-position' : '0px 0px' } ); $("#"+ratingContainer+" img").show() }
			);
		});
	});

}

function writeRatingControls(boolCanVote, intPostRating, intPostRatedCount) {
	if (!boolCanVote) {
		// nastaveni bgr hvezdicek na aktualni stav
		/*var tmp = -21*Math.round(intPostRating*1);
		$("#rateArticle").css( { 'background-position' :'0px '+tmp+'px' } );*/
		$(".rate img").animate({
			width: intPostRating * 20 + "%"
		},500).addClass("deact");
		$(".rate span").remove();
	} else {
		$(".rate img").each( function() {
			$(this).hover(
				function() { $(this).not(".deact").hide() },
				function() { $(this).not(".deact").show() }
			);
		});
		$(".rate img").animate({
			width: (intPostRating || 0) * 20+"%"
		},500);
		$(".rate").css( { "visibility" : "visible"} );
	}
}

