function discussion() {
	if($.browser.msie){ 
		return false; 
	}	
	
	if (document.location.href.lastIndexOf('#commentForm') == -1) {
		$(".comments h2").toggleClass("open");
		$(".unfold").hide();	
	}
	
	$(".comments h2").click(function(){
		$(this).toggleClass("open");
		$(".unfold").slideToggle();
	});
};

// Activate post rating
function Rating(obj_id, display) {
	this.selector = ".rating_" + obj_id;
	if (obj_id != null) {
		this.obj_id = obj_id;
	}
	if (display) {
		this.selector = ".post "+this.selector;
	}
};

$.extend(Rating.prototype, {
	activate: function() {
		this.pageSelector = this.selector;
		this.selector = "#commentForm " + this.selector;
		this.handleClick();
		this.showHovering();
	},
	
	handleClick: function() {
		var that = this;
		
		$(that.selector + " label").click(function(event) {
			this.preventDefault;
			$(this).find("input").attr("checked", "checked");
			
			var index = that.getIndex(that.selector + " label", this);
			
			that.setRating(index);
			// Send the rating to the server
			that.submitRating();
			
			return false;
		});
	},
	
	appendRating: function(selector, text) {
		$(selector).append(this.addToDOM(text));
	},
	
	addToDOM: function(text) {
		var template = ''
		+ '<label><input type="radio" value="1" name="rating" /></label>'
		+ '<label><input type="radio" value="2" name="rating" /></label>'
		+ '<label><input type="radio" value="3" name="rating" /></label>'
		+ '<label><input type="radio" value="4" name="rating" /></label>'
		+ '<label><input type="radio" value="5" name="rating" /></label>';
		
		if(text) {
			template += '<p>Rated <span class="rate">0</span> of 5 stars.</p>';
		}
		    
		template = '<div class="rating rating_' + this.obj_id + '">' + template + '</div>';
		
		return template;
	},
	
	disableRating: function() {
		var that = this;
		$(that.selector + " label").unbind();
	},
	
	enableRating: function() {
		this.addBehaviour();
	},
	
	showHovering: function() {
		var that = this;
		
		$(that.selector + " label").hover(
			function() {
				var index = that.getIndex(that.selector + " label", this);
				$(that.selector + " label:lt("+index+")").addClass("hover");
			},
			function() {
				var index = that.getIndex(that.selector + " label", this);
				$(that.selector + " label:lt("+index+")").removeClass("hover");
			}
		);
	},
	
	setMessage: function(message) {
		var that = this;
		
		if(message) {
			// console.log("Showing message", sending);
			$(that.selector + " p.text").hide();
			$(that.selector + " p.message").text(message).show();
		} else {
			// console.log("Showing rating", sending);
			$(that.selector + " p.text").show();
			$(that.selector + " p.message").hide();
		}
	},
	
	submitRating: function() {
		var that = this;
		
		that.setMessage("Sending your rating...");
		
		$.ajax({
			type: "POST",
			url: "/cms/blog/rate/" + this.obj_id + "/" + this.getRating() + "/",
			success: function(data) {
				if (data.success) {
					// console.log("Success", data);
					that.setMessage("Thanks for voting!");
					that.setRating(data.rating);				// Show the rating here
					that.updatePage(data.rating);				// Update the other ratings shown on the page
					that.disableRating();						//After the vote is accepted, block the rating
				} else {
					// Display the error somewhere? it is in data.error
					// console.log("Error", data);
					that.setMessage(data.error + " Did you rate this post already?");
					that.setRating(0);
				}
			},
			error: function(data, errorText) {
				that.setMessage(errorText);
			},
			dataType: "json"
		});
	},
	
	setRating: function(rating, selector) {
		var that = this;
		if (selector == null)
			selector = that.selector;
		// Reset:
		$(selector).find("label").removeClass("rated");
		$(selector).find("input").removeAttr("checked");
		// Set:
		$(selector).find("input:eq(" + rating + ")").attr("checked", true);
		$(selector).find("label:lt(" + rating + ")").addClass("rated");
		$(selector).find(".rate").text(rating);
	},
	
	getRating: function() {
		// Read the rating from the inputs
		var that = this;
		var rate = 0;
		var value = $(that.selector + " input:checked").val();
		if (value != undefined) {
			rate = value - 1;
		} else {
			rate = 5;
		}
		return rate;
	},
	
	updatePage: function(rating) {
		this.setRating(rating, this.pageSelector);
	},
	
	getIndex: function(haystack, needle) {
		return $(haystack).index(needle) + 1;
	}
});
