ratingWidget = {

	// *********************** Product rating properties ************************* //
	// Parent div ID for rating container
	ratingContainer: 'rate_product_js',
	// Hidden input field to store rating
	ratinginputID :'product_review_rating',
	// Images for active and inactive rating containers
	fullStarSRC : '/templates/images/star-full.gif',
	noStarSRC : '/templates/images/star-empty.gif',
	// Define array that stores rating text which appears next to stars
	// NOTE: To make this more efficient, consider using the stars alt text to generate the test displayed next to the image!
	ratingTextArray : new Array('I hate it','I don\'t like it','It\'s ok','I like it','I love it'),
	// *********************** Product rating properties ************************* //


  	init:function()
	{
		var container_js = document.getElementById(ratingWidget.ratingContainer);
		// If ratingContainer exists (were on product-review page)
		if(container_js)
		{
			// Remove hide style to show rating (we dont want to show the ratings for browsers that aint DOM 1 complient - it wont work!)
			helper.cssjs('remove', container_js, "hide_form");
			// Add the mouseover event behaviour to ratingContainer (displays active styles onmouseover)
			helper.addEvent(container_js, 'mouseover', ratingWidget.initialiseHover, false);
			// Check if a rating value exists (if the page has been posted and returned errors)
			var ratingContainer = document.getElementById(ratingWidget.ratinginputID);
			if (ratingContainer)
			{	
				// Store rating value in a variable rating
				ratingWidget.rating = ratingContainer.value;
				// Use function to set the rating
				ratingWidget.setRating();
			}	// Check to see if image container exists
		}
	},

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Product rating
	// Initialis hover function
	initialiseHover:function()
	{
		var container_js = document.getElementById(ratingWidget.ratingContainer);
		var img_element = container_js.getElementsByTagName('img');
		// Loop through images and assign the displayStar event
		for(var i=0; i<img_element.length; i++)
		{
			helper.addEvent(img_element[i],'mouseover',ratingWidget.displayStar,false);
		}
	},
	// Activate/deactive stars
	displayStar:function(e)
	{
		var target = helper.getTarget(e);		
		var container_js = document.getElementById(ratingWidget.ratingContainer);
		var img_element = container_js.getElementsByTagName('img');
		// Loop through rating images, setting the relevant img source
		// Get rating container (hidden form filed that stores the rating, ready for update)
		var ratingContainer = document.getElementById(ratingWidget.ratinginputID);
		
		for(var i=0; i<img_element.length; i++)
		{
			// To determine the current img element we use the attribute value (the img element has no id attribute)
			// If img is equal to star user is hovering on, set all stars above this image to noStarSRC and set i to length of img_element(Stops loop)
			if (img_element[i].alt == target.alt)
			{
				// Set other stars to noStarSRC image
				for(var j=i; j<img_element.length; j++)
				{
					img_element[j].setAttribute('src',ratingWidget.noStarSRC);
				}
				// Set hovered star
				target.setAttribute('src',ratingWidget.fullStarSRC);
				
				// Check if a p tag exists (stores the rating text)
				var para_element = container_js.getElementsByTagName('p');	
				if (!para_element.length)
				{
					var parElement=document.createElement('p');
					var text = document.createTextNode(ratingWidget.ratingTextArray[i]);
					parElement.appendChild(text);
					container_js.appendChild(parElement);
				}else
				{
					// Loop through paragraph node deleting children (deletes current text, ready for adding new stuff)
					while (para_element[0].firstChild) 
					{
						para_element[0].removeChild(para_element[0].firstChild);
					}
					var text = document.createTextNode(ratingWidget.ratingTextArray[i]);
					para_element[0].appendChild(text);
				}
				
				// Set hidden form field for rating to rating number
				ratingContainer.value=i+1;
				// Set i to img_element.length (stops loop)
				i = img_element.length;
			}else
			{
				// Set star to 'active' image
				img_element[i].setAttribute('src',ratingWidget.fullStarSRC);
			}
		}
	},

	// Activate rating stars on callback
	setRating:function()
	{	
		// Get parent
		var container_js = document.getElementById(ratingWidget.ratingContainer);
		// Get img elements
		var img_element = container_js.getElementsByTagName('img');	
		// Run loop setting the img src (loop argument is based on value of the forms ratinginputID (hidden field)
		for(var i=0; i<ratingWidget.rating; i++)
		{
			// Set star to 'active' image
			img_element[i].setAttribute('src',ratingWidget.fullStarSRC);
		}
		// Activate rating text
		var parElement=document.createElement('p');
		var text = document.createTextNode(ratingWidget.ratingTextArray[i-1]);
		parElement.appendChild(text);
		container_js.appendChild(parElement);
	}
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Product rating
}
// Initialise popup image function (for viewing large image popups)
helper.addEvent(window, 'load', ratingWidget.init, false);