/**
 * @author Walter Wimberly
 * @version	0.8
 * @copyright 2007
 */

/**
 * @type	int
 * <p>Default value to tell us how high the rating system can go.  Common
 * numbers are 5 or 10.</p>
 */
var MAX_RATING = 10;
/**
 * @type	String
 * <p>Default value to define the path to the strings as needs</p>
 */
var IMG_PATH = './images/ratings/';
/**
 * @type	String
 * <p>Default value to define the file extensions (.gif by default)</p>
 */
var RATING_EXT = '.gif';
/**
 * @type	boolean
 * <p>Default value to determin if the "empty" rating images should be 
 * shown or not. i.e. if the value of 4 out of 5 stars, then should the 
 * 5th star be shown.</p>
 */
var SHOW_EMPTY = true;

var SHOW_HOVER = true;


/**
 * <p>Initialize the ratings, that way we can check each ratting 
 * section to see if there are any divs which contain ratings so 
 * we can swap out the numbers with the images</p>
 * @param	{Function} clickFunction	The function which should be  
 * attached to the click event of the rating system.
 * @param	{Associative Array}	settings	An array of values that 
 * can be configure the rating system when it is loaded.
 * @param {String} settings.path	The path to the images, if this needs to be 
 * implicitly set.  If it is not passed, then the default is used. 
 * This is part of the settings parameter. If it is not passed, then the default
 * 'images/ratings/' is used.
 * @param {int} settings.max	The maximum number of stars/smily faces/ etc 
 * that can be used in this rating system.  If it is not passed,  
 * then the default of ten (10)is used.
 * @param {String} settings.ext	What extension is the file using.  this 
 * should include the '.'.  If it is not passed, then the default 
 * (.gif) is used.
 * @param {boolean} settings.showEmpty	Used to determin if the rating scale past 
 * the default values are allowed to be shown.
 */
function initRating(settings, clickFunction) {
	
	// alert('in init rating');
	
	// set the function if one is not passed
	if(!clickFunction) {
		clickFunction = clicked;
	}
	
	
	if(settings) {
		// set the global values if they are passed
		IMG_PATH = settings.path ? settings.path : IMG_PATH;
		MAX_RATING = settings.max ? settings.max : MAX_RATING;
		RATING_EXT = settings.ext ? settings.ext : RATING_EXT;
		SHOW_EMPTY = settings.showEmpty ? settings.showEmpty : SHOW_EMPTY;
		if( settings.showEmpty === false ) {
			SHOW_EMPTY = false;
			SHOW_HOVER = false;
		}
		if( settings.showHover === false ) {
			SHOW_HOVER = false;
		}
		
	}
	
	var ratings = document.getElementById("releaseInfo").getElementsByTagName('span');
	
	// alert(ratings + ' ' + ratings.length);
	
	for (var i = 0; i < ratings.length; i++) {
		
		// if the class isn't rating, we don't care about this div...
		if (!ratings[i].className || ratings[i].className.substring(0,6) != 'rating') {
			continue;
		}

		// alert(ratings[i].className);
		
		if(ratings[i].className == 'rating' || ratings[i].className.substring(0,7) == 'rating ') {
		} else {
			continue;
		}
		
		// alert('found a rating ' + ratings[i].id); 
		
		// get what the rating is for this div
		var rating = parseFloat(ratings[i].firstChild.nodeValue);
		ratings[i].removeChild(ratings[i].firstChild);
		
		// create the images for the rating
		for (var j = 0; j < MAX_RATING; j++) {
			var elem = document.createElement('img');
			
			// determine which rating to show
			if (rating >= 1) {
				elem.setAttribute('src', IMG_PATH + 'rating_on' + RATING_EXT);
				elem.setAttribute('class', 'on');
				rating--;
			}
			else if(rating >= 0.5) {
				elem.setAttribute('src', IMG_PATH + 'rating_half' + RATING_EXT);
				elem.setAttribute('class', 'half');
				rating = 0;
			}
			else {
				elem.setAttribute('src', IMG_PATH + 'rating_off' + RATING_EXT);
				elem.setAttribute('class', 'off');
			}
			
			if(!SHOW_EMPTY && rating < 0.5 ) {
				j = MAX_RATING + 1;
			}
			
			// get the name of the id, minus the 'rating_' prefix
			var widgetId = ratings[i].getAttribute('id').substr(7);
			elem.setAttribute('id', 'rating_' + widgetId + '_' + j);

			
			
			// add the rating to the DOM
			ratings[i].appendChild(elem);
			
			// if the user has 'voted' for this one already - 
			// no need for the roll over effects
			if(ratings[i].className.substring(7) != 'selected' && SHOW_HOVER) {
				// set the rollover effect
				myAttachEvent(elem, 'mouseover',ratingDisplayHover );
				myAttachEvent(elem, 'mouseout',ratingDisplayNormal );
				// set the click event so we can pass info via AJAX to store that info
				myAttachEvent(elem, 'click',clickFunction );
			}
			

		} 
	}
}

