eat.communityProfile = {    
    init: function() {
        this.follow.init();
    },
    
    follow: {
        init: function() {
            this.controls.init();
            this.userlist.init();
        },
        
        render: function() {
            this.controls.render();
            this.userlist.render();
        },
        
        controls: {
            data: {
                isFollowed: false
            },
            
            dom: {
                buttons: null,
                add: null,
                remove: null,
                progress: null
            },
            
            init: function() {
                var self = this;
                
                this.data.isFollowed = isFollowed;

                this.dom.buttons = $('#sidebar-follow-buttons');                
                this.dom.add = $('#sidebar-follow-buttons .follow-add');
                this.dom.remove = $('#sidebar-follow-buttons .follow-remove');
                this.dom.progress = $('#sidebar-follow-progress');
                
                this.dom.add.bind('click', function() {
                    self.toggle(this.id.split('-')[1]);
                });
                
                this.dom.remove.bind('click', function() {
                    self.toggle(this.id.split('-')[1]);
                });
                
                this.render();
            },
            
            render: function() {
                if(this.data.isFollowed) {
                    this.dom.remove.show();
                    this.dom.add.hide();
                }
                else {
                    this.dom.add.show();
                    this.dom.remove.hide();            
                }
                
                this.dom.buttons.show();
            },
            
            toggle: function(id) {
                var self = this;
    
                this.progress('show');
                
                $.post(eat.settings.followUrl, {user:id}, function(data) {
                    if(data==1) {
                        self.data.isFollowed = (self.data.isFollowed)?false:true;
                        
                        self.progress('hide');
                        self.render();
                        
                        eat.communityProfile.follow.userlist.loadData('followers', id);
                    }
                }, "json");
            },
            
            progress: function(state) {
                var self = this;
                
                if(state=='show') {
                    this.dom.progress.fadeIn(250);
                }
                else {
                    window.setTimeout(function() {
                        self.dom.progress.fadeOut(250);
                        self.render();
                    }, 500);
                }
            }
        },
        
        userlist: {
            data: {
                active: 'followers',
                following: null,
                followers: null
            },
            
            dom: {
                followingTab: null,
                followingCount: null,
                followersTab: null,
                followersCount: null,
                userlist: null
            },
            
            init: function() {
                var self = this;
                
                this.data.following = following;
                this.data.followers = followers;
                
                this.dom.followingTab = $('#sidebar-tabs-following');
                this.dom.followingCount = $('#sidebar-tabs-following span');
                this.dom.followingEmpty = $('#sidebar-follow-following-empty');
                this.dom.followersTab = $('#sidebar-tabs-followers');
                this.dom.followersCount = $('#sidebar-tabs-followers span');
                this.dom.followersEmpty = $('#sidebar-follow-followers-empty');
                this.dom.userlist = $('#sidebar-follow-userlist');
                this.dom.userlistData = $('#sidebar-follow-userlist-data');
                this.dom.userlistLink = $('#sidebar-follow-userlist-link');
                
                this.dom.followingTab.bind('click', function() {
                    self.selectTab('following');    
                });
                
                this.dom.followersTab.bind('click', function() {
                    self.selectTab('followers');    
                });
                
                this.render();
            },
            
            render: function() {
                if(this.data.active == 'following') {
                    this.dom.followingTab.addClass('on');
                    this.dom.followersTab.removeClass('on');
                }
                else {
                    this.dom.followingTab.removeClass('on');
                    this.dom.followersTab.addClass('on');                
                }
            
                this.dom.followingCount.html(((this.data.following)?String(this.data.following.length):'0'));
                this.dom.followersCount.html(((this.data.followers)?String(this.data.followers.length):'0'));
                
                this.dom.followingEmpty.hide();
                this.dom.followersEmpty.hide();
                
                // following
                if(this.data.active == 'following') {
                    if(this.data.following.length==0) {
                        this.dom.followingEmpty.show();
                        this.dom.userlist.hide();
                    }
                    else {
                        this.dom.userlistData.html(this.getUserlistHTML(this.data.following));
                    
                        this.dom.followingEmpty.hide();
                        this.dom.userlist.show();
                    }
                    
                    this.dom.userlistLink.show();
                }

                // following
                if(this.data.active == 'followers') {
                    if(this.data.followers.length==0) {
                        this.dom.followersEmpty.show();
                        this.dom.userlist.hide();
                    }
                    else {
                        this.dom.userlistData.html(this.getUserlistHTML(this.data.followers));
                        
                        this.dom.followersEmpty.hide();
                        this.dom.userlist.show();
                    }
                    
                    this.dom.userlistLink.hide();
                }
            },
            
            getUserlistHTML: function(list) {
                var html = '<div class="userlist-header"></div>';
                
                for(var a in list) {
                    var user = list[a];
                
                    html += '<a class="userlist-entry" href="'+user[2]+'">';
                        html += '<div class="userlist-entry-name">'+user[1]+'</div>';
                        
                        if(user[3]) {
                            html += '<div class="userlist-entry-image"><img src="'+user[3]+'" alt="'+user[1]+'" /></div>';
                        }
                        else {
                            html += '<div class="userlist-entry-no-image"></div>';                        
                        }
                    html += '</a>';
                }

                html += '<div class="userlist-footer"></div>';
                
                return html;
            },
            
            selectTab: function(id) {
                this.data.active = id;
                this.render();
            },
            
            loadData: function(type, id) {
                var self = this;
                
                if(type == 'following') {
                    $.post(eat.settings.followingJsonUrl, {user:id}, function(data) {
                        if(data!==0) {
                            self.data.following = data?data:null;
                            self.render();
                        }
                    }, "json");
                }
                
                if(type == 'followers') {
                    $.post(eat.settings.followersJsonUrl, {user:id}, function(data) {
                        if(data!==0) {
                            self.data.followers = data?data:null;
                            self.render();
                        }
                    }, "json");
                }
            }
        }
    }
};

