/*
	Comments and Ratings
	
	At the moment, ratings are stored by IP address.  There is no love for mega-proxies with that.  It could also be done with a session id, but I elected to stick with the ip address for the time being.
*/
(function(){
	var g_img_base = "/Graphics/star_";
	var g_img_star_empty = g_img_base + "empty.jpg";
	var g_img_star_hover = g_img_base + "hover.jpg";
	var g_img_star_100 = g_img_base + "100.jpg";
	var g_img_star_75 = g_img_base + "75.jpg";
	var g_img_star_50 = g_img_base + "50.jpg";
	var g_img_star_25 = g_img_base + "25.jpg";

	var g_user_rated = false;
	var g_user_rating = 0;

	var g_user_rating_request = {
		s:0,
		w:0,
		i:0,
		hil:function(){
			g_user_rating_request.i = null;
		}
	};

	Hemi.message.service.subscribe("space_service_initialized", function (s, v){
	//function InitializeRating(){
		if(
			typeof g_valid_content_id == "boolean"
			&&
			g_valid_content_id == true
			&&
			typeof g_rating_average == "number"
			&&
			typeof g_rate_low == "number"
			&&
			typeof g_rate_high == "number"
			&&
			typeof document.getElementById != "undefined"
		){

			var oMeta = document.getElementById("oRatingsContainer");
			if(oMeta != null){
				var oP = document.createElement("p");
				var oSpan = document.createElement("span");
				oP.appendChild(oSpan);
				oSpan.className = "metadatanote";
				oSpan.appendChild(document.createTextNode("User Rating: (" + (g_rating_average > 0 ? g_rating_average : "Not Rated") + ") "));
				var oImg;

				for(var i = g_rate_low; i <= g_rate_high; i++){
					oImg = document.createElement("img");
					oImg.setAttribute("height","20px");
					oImg.setAttribute("width","20px");
					oImg.setAttribute("src",g_img_star_empty);
					oImg.setAttribute("index",i);
					oImg.setAttribute("id","oStar_" + i);

					oSpan.appendChild(oImg);
					//oImg.src = g_image_cache[0].src;
					
					Hemi.event.addEventListener(oImg,"mouseover",handle_image_over);
					Hemi.event.addEventListener(oImg,"mouseout",handle_image_out);
					Hemi.event.addEventListener(oImg,"click",handle_image_click);
				}
				oMeta.appendChild(oP);
				
				setRatings();

			}

		}
	});

	function rateContent(index){
		g_user_rated = true;
		g_user_rating = index;
		
		var d = g_user_rating_request;
		d.i = new Image();
		d.i.onload = d.hil;
		d.i.onabort = d.hil;
		d.i.onerror = d.hil;
		d.i.src = g_add_rating_url + "&" + g_rating_query + "=" + index;
		
	}

	function handle_image_out(e){
		setRatings();
	}

	function handle_image_over(e){
		var oImg = Hemi.event.getEventSource(e);
		var index = 0;
		
		if(oImg != null){
			index = parseInt(oImg.getAttribute("index"));
			if(isNaN(index)) index = 0;
		}
		highlightStars(index);
		
	}

	function handle_image_click(e){
		var oImg = Hemi.event.getEventSource(e);
		var index = 0;
		
		if(oImg != null){
			index = parseInt(oImg.getAttribute("index"));
			if(isNaN(index)) index = 0;
		}
		if(index >= g_rate_low && index <= g_rate_high){
			rateContent(index);
		}
		else{
			alert("Unspecified error");
		}
		
	}

	function highlightStars(index){
		var oImg = null;
		for(var i = g_rate_low; i <= g_rate_high; i++){
			oImg = document.getElementById("oStar_" + i);
			if(i <= index){
				oImg.src = g_img_star_hover;
			}
			else{
				oImg.src = g_img_star_empty;
			}
		}
	}

	function setRatings(){
		var oImg = null;

		if(g_user_rated == true){
			highlightStars(g_user_rating);
			return;
		}
		
		if(g_rating_average == 0){
			highlightStars(0);
			return;
		}
		
		var whole_rating = Math.floor(g_rating_average);
		var fraction = g_rating_average - whole_rating;
		
		for(var i = g_rate_low; i <= g_rate_high; i++){
			oImg = document.getElementById("oStar_" + i);
			if(g_rating_average >= i){
				oImg.src = g_img_star_100;
			}
			else if(fraction > 0 && fraction < 1 && (whole_rating + 1) == i){
				if(fraction < .50) oImg.src = g_img_star_25;
				else if(fraction < .75) oImg.src = g_img_star_50;
				else oImg.src = g_img_star_75;
			}
			else{
				oImg.src = g_img_star_empty;
			}
		}
	}
}());