var Connect = {
	api: "1d821de02ed809b2794d953893f96e98",
	xd: "/utils/xd_receiver.htm",
	init: function(){
		FB_RequireFeatures(["XFBML"], function(){
			FB.init(Connect.api, Connect.xd, { ifUserConnected: Connect.login });
			Connect.loginButton = $(".fb-login");
			Connect.loginButton.attr("length", "long").attr("onlogin", "Comments.login").fbml("LoginButton");
			$(".fb-picture").each(function(){
				var pic = $(this);
				pic.attr("uid", pic.attr("title")).attr("size", "square").fbml("ProfilePic");
			});
		});
	},
	login: function(id){
		if(!id){ }
		FB.Facebook.apiClient.users_getInfo(id, "name", function(obj){
			var name = obj[0].name;
			$("div#comment-facebook-form ol li.full label").text(name + " skriver");
			$("div#comment-facebook-form input[name=author]").val(name);
			$("div#comment-facebook-form input[name=email]").val(id + "@facebook.skl");
			
			//$("img", Connect.loginButton).attr("src", "http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif");
			//$("a", Connect.loginButton).click(Connect.logout);
			
			Comments.loggedIn = true;
			Comments.setActiveTab(Comments.facebookTab, Comments.facebookForm);
		});
	},
	logout: function(e){
		e.preventDefault();
		e.stopPropagation();
		//console.log("logout");
	}
};
$(document).ready(Connect.init);
var Comments = {
	init: function(){
		Comments.emailTab = Comments.activeTab = $("#comment-email");
		Comments.emailForm = Comments.activeForm = $("#comment-email-form");
		
		Comments.facebookTab = $("#comment-facebook");
		Comments.facebookForm = $("#comment-facebook-form");
		
		Comments.emailTab.click(function(){
			Comments.setActiveTab(Comments.emailTab, Comments.emailForm);
		});
		Comments.facebookTab.click(function(){
			if(!Comments.loggedIn){
				FB.Connect.requireSession(function(){
					Comments.loggedIn = true;
					Comments.setActiveTab(Comments.facebookTab, Comments.facebookForm);
				});
			}else{
				Comments.setActiveTab(Comments.facebookTab, Comments.facebookForm);
			}
		});
		
		$("div.comment form").each(function(){
			var cm = new CommentForm($(this));
		});
	},
	setActiveTab: function(tab, form){
		if(Comments.activeTab){
			Comments.activeTab.removeClass("active");
		}
		if(Comments.activeForm){
			Comments.activeForm.hide();
		}
		
		Comments.activeTab = tab;
		Comments.activeForm = form;
		
		Comments.activeTab.addClass("active");
		Comments.activeForm.show();
	}
};
var CommentForm = function(el){
	this.el = el;
	this.el.submit($.fnbind(this.submit, this));
	this.fields = $("input[type=text], input[type=hidden], textarea", this.el);
	this.agree = $("input[name=agree]", this.el);
	this.buttonWrap = $("<div class=\"button-wrap\" />");
	$("input.button", this.el).wrap(this.buttonWrap);
};
CommentForm.prototype = {
	submit: function(e){
		e.preventDefault();
		var error = false;
		var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		this.fields.each(function(){
			var input = $(this);
			input.val($.trim(input.val()));
			
			if(input.val() == "" || (input.attr("name") == "email" && !reg.test(input.val())) ){
				error = true;
				input.addClass("error");
			}else{
				input.removeClass("error");
			}
		});
		
		if(!this.agree.attr("checked")){
			error = true;
			this.agree.parents("li").addClass("error");
		}else{
			this.agree.parents("li").removeClass("error");
		}
		
		if(!error){
			var action = this.el.attr("action");
			var data = this.el.serialize();
			data += "&ajax=true";
			$.post(action, data, $.fnbind(this.complete));
			this.buttonWrap.addClass("loading");
			this.fields.attr("disabled", true);
		}
	},
	complete: function(data){
		var comments = $("ol.comments");
		if(comments.length < 1){
			comments = $("<ol class=\"comments\" />");
			$("p.no-comments").replaceWith(comments);
		}
		var comment = $(data);
		comments.prepend(comment);
		
		var count = $("div.comment-list h2 span span");
		count.text(Math.floor(count.text()) + 1);
		var pic = $(".fb-picture", comment);
		if(pic.length > 0){
			// Facebook
			pic.attr("uid", pic.attr("title")).attr("size", "square").fbml("ProfilePic");
		}else{
			// !Facebook
			$("div.comment-form").hide();
			$("div.comment-list").before("<div class=\"message\"><p>Tack! Ett meddelande har skickats till din e-postadress.<br />Följ länken i meddelandet för att verifiera din kommentar.</p></div>");
		}
		this.buttonWrap.removeClass("loading");
		//comment.hide().show(250);
	}
};
$(document).ready(Comments.init);
