var comments = {
  timeout : null,

  getCommentsCount : function(prefix, context, contextId)
  {
    var commentsCount = $("#" + comments.getCountId(prefix, context, contextId)).find("a");
    if (commentsCount.length != 0) {
      if (commentsCount.html() == "comment") {
        return 0;
      }
      else {
        return parseInt(commentsCount.html().split(' ')[0]);
      }
    }
    else {
      return -1;
    }
  },

  setCommentsCount : function(commentableId, prefix, context, contextId, value)
  {
    var commentsCount = $("#" + comments.getCountId(prefix, context, contextId));
    if (commentsCount.length != 0) {
      if (value == 0) {
        commentsCount.html("<a href='#'>comment</a>");
      }
      else {
        var commentable = $("#" + commentableId);
        comments.onMouseOver(commentable, prefix, context, contextId);
        if (value == 1) {
          commentsCount.html("<a href='#'>1 comment</a>");
        }
        else {
          commentsCount.html("<a href='#'>" + value + " comments</a>");
        }
      }
    }
  },

  getErrorId : function(prefix, context, contextId)
  {
    return 'comments-errors-' + prefix + '-' + context.toLowerCase() + '-' + contextId;
  },

  getCountId : function(prefix, context, contextId)
  {
    return 'comments-count-' + prefix + '-' + context.toLowerCase() + '-' + contextId;
  },

  getId : function(prefix, context, contextId)
  {
    return 'comments-' + prefix + '-' + context.toLowerCase() + '-' + contextId
  },

  getContext : function(id)
  {
      var arr = id.split("-");
      var context = arr[arr.length - 2].toUpperCase();
      var contextId = arr[arr.length - 1];
      return {type:context, id:contextId}
  },

  onMouseOver: function(elem, prefix, context, contextId)
  {
    elem.addClass('selected');
    $('#' + comments.getCountId(prefix, context, contextId)).show();
    if (comments.timeout != null){
      clearTimeout(comments.timeout);
    }
    comments.timeout = setTimeout(function(){ comments.onClick(elem); }, 3000);
  },

  onMouseLeave : function(elem, prefix, context, contextId)
  {
    elem.mouseover(function(){
      comments.onMouseOver($(this), prefix, context, contextId);
    });
    elem.removeClass('selected');
    commentsCount = $('#' + comments.getCountId(prefix, context, contextId));
    if (comments.getCommentsCount(prefix, context, contextId) > 0) {
      commentsCount.show();
    }
    else {
      commentsCount.hide();
    }
    $('#' + comments.getId(prefix, context, contextId)).hide();
    if (comments.timeout != null){
      clearTimeout(comments.timeout);
    }
  },

  onClick : function(elem){
    elem.unbind('mouseleave');
  },

  prepare : function(commentableId, prefix, context, contextId)
  {
    context = context.toUpperCase();
    $('#' + commentableId).mouseover(function(){
      comments.onMouseOver($(this), prefix, context, contextId);
    }).mouseleave(function(){
      comments.onMouseLeave($(this), prefix, context, contextId);
    }).click(function(){
      comments.onClick($(this));
    });
    $('#' + comments.getCountId(prefix, context, contextId))
      .click(function(){
        var n = comments.getCommentsCount(prefix, context, contextId);
        if (GLOBALS.uid < 0 &&  n <= 0) {
          window.location = GLOBALS.loginUrl + '?subjectId=' + GLOBALS.sid + '&next=viewSubject';
          return false;
        }
        var commentable = $("#" + commentableId);
        comments.onClick(commentable);
        var commentsContainer =  $('#' + comments.getId(prefix, context, contextId));
        commentsContainer.show();
        commentable.unbind('mouseover');
        $(this).hide();
        return false;
      });
    var settings = {
      animated: "fast",
      url: GLOBALS.contextPath + "/ajax/loadComments",
      url_new: GLOBALS.contextPath + "/ajax/comment"
    };
    $('#' + comments.getId(prefix, context, contextId)).treeview(settings, commentableId, prefix, context, contextId);
  },

  include : function(comments_container_id, comments_prefix, comments_context, comments_context_id)
  {
	var count_id = comments.getCountId(comments_prefix, comments_context, comments_context_id);
	var id = comments.getId(comments_prefix, comments_context, comments_context_id);
	var error_id = comments.getErrorId(comments_prefix, comments_context, comments_context_id);
	var comment = '<br/><span id="' + count_id + '" class="comments-count" style="display:none">&nbsp;</span>' +
	'<ul id="' + id + '" class="comments" style="display:none">';
  if (GLOBALS.uid) {
		comment +=
      '<form class="enter-comment">{ <input title="enter comment" type="text" name="comment-content"/> } &nbsp;&nbsp;<a href="#" class="save">save</a> </form> &nbsp;&nbsp;<a href="#" class="hide">X</a>' +
		  '<span id="' + error_id + '" class="comments-errors"></span>';
  }
  comment +=
		'<ul style="display: block;">' +
			'<li class="last">' +
				'<span class="placeholder">&nbsp;</span>' +
			'</li>' +
		'</ul>' +
	'</ul>' +
	'<script type="text/javascript">' +
		'comments.prepare(' +
			'"' + comments_container_id + '",' +
			'"' + comments_prefix + '",' +
			'"' + comments_context + '",' +
			comments_context_id +
		');' +
	'</script>';
	return comment;
  }
};