/**
 * <p>Attached a function to an event for a given object.  Checks to make 
 * sure it can be done for different browsers.</p>
 * <p>This presents a unified method of adding the function as the event 
 * handler, and abstracts this level so the same code is not repeated over and over.</p>
 * @private
 * @param {DOMObject} obj
 * @param {Event} evt	The event (usually onblur) which caused this function to be called.
 * @param {Function} func
 * @type Boolean	
 * @return returns if the event was successfully added to the handler 
 * or not
 */
function myAttachEvent(obj,evt,func){
	var ret = false;
	if (obj.addEventListener){
		obj.addEventListener(evt,func,false);
		ret = true;
	} else if (obj.attachEvent) { 
		// ret = obj.attachEvent("on"+evt,func+'(\'' + obj.getAttribute('id') +'\', '+j+')');
		ret = obj.attachEvent("on"+evt,func);
	}
	return ret;
}

/**
 * <p>The create a simple object with no methods for retrieving and 
 * storing the values of the rating system to use in the methods 
 * that are attached to the images.</p>
 * 
 * @param {Object} evtSrc	The element source of the target if using 
 * Firefox or Opera, otherwise it would be the event object for IE.
 */
function EventAttributes(evtSrc) {
	var elemId;
	this.rating = 0;
	this.ratingId = '';
	
	try {
		elemId = event.srcElement.getAttribute('id');
		event.cancelBubble=true;
	} 
	catch(exception) {
		// didn't like the try - so you must be a non-IE browser
		elemId = evtSrc.getAttribute('id');
	}
	
	uPos = elemId.lastIndexOf('_');
	
	
	this.rating = parseInt(elemId.substring(uPos + 1), 10);
	this.ratingId = elemId.substring(0, uPos);
	
}

/**
 * <p>This function is the return to normal part of the rollover 
 * function for the rating system allowing an image to go back
 * to its normal state.</p>
 */
function ratingDisplayNormal() {
	var ea = new EventAttributes(this);
	
	for (var i = 0; i <= ea.rating; i++) {
		var id_name = ea.ratingId+'_'+i;
		var elem = document.getElementById(id_name);
		var status = elem.getAttribute('class');
		elem.setAttribute('src', IMG_PATH + 'rating_' + status + RATING_EXT);
	}
}

/**
 * <p>The hover function for the images so roll overs can be seen.</p>
 */
function ratingDisplayHover() {
	var ea = new EventAttributes(this);
	for (var i = 0; i <= ea.rating; i++) {
		var id_name = ea.ratingId+'_'+i;
		var elem = document.getElementById(id_name);
		elem.setAttribute('src', IMG_PATH + 'rating_over' + RATING_EXT);
	}
	
}

/**
 * <p>This is a 'throw-away' method.  It can be replaced by another 
 * method, or it can call the other method so it gets set when a
 * user clicks on a rating system.</p>
 * 
 * <p>By being aware of this, custom options can be made available.</p>
 */
function clicked() {
	var ea = new EventAttributes(this);
	
	// alert('clicked: ' + ea.ratingId + ':' + (ea.rating + 1));
	// this function can call another function if preferred.
	// or it can be called from another file for specific handling
}

