
var ENTRY_RATING_URI               = 'EntryRatingUri_';

//Ajax
//保存
function star_saveEntryRating(param, pos)
{
    //cookieをチェック
    if(checkRatingDone()){
      showInfo("既に評価済みです");
    }else {
      var url = '/SaveEntryRating.blog?' + param + '&rating=' + pos;
      var draftAjax = new Ajax.Request( url,
		              {method: 'post', onComplete: showState, onFailure: showFailure }
		              );
		  }
}



var gRatingCookie = new Cookie(document, document.location, 30 * 24, '/');

function showState( originRequest ){

    var saveResultTxt = originRequest.responseText;
    var jsonObj = eval( "(" + saveResultTxt + ")" );
    var state = jsonObj.state;
    if( state == 'success' ){
      var showMsg;
      showMsg = '記事の評価を受付ました。';
      showInfo( showMsg );

      //cookieにURIを書き込む
      exp = new Date();
      exp.setTime(exp.getTime() + (1 * 1000 * 60 * 60 * 24));
      var uri = new String(document.location);
      uri = uri.split("#");
      setCookie(ENTRY_RATING_URI + uri[0], "done", exp);
      
    }else{
      showFailure(originRequest);
    }
}

//評価済みかチェック
function checkRatingDone(){
  var uri = new String(document.location);
  uri = uri.split("#");
  
  if(getCookie(ENTRY_RATING_URI + uri[0]) == "done"){
    return true;
  }else {
    return false;
  }
}

function showInfo( infoMsg ){
    $( 'ratingState' ).innerHTML = infoMsg;
}
function showFailure( originRequest ){
    var saveResultTxt = originRequest.responseText;
    var jsonObj = eval( "(" + saveResultTxt + ")" );
    var errState = jsonObj.errState;
    if(errState == "notuser"){
	    showInfo( 'ログインしてから評価してください。' );
    }else if(errState == "notPermit"){
	    showInfo( 'ブログ管理者が評価を受け付けていません。' );
    }
}


//星画像の表示
function star_display(pos) {
  for (i=1;i<=5;i++) {
    if(i <= pos){
       document.getElementById("star_img_"+i).src = '/image/rating/star_on.gif';
    } else {
       document.getElementById("star_img_"+i).src = '/image/rating/star_off.gif';
    }
  }
}

//マウスが画像にのった時
function star_onmouseover(pos) {
  star_display(pos);
}

//マウスが画像から離れたとき
function star_onmouseout() {
  star_display(0);
}
