Window.onDomReady(function() {

    //
    // set up mouseover, mouseout events and ajax click event (to change the rating)
    //
  
    for( var i=0; i <= 5; i++ ) {

		if ( $('my_rate_' + i) ) {
			$('my_rate_' + i ).addEvent('mouseover', coming);
			$('my_rate_' + i ).addEvent('mouseout', going);
    
			$('my_rate_' + i).addEvent('click', function(e) {
				if (e && e.preventDefault) {
					e.preventDefault(); 
				}

				//
				// pull out the rate from the id
				//
				var id = this.id;
				id = id.charAt(id.length-1);
				id = parseInt(id);

				var l = window.location;
				var url = l.href.replace(/.[^\/]*\.html$/,'') + '/rate/' + id + '/?_a=1';

				//
				// update the average rating as well
				//
				new Ajax(url, {
				method: 'get',
				update: $('avg_rating')
				}).request();

				my_rating = id;
				
				return false;

			});
		}

	}

});

function coming(e) {
    if (e && e.preventDefault) {
        e.preventDefault(); 
    }
    
	var id = this.id;
	id = id.charAt(id.length-1);
	id = parseInt(id);

	var index = 1;
	while( index <= id ) {
		$('my_rate_img_' + index ).src = '/images/rating_on.gif';
		index = index + 1;
	}

	for( var jindex = index; jindex <= my_rating; jindex++ ) {
		$('my_rate_img_' + jindex ).src = '/images/rating_off.gif';
	}

    return false;
}


function going(e) {
    if (e && e.preventDefault) {
        e.preventDefault(); 
    }

	var id = this.id;
	id = id.charAt(id.length-1);
	id = parseInt(id);

	var index = 1;
	while( index <= id ) {
		$('my_rate_img_' + index ).src = '/images/rating_off.gif';
		index = index + 1;
	}

	for( var jindex = 1; jindex <= my_rating ; jindex++ ) {
		$('my_rate_img_' + jindex ).src = '/images/rating_on.gif';	
	}

    return false;
}

