$(document).ready(function(){
  jQuery.each(['watch', 'comment', 'review', 'twit', 'mail'], function(){
    var func_name = this;
    $('div#' + func_name + '-form-show-button').click(function(){
      $('div#' + func_name + '-form').slideDown('normal');
    });
    $('div#' + func_name + '-form-show-button-bottom').click(function(){
      $.scrollTo($('div#item-detail-' + func_name), {duration:500});
      $('div#' + func_name + '-form').slideDown('normal');
    });
  });
  $('a#shop-more-images').click(function(){
    $('div#more-images').slideToggle();
    });

  initRatingPanel();

  changeRatingColor($('input#rating').val());

  initPostContent('comment');
  initPostContent('review');
  initPostContent('watch');
});

function initRatingPanel() {
  jQuery.each([1, 2, 3, 4, 5], function(){
    var rating = this;
    $('span#rating-' + this).click(function(){
      $('input#rating').val(rating);
    });
    $('span#rating-' + this).mouseover(function(){
      changeRatingColor(rating);
    });
    $('span#rating-' + this).mouseout(function(){
      changeRatingColor($('input#rating').val());
    });
  });
}

function loadSortContent(content_type, item_id, order, comment_only) {
  $('div#'+content_type+'-sort-conditions').append('&nbsp;<img id="indicator" class="indicator" src="/images/indicator.gif" />');
  $.get('/'+ content_type +'/load/' + item_id, {comment_only:comment_only,order:order}, function(html){$('div#user-' + content_type + 's').empty();$('div#user-' + content_type + 's').append(html); $('img#indicator').replaceWith('');});
}

function loadMoreContent(content_type, item_id, order, offset, comment_only) {
  $('div#item-'+content_type+'-more').prepend('&nbsp;<img id="indicator" class="indicator" src="/images/indicator.gif" />');
  $.get('/'+ content_type +'/load/' + item_id, {comment_only:comment_only,offset:offset,order:order}, function(html){$('div#item-' + content_type + '-more').replaceWith(html);});
}

function initPostContent(content_type) {
  item_id = $('input#item_id').val();
  $('input#'+content_type+'-submit').click(function(){
    var data = [];
    $('div#'+content_type+'-form input').each(function(){
      data.push($(this).attr('name') + "=" + $(this).val()); 
    });
    $('div#'+content_type+'-form textarea').each(function(){
      data.push($(this).attr('name') + "=" + $(this).val()); 
    });
    $('input#'+content_type+'-submit').after('&nbsp;<img id="indicator" class="indicator" src="/images/indicator.gif" />');
    $('input#'+content_type+'-submit').attr('disabled', 'disabled');
                  
    $.ajax({ 
type: "POST", 
url: '/'+content_type+'/create/' + item_id, 
data: data.join('&'), 
complete: function(){
    $('img#indicator').replaceWith('');
},
success: function(html){
$('div#'+content_type+'-form').slideUp('normal');
/*
$.get('/'+content_type+'/count/' + item_id, {}, function(html){$('span#user-' + content_type + '-total-count').empty();$('span#user-' + content_type + '-total-count').append(html);});
if (content_type == 'review') {
  $.get('/'+content_type+'/rating/' + item_id, {}, function(html){$('span#user-' + content_type + '-total span.rating').empty();$('div#user-' + content_type + '-total span.rating').append(html);});
}*/
setTimeout(function(){
  $('div#'+content_type+'-form').replaceWith(html);
  $('div#'+content_type+'-form').slideDown('slow');

  initPostContent(content_type); // エラーが起きた時用

  initRatingPanel();
  changeRatingColor($('input#rating').val());

  }, 500);
}, 
error: function(xhr, stat, err){
$('div#'+content_type+'-form').prepend('<div class="error">現在サイトにエラーが発生しています。<br />申し訳ありませんが時間をおいて再度お試しください。</div>');

} });
  });
}

function changeRatingColor(rating) {
  jQuery.each([1, 2, 3, 4, 5], function(){
    if (rating >= this) {
      $('span#rating-' + this).css('background-color', '#f90');
      $('span#rating-' + this).css('color', '#fff');
    } else {
      $('span#rating-' + this).css('background-color', '#eee');
      $('span#rating-' + this).css('color', '#ccc');
    }
  });
}

