﻿
/*================================================================
 
    JavaScript routines written by Peter Deppe
 
================================================================== */

var _userRatingClassName = 'fstUserRating';
var _averageRatingClassName = 'fstAvgRating';
var NUMBER_OF_STARS = 5;

// star image prefix
var AVG_STAR_PREFIX = 'sm_spark_';
var USER_STAR_PREFIX = 'lg_spark_';

// average rating stars
var AVG_STAR_ON = AVG_STAR_PREFIX + "on.gif";
var AVG_STAR_OVER = AVG_STAR_PREFIX + "over.gif";
var AVG_STAR_OFF = AVG_STAR_PREFIX + "off.gif";
var AVG_STAR_HALF = AVG_STAR_PREFIX + "half.gif";

// user rating stars
var USER_STAR_ON = USER_STAR_PREFIX + "on.gif";
var USER_STAR_OVER = USER_STAR_PREFIX + "over.gif";
var USER_STAR_OFF = USER_STAR_PREFIX + "off.gif";
var USER_STAR_HALF = USER_STAR_PREFIX + "half.gif";


/*---------------------------------------------------------------
name        : fst_RatingsInit()
description : setup the user ratings
----------------------------------------------------------------- */
function fst_RatingsInit()
{
    if (DEBUG == true) { alert('fst_RatingsInit'); }
	
	fst_InitUserRating();
	
	fst_InitAverageRatings();
}

/*---------------------------------------------------------------
name        : fst_InitUserRating2()
description : 
----------------------------------------------------------------- */
function fst_InitUserRating()
{    
    if (DEBUG == true) { alert('fst_InitUserRating'); }

	$$('span.fstUserRating').each(function(n)
			{
				fst_GetUserRating(n.id);
			});
}

/*---------------------------------------------------------------
name        : fst_InitAverageRatings()
description : initialize the average rating setup
----------------------------------------------------------------- */
function fst_InitAverageRatings()
{
    if (DEBUG == true) { alert('fst_InitAverageRatings'); }
    
	var avgratings = document.getElementsByTagName('span');
	
	//now loop thru and grab the rating widgets
	for (var i = 0; i < avgratings.length; i++)
	{
		// if it's not a rating div simply ignore it
		if (avgratings[i].className != _averageRatingClassName)
			continue;
	    	    
		// grab the rating within the div tag of the current div item
		var rating = avgratings[i].firstChild.nodeValue;
		
		// remove the value from the div
		avgratings[i].removeChild(avgratings[i].firstChild);
						
		// either round down or set it to x.5
		var tempRating = Math.floor(rating);
		var rem = rating % 1;
		
		if(rem >= 0.5)
			rating = tempRating + 0.5;
		else
			rating = tempRating;
				
		// form a loop to generate the stars
		for (var j = 0; j < NUMBER_OF_STARS; j++)
		{
			var star = document.createElement('img');
			
			if (rating >= 1)
			{
				star.setAttribute('src', STAR_IMAGE_PATH + AVG_STAR_ON);
				rating--;
			}
			else if(rating == 0.5)
			{
				star.setAttribute('src', STAR_IMAGE_PATH + AVG_STAR_HALF);
				rating = 0;
			}
			else
			{
				star.setAttribute('src', STAR_IMAGE_PATH + AVG_STAR_OFF);
			}
						
			// append the star
			star.className = 'star';
			avgratings[i].appendChild(star);
		}
	}
}

/*---------------------------------------------------------------
name        : fst_StarHover()
description : when hovering over the widget show the 'on' state on each star
----------------------------------------------------------------- */
function fst_StarHover(itemID, star, type)
{
	for (var i = 0; i <= star; i++)
	{
		document.getElementById('star_' + type + '_' + itemID + '_' + i).setAttribute('src', STAR_IMAGE_PATH + USER_STAR_OVER);
	}
}

/*---------------------------------------------------------------
name        : fst_StarNormal()
description : take care of the mouse off by restoring the old star state
----------------------------------------------------------------- */
function fst_StarNormal(itemID, star, type)
{
	for (var i = 0; i <= star; i++)
	{
		var status = document.getElementById('star_' + type + '_' + itemID + '_' + i).className;
		document.getElementById('star_' + type + '_' + itemID + '_' + i).setAttribute('src', STAR_IMAGE_PATH + USER_STAR_PREFIX + status + '.gif');
	}
}

