star = {

	setCount: function(ol, starCount) {
		ol.select('li').each( function(li, index) {
			if (index+1<=starCount) {
				li.removeClassName('empty');
			} else {
				li.addClassName('empty');
			}
		});
	},

	// Supply a form element, containing 1 empty <ol> and 1 <select> element  
	create: function(form, allowRating) {
		if (!form) return false;
		var ol     = form.select('ol')[0];
		var popup  = form.select('select')[0];
		var submit = form.select('div.submit')[0];
		
		var li = '';
		popup.select('option').each( function(option) {
			li += '<li class="star" alt="' + option.value + '">' + option.value + '</li>';	
		});
		ol.innerHTML = li;
		
		// Copy the label TD value into it own alt attribute, for reset purposes
		var label = ol.parentNode.parentNode.parentNode.select('td.tutor-label')[0];
		label.writeAttribute('alt', label.innerHTML)
		
		if (allowRating) {
			ol.observe('click', function(event) {
				var clickedStar = event.findElement();
				if (clickedStar!=ol) star.clicked(ol, popup, form, clickedStar, label);
			});
		}
		
		ol.observe('mouseover', function(event) {
			var hoveredStar = event.findElement();
			if (hoveredStar!=ol) star.hovered(ol, hoveredStar, allowRating);
		});
		
		ol.observe('mouseout', function(event) { star.reset(ol);	});
		star.reset(ol);
		
		ol.addClassName('clickable');
		popup.hide();	
		submit.hide();		
	},
	
	destroy: function(ol) {
		ol.stopObserving('mouseover');
		ol.stopObserving('click');
		ol.stopObserving('mouseout');
		ol.removeClassName('clickable');
	},
	
	hovered: function(ol, hoveredStar, allowRating) {
		var hoveredStarNum = parseInt(hoveredStar.readAttribute('alt'));
		if (allowRating) {
			star.setCount(ol, hoveredStarNum);
			ol.parentNode.parentNode.parentNode.select('td.tutor-label')[0].innerHTML = 'Rate ' + hoveredStarNum + ' star' + (hoveredStarNum==1 ? '' : 's');
		} else {
			ol.parentNode.parentNode.parentNode.select('td.tutor-label')[0].innerHTML = 'You have already rated this user';
		}
	},
	
	clicked: function(ol, popup, form, clickedStar, label) {
		var clickedStarNum = parseInt(clickedStar.readAttribute('alt'));
		popup.selectedIndex = clickedStarNum-1;
		form.select('input[type=submit]')[0].click();
				
		// Calculate the new rating. Turned off because the behaviour seems unexpected (ie. click 5 stars, then displaying 1 star)
		/*
		var oldRating = parseInt(ol.readAttribute('alt'));
		var numTimesRated = parseInt(label.readAttribute('alt').match(/rated ([0-9]+) time/)[1]);
		var newRating = parseInt((oldRating+clickedStarNum)/(numTimesRated+1));
		ol.writeAttribute('alt', newRating);
		*/
		
		ol.writeAttribute('alt', clickedStarNum);
		
		label.writeAttribute('alt', 'You rated this tutor ' + clickedStarNum + '/5');
		
		star.reset(ol);
		star.destroy(ol);
	},
	
	reset: function(ol) {
		var actualRating = parseInt($('starRating').readAttribute('alt'));
		star.setCount(ol, actualRating);
		
		var label = ol.parentNode.parentNode.parentNode.select('td.tutor-label')[0];
		label.innerHTML = label.readAttribute('alt');
	}
};