$(document).ready(function() {
	$(".share").click(function(e) {
		var href = $(this).attr("href");
		var share = {
			method: 'stream.share',
			u: href
		};
		
		FB.ui(share);
		try {
			e.preventDefault();
			e.stopPropagation();
		} catch (error) {
			e.cancelBubble();
		}
		return false;
	});
	
	var linkPurposes = "getpurposes.php?id=";
	
	$("#links a.all").click(function(e) {
		$("#links a").animate({color: "#000"})
		$(this).animate({color: "#FFF"});
		$("#purposes").animate({"opacity": "0.5"});
		
		$.get("getpurposes.php?id=1", function(data) {
			$("#purposes").html(data);
			$("#purposes").animate({"opacity": "1"});
			linkPurposes = "getpurposes.php?id=";
			
		});
		
		try {
			e.preventDefault();
			e.stopPropagation();
		} catch (error) {
			e.cancelBubble();
		}
		return false;
	});
	
	$("#links a.liked").click(function(e) {
		$("#links a").animate({color: "#000"})
		$(this).animate({color: "#FFF"});
		$("#purposes").animate({"opacity": "0.5"});
		
		$.get("getliked.php?id=1", function(data) {
			$("#purposes").html(data);
			$("#purposes").animate({"opacity": "1"});
			linkPurposes = "getliked.php?id=";
		});
		
		try {
			e.preventDefault();
			e.stopPropagation();
		} catch (error) {
			e.cancelBubble();
		}
		return false;
	});
	
	$("#purposes .like a").live("click" , function(e) {
		var link = $(this);
		var number = $("span", link.parent());
		var id = $(this).attr("class");
		
		link.unbind("click");
		
		$.get("like.php?id="+id, function(data) {
			if (data != "error") {
				link.fadeOut(function() {
					number.html(data);
					number.animate({"line-height": "40px"});
				});
			} else {
				// Error
			}
		});
		
		try {
			e.preventDefault();
			e.stopPropagation();
		} catch (error) {
			e.cancelBubble();
		}
		return false;
	});
	
	$("#pagination.purposes .page").click(function(e) {
		$("#pagination #active.page").attr("id", "");
		$(this).attr("id", "active");
		
		var id = $(this).attr("href").substr(1);
		
		//$(location).attr("href", "#links")
		window.scrollTo(0,$("#links").offset().top);
		
		$("#purposes").animate({"opacity": "0.5"});
		
		$.get(linkPurposes+id, function(data) {
			$("#purposes").html(data);
			$("#purposes").animate({"opacity": "1"});
		});
	});
});
