var pokkariComments = {

  postComment: function() {
        $("#post_comment").attr('disabled','disabled');

        $.ajax({url:"/comment/post",
                type: "POST", success:pokkariComments.postedComment, error:pokkariComments.postFailed,
				data: $('#discussion').serialize() + "&skin=xmlhttprequest"});
  },

  postedComment: function(response) {
 
    if (response) {
	if ($(response).find("notice").length > 0 ) {
	    var notice = $(response).find("notice")[0];
	    alert($(notice).text());

	} else {
	    pokkariComments.addComment({
		body: $($(response).find("body")[0]).text(),
		id: $($(response).find("id")[0]).text(),
		attached_to: $($(response).find("attached_to")[0]).text(),
	        displayname: $($(response).find("displayname")[0]).text(),
	        login: $($(response).find("login")[0]).text(),
                usertype: $($(response).find("usertype")[0]).text(),
                userlink: $($(response).find("userlink")[0]).text(),
                usericon: $($(response).find("usericon")[0]).text()
	    }); 
            //comment posted..clear input            
            pokkariComments.resetFormSubmit();
	}
    } else {
	alert('Comment failed to post.');
    }
 
    pokkariComments.resetFormCaptcha();
    $("#post_comment").attr('disabled','');
  },


  resetFormCaptcha: function() {
 
    $("#CaptchaIncluder").html('');
    $("#CaptchaInput").val(''); 
    PokkariCaptcha.appendChildTo($("#CaptchaIncluder")[0]);
    
  },


  resetFormSubmit: function() {
 
    if ($("#discussion")) {
      $("#CommentTextArea").val('');
    }

  },

  addComment : function(c) {

    var div = document.createElement('div');
    div.className = "CommentEntry";
    div.id = "comment_" + c.id;

    var icon = document.createElement('div');
    icon.className = "CommentUserIcon";
    icon.innerHTML = '<a href ="' + c.userlink + '"><img src="' + c.usericon  + '" width="50" height="50" alt=""></a>';
 
    var role = c.usertype;
    var role_display;

    if (role == '' || role == 'user') {
      role_display = '';
    } else {
      role_display = '(' + role + ') ';
    }
 
    var text = document.createElement('div');
    text.className = "CommentText";
    text.innerHTML = '<span class="Said"><a href ="' + c.userlink + '" class="Said">' + c.displayname + '</a> ' + role_display + 'said: <a name="c' + c.id + '"></a></span>' +
                     '<span class="CommentDateTime">posted just now '+
                     '(<a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to remove this comment?\')) { pokkariComments.removeComment(' + c.id + ',\'' + c.attached_to  + '\'); return false; } else { return false; }">Delete</a>)' +
                     '</span>';
    text.innerHTML += "<div class='Clear'></div>";
    
    var body  = document.createElement('div'); 
    body.className = "CommentTextBody";
    body.innerHTML = c.body;

    var clear = document.createElement('div');
    clear.className = "Clear";

    div.appendChild(icon);
    div.appendChild(text);
    text.appendChild(body);

    div.style.filter = "alpha(opacity=0)";
    
    if ($("#CommentEmpty")) {
      $("#CommentEmpty").hide();
    }
 
	
    $("#CommentsList").append(div);
    $("#CommentsList").append(clear);

    },

  postFailed: function() {
    alert('There has been an error posting your comment.');
  },



    removeComment: function(comment_id,attached_to) {
	$.ajax({url:"/comment/delete/" + comment_id + "?attached_to=" + attached_to + "&skin=xmlhttprequest", 
                type: "POST", success:pokkariComments.removedComment, error:pokkariComments.removeFailed});
  },

  removedComment: function(response) {
     var error;
     if (response) {
	if ($(response).find("attached_to").length > 0 && $(response).find("id").length > 0 ) {
          var attached_to = $($(response).find('attached_to')[0]).text();
          var id = $($(response).find('id')[0]).text();
          if ( id &&  attached_to) {
	     $("#comment_" + id).hide();
          }
          else { 
 	     error = 1;
          }
       } else {
         //no id or attached_to element
         error = 1;
       }
    } else {
      //no response
      error = 1;
    }

    if (error)
      alert('There has been an error removing your comment.');

  },

  removeFailed: function() {
    alert('There has been an error removing your comment.');
  }


}
