//var modal;

function cancelDelete()
{
    modal.close();
}

function showComment()
{
    // hide the currnet rating and show the comment area\
    $('already-rated').hide();
    $('comment-area').show();
}

// reply to comments
function replyComment(commentId)
{
    // get the comment id
    //var id = divId.gsub('comment_', '');

    // make sure the user is loggd in
    if(isLoggedIn)
    {
    // show the comment field
    $('comment_' + commentId).insert({bottom: '<dl style="display:none" class="clearfix" id="reply_area_'+commentId+'"><dd class="reply-form"><fieldset><div><form onsubmit="javascript:sendReply($(this).serialize(true)); return false;" id="form_'+commentId+'" method="post" accept-charset="utf-8" ><textarea cols="10" rows="3" name="comment"></textarea><p><input type="hidden" name="comment_id" value="' + commentId + '"><input class="greenBtn" type="submit" id="submit_'+commentId+'" value="Submit Comment"></p></form></div></fieldset></dd></dl>'});

    // do the sliding effect
    Effect.SlideDown('reply_area_'+commentId, { duration: 1 });
    }
    else
    {
      // show the login message
      $('comment_' + commentId).insert({bottom: pleaseLogInMessage()});
    }
}

function sendNewComment(vars, vote)
{
   // make the Ajax request
   url = baseUrl + '/ajax/comment/save.new.comment/format/json';
   params = vars;
   //Object.toJSON(params);

   new Ajax.Request(url, {
   parameters: params,
   onCreate: function(){
   
   $('send-new-comment-button').writeAttribute('disabled', 'disabled');
   $('send-new-comment-button').writeAttribute('value', 'Sending...');
        
   },
   onSuccess: function(transport) {
   
       var res = transport.responseJSON;
       
       $('send-new-comment-button').disabled = false;
       $('send-new-comment-button').writeAttribute('value', 'Submit');
       
       // remove the error message
       if($('show_message'))
       {
           $('show_message').remove();
       }
        
       if(res.status == 'success')
       {
          //hide the comment box
          if(!$('success'))
              $('new-comment-form').insert({top:'<div id="show-message"><ul id="success"><li>Thank you for your comment!</li></ul></div>'});
              
          if($('error'))
              $('error').remove();
         
          // add the reply to the list
          $('comment_block').insert({top: '<dl class="clearfix" id="comment_'+res.commentId+'"><dt><div class="roundbox"><div class="ltop"></div><div class="rtop"></div><a href="'+baseUrl + '/poet/'+res.username+'"><img src="'+baseUrl+'/pic/'+res.username+'/s" /></a><div class="lbtm"></div><div class="rbtm"></div></div><p class="username"><a href="'+baseUrl+'/poet/'+res.username+'">'+res.username+'</a></p><p class="date">sent a moment ago</p></dt><dd class="comment">'+vars.comment+'</dd><dd class="reply-link" ><a href="javascript:void(0)" onclick="replyComment('+res.commentId+')">Reply to Comment</a></dd></dl>'});

          //clear out the comment text
          $('comment').value = '';
          
          if(vote == 'Yes')
          {
              // show the vote system
              $('comment-area').hide();
              $('rate-area').show();
          }
          
       }
       else if(res.status == 'error')
       {
            
            // get the form errors and slipt them
			errors = $A(res.error.split('|'));
			var errorString = '';
			    errors.each(function(value, index)
			    {
			        errorString += '<li>' + value + '</li>';
				    return errorString;
			    }	
		    );
		    
		    // remove the old error
		    if($('error'))
		    {
		        $('error').remove();
		    }
		    
		    if($('success'))
		        $('success').remove();
		    
		    // show the error string above the
		    $('new-comment-form').insert({top: '<ul id="show_message" class="error">'+errorString+'</ul>'});
		    
       }

          },
          method: 'post'
        });
}

