function ZoolooGraffiti(){
    var _singlePost = false;
    var _followerPage = 1;
	var _followeePage = 1;
    var _ratingObjectTypeId = 7;
	
    this.setup = function(graffitiURL, singlePost){
		
        _singlePost = singlePost;
        
        $('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_item').hover(function() {
            var postId = $(this).attr('id').split('_')[3];
            $('.graffit_rank_container_right[name='+postId+']').css('display','');
        }, function() {
            var postId = $(this).attr('id').split('_')[3];
            $('.graffit_rank_container_right[name='+postId+']').css('display','none');
        });
        ZFramework.tooltip('.followTooltip', {className:'skin_border_color skin_text_color skin_background_color'});
    };
    
    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
        }, true, 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'});
                if (_followerPage == response.data.maxPages) {
                    $('#show_more_followers').hide();
                }
            }
            else {
                ZFramework.alert(response.error);
            }
        });
    }
	
	function _getFollowee(){
        ZFramework.zfAjaxPost('/zGraffiti/getFollowees', {
            page: _followeePage
        }, true, 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'});
                if (_followeePage == response.data.maxPages) {
                    $('#show_more_followees').hide();
                }
            }
            else {
                ZFramework.alert(response.error);
            }
        });
    }
	
	function _setupRating() {
		$('.home_zgraffiti_rating, .home_zgraffiti_rating_voted').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: _ratingObjectTypeId,
				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 = $('.zg_rank_like[name='+postId+']').html().split(' ')[0]*1;
							var unlikeNum = $('.zg_rank_dislike[name='+postId+']').html().split(' ')[0]*1;
							if(ratingType == 1) {
								likeNum++;
								$('.zg_rank_like[name='+postId+']').html(likeNum + ' Like It');
								$('.zg_rank_like[name='+postId+']').removeClass('rank_inactive_status').addClass('rank_active_status');
								if(target.hasClass('home_zgraffiti_rating_voted')) {
									unlikeNum--;
									$('.zg_rank_dislike[name='+postId+']').html(unlikeNum + ' Don\'t Like It');
									$('.zg_rank_dislike[name='+postId+']').removeClass('rank_active_status').addClass('rank_inactive_status');
								}
								target.parent().html('<a class="home_zgraffiti_rating_voted graffiti_dontlike" href="#" name="'+postId+'_2">&nbsp;</a>');
							} else {
								unlikeNum++;
								$('.zg_rank_dislike[name='+postId+']').html(unlikeNum + ' Don\'t Like It');
								$('.zg_rank_dislike[name='+postId+']').removeClass('rank_inactive_status').addClass('rank_active_status');
								if(target.hasClass('home_zgraffiti_rating_voted')) {
									likeNum--;
									$('.zg_rank_like[name='+postId+']').html(likeNum + ' Like It');
									$('.zg_rank_like[name='+postId+']').removeClass('rank_active_status').addClass('rank_inactive_status');
								} 
								target.parent().html('<a class="home_zgraffiti_rating_voted graffiti_like" href="#" name="'+postId+'_1">&nbsp;</a>');
							}
							_setupRating();
						}
					} else {
		            	ZFramework.alert(response.error);
		            }
				},
				function(){}
			);
			e.preventDefault();
		});
	}
}
ZGraffiti = new ZoolooGraffiti();