$(function() {

	var cartHandler = "/blog/wp-content/themes/gaston/cart.php";
	createSlide($("body"));

	//Ça créé le endless scroll
	if ($("#nextEntry").length > 0)
	{	$("#nextEntry").remove();	
		var page = 1;
		var activity=false;
		var noMorePage=false;
	
		$(window).scroll(function () { 

			if (($(".clearfooter").offset().top-$(window).scrollTop()) <= 1500)
			{	
				//Ok, fetch a new page..
				if(activity==true || noMorePage==true)
					return;
				activity=true;
				++page;
				var urlToFetch = document.location.href;
				if (urlToFetch.indexOf("?") > 0)
					urlToFetch += "&paged=" + page;
				else
					urlToFetch += "?paged=" + page;
				$.get( urlToFetch, {}, function(r) {
					var newPosts = $(r);
					createSlide(newPosts.find("#content"));
					newPosts.find("#previousEntry").remove();
					if (newPosts.find("#nextEntry").length == 0)
						noMorePage=true;
					else
						newPosts.find("#nextEntry").remove();
					newPosts.find("#content").children().hide().appendTo("#content").slideDown("slow");
					activity=false;
				}, "html" )
			}
	    });
	}



	$(".tagMenu").click(function() {
			$(".menuItemContainer").slideUp();
			menuParent = $(this).parent();
			var clickedItem = $(this);
			var container = menuParent.find(".menuItemContainer");
			if (menuParent.find(".menuItemContainer").length == 0)
			{	//hmm... there is supposed to be some elements to display here... let's query the db and create it
				var cat = $(this).attr("data-cat");
				$.getJSON("qMenu.php", {"cat": cat}, function(data)
				{	container = $("<div class=menuItemContainer/>").css({"display":"none"});
					$.each(data, function(index, record)
					{	container.append($("<a href=?tag=" + record.slug + "+" + record.slug + "&cat=" + cat + ">" + record.slug + " (" + record.count + ")</a><br>"));	});
					menuParent.append(container);
					clickedItem.click();
				});
			}

			container.slideToggle("fast");
	});

	$(".menuItemContainer").find("h2").remove();

	$(".shoppable").each(function() {
		var shoppableItem = $(this);
		$(this).click(function() {
			var template = $(".sideShoppingCartItemTemplate");
			var newRow = template.clone().attr("data-price",shoppableItem.attr("data-price")).attr("data-qty",1).attr("data-id",shoppableItem.attr("data-id"));
			newRow.removeClass("sideShoppingCartItemTemplate").addClass("sideShoppingCartItem").show("slow");
			newRow.find(".sideShoppingCartItemPicture").html("<img class=thumbPic src='/wp-content" + shoppableItem.attr("data-thumb") + "'>");
			newRow.find(".sideShoppingCartItemName").text(shoppableItem.attr("data-itemName") + " - " + shoppableItem.attr("data-price") + ".00 $");
			template.after(newRow);
			newRow.parent().show("slow");
			$.getJSON(cartHandler, {action:"addItem",id:$(this).attr("data-id"), qty:1});
			updateTotal();			
		});

	$(".smallx").live("click",function() {
			var row = $(this).parent();
			row.hide("slow", function() {
				$.getJSON(cartHandler, {action:"addItem",id:row.attr("data-id"), qty:0});
				updateTotal();
				$(this).remove();
		 		if ($(".sideShoppingCartItem").length == 0)
					$("#sideShoppingCart").hide("slow");

			  });
	});

	function updateTotal()
	{	var total=0;
		$(".sideShoppingCartItem").each(function() { total = total + (parseInt($(this).attr("data-price")) * parseInt($(this).attr("data-qty"))); });
		$("#cartPriceContainer").text("total: " + total + ".00 $");	
	}
	function removeCartIfEmpty() {
	}
	
	$("#sideShoppingCartPayButton").click(function() {
		$("#blockOverlay").show("slow");
		$("#modalMapMonde").show("slow");

	});
	
	$(".mapRegion").click(function() {
		$("#modalMapMonde").fadeOut("slow");
		$("#payNow").load(cartHandler,"action=payNow&region=" + $(this).attr("data-region"), function() { $("#payNow").submit(); });
	
	
	});
	
	
	});


});


function createSlide(container)
{
	if(container.find(".entry").not(".noeffect").length > 1)
	{	//Man, tellement de post qu'on va etre oblige de faire l'effet
		container.find(".entry").data("folded","true").css({height:"100px"}).find("img").addClass("fakeLink").click(function() 
		{	var entry = $(this).parents(".entry");
			if (entry.data("folded") == "true")
			{	entry.data("folded","false");
				entry.slideUp(200, function()
				{	entry.css({height:""}).find("img:first").css({"margin-top":"-100px"});
					entry.slideDown("slow");
				})
			}
			else {
				entry.data("folded","true");
				entry.slideUp("slow", function()
				{	$(this).find("img:first").css({"margin-top":""});
					entry.css({height:"100px"}).slideDown("slow");
				})
			}
		});
	}
	else
		container.find(".entry").not(".noeffect").find("img:first").css({"margin-top":"-100px"});
}