$(document).ready(function() {
	eat.communityProfile.init();
});eat.communityProfileReviews = {    
    init: function() {
        //
    }
};

$(document).ready(function() {
	eat.communityProfileReviews.init();
});imagebox = {
	zindex: 10000,
	visible: false,
	moving: false,
	currentImageTop: 0,
	currentThumbTop: 107,
	currentImage: 0,
	imageHeight: 269,
	thumbHeight: 60,
	images: [],
	thumbs: [],
	captions: [],

	init: function() {
		var self = this;
		
		$('.imagebox').each(function() {		
			$(this).bind('click', function(e) {
				e.stopPropagation();
			
				var id = this.id.split('-')[1];
				
				self.open(this, id);
			});
		});
		
		$('#body').bind('click', function() {
			self.close();
		});
	},
	
	open: function(obj, id) {
		var self = this;
		
		if(!this.visible) {
		
			// set basic state
			this.visible = true;
			this.currentImage = 0;
			this.currentImageTop = 0;
			this.currentThumbTop = 107;
			this.captions = $('#imagebox-'+id+'-caption-list').val().split('\|');
			this.images = $('#imagebox-'+id+'-image-list').val().split(',');
			this.thumbs = $('#imagebox-'+id+'-thumb-list').val().split(',');
		
			var html = '';
			
			html += '<div id="imagebox">';
			
			html += '<div id="imagebox-close"></div>';
	
			html += '<div id="imagebox-roll">';
				html += '<div id="imagebox-roll-previous"></div>';
					
				html += '<div id="imagebox-roll-images">';			
				
					for(var a in this.thumbs) {
						html += '<div class="imagebox-roll-image"><img src="'+this.thumbs[a]+'" alt="" width="50" height="50" /></div>';
					}
					
				html += '</div>';
				
				html += '<div id="imagebox-roll-next"></div>';
			html += '</div>';
			
			html += '<div id="imagebox-images">';
				html += '<div id="imagebox-images-holder">';			
				
					for(var a in this.images) {
						html += '<div class="imagebox-images-image">';
						html += '<img src="'+this.images[a]+'" alt="" />';
						
						if(this.captions[a]) {
							html += '<div class="imagebox-images-image-caption">'+this.captions[a]+'</div>';
						}
							
						html += '</div>';
					}
					
				html += '</div>';
			html += '</div>';
			
			html += '</div>';
			
			imagebox.zindex++;
			
			$(obj).parents('.entry').eq(0).css('z-index', this.zindex).append(html);
			
			this.updateLinks();
			
			// bind events
			window.setTimeout(function() {			
				$('#imagebox-close').bind('click', function() {
					self.close();
				});
				
				$('#imagebox-roll-next').bind('click', function(e) {
					e.stopPropagation();
					self.nextImage();
				});
				
				$('#imagebox-roll-previous').bind('click', function(e) {
					e.stopPropagation();
					self.previousImage();
				});
				
				if (core.browser.msie6) {
					$('#imagebox').show();
				}
				else {
					$('#imagebox').fadeIn(250);
				}
			}, 50);
		}
	},
	
	close: function() {
		var self = this;
		
		if (core.browser.msie6) {
			$('#imagebox').hide().remove();
			self.visible = false;
		}
		else {
			$('#imagebox').fadeOut(250, function(){
				$('#imagebox').remove();
				self.visible = false;
			});			
		}
	},
	
	nextImage: function() {
		var self = this;
		
		if(!this.moving) {		
			if(this.currentImage<this.images.length-1) {
				this.moving = true;
				
				var thumbTop = this.currentThumbTop-this.thumbHeight;
				var imageTop = this.currentImageTop-this.imageHeight;
			
				// roll thumb
				$('#imagebox-roll-images').animate({
					top:thumbTop
				}, 250, function() {
					self.moving = false;
					self.currentThumbTop = thumbTop;
				});
				
				// roll image
				$('#imagebox-images-holder').animate({
					top:imageTop
				}, 250, function() {
					self.moving = false;
					self.currentImageTop = imageTop;
					
					self.updateLinks();
				});
				
				this.currentImage++;
			};
		};
	},
	
	previousImage: function() {
		var self = this;
		
		if(!this.moving) {		
			if(this.currentImage>0) {
				this.moving = true;
				
				var thumbTop = this.currentThumbTop+this.thumbHeight;
				var imageTop = this.currentImageTop+this.imageHeight;
			
				// roll thumb
				$('#imagebox-roll-images').animate({
					top:thumbTop
				}, 250, function() {
					self.moving = false;
					self.currentThumbTop = thumbTop;
				});
				
				// roll image
				$('#imagebox-images-holder').animate({
					top:imageTop
				}, 250, function() {
					self.moving = false;
					self.currentImageTop = imageTop;
					
					self.updateLinks();
				});
				
				this.currentImage--;
			};
		};
	},
	
	updateLinks: function() {
		
		if(this.currentImage == this.images.length-1) {
			$('#imagebox-roll-next').addClass('disabled');
		}
		else {
			$('#imagebox-roll-next').removeClass('disabled');		
		};

		if(this.currentImage == 0) {
			$('#imagebox-roll-previous').addClass('disabled');
		}
		else {
			$('#imagebox-roll-previous').removeClass('disabled');
		};
			
	}
};

jQuery(document).ready(function() {
	imagebox.init();
});

