olga.pages_home = {
    init: function() {
        this.comments.init();
        this.archive.init();
        this.actions.init();
	},
	
	comments: {
	    dom: {},
	    
	    init: function() {
	    	this.dom.morelinks = $(".showmore span");
	    	
	    	this.render();
        },
        
        render: function() {
        	this.dom.morelinks.click(function() {
            	$(this).find("b").hide().closest(".showmore").removeClass("showmore")
            		.closest(".case").find(".entry-wrapper.hidden").toggleClass("hidden");
            });
        }
	},
	
	archive: {
		dom: {},
		
		init: function() {
			this.dom.container = $("#acute-now");
			
			this.render();
		},
		
		render: function() {
			this.dom.link = $("#more-cases");
			var self = this;
			
			this.dom.link.click(function() {
				self.handleClick(self);
			});
		},
		
		handleClick: function(self) {
			$.ajax({ url: olga.settings.loadCasesUrl, 
				data: { start: self.dom.container.find(".case").size() }, 
				success: function(data) {
					self.dom.link.replaceWith(data);
					
					self.render();
				}
			});
		}
	},
	
	actions: {
	    init: function() {
	        this.thumb.init();
	    },
	    
	    thumb: {
	        dom: {},
	        
	        init: function() {
	            var self = this;
	            
	            this.dom.core = $('.case-actions .thumb');
	            
	            this.dom.core.click(function() {
	                self.vote(this.id.split('-')[1]);
	            });
	        },
	        
	        vote: function(id) {
	            var self = this;
	            
	            $.post(olga.settings.thumbUrl, {id:id}, function(data) {
	                if(parseInt(data)==1) {
	                	var el = $("#thumb-" + id + " strong");
        	            el.html(parseInt(el.html())+1);
        	        }
        	        else {
        	            alert("Tyyppi yrittää huijata. Hyvä piirre sussa mut just nyt ei onnistu.");
        	        }
    	        });
	        }
	    }
	}
}

jQuery(document).ready(function() {
    olga.pages_home.init();
});
