$(document).ready(function(){
  var commentArea = $('#mComment').wysiwyg();

  $('#send').click(function(){
    var msg = $('#mComment').val();
    msg = msg.replace(/^[&nbsp;]+/, '');
    if ((msg.length) <= 2) {
      $('#error > p').html('Lütfen yorum yazınız.');
      $('#error').fadeIn('slow');
      return false;
    } else {
      $('#pollVoteForm').submit();
    }
  });


  $('.delComment').bind('click', function(e) {
    var control = confirm('Yorumunuzu silmek istediğinizden emin misiniz?');
    if (control == false) {
      return false;
    }
    cancelEvent(e);
    stopEvent(e);
    var info = $(this.parentNode);
    info.html('<img src=\"/include/img/ajax-loader.gif\" height=\"16\" width=\"16\" /> işlem yapılıyor...');
    var choice = ($(this).attr("href")).split('/');
    var comment = $('#comment-' + choice[3]);
    $.ajax({
      type: 'POST',
      data: "commentId=" + choice[3] + "&action=" + choice[4],
      url: '/ajax/comment/',
      dataType: 'json',
      success: function (j) {
        if (j.ok == true) {
          info.html('');
          comment.fadeOut(500);
        } else {
          if (j.msg == "requireLogon") {
            location.href = "/users/logon";
          } else {
            info.html(j.msg);
          }
        }
      }
    });
  });

  $('.changeComment').bind('click', function(e) {
    cancelEvent(e);
    stopEvent(e);
    var choice = ($(this).attr("href")).split('/');
    var commentId = '#commentText-' + choice[3];
    var comment = $(commentId);

    if (comment.css('display') == 'block') {
      var commentVal = $(commentId + ' pre').html();
      var commentHeight = $(commentId + ' pre').outerHeight() + 40;
      var content = "<div id=\"newcommentText-" + choice[3] + "\" style=\"float: left;\"><textarea id=\"newmComment-" + choice[3] + "\" wrap=\"virtual\" style=\"height: " + commentHeight + "px; width: 290px;\">" + commentVal + "</textarea><br /><input type=\"button\" id=\"send-" + choice[3] + "\" name=\"send\" value=\"Gönder\" onclick=\"buttonEvents(this);\" /><input type=\"button\" id=\"cancel-" + choice[3] + "\" name=\"cancel\" value=\"Çık\" onclick=\"buttonEvents(this);\" /></div>";
      comment.hide();
      comment.before(content);
      $("#newmComment-" + choice[3]).wysiwyg();
    }
  });
});

function buttonEvents(button) {
  var commentId = (button.id).split('-');
  var buttonName = $(button).attr('name');
  var newCommentObj = $('#newcommentText-' + commentId[1]);
  var oldCommentObj = $('#commentText-' + commentId[1]);
  if (buttonName == 'cancel') {
    newCommentObj.remove();
    oldCommentObj.show();
  } else if (buttonName == 'send') {
  	var newCommetVal = $('#newmComment-' + commentId[1]).val();
    newCommentObj.html('<img src=\"/include/img/ajax-loader.gif\" height=\"16\" width=\"16\" /> işlem yapılıyor...');
    $.ajax({
      type: 'POST',
      data: "commentId=" + commentId[1] + "&action=edit&mComment=" + newCommetVal,
      url: '/ajax/comment/',
      dataType: 'json',
      success: function (j) {
        if (j.ok == true) {
          newCommentObj.remove();
          oldCommentObj.html('<pre>' + newCommetVal + '</pre>');
          oldCommentObj.show();
        } else {
          if (j.msg == "requireLogon") {
            location.href = "/users/logon";
          } else {
            newCommentObj.html(j.msg);
          }
        }
      }
    });
  }
}
