function ZoolooComment () {
    var _objectTypeId;
    var _objectId;
	
    this._blurCallback = function () {};
    this._focusCallback = function () {};
    this._addCallback = function () {};
    this._deleteCallback = function () {};

    this.setup = function (objectTypeId, objectId, addCallback, deleteCallback) {
        this._objectTypeId = objectTypeId;
        this._objectId = objectId;
		if($('#comment_content').attr('name') == 'not_login') {
			$('#comment_content').attr('readonly', 'readonly');
			$('#comment_content').click(function(){
				$('#follow_promo').modal({
					onClose: function(dialog) {
						$.modal.close(false);
					}
				});
			});
		}
        $('#comment_content').keydown(function (e) {
            e.stopPropagation()
        });

        if (typeof addCallback == 'function') {
            this._addCallback = addCallback;
        }

        if (typeof deleteCallback == 'function') {
            this._deleteCallback = deleteCallback;
        }

        this.setupAddComment();
        this.setupModifyComment();
    };

    this.setupModifyComment = function() {
        $('div .zmedia_deleteComment').unbind("click").click(function(){
            var commentId = $(this).attr('name');
            $(this).parents('.global_comments').remove();
            ZFramework.zfAjaxPost("/zConnect/deleteComment", {
                comment_id: commentId
            }, false, function(response){
                ZComment._deleteCallback();
                $.jGrowl('Comment deleted');
            }, function(){
                ZFramework.alert('failed to delete comment');
            });
        });

        $('div .zGeneralCommentHide').unbind("click").click(function(){
            $(this).removeClass('zGeneralCommentHide').addClass('zGeneralCommentShow').attr('title','Show Comment').html('Show');
            var commentId = $(this).attr('name');
            //$(this).parent().parent().remove();
            $(this).parents('.global_comments').find('.global_comments_comment_box').addClass('zmedia_hiddenComment');
            ZComment.setupModifyComment();
            ZFramework.zfAjaxPost("/zConnect/hideComment", {
                comment_id: commentId
            }, false, function(response){
            }, function(){
                ZFramework.alert('failed to hide comment');
            });
        });

        $('div .zGeneralCommentShow').unbind("click").click(function(){
            $(this).removeClass('zGeneralCommentShow').addClass('zGeneralCommentHide').attr('title','Hide Comment').html('Hide');
            var commentId = $(this).attr('name');
            $(this).parents('.global_comments').find('.global_comments_comment_box').removeClass('zmedia_hiddenComment');
            ZComment.setupModifyComment();
            ZFramework.zfAjaxPost("/zConnect/showComment", {
                comment_id: commentId
            }, false, function(response){
            }, function(){
                ZFramework.alert('failed to delete comment');
            });
        });
    };

    this.setupAddComment = function() {
        $('#add_comment').click(function(){
            var comment = $.trim($('#comment_content').val());
            if (comment.length === 0) {
                ZFramework.alert(ZMsg.ZPHOTO_VIEWER_COMMENT_NULL);
                return false;
            }
            ZFramework.zfAjaxPost("/zConnect/addComment", {
                type_id: ZComment._objectTypeId,
                id: ZComment._objectId,
                content: comment
            }, true, function(response){
				var commentId = response.data.commentId;
				if (commentId !== 0) {
					ZComment._addCallback();
					$('#comment_content').val('');
					$('#comment_list').append(ZComment.generateNewComment(commentId, response.data.comment));
					ZComment.setupModifyComment();
				}
				else {
					ZFramework.alert("Please login to add comments");
				}
            }, function() {

            });
        });
                
        $('#clear_comment').click(function() {
        	$('#comment_content').val('');
        });
    };

    this.populateComments = function(objectTypeId, objectId, comments, isOwner, entityId) {
        if (typeof isOwner == 'undefined') {
            isOwner = true;
            entityId = 0;
        }
        this._objectTypeId = objectTypeId;
        this._objectId = objectId;

        $('#comment_list').html('');
        var template = $('<div class="global_comments">'+$('#comment_template').html()+'</div>');
        var deleteTarget = $('div .zmedia_deleteComment', template);
        var commentTarget = $('div .global_comments_comment', template);
        var avatarTarget = $('.global_comments_avatar img', template);
        var nameTarget = $('div .zgeneralCommentName', template);
        var dateTarget = $('div .global_comments_date', template);
        var visibilityTarget = $('div .zGeneralCommentVisibility', template);
        var boxTarget = $('div .global_comments_comment_box', template);
        var str = '';
        for (var i = 0; i < comments.length; i++) {
            deleteTarget.attr('name',comments[i]['id']);
            visibilityTarget.attr('name',comments[i]['id']);
            commentTarget.text(comments[i]['content']);
            avatarTarget.attr('src', '/zProfile/getSiteProfilePic/entity_id/' + comments[i]['poster_entity_id']);
            nameTarget.html(comments[i]['poster_name']);
            dateTarget.html(comments[i]['post_date']);

            if (isOwner || comments[i]['poster_entity_id'] == entityId) {
                deleteTarget.show();
            } else {
                deleteTarget.hide();
            }

            if (comments[i]['hidden'] == 't') {
                boxTarget.addClass('zmedia_hiddenComment');
            } else {
                boxTarget.removeClass('zmedia_hiddenComment');
            }

            try {
                if (comments[i]['hidden'] == 't') {
                    visibilityTarget.attr('className','zGeneralCommentVisibility zGeneralCommentShow').
                        attr('name', comments[i]['id']).
                        attr('title', 'Show Comment');
                } else {
                    visibilityTarget.attr('className','zGeneralCommentVisibility zGeneralCommentHide').
                        attr('name', comments[i]['id']).
                        attr('title', 'Hide Comment');
                }
            } catch (ex) {}
            str += template.html();
        }
        $('#comment_list').html(str);
        this.setupModifyComment();
    };

    this.generateNewComment = function(commentId, commentContent){
        var template = $('#comment_template');
        $('div .zGeneralCommentDelete', template).attr('name',commentId).show();
        $('div .zGeneralCommentHide', template).attr('name',commentId);
        $('div .global_comments_comment', template).html(commentContent);
        return template.html();
    };
}

ZComment = new ZoolooComment();
