// This is the main javascript file handling client-side cart and basket animation

var cartCookieName = 'serpodile_cart'

// Add a product given in href to a cookie
function addToBasket(addToBasketLink, basketMessageDiv) {
	$(addToBasketLink).click(function() {
        scrollToTop();
		var product_id = $(this).attr('id');
		var shoppingCart = getKartFromCookie();
		shoppingCart.addSingleProduct(product_id);
		$.setCookie(cartCookieName, shoppingCart.toJson(), cookieOptions);
		$(basketMessageDiv).show('slow', function() {
			$(basketMessageDiv).hide("slow");
		});
		displayBasket("#caddie",true);
		return false;
	});
};

function changeBasketCountry(newCountry) {
    var shoppingCart = getKartFromCookie();
    shoppingCart.changeCountry(newCountry);
    $.setCookie(cartCookieName, shoppingCart.toJson(), cookieOptions);
}

function displayBasket(basketImg, animate) {
	var shoppingCart = getKartFromCookie();
	var intitule;
	// setup panier image
	var taille = shoppingCart.size();
	if (taille == 0) {
		$(basketImg).attr("src", "/img/panier_vide.png");
		intitule = "";
	} else {
		$(basketImg).attr("src", "/img/panier_plein.png");
		intitule = taille + " article"
		if (taille > 1) {
			intitule += "s";
		}
	}
	// setup intitule
	$("#nb-article").text(intitule);
	if (animate) {
		animateBasket();
	}
}

function animateBasket() {
	$("#intitule-article").attr("style", "background: url(/img/increment.gif) 45% 50% no-repeat;");
	$("#intitule-article").delay(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
	quake();
}

function scrollToTop() {
    $('html, body').animate({scrollTop:0}, 'fast');
}

function quake() {
	var $panier = $("#panier a img");
	var speedy = 100;

	$panier.css("position", "relative");
	$panier.animate({
		left : '-5px'
	}, speedy, function() {
		$(this).animate({
			left : '5px'
		}, speedy * 2, function() {
			$(this).animate({
				left : '-5px'
			}, speedy * 2, function() {
				$(this).animate({
					left : '5px'
				}, speedy * 2, function() {
					$(this).animate({
						left : '0px'
					}, speedy);
				});
			});
		});
	});
}

function deleteALine(deleteLink) {
	$(deleteLink).click(function() {
		var productId = $(this).attr('id');
		var shoppingCart = getKartFromCookie();
		shoppingCart.removeProduct(productId);
		$.setCookie(cartCookieName, shoppingCart.toJson(), cookieOptions);
		$(basketMessageDiv).show('slow', function() {
			$(basketMessageDiv).hide("slow");
		});
		return true;// We need to follow up the link in that case.
	});
}

function changeAmount(form, inputBox) {
	$(inputBox).change(function() {
		var amount = parseInt($(this).val());
		var productId = $(this).attr('id');
		var shoppingCart = getKartFromCookie();
		shoppingCart.updateProduct(amount, productId);
		$.setCookie(cartCookieName, shoppingCart.toJson(), cookieOptions);
		location.reload(); //<-- this is dirty.
	});
}

// Get the content of the cookie as a Kart object.
function getKartFromCookie() {
	var shoppingCart = new Kart();
	var cookieContent = $.readCookie(cartCookieName);
	if (cookieContent) {
		shoppingCart.fromJson(cookieContent);
	}
	return shoppingCart;
}

// Show the content of the cookie
function showCookieContent(showCookieLink, messageDiv) {
	$(showCookieLink).click(function() {
		var shoppingCart = getKartFromCookie();
		$(messageDiv).text(shoppingCart.display());
		return false;
	});
}

function emptyCart(emptyCartLink) {
	$(emptyCartLink).click(function() {
		$.delCookie(cartCookieName);
		return true; // We need to follow up the link in that case.
	});
}

// -- Cookie Option
var cookieOptions = {
	duration : 1, // In days
	path : '/',
	domain : '',
	secure : false
}

// -- KART object --
function Kart() {
	this.products = new Object();
    this.country = "France";

    this.changeCountry = function(country) {
        this.country = country;
    }

    this.addSingleProduct = function(productId) {
        this.addProduct(1, productId);
    }

	this.addProduct = function(number, productId) {
		if (this.products[productId] == null) {
			this.products[productId] = number;
		} else {
			this.products[productId] += number;
		}
	}

    this.updateProduct = function(number,productId) {
        this.products[productId] = number;
    }

	this.removeProduct = function(productId) {
		delete this.products[productId];
	}

	this.size = function() {
        var numberSize = 0;
        for (var productId in this.products) {
            numberSize += this.products[productId];
        }
        return numberSize;
	}

	this.toJson = function() {
		return JSON.stringify(this);
	}

	this.fromJson = function(jsonContent) {
        var fromJson = JSON.parse(jsonContent);
		this.products = fromJson.products;
        this.country = fromJson.country;
	}

    this.display = function() {
        var buffer = "";
        for (var productId in this.products) {
            buffer += this.products[productId] + " x " + productId + " ";
        }
        alert(buffer);
    }

}
