google.load("swfobject", "2.1");

var Recipes = new Class({
	recipeID:null,
	initialize:function() {
		var recipeLikeBtn = $$("#recipe-like");
		if (recipeLikeBtn.length > 0) {
			recipeLikeBtn = recipeLikeBtn[0];
			recipeLikeBtn.addEvent("click", this.handleLikeClicked.bind(this));
		}
	},
	handleLikeClicked:function(e) {
		Recipes.recipeID = e.target.parentNode.getAttribute("rel");
		
		// kill the button
		e.target.parentNode.parentNode.removeChild(e.target.parentNode);
		
		// fade down value
		var morphObject = new Fx.Morph($("recipe-like-number"));
 
		morphObject.start({
			'opacity':0
		});
		
		morphObject.onComplete = function(){
			sendLikeData(Recipes.recipeID);
		}
		
	}
});

function sendLikeData(id) {
	var req = new Request({
		method: 'get',
		url: "/recipe-like.php",
		data: { 'recipe' : id },
		onComplete:recipeLiked
	}).send();
}

function recipeLiked(response) {
	$("recipe-like-number").innerHTML = response;
	
	if (response == "1") {
		// need to use the word "person";
		$("recipe-like-content").innerHTML = "Person liked this recipe";
	} else {
		$("recipe-like-content").innerHTML = "People liked this recipe";		
	}
	
	var morphObject = new Fx.Morph($("recipe-like-number"));

	morphObject.start({
		'opacity':1
	});	
}

function initRecipes() {
	new Recipes();
}

window.addEvent("domready", initRecipes);