﻿var ReferenceUser = '';

$(document).ready(function () {
    ReferenceUser = UserReference; //Variable on the master page
    $(".btn-command").click(function () {
        AddProduct($(this));
        return false;
    });


    $(".xpress-checkout").bind("click", function () {
        BlockButton();
    });

    $(".product-video-lightbox").live("click", function (event) {
        var object = $(".video-path", $(this));
        var width = 500;
        var height = 500;
        if ($("object", object).attr("width") != null && $("object", object).attr("width") != undefined) { width = $("object", object).attr("width"); }
        if ($("object", object).attr("height") != null && $("object", object).attr("height") != undefined) { height = $("object", object).attr("height"); }
        OpenShadowBox(object.html(), $(".video-product-name", $(this)).html(), width, height);
        event.preventDefault();
    });


});

function OpenShadowBox(content, title, width, height) {
    Shadowbox.open({
        content: content,
        player: "html",
        title: title,
        width: width,
        height: height
    });
}

function AddProduct(obj) {
    BlockUI();
    var Product = { "ProduitId": obj.siblings(".product-id").val(), "UserId": ReferenceUser, "Quantity": obj.siblings(".product-quantity").val(), "Price": obj.siblings(".product-price").val() };
    $.ajax({
        type: "POST",
        url: "/modules/eshop/webservice/eshopService.asmx/AddProduct",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(Product),
        async: false,
        success: function (data) {
            var objJs = JSON.parse(data);
            if (objJs.result != "error") {
                //mettre ajour les info de qte + total
                $(".eshop-totalprice").html(objJs.Total)
                $(".eshop-nb-item").html(objJs.NbItem)
            }
            else {
            }
        }
    });
    setTimeout(function () { $.unblockUI(); }, 1500)
}

function BlockUI() {
    $.blockUI({ message: "Ajout d'un produit / Add product",
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff'

        }
    });
}

function BlockButton() {
    $.blockUI({ message: "Patienter pendant la transaction / Wait for the transaction",
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff'

        }
    });
}