/*---------------------------------------------------------------
name        : fst_UserRatingClickAction()
description : the action to be fired when a star rating is clicked
----------------------------------------------------------------- */
function fst_UserRatingClickAction(evt)
{
	var obj = Event.element(evt);
		
	// extract the items from the id
	var tmp = obj.id.substring(5); // leaves article_1772_1
	var itemType = tmp.substring(0, tmp.indexOf('_'));
	
	// strip the type
	var tmp2 = tmp.substring(tmp.indexOf('_') + 1);
	
	// now get the ID and rating
	var itemID = tmp2.substring(0, tmp2.indexOf('_'));
	var rating = tmp2.substring(tmp2.indexOf('_')+1);
	
	if(DEBUG) { alert('Item: ' + itemID + ', type: ' + itemType + ', rating: ' + rating); }
	
	// increment the rating as it's zero based
	rating ++;
	
	// fire off an event to set the rating
	new Ajax.Request(URL_ADDRATING,
	{
	  method: 'post',
	  parameters: { id: itemID, type: itemType, r: rating },
	  onSuccess: function(transport)
		{
			fst_ProcessUserRatingClick(transport.responseText)
		},
	  onFailure: function()
		{
			alert('Error submitting the rating');
		}
	});		
}

/*---------------------------------------------------------------
name        : fst_ProcessUserRatingClick()
description : does the necessary actions once the user rating has been processed
----------------------------------------------------------------- */
function fst_ProcessUserRatingClick(retVal)
{
    if(DEBUG) { alert(retVal); }
    
	if(retVal == 'success')
		fst_RefreshPage();
	else
	{
		alert('error - unable to submit rating');
	}	
}

/*---------------------------------------------------------------
name        : fst_GetUserRating()
description : 
----------------------------------------------------------------- */
function fst_GetUserRating(id)
{
    if (DEBUG == true) { alert('fst_GetUserRating'); }

    // remove 'urating_' from the id
	var temp = id.substring(8);
	
	// extract the ID and type
	var itemType = temp.substring(0, temp.indexOf('_'));
	var itemID = temp.substring(temp.indexOf('_')+1);
	
	if(DEBUG) { alert('Item: ' + itemID + ', type: ' + itemType); }
	
	// go get the users rating for this item (using post so it doesn't cache the response)
	new Ajax.Request(URL_GETUSERRATING,
	{
	  method: 'post',
	  parameters: { id: itemID, type: itemType },
	  onSuccess: function(transport)
		{
			fst_SetUserRating(itemID, itemType, transport.responseText)
		},
	  onFailure: function()
		{
			alert('Error retrieving user rating');
		}
	});	
}

/*---------------------------------------------------------------
name        : fst_SetUserRating()
description : 
----------------------------------------------------------------- */
function fst_SetUserRating(itemID, itemType, rating)
{
	if(DEBUG)
	{
	    alert('fst_SetUserRating - itemID: ' + itemID + ', type: ' + itemType + ', rating: ' + rating);
	}
	
	var ratingID = 'urating_' + itemType + '_' + itemID;
	
	// get a reference to the item in question
	var item = $(ratingID);
		
	// set the star value
	if(rating != 'error')
	{		
		if (rating > NUMBER_OF_STARS || rating < 0)
		{
			alert('fst_SetUserRating : error 1');
		}
				
		// add the stars
		for (var j = 0; j < NUMBER_OF_STARS; j++)
		{	
			var star = document.createElement('img');
				
			if (rating >= 1)
			{
				star.setAttribute('src', STAR_IMAGE_PATH + USER_STAR_ON);
				star.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				star.setAttribute('src', STAR_IMAGE_PATH + USER_STAR_HALF);
				star.className = 'half';
				rating = 0;
			}
			else
			{
				star.setAttribute('src', STAR_IMAGE_PATH + USER_STAR_OFF);
				star.className = 'off';
			}
						
			// configure the attributes of the star and add to the widget
			star.setAttribute('id', 'star_' + itemType + '_' + itemID + '_' + j);
			star.onmouseover = new Function("evt", "fst_StarHover(" + itemID + ", " + j + ", '" + itemType + "');");
			star.onmouseout = new Function("evt", "fst_StarNormal(" + itemID + ", " + j + ", '" + itemType + "');");
			
			// add the star to the item
			item.appendChild(star);
		}		
	}
	else
	{
		alert('fst_SetUserRating : error 2');
	}
	
	// now observe the click actions for each star
	$(ratingID).immediateDescendants().each(function(c)
	{
		Event.observe(c, 'click', fst_UserRatingClickAction);
	});
}


