var Rating = Class.create( );
Rating.prototype = {
	initialize: function ( song_short_name, user_id ) {
		this.song_short_name = song_short_name;
		this.user_id = user_id;
		new RatingStar( 1, this );
		new RatingStar( 2, this );
		new RatingStar( 3, this );
		new RatingStar( 4, this);
		new RatingStar( 5, this);
		document.write( '<div style="clear:both;"></div>' );
	},
	set: function ( value ) {
		this.current = value;
	},
	toggleDevilSelector: function ( ) {
		if ( $( 'rating-' + this.song_short_name + '-6' ) ) {
			if ( 6 == this.current ) {
				this.hideDevilSelector( );
			} else {
				this.showDevilSelector( );
			}
		}
	},
	showDevilSelector: function ( ) {
		$( 'rating-' + this.song_short_name + '-6' ).show( );
// 		$( 'devil-separator-' + this.song_short_name ).show( );
	},
	hideDevilSelector: function ( ) {
		$( 'rating-' + this.song_short_name + '-6' ).hide( );
// 		$( 'devil-separator-' + this.song_short_name ).hide( );
	},
	song_short_name: null,
	current: 0,
	user_id: 0
}

var MusicRating = Class.create( Rating, { 
	initialize: function ( song_short_name, user_id, score ) {
		this.song_short_name = song_short_name;
		this.user_id = user_id;
		this.current = score;
		new RatingStar( 1, this );
		new RatingStar( 2, this );
		new RatingStar( 3, this );
		new RatingStar( 4, this );
		new RatingStar( 5, this );
// 		document.write( '<div id="devil-separator-' + song_short_name + '" class="inline floatleft">|</div>' );
		new RatingStar( 6, this );
		document.write( '<div style="clear:both;"></div>' );
	}
} );

var StaticRating = Class.create( );
StaticRating.prototype = {
	initialize: function ( score ) {
		for ( var i = 1; i <= 5; ++i )
		{
			if ( i <= score )
				document.write( '<a class="dots-static-active"><span>image</span></a>' );
			else
				document.write( '<a class="dots-static"><span>image</span></a>' );
		}
		document.write( '<div style="clear:both;"></div>' );
	}
}

var RatingStar = Class.create( );
RatingStar.prototype = {
	initialize: function ( index, rating_object ) {
		var is_devils = ( 6 == index );
		var user_id = rating_object.user_id;
		var song_short_name = rating_object.song_short_name;
		var id = 'rating-' + song_short_name + '-' + index;

		if ( ( 6 == rating_object.current && index < 6 ) || 6 == index )
			var which_class = 'dots-select-devils';
		else
			var which_class = ( index <= rating_object.current ) ? 'dots-select-active' : '';

		document.write( '<a class="dots-select ' + which_class + '" id="' + id + '"><span>image</span></a>' );

		if ( 6 == rating_object.current && 6 == index ) {
			$( 'rating-' + song_short_name + '-6' ).hide( );
			$( 'devil-separator-' + song_short_name ).hide( );
		}

		$( id ).onmouseover = function ( ) {
			for ( var i = 1; i <= 5; ++i ) {
				if ( !is_devils )
				{
					$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-devils' ).removeClassName( 'dots-select-active' );
					if ( i <= index ) {
						$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-active' );
					} else {
						$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-active' );
					}

				}
				else
					$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-devils' );
			}

			rating_object.toggleDevilSelector( );
		};
		$( id ).onmouseout = function ( ) {
			for ( var i = 1; i <= 5; ++i ) {
				if ( $( 'rating-' + song_short_name + '-' + i ) )
				{
					if ( 6 == rating_object.current ) {
						$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-devils' );
					} else {
						$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-devils' );
						if ( i <= rating_object.current ) {
							$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-active' );
						} else {
							$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-active' );
						}
					}
				}
// 				if ( 6 != rating_object.current )
// 					$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-devils' );
			}

			rating_object.toggleDevilSelector( );
		};
		$( id ).onclick = function ( ) {
			rating_object.set( index );
			new Ajax.Request( '/site/addSongRating_Ajax', { 
				method: 'post', 
				parameters: { song_short_name: song_short_name, user_id: user_id, value: index }, 
				onComplete: function ( response ) { $( 'notifier' ).update( response.responseText ); } 
			} );

			for ( var i = 1; i <= 5; ++i ) {
				if ( $( 'rating-' + song_short_name + '-' + i ) )
					$( 'rating-' + song_short_name + '-' + i ).removeClassName( 'dots-select-active' ).removeClassName( 'dots-select-devils' );
				if ( i <= index )
				{
					if ( !is_devils )
						$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-active' );
					else
						$( 'rating-' + song_short_name + '-' + i ).addClassName( 'dots-select-devils' );
				}
			}

			rating_object.toggleDevilSelector( );
		};
	}
}
