YAHOO.namespace( "ME" );
var me = YAHOO.ME;
me.GROWTH_FEEDBACK_SERVICE_LINK = unescape( getMetaValueOf( "gfsl" ) ).replace( /&amp;/g, "&" );
me.currentEvaluation = -1.0;
me.COEFFICIENT = 1.0 / 11.0;
me.stars = new Array();
me.stars[ 0 ] = document.getElementById( "star_1" );
me.stars[ 1 ] = document.getElementById( "star_2" );
me.stars[ 2 ] = document.getElementById( "star_3" );
me.stars[ 3 ] = document.getElementById( "star_4" );
me.stars[ 4 ] = document.getElementById( "star_5" );
me.evalComment = document.getElementById( "eval_comment" );

me.EVALUATE_SUCCESS_DEGREE_CALLBACK = {
	success: function( response ){		
		var root = response.responseXML.documentElement;
		var id = root.getElementsByTagName( "id" )[ 0 ].firstChild.nodeValue;
		me.currentEvaluation = root.getElementsByTagName( "evaluation" )[ 0 ].firstChild.nodeValue;
		
		for( var index = 1; index <= 5; index++ ){
			var star = me.stars[ index - 1 ];
			star.onmouseover = function(){};
			star.onmouseout = function(){};
			star.onclick = function(){};
			setStarImage( star, me.COEFFICIENT * (2 * index - 1) );
		}

		me.evalComment.innerHTML = "";
	},
	
	failure: function( response ){
		me.evalComment.innerHTML = "（エラー）";
	},
	
	timeout: 5000
};

function setStarImage( star, value ){
	if( me.currentEvaluation < value ){
		star.src = "img/star0_0.gif";
	}else if( me.currentEvaluation <= value + me.COEFFICIENT ){
		star.src = "img/star0_5.gif";
	}else{
		star.src = "img/star1_0.gif";
	}
}

function initStar( bulletinId ){
	var callback = {
		success: function( response ){	
			var root = response.responseXML.documentElement;
			var id = root.getElementsByTagName( "id" )[ 0 ].firstChild.nodeValue;
			me.currentEvaluation = root.getElementsByTagName( "evaluation" )[ 0 ].firstChild.nodeValue;

			for( var index = 1; index <= 5; index++ ){
				var star = me.stars[ index - 1 ];

				star.onmouseover = function(){
					var selected = this.id.split( "_" )[ 1 ];
					var index = 1;
					for( ; index <= selected; index++ ){
						me.stars[ index - 1 ].src = "img/star1_0.gif";
					}
					for( ; index <= 5; index++ ){
						me.stars[ index - 1 ].src = "img/star0_0.gif";
					}
				};

				star.onmouseout = function(){
					for( var index = 1; index <= 5; index++ ){
						var star = me.stars[ index - 1 ];
						setStarImage( star, me.COEFFICIENT * (2 * index - 1) );
					}
				};

				star.onclick = function(){
					var degree = this.id.split( "_" )[ 1 ] * 2 / 10.0;
					evaluate( bulletinId, degree );
				};

				setStarImage( star, me.COEFFICIENT * (2 * index - 1) );
			}
			
			if( me.currentEvaluation < 0 ){
				me.evalComment.innerHTML = "（未評価）";
			}else{
				me.evalComment.innerHTML = "";
			}
		},

		failure: function( response ){
			me.evalComment.innerHTML = "（エラー）";
			for( var index = 1; index <= 5; index++ ){
				var star = me.stars[ index - 1 ];
				star.onmouseover = function(){};
				star.onmouseout = function(){};
				star.onclick = function(){};
			}
		},

		timeout: 5000
	};
	
	var url = me.GROWTH_FEEDBACK_SERVICE_LINK.replace( "<:ID:>", bulletinId ).replace( "<:EVALUATION:>", -1.0 );
	YAHOO.util.Connect.asyncRequest( "POST", url, callback, null );
}

function evaluate( id, degree ){
	var url = me.GROWTH_FEEDBACK_SERVICE_LINK.replace( "<:ID:>", id ).replace( "<:EVALUATION:>", degree );
	YAHOO.util.Connect.asyncRequest( "POST", url, me.EVALUATE_SUCCESS_DEGREE_CALLBACK, null );
}

