/*##########################################################################
# About:			/hp/js: object_voting								   #
# Written by:		Beepworld GmbH             							   #
# Created:			09/17/2007											   #
# Last modified:  	04/04/2008											   #
############################################################################

/* control star size for old code */
if (typeof(bwv_star_size) == "undefined") {
	bwv_star_size = "normal";
}

function bwv_set_star(id, unique)
{
	if (window["_bwv_rate_flag_" + unique] == 1) {
		var star_grey = template_files + "/images/apps/object_voting/star_grey_" + bwv_star_size + ".png";
		var star_green = template_files + "/images/apps/object_voting/star_green_" + bwv_star_size + ".png";

		for (var i = 1; i <= 5; i++)
		{
			if (id >= i) {
				document.getElementById("bwv_star_" + i + "_" + unique).src = star_green;
			} else {
				document.getElementById("bwv_star_" + i + "_" + unique).src = star_grey;
			}	
		}
		if (id > 1) document.getElementById("bwv_rate_" + unique).innerHTML = parse_phrase(bwv_phraselist[6], "points", id);	//"Gib " + id + " Punkte ab";
		else document.getElementById("bwv_rate_" + unique).innerHTML = parse_phrase(bwv_phraselist[5], "point", id);			//"Gib " + id + " Punkt ab";
		
		document.getElementById("bwv_rate_" + unique).style.display = "inline";
		document.getElementById("bwv_rating_details_" + unique).style.display = "none";
	}
}

function bwv_set_stars_back(unique)
{							
	var star_grey = template_files + "/images/apps/object_voting/star_grey_" + bwv_star_size + ".png";
	var star_yellow = template_files + "/images/apps/object_voting/star_yellow_" + bwv_star_size + ".png";
	var star_yellow_half = template_files + "/images/apps/object_voting/star_yellow_half_" + bwv_star_size + ".png";
	
	// translate to integer/float;
	window["_bwv_rating_" + unique] *= 1;
	
	for (var i = 1; i <= 5; i++)
	{
		if (window["_bwv_rating_" + unique] >= i) {
			document.getElementById("bwv_star_" + i + "_" + unique).src = star_yellow;
		} else {
			
			if ((window["_bwv_rating_" + unique] - i + 1) >= 0.5) {
			
				// HALF STAR			
				document.getElementById("bwv_star_" + i + "_" + unique).src = star_yellow_half;
			
			} else {
				
				// EMPTY STAR			
				document.getElementById("bwv_star_" + i + "_" + unique).src = star_grey;
			}
		}
	}	
	bwv_set_votes(unique);			
	document.getElementById("bwv_rating_" + unique).innerHTML = window["_bwv_rating_" + unique].toFixed(1);
	document.getElementById("bwv_rate_" + unique).style.display = "none";
	document.getElementById("bwv_rating_details_" + unique).style.display = "inline";	
}

function bwv_set_votes(unique)
{
	// set output of votes
	if (window["_bwv_votes_" + unique] == 1) {
		document.getElementById("bwv_votes_" + unique).innerHTML = window["_bwv_votes_" + unique] + " " + bwv_phraselist[1];
	} else {
		document.getElementById("bwv_votes_" + unique).innerHTML = window["_bwv_votes_" + unique] + " " + bwv_phraselist[2];
	}

	if (window["_bwv_rate_flag_" + unique] == 1) {
		document.getElementById("bwv_rating_description_" + unique).innerHTML = bwv_phraselist[4];
	}
	if (window["_bwv_rate_flag_" + unique] == 0) {
		document.getElementById("bwv_rating_description_" + unique).innerHTML = parse_phrase(bwv_phraselist[3], "rating_value", window["_bwv_rating_value_" + unique]);				
	}
}

// ####################################
// parse
function parse_phrase(raw_phrase, search_value, replace_value) 
{ 
	search_value = 'x' + 'x' + 'x' + search_value + 'x' + 'x' + 'x'; 
	var length_search_value = search_value.length; 
	var length_replace_value = replace_value.length; 
	var Pos = raw_phrase.indexOf(search_value, 0); 

	while (Pos >= 0) 
	{ 
		raw_phrase = raw_phrase.substring(0, Pos) + replace_value + raw_phrase.substring(Pos + length_search_value); 
		Pos = raw_phrase.indexOf(search_value, Pos + length_replace_value); 
	} 
	return raw_phrase; 
}

// INIT ON START
bwv_set_votes(_bwv_unique);
