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

    this.setup = function (objectTypeId, objectId, enableAnonymousComment, addCallback, deleteCallback) {
        this._objectTypeId = objectTypeId;
        this._objectId = objectId;

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

        if (typeof deleteCallback == 'function') {
            this._deleteCallback = deleteCallback;
        }
		
		if(enableAnonymousComment == 0) {
			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();
	        });
		}
		$('#comment_content').focus(function(){
			$(this).addClass('comment_active');
		}).blur(function(){
			$(this).removeClass('comment_active');
		});
        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').unbind('click').click(function(){
            var comment = $.trim($('#comment_content').val());
			var commentName = $.trim($('#comment_name').val());
			var commentWebSite = $.trim($('#comment_website').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,
				name: commentName,
				web: commentWebSite
            }, true, function(response){
				var commentId = response.data.commentId;
				var isLogin = response.data.isLogin;
				if (commentId !== 0) {
					ZComment._addCallback();
					$('#comment_content, #comment_name, #comment_website').val('');
					if(!isLogin) {
						commentName = response.data.visitorName;
					} else {
						commentName = null;
					}
					ipAddress = response.data.ip;
					$('#comment_list').append(ZComment.generateNewComment(commentId, response.data.comment, commentName, ipAddress, commentWebSite));
					ZComment.setupModifyComment();
				}
				else {
					ZFramework.alert("Please login to add comments");
				}
            }, function() {

            });
        });
                
        $('#clear_comment').unbind('click').click(function() {
        	$('#comment_name').val('');
        	$('#comment_website').val('');
        	$('#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;
		var template;
        $('#comment_list').html('');
	    var str = '';
        for (var i = 0; i < comments.length; i++) {
			if (comments[i]['poster_entity_id'] == 0) {
				template = $('<div class="global_comments">' + $('#comment_template_anonymous').html() + '</div>');
			} else {
				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 .zGeneralCommentAuthor', template);
	        var dateTarget = $('div .zGeneralCommentDate', template);
	        var visibilityTarget = $('div .zGeneralCommentVisibility', template);
	        var boxTarget = $('div .global_comments_comment_box', template);
			var ipTarget = $('div .global_comments_ip', template);
			var domainTarget = $('div .zGeneralCommentDomain', template);
			var deleteTarget = $('div .zGeneralCommentDelete', template);
            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]['nickname']);
            dateTarget.html(comments[i]['post_date']);
			if(comments[i]['poster_entity_id'] == 0) {
				ipTarget.html(comments[i]['ip_address']);
			} else {
				domainTarget.attr('href', 'http://'+comments[i]['domain']);
			}
            if (isOwner || comments[i]['poster_entity_id'] == entityId) {
                deleteTarget.show();
            } else {
                deleteTarget.hide();
            }
			if(isOwner == true) {
				deleteTarget.show();
			}  else {
				deleteTarget.hide();
			}
            if (comments[i]['hidden'] == 't') {
                boxTarget.addClass('zmedia_hiddenComment');
            } else {
                boxTarget.removeClass('zmedia_hiddenComment');
            }
			if(comments[i]['domain'] != null && comments[i]['poster_entity_id'] == 0) {
				domain = '<a href="http://'+comments[i]['domain']+'" target="_blank">'+$('div .zGeneralCommentDomain', template).html()+'</a>';
				$('div .zGeneralCommentDomain', template).html(domain);
				domain = '<a href="http://'+comments[i]['domain']+'" target="_blank">'+$('div .global_comments_avatar', template).html()+'</a>';
				$('div .global_comments_avatar', template).html(domain);
			}
            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, visitorName, ipAddress, commentWebSite){
        var template;
		if (visitorName !== null) {
			template = $('#comment_template_anonymous').clone();
			$('div .zGeneralCommentAuthor', template).html(visitorName);
			$('div .global_comments_ip', template).html('IP:'+ipAddress);
			$('div .zmedia_deleteComment', template).remove();
			if(commentWebSite != null && ($.trim(commentWebSite)).length>0) {
				if(commentWebSite.indexOf('http://') == -1 && commentWebSite.indexOf('https://') == -1 ) {
					commentWebSite = 'http://'+commentWebSite;
				}
				var html = '<a href="'+commentWebSite+'" target="_blank">'+$('div .zGeneralCommentDomain', template).html()+'</a>';
				$('div .zGeneralCommentDomain', template).html(html);
				html = '<a href="'+commentWebSite+'" target="_blank">'+$('div .global_comments_avatar', template).html()+'</a>';
				$('div .global_comments_avatar', template).html(html);
			}
		} else {
			template = $('#comment_template');
		}
        $('div .zGeneralCommentHide', template).attr('name',commentId);
        $('div .global_comments_comment', template).html(commentContent);
        return template.html();
    };
}

ZComment = new ZoolooComment();
