//alert("rate.js")

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var rateMsgArr = new Array("Rate it:", "Boring!", "Eh...", "OK", "Good!", "Awesome!", "Thanks!");

var empty_star = new Image();
var full_star = new Image();
var half_star = new Image();
function LoadStars(star_path)
{
	empty_star.src = star_path + "empty_star.gif"; 
	full_star.src = star_path + "full_star.gif";
	half_star.src = star_path + "half_star.gif";
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var divs_counter = null;
var RateItemArr = new Array();

// CLASS RateItem
function RateItem(index, category, activity, ratingValue, numOfRates, ratingTotal)
{
	this.index = index;
	
	// info
	this.category = category;
	this.activity = activity;
	this.ratingValue = Math.round(ratingValue*2)/2;
	this.numOfRates = numOfRates;
	this.ratingTotal = ratingTotal;
	
	// params	
	this.stars_timer;
	this.isRate = false;
	
	// graphic objects
	
	// divs
	this.rating_div_id = "ratingDiv_" + index;
	this.stars_div_id = "starsDiv_" + index;
	
	// span
	this.rate_msg_id = "rateMsg_" +  index;
	this.num_of_rates_msg_id = "numOfRatesMsg_" +  index;
	
	// stat img
	this.stat_img_id = "statImg_" + index;	
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// MAIN FUNCTION
function BuildRatingDiv(category, activity, ratingValue, numOfRates, ratingTotal)
{
	//alert("in BuildRatingDiv()\n\n ratingValue="+ratingValue+"\nnumOfRates="+numOfRates+"\nratingTotal="+ratingTotal)
	//alert("divs_counter="+divs_counter);
	
	// set value of divs_counter
	(divs_counter==null) ? (divs_counter=0) : (divs_counter++);
	
	// update the array
	RateItemArr[divs_counter] = new RateItem(divs_counter, category, activity, ratingValue, numOfRates, ratingTotal);
	
	// create the graphic div item
	var rateBuffer = "";
	rateBuffer += "<div id='"+RateItemArr[divs_counter].rating_div_id+"' class='rating_div_style'>";
	rateBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=280 height=20>";
	rateBuffer += "<tr>";
	rateBuffer += "<td width=70 align=right valign=middle><span id='"+RateItemArr[divs_counter].rate_msg_id+"' class='rating_message_style'>"+rateMsgArr[0]+"</span></td>";
	rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=85 align=center valign=middle><div id='"+RateItemArr[divs_counter].stars_div_id+"' onmouseover='javascript:clearTimeout(RateItemArr[divs_counter].stars_timer)' onmouseout='javascript:delay("+divs_counter+")'>"+GetStarsDivBuffer(divs_counter)+"</div></td>";
	rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=115 align=left valign=middle NOWRAP><span id='"+RateItemArr[divs_counter].num_of_rates_msg_id+"' class='rating_message_style'>("+GetNeatNumber(numOfRates)+" ratings)</span></td>";
	rateBuffer += "</tr>";
	rateBuffer += "</table>";
	rateBuffer += "<img id='"+RateItemArr[divs_counter].stat_img_id+"' name='"+RateItemArr[divs_counter].stat_img_id+"' border=0 src='' width=0 height=0>";
	rateBuffer += "</div>";
	document.write(rateBuffer);
	
	// set the view stars by the rating info(1-5)
	SetStarsByRate(divs_counter);
}

function BuildRatingDisableDiv(category, activity, ratingValue, numOfRates, ratingTotal)
{
	// set value of divs_counter
	(divs_counter==null) ? (divs_counter=0) : (divs_counter++);
	
	// update the array
	RateItemArr[divs_counter] = new RateItem(divs_counter, category, activity, ratingValue, numOfRates, ratingTotal);
	
	// create the graphic div item
	var rateBuffer = "";
	rateBuffer += "<div id='"+RateItemArr[divs_counter].rating_div_id+"' class='rating_disabled_div_style'>";
	rateBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=205 height=20>";
	rateBuffer += "<tr>";
	//rateBuffer += "<td width=70 align=right valign=middle><span id='"+RateItemArr[divs_counter].rate_msg_id+"' class='rating_message_style'>"+rateMsgArr[0]+"</span></td>";
	//rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=85 align=center valign=middle><div id='"+RateItemArr[divs_counter].stars_div_id+"'>"+GetStarsDisableDivBuffer(divs_counter)+"</div></td>";
	rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=115 align=left valign=middle NOWRAP><span id='"+RateItemArr[divs_counter].num_of_rates_msg_id+"' class='rating_message_style'>("+GetNeatNumber(numOfRates)+" ratings)</span></td>";
	rateBuffer += "</tr>";
	rateBuffer += "</table>";
	//rateBuffer += "<img id='"+RateItemArr[divs_counter].stat_img_id+"' name='"+RateItemArr[divs_counter].stat_img_id+"' border=0 src='' width=0 height=0>";
	rateBuffer += "</div>";
	document.write(rateBuffer);
	
	// set the view stars by the rating info(1-5)
	//SetStarsByRate(divs_counter);
}

function LoadRatingDisableDiv(category, activity, ratingValue, numOfRates, ratingTotal)
{
	// load div info
	document.getElementById("myRateDiv").innerHTML = GetRatingDisableDiv(category, activity, ratingValue, numOfRates, ratingTotal);
}

function GetRatingDisableDiv(category, activity, ratingValue, numOfRates, ratingTotal)
{
	// set value of divs_counter
	(divs_counter==null) ? (divs_counter=0) : (divs_counter++);
	
	// update the array
	RateItemArr[divs_counter] = new RateItem(divs_counter, category, activity, ratingValue, numOfRates, ratingTotal);
	
	// create the graphic div item
	var rateBuffer = "";
	rateBuffer += "<div id='"+RateItemArr[divs_counter].rating_div_id+"' class='rating_disabled_div_style'>";
	rateBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=205 height=20>";
	rateBuffer += "<tr>";
	//rateBuffer += "<td width=70 align=right valign=middle><span id='"+RateItemArr[divs_counter].rate_msg_id+"' class='rating_message_style'>"+rateMsgArr[0]+"</span></td>";
	//rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=85 align=center valign=middle><div id='"+RateItemArr[divs_counter].stars_div_id+"'>"+GetStarsDisableDivBuffer(divs_counter)+"</div></td>";
	rateBuffer += "<td width=5></td>";
	rateBuffer += "<td width=115 align=left valign=middle NOWRAP><span id='"+RateItemArr[divs_counter].num_of_rates_msg_id+"' class='rating_message_style'>("+GetNeatNumber(numOfRates)+" ratings)</span></td>";
	rateBuffer += "</tr>";
	rateBuffer += "</table>";
	//rateBuffer += "<img id='"+RateItemArr[divs_counter].stat_img_id+"' name='"+RateItemArr[divs_counter].stat_img_id+"' border=0 src='' width=0 height=0>";
	rateBuffer += "</div>";
	//document.write(rateBuffer);
	return (rateBuffer);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// return 5 stars images
function GetStarsDivBuffer(div_index)
{
		var star_id;
		var starBuffer = "";
		starBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=85>";
		starBuffer += "<tr>";
		var i, star_id;
		for(i=1; i<6; i++)
		{
			star_id = "star_" + div_index + "_" + i;
			starBuffer += "<td><a href='javascript:OnStarClick("+div_index+","+i+")'><img id='"+star_id+"' name='"+star_id+"' border=0 src='' onmouseover='javascript:OnStarOver("+div_index+","+i+")' onmouseout='javascript:OnStarOut()'></a></td>";
		}
		starBuffer += "</tr>";
		starBuffer += "</table>";

		return starBuffer;
}

function GetStarsDisableDivBuffer(div_index)
{
	var star_id, counter=1;
	var starBuffer = "";
	starBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=85>";
	starBuffer += "<tr>";
	var i, star_id;
	for(i=1; i<6; i++)
	{
		star_id = "star_" + div_index + "_" + i;
		star_src = "";
		// NEW
		if(i <= RateItemArr[div_index].ratingValue)
		{
			star_src = full_star.src;
			counter++;
		}
		else
		{
			if ((counter === 5) && (RateItemArr[div_index].ratingValue.toString().indexOf(".") != -1))
			{
				star_src = half_star.src;
			}
			else
			{
				star_src = empty_star.src;
			}
			
		}
		// END_NEW
		starBuffer += "<td><img id='"+star_id+"' name='"+star_id+"' border=0 src='"+star_src+"'></td>";
	}
	starBuffer += "</tr>";
	starBuffer += "</table>";

	return starBuffer;
}


// set the view stars by the rating info(1-5)
function SetStarsByRate(div_index)
{
	var i, counter=1,  star_id;
	for(i=1; i<6; i++)
	{
		star_id = "star_" + div_index + "_" + i;
		if(i <= RateItemArr[div_index].ratingValue)
		{
			document.getElementById(star_id).src = full_star.src;
			counter++;
		}
		else
		{
			document.getElementById(star_id).src = empty_star.src;
		}
		
	}

	if(RateItemArr[div_index].ratingValue.toString().indexOf(".") != -1)
	{
		star_id = "star_" + div_index + "_" + counter;
		document.getElementById(star_id).src = half_star.src;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// functions for the stars events 
function OnStarClick(div_index,star_index)
{
	// update the flag of rating
	RateItemArr[div_index].isRate = true;
	
	var starBuffer = "";
	starBuffer += "<table border=0 cellpadding=0 cellspacing=0 width=85>";
	starBuffer += "<tr>";
	
	var i, star_id;
	for(i=1; i<6; i++)
	{
		star_id = "star_" + div_index + "_" + i;
		starBuffer += "<td><img id='"+star_id+"' name='"+star_id+"' border=0 src=''></td>";
	}
	starBuffer += "</tr>";
	starBuffer += "</table>";
	document.getElementById(RateItemArr[div_index].stars_div_id).innerHTML = starBuffer;
	
	// update ratingValue and numOfRates
	RateItemArr[div_index].numOfRates++;
	//alert("numOfRates = " + RateItemArr[div_index].numOfRates)
	
	//alert("ratingTotal 1= " + RateItemArr[div_index].ratingTotal)
	RateItemArr[div_index].ratingTotal = parseInt(RateItemArr[div_index].ratingTotal)
	RateItemArr[div_index].ratingTotal += star_index;
	//alert("ratingTotal 2= " + RateItemArr[div_index].ratingTotal)
	
	RateItemArr[div_index].ratingValue = parseInt(RateItemArr[div_index].ratingValue)
	RateItemArr[div_index].ratingValue = (RateItemArr[div_index].ratingTotal / RateItemArr[div_index].numOfRates);
	//alert("New Avrg 1 = " + RateItemArr[div_index].ratingValue)
	RateItemArr[div_index].ratingValue = Math.round(RateItemArr[div_index].ratingValue*2)/2;
	//alert("New Avrg 2 = " + RateItemArr[div_index].ratingValue)
	
	// set the view stars by the rating info(1-5)
	SetStarsByRate(div_index);
	
	// update the text on the left
	document.getElementById(RateItemArr[div_index].rate_msg_id).innerHTML = rateMsgArr[rateMsgArr.length-1];
	
	// update num of rates info
	var int_numOfRates = GetNeatNumber(parseInt(RateItemArr[div_index].numOfRates));
	//alert(int_numOfRates)
	document.getElementById(RateItemArr[div_index].num_of_rates_msg_id).innerHTML = "(" + int_numOfRates + " ratings)";
	
	// send the rate...
	var rate_link = "http://www.girlsense.com/tools/ranking/index.pl?action=rate&category="+RateItemArr[div_index].category+"&activity="+RateItemArr[div_index].activity+"&rating=" + star_index;
	document.getElementById(RateItemArr[div_index].stat_img_id).src = rate_link;
	
}
function OnStarOver(div_index,star_index)
{
	var i, star_id;
	for(i=1; i<6; i++)
	{
		star_id = "star_" + div_index + "_" + i;
		
		if(i <= star_index)
		{
			document.getElementById(star_id).src = full_star.src;
			document.getElementById(RateItemArr[div_index].rate_msg_id).innerHTML = rateMsgArr[star_index];
		}
		else
		{
			document.getElementById(star_id).src = empty_star.src;
		}
	}
}

function OnStarOut()
{
	return;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// functions for the inner stars div

function delay(div_index)
{
	RateItemArr[div_index].stars_timer = window.setTimeout( "OnStarsDivOut("+div_index+")", 200 );
}

function OnStarsDivOut(div_index)
{
	if(RateItemArr[div_index].isRate == false)
	{
		document.getElementById(RateItemArr[div_index].stars_div_id).innerHTML = GetStarsDivBuffer(div_index);
		
		// set the view stars by the rating info(1-5)
		SetStarsByRate(div_index);
		
		document.getElementById(RateItemArr[div_index].rate_msg_id).innerHTML = rateMsgArr[0];
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function GetNeatNumber(theNumber)
{
	if(theNumber > 999)
	{
		theNumber = "" + theNumber;
		theNumberNeat = "";
		digitPointer = theNumber.length
		while(digitPointer-3>0)
		{
			digitPointer = digitPointer-3;
			theNumberNeat = theNumber.substr(digitPointer,3) + theNumberNeat;
			if(digitPointer>3)
				theNumberNeat = ","  + theNumberNeat;		
			if(digitPointer<=3)
				theNumberNeat = theNumber.substr(0,digitPointer) + ","  + theNumberNeat;		
		}
		return(theNumberNeat)
	}	
	
	return(theNumber)
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
