﻿// alert message on some failure
function CallFailed(res) {
    alert(res.get_message());
}

function RefreshPage() { };

function AddProduct(idProduct, quantity) {
    if (!quantity)
        quantity = 1;
    PageMethods.AddToCommand(idProduct, quantity, function(obj, idp) { RefreshPanier(); ShowAddToPanier(idp); }, CallFailed, idProduct);
    $("#AddToPanier").find('.art-count').text(quantity);
}

function SetProductQuantity(idProduct, quantity, priceElem) {
    if (quantity)
        PageMethods.SetQuantityInCommand(idProduct, quantity, RefreshPanier, CallFailed, priceElem);
}

function RemoveProduct(idProduct) {
    PageMethods.RemoveFromCommand(idProduct, RefreshPanier, CallFailed);
    $("#AddToPanier").find('.close').trigger('click');
}

function ToggleCalCount(idProduct) {
    PageMethods.ToggleCals(idProduct, RefreshPanier, CallFailed);
}

function EditSalad(idSalad) {
    PageMethods.EditSalad(idSalad, RedirectToSSM, CallFailed);
}

function RedirectToSSM() {
    document.location.href = "Commande.aspx?IDTypeProduit=5";
}

function RefreshPanier(value, priceElem) {
    __doPostBack(idPanier, '');
    //$('#' + idPanier).effect("bounce", {}, 300);
    if (value && priceElem)
        $(priceElem).text(value.toFixed(2).replace(".", ",") + " €");
    RefreshPage();
}

function ShowAddToPanier(idProduct, message) {
    $("#AddToPanier").find(".products-table").html("Chargement...");
    if (message) {
        $("#AddToPanier").append($('<div />').addClass("inner").html(message));
    }
    Overlay("#AddToPanier");
    
        PageMethods.GetProductInfo(idProduct,
        function(html) {
            $("#AddToPanier").find(".products-table").html(html);
            $(".unselect").unselectable();
            RefreshPanier();
        }, CallFailed);
}

function AddIngredient(idIngredient, grande) {
    if (!grande)
        grande = false;
    PageMethods.AddIngredient(idIngredient, grande, RefreshSSM, CallFailed);
}

function SetIngredient(idIngredient, quantity) {
    if (quantity)
        PageMethods.SetIngredient(idIngredient, quantity, RefreshSSM, CallFailed);
}

function RemoveIngredient(idIngredient) {
    PageMethods.RemoveIngredient(idIngredient, RefreshSSM, CallFailed);
}

function RefreshSSM() {
    __doPostBack(idSSM, '');
}

function ToggleVote(idProduit, link) {
    PageMethods.ToggleVote(idProduit, function (votes) {
        $(link).closest('.shared-salad').find(".vote-count").text(votes);
        $(link).toggleClass('cancel-vote').toggleClass('do-vote');
    }, CallFailed);
}

function ShareSalad(tempID) {
    name = prompt("Quel nom voulez-vous donner à cette recette?");
    if (name != '') {
        PageMethods.ShareSalad(tempID, name, function (message) {
            alert(message);
        }, CallFailed);
    } else {
        alert('Vous devez spécifier un nom.');
    }
}

$(function () {
    $('a.vote').click(function () {
        var productId = $(this).attr('data-id');
        ToggleVote(productId, this);
        return false;
    });
});

function AddToPanier(productID) {
    PageMethods.AddToPanier(productID, function () {
        alert("La salade a été ajoutée à votre panier sur jour.fr");
        RefreshPanier();
    }, CallFailed);
}

$(function () {
    $('a.add-panier').click(function () {
        var productId = $(this).attr('data-id');
        AddToPanier(productId);
        return false;
    });
});
