/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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;
var RateItemArr = new Array();

// CLASS RateItem
function RateItem(index, nickname, ratingValue, numOfRates, ratingTotal)
{
	this.index = index;
	
	// info
	this.nickname = nickname;
	//this.ratingValue = Math.round(ratingValue*2)/2;
	this.ratingValue = ratingValue; 
	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(nickname, ratingValue, numOfRates, ratingTotal,slideId)
// id = Slide's ID 
//function BuildRatingDiv(category, activity, ratingValue, numOfRates, ratingTotal)
{
	//alert("in BuildRatingDiv()\n\nratingValue="+ratingValue+"\nnumOfRates="+numOfRates+"\nratingTotal="+ratingTotal)
	
	// set value of divs_counter
	//(divs_counter==null) ? (divs_counter=0) : (divs_counter++);
	divs_counter = 0;
	
	// update the array
	RateItemArr[divs_counter] = new RateItem(divs_counter, nickname,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=1 width=110>";
	rateBuffer += "<tr><td width=110 align=center valign=middle><span id='"+RateItemArr[divs_counter].rate_msg_id+"' class='rating_message_style_title'>"+rateMsgArr[0]+"</span></td></tr>";
	rateBuffer += "<tr><td width=110 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,slideId)+"</div></td></tr>";
	rateBuffer += "<tr><td width=110 align=center valign=middle NOWRAP><span id='"+RateItemArr[divs_counter].num_of_rates_msg_id+"' class='rating_message_style_votes'>"+GetNeatNumber(numOfRates)+" ratings</span></td></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);
	return rateBuffer;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// return 5 stars images
function GetStarsDivBuffer(div_index,slideId)
{
		var star_id;
		var starBuffer = "";
		starBuffer += "<table border=0 cellpadding=0 cellspacing=2 width=85>";
		starBuffer += "<tr>";
		var i, star_id;
		for(i=1; i<6; i++)
		{
			star_id = "star_" + div_index + "_" + i;
			//alert("init, SetStarsByRate, index = " + div_index);
			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;
}

// set the view stars by the rating info(1-5)
function SetStarsByRate(div_index)
{
	//alert("SetStarsByRate, index = " + div_index);
	
	var i, counter=1,  star_id;
	//alert(RateItemArr[div_index].ratingValue);
	for(i=1; i<6; i++)
	{
		star_id = "star_" + div_index + "_" + i;
		//alert("star_id="+star_id)
		if(i <= RateItemArr[div_index].ratingValue)
		{
			document.getElementById(star_id).src = full_star.src;
			counter++;
		}
		else
		{
			//alert('true');
			document.getElementById(star_id).src = empty_star.src;
		}
		
	}

	if(RateItemArr[div_index].ratingValue.toString().indexOf(".") != -1)
	{
		//alert('half');
		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 slideId = playingSlideId;
	
	var starBuffer = "";
	starBuffer += "<table border=0 cellpadding=0 cellspacing=2 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);
	var rateIndex4DB = (star_index*2)  // The db we save the rating from 1 to 10.
	RateItemArr[div_index].ratingTotal = RateItemArr[div_index].ratingTotal + rateIndex4DB;
	
	//RateItemArr[div_index].ratingValue = parseInt(RateItemArr[div_index].ratingValue);
	RateItemArr[div_index].ratingValue =  (Math.round(RateItemArr[div_index].ratingTotal/RateItemArr[div_index].numOfRates)/2);
	//alert("New Avrg 1 = " + RateItemArr[div_index].ratingValue)
		
	// set the view stars by the rating info(1-5)
	SetStarsByRate(0);
	
	// 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";
	
	//updates the Array (client side) to show the correct voting in the list
	var currentRatingIndex = playingFlashIndex;
	updateArray(currentRatingIndex,RateItemArr[div_index].ratingTotal,RateItemArr[div_index].numOfRates,RateItemArr[div_index].ratingValue);
	
	// update DB
	var rate_link = "http://www.girlsense.com/premium/slideShows/index.pl?action=addRating&id=" + slideId + "&rating=" + rateIndex4DB;	
	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)
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
