﻿<!--
var replycomment = -1;
var sub = "";
(function($){
 var template = "tpl";
 $.prepairCommentForm = function(settings){
 settings = jQuery.extend({
 mainCommentForm : '#commentForm',
 commentFormHeader : '#commentFormHeader',
 userinfo : '#userinfo',
 textarea : '.commentFormTextarea',
 view : '#prevCommentButton',
 add : '.addCommentButton'
 },settings);
 
 $(settings.commentFormHeader + ' a').bind('click', function(){$.showCommentForm();});
 //$(sub + settings.add).bind('click', function(){$.addComment();});
 $(settings.commentFormHeader + ' a').css({'border-bottom':'1px dashed #000000', 'text-decoration':'none'});
 }
 
$.showCommentForm = function(){
replycomment = -1;
sub = "";
$('#commentreply').remove();
$('#commentForm').css({'display':'block'})
}

$.getTemplate = function(obj){
 try{   
 var tpl = "<li class=\"" + $(obj).attr('class') + "\">";
     tpl += $(obj).html();
 tpl += "</li>";
    
 if ($(obj).attr('class').indexOf('commenttemplate') != -1){
        template = tpl;
        $.clearTemplate();
    }
    else
    {
     $.getTemplate($(obj).parent());
    }
  }
  catch(obj){
  if (String(location).indexOf('#') != -1)
    location=String(location).substr(String(location).indexOf('#'), String(location).length);
  else
    location = String(location);
  }
}

$.clearTemplate = function()
{
 var ids = template.match(/ id=\"[^ ]*\"/);
 var i = 0;
 if (ids != null){
 for (i;i<ids.length;i++)
 {
  template = template.replace(ids[i], '');
 }
 $.clearTemplate();
 }   
}

$.parceResult = function(xml){

 var comments = $('comment', xml);

 $('.addCommentButton').removeAttr('disabled');
 $(".commentFormTextarea").attr('value', '');
 $("#commentreply").remove();
 
 $.getTemplate($('.NestedComments')[$('.NestedComments').length-1]);
 
 var tmpElem = "<div id=\"tempcomment\" style='display:none;'>" + template + "</div>"
 $(tmpElem).appendTo('form');

 $('#tempcomment .rating').remove();
 $('#tempcomment .freiting').remove();
 
 $('#tempcomment .itemAuthor').html($(comments[0]).attr('user'));
 $('#tempcomment .itemDateCreated').html($(comments[0]).attr('date'));
 $('#tempcomment .itemCommentText').html($('text', comments[0]).text());
 $('#tempcomment .reply').html('<a href="#reply" class="ListReplyLink" onclick="$.replyComment(this, ' + $(comments[0]).attr('id') + ');">Ответить</a>');

 $('#tempcomment span.reply-comment').removeAttr('reply');
 
 var tmpcomment = $('#tempcomment').html();
 $('#tempcomment').remove();
 
 if (replycomment == -1)
    $(tmpcomment).appendTo($('#allcomments'));
 else
    $(tmpcomment).appendTo($('span[@reply=' + $(comments[0]).attr('reply') + ']').parent().find('.table-nested-comment div')[0]);
}

$.addComment = function(){
  $(sub + '.addCommentButton').attr('disabled', 'disabled');
  
  $.post(
     "C" + "o" + "mm" + "entsA" + "jax" + "." + "aspx",
      {
       mode: "WRITE",
       text: $(sub + ".commentFormTextarea").attr('value'),
       replyid : replycomment,
       time: String(new Date().getTime())
      },
      $.parceResult
     );
}

$.replyComment = function(elem, id){

    replycomment = id;
    
    $('#commentreply').remove();
    $('#commentForm').css({'display':'none'});
    
    sub = "#commentreply ";
    
    var tmpForm = "<div id=\"commentreply\"><br/>" + $('div.sub-somment-form').html() + "</div>";
    
    $($('span.reply-comment', $(elem).parent().parent().parent())[0]).attr('reply', id);
    
    $(tmpForm).appendTo($($('span.reply-comment', $(elem).parent().parent().parent())[0]));
    //$(sub + ".addCommentButton").bind('click', function(){$.addComment();});
}

})(jQuery);


$(document).ready(function(){
$.prepairCommentForm();
});
-->