$(function()
{   
   // load comments
   function loadComments(page)
   {   
      window.location.reload()
   }
   
   // Comment Replies
   
   var aDeleteReply_click=function()
   {
      if (!confirm('Selected comment will be deleted. Continue?')) return false;
      
      var id=$(this).parent().parent().parent().parent().parent('div.reply').attr('id').substring(5);

      var th=$(this);
      $.get(tgSitesUri+'/site/commdelajax/'+id+'/',
         function(result,text)
         {
            if (text=='1')
            {
               alert('Comment has been deleted.');

               loadComments();
               
               $('#layer').hide();
               
               return false;
            }
            else if (text=='0') alert(result.error);
            else alert('Comment has not been deleted.');
         });
      return false;
   };
   $('a.aDeleteReply').bind('click',aDeleteReply_click);
   
   
   var btnSaveReply_click=function()
   {
      var id=$('#commid').val();
      var th=$(this);
      th.attr('disabled',true);
      th.next('input').attr('disabled',true);
      $.post(tgSitesUri+'/site/commupdateajax/',
         {commid:$('#commid').val(), commtext:$('#commedittext').val()},
         function(result,text)
         {
            if (text=='1')
            {
               alert('Comment has been updated.');
               
               $('#comm_'+id+' div.body').html(result.commtext);
               commText='';
               
               return false;
            }
            else if (text=='0') alert(result.error);
            else alert('Comment has not been updated.');

            th.attr('disabled',false);
            th.next('input').attr('disabled',false);
         });
   };
     
   var btnCancelReplyUpdate_click=function()
   {
      $('#comm_'+$('#commid').val()+' div.body').html(commText);
      commText='';
   };
   
   var aEditReply_click=function()
   {
      if ($(this).parent().next().next('div.body').find('textarea').is('textarea'))
         return false;
         
      var id=$(this).parent().parent().parent().parent().parent('div.reply').attr('id').substring(5);
      commText=$(this).parent().next().next('div.body').html();
      var html=commText.replace(/<br[\/]?>/gi,"\n");
      
      $('#commedittext').parent('div.body').html($('#commedittext').val());
      $(this).parent().next().next('div.body')
         .html('<textarea cols="75" style="width:100%" rows="10" id="commedittext" meta:validator="trim required strip_tags xss_clean">'+html+'</textarea><br/><input type="hidden" id="commid" value="'+id+'"/><input type="button" id="btnSaveReply" value="Submit"/> <input type="button" id="btnCancelReplyUpdate" value="Cancel"/>')
         .find('#btnSaveReply').bind('click',btnSaveReply_click);
      $(this).parent().next().next('div.body')
         .find('#btnCancelReplyUpdate').bind('click',btnCancelReplyUpdate_click);
      
      return false;
   };
   $('a.aEditReply').bind('click',aEditReply_click);
      
// Comment Reply End

// FR Reply

   var btnFRReplyComment_click=function()
   {
      var id=$('#commid').val();
      var th=$(this);
      th.attr('disabled',true);
      th.next('input').attr('disabled',true);
      $.post(tgSitesUri+'/site/frreplyajax/',
         {commid:$('#commid').val(), commtext:$('#commedittext').val(), siteid:$('#siteid').val()},
         function(result,text)
         {
            if (text=='1')
            {
               loadComments();
               return false;
            }
            else if (text=='0') alert(result.error);
         });
   };   
   
   var btnCancelFRReply_click=function()
   {
      $('#post_'+$('#commid').val()+' div.clearer').html('');
   };   
   
   var aFRPostReply_click=function()
   {
	  if ($(this).parent().parent().next('div.clearer').find('textarea').is('textarea'))
      return false;
      
	  var id=$(this).parent().parent().parent().parent().parent().parent().parent('div.main').attr('id').substring(5);
      var html="";
      $(this).parent().parent().next('div.clearer')
         .html('<textarea cols="75" style="width:100%" rows="10" id="commedittext" meta:validator="trim required strip_tags xss_clean">'+html+'</textarea><br/><input type="hidden" id="commid" value="'+id+'"/><input type="button" id="btnFRReplyComment" value="Submit"/> <input type="button" id="btnCancelFRReply" value="Cancel"/>')
         .find('#btnFRReplyComment').bind('click',btnFRReplyComment_click);
      $(this).parent().parent().next('div.clearer')
         .find('#btnCancelFRReply').bind('click',btnCancelFRReply_click);
      
      return false;
   };
   $('a.aFRPostReply').bind('click',aFRPostReply_click);
   
   // comment tool
   // report
   $('a.aRepReportAdmin').click(function()
   {
      var id=$(this).parent().parent().parent().parent().parent('div.reply').attr('id').substring(5);
      var th=$(this);
      $.get(tgSitesUri+'/site/commreportajax/'+id+'/',
         function(result,text)
         {
            th.parent().hide();
            alert('Report has been sent.');
         });
      return false;
   });     
   
// FR Reply End

   
   // debug
   $("#msg").ajaxStart(function(){
      $(this).append("<li style=\"color:#00aa00\">Starting</li>");
   });
   $("#msg").ajaxSend(function(jq, request, settings){
      $(this).append("<li style=\"color:#0000ff\">Sending "+settings.url+"</li>");
   });
   $("#msg").ajaxComplete(function(request, settings){
      $(this).append("<li>Request Complete.</li>");
   });
   $("#msg").ajaxStop(function(){
      $(this).append("<li style=\"color:#c0c0c0\">Stopped</li>");
   });
   $("#msg").ajaxError(function(jq, request, settings){
      $(this).append("<li style=\"color:#ff0000\">Error requesting page " + settings.url + "</li>");
   });
});