var xmlDoc;
var totalStars;
var ratingOpen = false;

function getStats(obj)
{
	getXMLDoc($(obj).parent().parent().children("input").val());
	totalStars = $(xmlDoc).children("totalvotes").text();
	statToAppend = "<table summary='Rating Statistics'>";
	statToAppend += "<caption>"+totalStars+" Reviews</caption>";
	
	$(xmlDoc).children("ratings").children().each(function(i){
		statToAppend += "<tr><th>"+ (parseInt(i+1)) +" stars:</th><td class='bar'><div style='width:"+ Math.round((parseInt($(this).text())/totalStars)*100) +"%'>"+hasValue(parseInt($(this).text()))+"</div></td><td>("+parseInt($(this).text())+")</td></tr>";
	});
	
	statToAppend += "</table>";
	
	$(obj).parent().parent().children(".ratingStats").html(statToAppend);	
}

function getXMLDoc(xmlText){
	
	//alert(xmlText);
	if (window.ActiveXObject)
	{
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(xmlText);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser=new DOMParser();
		var doc=parser.parseFromString(xmlText,"text/xml");
	}
	
	xmlDoc=doc.documentElement;
}

function updateStars(moduleID)
{
	getXMLDoc($("#"+moduleID).val());
	totalStars = $(xmlDoc).children("totalvotes").text();
	$("#"+moduleID).parent().children(".rating").children(".totalVotes").html("("+totalStars+")");
	value = String(Math.round($(xmlDoc).children("averagerating").text()*100)/100);
	srcValue = 1;
	if (value.length > 1)
	{
		decValue = value.split(".")[1];
		if (decValue.length == 1) {
			decValue = decValue * 10;
		}
		if (decValue > 0 && decValue < 25)
		{
			srcValue =  Math.floor(value);
		}
		if ((decValue >= 25 && decValue <= 50) || (decValue > 50 && decValue < 75))
		{
			srcValue =  Math.floor(value) + String(5);
		}
		if (decValue >= 75)
		{
			srcValue =  Math.ceil(value)
		}
	}
	else
	{
		srcValue =  value;
	}
	//alert(value);
	
	$('#'+moduleID).parent().children(".rating").children("a").children(".rateAvg").attr("src",imageContextPath+"stars"+srcValue+".gif");
		
}

function updateModules()
{
	$(".rateAvg").each(function(i){
		getXMLDoc($(this).parent().parent().parent().children("input").val());
		totalStars = $(xmlDoc).children("totalvotes").text();
		$(this).parent().next("span").html("("+totalStars+")");
		value = String(Math.round($(xmlDoc).children("averagerating").text()*100)/100);
		srcValue = 1;
		if (value.length > 1)
		{
			decValue = value.split(".")[1];
			if (decValue.length == 1) {
				decValue = decValue * 10;
			}
			if (decValue > 0 && decValue < 25)
			{
				srcValue =  Math.floor(value);
			}
			if ((decValue >= 25 && decValue <= 50) || (decValue > 50 && decValue < 75))
			{
				srcValue =  Math.floor(value) + String(5);
			}
			if (decValue >= 75)
			{
				srcValue =  Math.ceil(value)
			}
		}
		else
		{
			srcValue =  value;
		}
		
		
		$(this).attr("src",imageContextPath+"stars"+srcValue+".gif");
		
	});
}

function hasValue(value)
{
	if (value != 0)
	{
		return "&#160;"
	}
	else
	{
		return "";
	}
}

function ratingInit()
{
	$("a").filter(".rateThis").hover(function(){
		getStats($(this));
		$(this).parent().parent().css("overflow-x","visible");
		$(this).parent().parent().css("overflow-y","visible");
		$(this).parent().parent().children(".ratingStats").show();
	},function(){
		$(this).parent().parent().children(".ratingStats").hide();
	});
	
	$("a").filter(".NOrateThis").hover(function(){
		getStats($(this));
		$(this).parent().parent().css("overflow-x","visible");
		$(this).parent().parent().css("overflow-y","visible");
		$(this).parent().parent().children(".ratingStats").show();
	},function(){
		$(this).parent().parent().children(".ratingStats").hide();
	});
	
	$("area").filter(".ratehover").mouseover(function(){
		$(this).parent().prev().attr("src", imageContextPath+"stars"+$(this).attr("title")+".gif");
	});
	
	$("area").filter(".ratehover").click(function(){
		$("body").unbind( "click" );
		$("div").filter(".ratingSet").each(function(i){
			if ($(this).css("display") == "block")
			{
				$(this).children().hide();
				$(this).append("<span><b>Thank you for your vote.</b></span>");
		    	setTimeout(function(){
		    		$("div").filter(".ratingSet").each(function(i){
		    			if ($(this).css("display") == "block")
		    			{
		    				$(this).fadeOut("normal",function(){$(this).children().show();$(this).children("span").hide();});
		    				
		    			}
		    		});
		    	},2000);
			}
		});	
		// use this function loaded in browselearning.aspx ---
		if(window.updateUserRating)
		{
		    updateUserRating($(this).parent().parent().parent().parent().children("input").attr("id").split("_")[1],$(this).attr("title"));
		}	
		//doRating(
		//    pluginContextPath + 'rateCourse.action', $(this).parent().parent().parent().parent().children("input").attr("id").split("_")[1], $("#userGUID").val(), $(this).attr("title")
		//);
		
		
	});
	
	$("a").filter(".rateThis").click(function(){
	    if(ratingOpen){clearRating();}
	    ratingOpen=true;
		$(this).parent().parent().children(".ratingSet").fadeIn("normal", function(){
			$("body").bind("click", function(){
				$("body").unbind( "click" );
				$("div").filter(".ratingSet").each(function(i){
					if ($(this).css("display") == "block")
					{
					    ratingOpen=false;
						$(this).fadeOut("normal");
					}
				});				
			});			
		});
	});
    // hide rating areas in dlaModule getRatingHTMLForUser ---	
	//$(".ratingStats").hide();
	//$(".ratingSet").hide();
	updateModules();
	
}

function clearRating()
{
	$("body").unbind( "click" );
	$("div").filter(".ratingSet").each(function(i){
		if ($(this).css("display") == "block")
		{
		    ratingOpen=false;
			$(this).fadeOut("normal");
		}
	});
}


//$(document).ready(ratingInit);
//window.onload = render;
//function render() {
//var foo = setTimeout("ratingInit()",50);
//}
