var FeedbacksTag = '';
function cbSubContents(data){
 try{
  var fblist = '<h3>最新のコメント<\/h3>';
  var display = 1;
  if (data.feedbacks.length >0 ) {
   data.feedbacks.each(function(a){
     if(display<=4){
       fblist += '<div class="user-comment"><h4>' + a.title + '<\/h4>';
       if (a.comment.length > 30) {
         fblist += '<p>' + a.comment.substr(0,30) + '&hellip;';
       } else {
         fblist += '<p>' + a.comment;
       }
       fblist += '<\/p><\/div>\n';
       display++;
     }
   });
  } else {
   fblist += '<div class="nouser-comment"><p>コメントはありません<\/p><\/div>\n';
  }
  $('comment-sub').innerHTML = fblist;
 } catch(e){
 }
 FeedbacksTag.remove();
}
function commentSubContents(aurl){
 FeedbacksTag = new MWjsonScript('feedbacks_article','cbSubContents',aurl);
}
function cbFeedbacks(data){
 try{
  var avg = data.avg;
  if(avg != null) {
   avg = parseInt(avg);
  } else {
   avg = '-';
   if($('fb-result-id') != undefined) {$('fb-result-id').style.display = 'none';}
  }
  if ($('fb-average-point') != undefined) {
   $('fb-average-point').innerHTML = avg + '%';
  }
  var fblist = '';
  var display = 1;
  if (data.feedbacks.length >0 ) {
   data.feedbacks.each(function(a){
     if(display<=4){
       var pubdate = a.publish_at.split(' ')[0];
       var pubdate = pubdate.split('/')[0] + '年' + pubdate.split('/')[1] + '月' + pubdate.split('/')[2] + '日';
       fblist += '<li><h5>「' + a.title + '」<\/h5>';
       if (a.comment.length > 30) {
         fblist += '<p>' + a.comment.substr(0,30) + '&hellip;';
       } else {
         fblist += '<p>' + a.comment;
       }
       fblist += '<\/p><\/li>\n';
       display++;
     }
   });
  } else {
   if($('fb-more-id') != undefined) {$('fb-more-id').style.display = 'none';}
   fblist = '<li><p align="center" style="margin:10px 0; color:#666;">現在コメントはありません<\/p><\/li>\n';
  }
  $('latestfeedback').innerHTML = fblist;
 } catch(e){
 }
 FeedbacksTag.remove();
}
function articleFeedbacks(aurl){
 FeedbacksTag = new MWjsonScript('feedbacks_article','cbFeedbacks',aurl);
}
function commentWindowOpen(aid) {
 var param = 'url=' + encodeURIComponent(aid);
 var url = 'http://www.ismedia.jp/feedbacks/new?' + param;
 var opt = 'width=700,height=460,location=yes,menubar=no,status=no,scrollbars=yes,resizable=yes,toolbar=no';
 if (window.screen.width > 700) {
   opt += ',left=' + (window.screen.width - 700)/2;
 }
 if (window.screen.height > 460) {
   opt += ',top=' + (window.screen.height - 460)/2;
 }
 var w = window.open(url,null,opt);
}
function abuseWindowOpen(cid) {
 var url = 'http://www.ismedia.jp/feedbacks/' + cid + '/abuses/new';
 var opt = 'width=420,height=360,location=yes,menubar=no,status=no,scrollbars=yes,resizable=yes,toolbar=no';
 if (window.screen.width > 420) {
   opt += ',left=' + (window.screen.width - 420)/2;
 }
 if (window.screen.height > 380) {
   opt += ',top=' + (window.screen.height - 380)/2;
 }
 var w = window.open(url,null,opt);
}
function commentWindowClose() {
 window.close();
}
function abuseWindowClose() {
 window.close();
}
function commentRecommend(fbid) {
 var url = 'http://www.ismedia.jp/feedbacks/recommend/' + fbid;
 var goodcounter = 'good' + fbid;
 var currentcount = $(goodcounter).innerHTML;
 var aj = new Ajax.Request(url,{'method':'GET',
     onSuccess:function(req){$(goodcounter).innerHTML = parseInt(currentcount) + 1;},
     onFailure:function(req){alert('現在、この機能は利用できません。');},
     onException:function(req,e){alert('現在、この機能は利用できません。');}
     });
}
function commentUnRecommend(fbid) {
 var url = 'http://www.ismedia.jp/feedbacks/unrecommend/' + fbid;
 var nogoodcounter = 'nogood' + fbid;
 var currentcount = $(nogoodcounter).innerHTML;
 var aj = new Ajax.Request(url,{'method':'GET',
     onSuccess:function(req){$(nogoodcounter).innerHTML = parseInt(currentcount) + 1;},
     onFailure:function(req){alert('現在、この機能は利用できません。');},
     onException:function(req,e){alert('現在、この機能は利用できません。');}
     });
}
function articleRating(event) {
 Event.stop(event);
 var obj = Event.element(event);
 var param = Form.serialize(obj);
 var elements = Form.getElements(obj);
 var link = 'http://www.ismedia.jp/feedbacks?url=';
 elements.each(
  function (element) {
    if (element.name == 'url' ) {
     reg = new RegExp('http:\/\/.*\.ismedia\.jp\/','i');
     if ( element.value.match(reg) ) {
       link += element.value;
     }
    }
  }
 );
 var ratingsurl = '/fload/ratings';
 var aj = new Ajax.Request(ratingsurl,{'method':'POST','parameters':param,
     onSuccess:function(req){location.href = link;},
     onFailure:function(req){alert('この機能を利用するにはログインが必要です。');},
     onException:function(req,e){alert('現在、この機能は利用できません。');}
     });
}
function articleComment(event) {
 Event.stop(event);
 var obj = Event.element(event);
 var param = Form.serialize(obj);
 var elements = Form.getElements(obj);
 try{
 elements.each(
  function (element) {
    var pvalue = '';
    if (element.name == 'feedback[title]' ) {
      pvalue = element.value;
      pvalue = pvalue.replace(/( |　)/g,'');
      if (pvalue == '') {throw new Error('コメントのタイトルを入力してください。')}
    }
    if (element.name == 'feedback[comment]' ) {
      pvalue = element.value;
      pvalue = pvalue.replace(/( |　)/g,'');
      pvalue = pvalue.replace(/(\n|\r)/g,'');
      if (pvalue == '') {throw new Error('コメントの本文を入力してください。')}
    }
  }
 );
 var feedbackurl = '/feedbacks';
 var aj = new Ajax.Request(feedbackurl,{'method':'POST','parameters':param,
     onSuccess:function(req){thanksDisplay();},
     onFailure:function(req){throw new Error('現在、この機能は利用できません。');},
     onException:function(req,e){throw new Error('現在、この機能は利用できません。');}
     });
 } catch(e){
   alert(e.message);
 }
}
function commentAbuse(event) {
 Event.stop(event);
 var obj = Event.element(event);
 var param = Form.serialize(obj);
 try{
 var abuseurl = '/feedbacks/' + $('commentid').innerHTML + '/abuses';
 var aj = new Ajax.Request(abuseurl,{'method':'POST','parameters':param,
     onSuccess:function(req){thanksDisplay();},
     onFailure:function(req){throw new Error('現在、この機能は利用できません。');},
     onException:function(req,e){throw new Error('現在、この機能は利用できません。');}
     });
 } catch(e){
   alert(e.message);
 }
}
function thanksDisplay() {
 if ($('wrapper') != undefined) {
  var html = '<h1>ご協力いただき、ありがとうございました！</h1><div style="text-align:center;"><a href="javascript:void(0);" onclick="commentWindowClose();"><img src="http://www.ismedia.jp/common/images/jbp/v1/article/close.gif" alt="close" /></a></div>';
  $('wrapper').innerHTML = html; 
 }
}
function initFeedBackSlider(){
  new Control.Slider('fb-pointer','fb-line',
    {sliderValue:0.5,
      onChange:function(val){
        var score = val * 100;
        var intscore = Math.ceil(score);
        $('score').value = score;
        $('score').innerHTML = String(intscore) + '%';
        if ($('rating_rate') != undefined){$('rating_rate').value = intscore;}
      },
      onSlide:function(val){
        $('score').innerHTML = String(Math.ceil(val * 100)) + '%';
      }
    }
  );
}
function initFeedbacks(){
if ($('fb') != undefined){initFeedBackSlider();}
if ($('new_rating') != undefined){Event.observe('new_rating','submit',function(e){articleRating(e);},true);}
if ($('new_feedback') != undefined){Event.observe('new_feedback','submit',function(e){articleComment(e);},true);}
if ($('new_abuse') != undefined){Event.observe('new_abuse','submit',function(e){commentAbuse(e);},true);}
}
document.observe('dom:loaded',function(e){initFeedbacks();});

function userWindowOpen(uid) {
 var url = 'http://www.ismedia.jp/feedbacks/user/' + uid;
 var opt = 'width=630,height=550,location=yes,menubar=no,status=no,scrollbars=yes,resizable=yes,toolbar=no';
 var name = 'user_window';
 var w = window.open(url,name,opt);
}
function disp(url){
	if(!window.opener || window.opener.closed){ // メインウィンドウの存在をチェック
		window.alert('メインウィンドウがありません'); // 存在しない場合は警告ダイアログを表示
	}
	else{
		window.opener.location.href = url; // 存在する場合はページを切りかえる
	}
}
