function ZoolooGraffiti(){
    var _singlePost = false;
    var _followerPage = 1;
	var _followeePage = 1;
    var _objectTypeId = 7;
	var _enableAnonymousComment = 0;
	
    this.setup = function(graffitiURL, singlePost, enableAnonymousComment){
		
		_enableAnonymousComment = enableAnonymousComment;
        _singlePost = singlePost;
        
        if($('#my_follower_area').length>0) _getFollower();
        if($('#my_followee_area').length>0) _getFollowee();
        
        $('div .zgraffiti_items').hover(function(){
            $('.zgraffiti_delete', this).show();
        }, function(){
            $('.zgraffiti_delete', this).hide();
        });
        
        $('#z_graffiti_follow').click(function(e){
            ZGraffiti.follow($(this).attr('name'));
            e.preventDefault();
        });
        
        $('.zgraffiti_sharelink').click(function(e){
            var id = $(this).attr('id').split('_')[1];
            $('#zshare_' + id).toggle();
            e.preventDefault();
        });
        
        $('.zgraffiti_repost').click(function(e){
            var postId = $(this).attr('name');
            ZFramework.openWindow(graffitiURL + 'repost=1&post_id=' + postId, 'zbutton', '',
                function() {
                    window.location.reload(true);
                });
            e.preventDefault();
        });
        
        $('.zgraffiti_edit_button').click(function(e){
            var postId = $(this).attr('name');
            ZFramework.openWindow(graffitiURL + 'edit=1&post_id=' + postId, 'zbutton', '',
                function() {
                    window.location.reload(true);
                });
            e.preventDefault();
        });
        
        $('.zgraffiti_delete_button').unbind('click').click(function(e){
            var postId = $(this).attr('name');
            ZFramework.confirm(ZFramework.i18n(ZMsg.ZGRAFFITI_CONFIRM_DELETE), '', function(){
                ZFramework.zfAjaxPost('/zGraffiti/deletePost', {
                    post_id: postId
                }, false, function(response){
                    if (response.status == 1) {
                        if (_singlePost) {
                            window.location = '/?page_name=_Graffiti';
                        } else {
                            $('#z_graffiti_post_' + postId).slideUp('fast', function(){
                                $(this).remove()
                            });
                            ZFramework.notify(ZFramework.i18n(ZMsg.ZGRAFFITI_DELETE_SUCCESS));
                        }
                    }
                    else {
                        ZFramework.alert(ZFramework.i18n(ZMsg.DEFAULT_MESSAGE))
                    }
                }, function(){
                        ZFramework.alert(ZFramework.i18n(ZMsg.DEFAULT_MESSAGE))
                });
            });
            e.preventDefault();
        });
        
        
        $('.zgraffiti_img').unbind('click').click(function(e){
            var postId = $(this).attr('name');
            $('#home_zgraffiti_photo_' + postId + ' a').lightbox({
                start: true,
                events: false
            });
            e.preventDefault();
        });
        
        $('#show_more_followers').click(function(e){
			_followerPage++;
            _getFollower();
            e.preventDefault();
        });
		
		$('#show_more_followees').click(function(e){
			_followeePage++;
            _getFollowee();
            e.preventDefault();
        });
		
		$('#zg_connact_me').click(function(e){
			if($(this).attr('name') == 'not_login') {
				$('#follow_promo').modal({
					onClose: function(dialog) {
						$.modal.close(false);
					}
				});
				e.preventDefault();
			}
		});
		
		$('#zg_type_selector').change(function(){
			window.location.href = $(this).val();
		});

		_setupRating();
		
		$('.zg_archive_year').click(function(e){
			var year = $(this).attr('name');
			$('.zg_archive_year_item[name!='+year+']').hide();
			$('.zg_archive_year_item[name='+year+']').toggle();
			e.preventDefault();
		});
		
        ZFramework.tooltip('.followTooltip', {className:'skin_border_color skin_text_color skin_background_color'});
    };
    
	this.addCommentCallback = function(){
		var postId = $('.zg_comment_active').attr('name').split('_')[0];
		var commentCount = $('.zg_comment_active').attr('name').split('_')[1]*1;
		commentCount++;
		if(commentCount == 1) {
			var words = '1 Comment';
		} else {
			var words = commentCount + ' Comments';
		}
		$('.zg_comment_active').attr('name', postId+'_'+commentCount).html(words);
	};
	
	this.deleteCommentCallback = function() {
		var postId = $('.zg_comment_active').attr('name').split('_')[0];
		var commentCount = $('.zg_comment_active').attr('name').split('_')[1]*1;
		commentCount--;
		if(commentCount == 1) {
			var words = '1 Comment';
		} else if(commentCount == 0) {
			var words = 'Comment';
		} else {
			var words = commentCount + ' Comments';
		}
		$('.zg_comment_active').attr('name', postId+'_'+commentCount).html(words);
	};
	
    this.follow = function(type){
        ZFramework.zfAjaxPost('/zInvite/follow', {
            type: type
        }, true, function(response){
            if (response.status == 1) {
                if (response.login == false) {
                    $('#follow_promo').modal({
                        onClose: function(dialog){
                            $.modal.close(false);
                        }
                    });
                }
                else {
                    if (type == 'follow') {
                        //ZFramework.alert(response.data.message, 'Follow');
                        $('#z_graffiti_follow').html('<span></span><p>Unfollow</p>').attr('name', 'unfollow');
                    }
                    else {
                        //ZFramework.alert(response.data.message, 'Unfollow');
                        $('#z_graffiti_follow').html('<span></span><p>Follow</p>').attr('name', 'follow');
                    }
                }
            }
            else {
                ZFramework.alert(response.error);
            }
        });
    };
    
    function _getFollower(){
        ZFramework.zfAjaxPost('/zGraffiti/getFollowers', {
            page: _followerPage
        }, false, function(response){
            if (response.status == 1) {
                $('#my_follower_area').append(response.data.html);
                $('.followTooltip').unbind();
        		ZFramework.tooltip('.followTooltip', {className:'skin_border_color skin_text_color skin_background_color'});
        		var total = response.data.total;
                if (_followerPage == response.data.maxPages || total == 0) {
                    $('#show_more_followers').hide();
                }
                else $('#show_more_followers').show();
                $('#zgraffiti_followers_count').html(total);
            }
            else {
                ZFramework.alert(response.error);
            }
        });
    }
	
	function _getFollowee(){
        ZFramework.zfAjaxPost('/zGraffiti/getFollowees', {
            page: _followeePage
        }, false, function(response){
            if (response.status == 1) {
                $('#my_followee_area').append(response.data.html);
                $('.followTooltip').unbind();
        		ZFramework.tooltip('.followTooltip', {className:'skin_border_color skin_text_color skin_background_color'});
        		var total = response.data.total;
                if (_followeePage == response.data.maxPages || total == 0) {
                    $('#show_more_followees').hide();
                }
                else $('#show_more_followees').show();
                $('#zgraffiti_followees_count').html(total);
            }
            else {
                ZFramework.alert(response.error);
            }
        });
    }
	
	function _setupRating() {
		$('.home_zgraffiti_rating').unbind('click').click(function(e){
			var data = $(this).attr('name').split('_');
			var postId = data[0];
			var ratingType = data[1];
			var target = $(this);
			var rated = false;
			if (target.hasClass('home_zgraffiti_rating_voted')) {
				rated = true;
			}
			ZFramework.zfAjaxPost('/zConnect/addRating', {
				object_id: postId,
				object_type: _objectTypeId,
				rating_type: ratingType,
				rated: rated
			}, true,
				function(response) {
					if (response.status == '1') {
						if (response.login == false) {
		                    $('#follow_promo').modal({
		                        onClose: function(dialog){
		                            $.modal.close(false);
		                        }
		                    });
		                } else {
							var likeNum = $('.zgraffiti_post_rank[name='+postId+']').parent().attr('name')*1;
							likeNum++;
							likeplural='';
							if (likeNum == 1) {
								likeplural = 's';
							}
							$('.zgraffiti_post_rank[name='+postId+']').html('<strong>'+likeNum+'</strong> <span> Like'+likeplural+' It</span>');
							target.parent().html('You Like This');
							$('.zgraffiti_post_rank[name='+postId+']').parent().attr('name', likeNum).show();
						}
					} else {
		            	ZFramework.alert(response.error);
		            }
				},
				function(){}
			);
			e.preventDefault();
		});
	}
}
ZGraffiti = new ZoolooGraffiti();