// send the reply off to a better place :)
function sendReply(vars)
{
   // make the Ajax request
   url = baseUrl + '/ajax/comment/save.comment.reply/format/json';
   params = vars;
   //Object.toJSON(params);

   new Ajax.Request(url, {
   parameters: params,
   onCreate: function(){
   
       // disable the submit button
       if($('submit_'+vars.comment_id))
       {
           $('submit_'+vars.comment_id).writeAttribute('disabled', 'disabled');
           $('submit_'+vars.comment_id).writeAttribute('value', 'Sending...');
       }     
   },
   onSuccess: function(transport) {
   
       $('submit_'+vars.comment_id).disabled = false;
       $('submit_'+vars.comment_id).writeAttribute('value', 'Submit');
   
       var res = transport.responseJSON;
  
       if(res.status == 'success')
       {
          //hide the comment box
          $('reply_area_'+vars.comment_id).update('<ul id="success"><li>reply Sent!</li></ul>');
         
          // add the reply to the list
          $('comment_replys_' + vars.comment_id).insert({bottom: '<dl id="new_reply_'+vars.comment_id+'" class="clearfix"><dt class="reply"><div class="roundbox"><div class="ltop"></div><div class="rtop"></div><a href="'+baseUrl+'/poet/'+res.username+'"><img src="'+baseUrl+'/pic/'+res.username+'/s" alt="'+res.username+'" ></a><div class="lbtm"></div><div class="rbtm"></div></div><p class="username"><a href="'+baseUrl+'/poet/'+res.username+'">'+res.username+'</a></p><p class="date">just sent</p></dt><dd class="reply-comment">'+vars.comment+'</dd><dd class="reply-btm"></dd></dl>'});         
          Effect.SlideDown('comment_replys_' + vars.comment_id, { duration: 1 });
          
          // move to the comment
          Effect.ScrollTo('new_reply_'+vars.comment_id); return false;
       }
       else if(res.status == 'error')
       {
            // get the form errors and slipt them
			errors = $A(res.error.split('|'));
			var errorString = '';
			    errors.each(function(value, index)
			    {
			        errorString += '<li>' + value + '</li>';
				    return errorString;
			    }	
		    );
		    
		    // remove the old error
		    if($('error'))
		    {
		        $('error').remove();
		    }
		    
		    // show the error string above the
		    $('reply_area_'+vars.comment_id).insert({top: '<ul class="error">'+errorString+'</ul>'});
       }

          },
          method: 'post'
        });
}

function deleteCommentModal(commentId)
{
    // show the alert window
    modal = new Control.Modal($(document.body).down('[href=#delete-comment]'),{  
        overlayOpacity: 0.50,  
        className: 'modal' 
        //fade: true,
        //height: 100,
        //width: 200
        //closeOnClick: $('close')
 });
 
 var confirmMessage = '<div class="action-modal"><h4>Are you sure you want to delete this comment?</h4><div class="greenBtn" onclick="deleteComment('+commentId+');"><a href="javascript:void(0)">Delete</a></div><div class="redBtn" onclick="cancelDelete();"><a href="javascript:void(0)">Cancel</a></div></div>';
 
 modal.container.insert(confirmMessage);
 modal.open();
   
}

function deleteComment(commentId)
{
    // send the ajax request to remove the comment
    url = baseUrl + '/ajax/comment/delete/format/json';
    params = {commentId: commentId};
    
    new Ajax.Request(url, {
        method: 'post',
        parameters: params,
        onSuccess: function(transport) {
            var res = transport.responseJSON;
            
            if(res.status == 'success')
            {
                // take the comment out of the DOM
                if(res.hasChildren == 'yes')
                {
                    // remove all the comments from the DOM
                    $('comment_'+commentId).remove();
                }
                else
                {
                   // remove one of the child
                   $('child_'+commentId).remove();
                }
                
            }
            
            // cliose the modal box
            this.modal.close();
        }
    });
     
}




