﻿
function LoadRendering(rendering, placeholder) {
    var id = jQuery("#scID").attr("content");

    jQuery.ajax({
        type: "POST",
        url: "/layouts/TL/Ajax.aspx/LoadRendering",
        data: "{rendering:'" + rendering + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            placeholder.html(msg.d);
        }
    });
}

function LoadSublayout(sublayout, placeholder, callback) {
    var id = jQuery("#scID").attr("content");

    jQuery.ajax({
        type: "POST",
        url: "/layouts/TL/Ajax.aspx/LoadSublayout",
        data: "{sublayout:'" + sublayout + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: (callback) ? callback : function (msg) {
            placeholder.html(msg.d);
        }
    });
}


function SetCurrentTab(tab) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/TL/Ajax.aspx/SetCurrentTab",
        data: "{tab:'" + tab + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


function AddReview(title, text, rate) {
    var id = jQuery("#scID").attr("content");
    jQuery.ajax({
        type: "POST",
        url: "/layouts/TL/Ajax.aspx/AddReview",
        data: "{title:'" + title + "', text :'" + text + "', rate:'" + rate + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

var addToShoppingCartIsProcessing = false;

function AddToShoppingCart(productId, quantity, uom) {
    if (!addToShoppingCartIsProcessing) {
        addToShoppingCartIsProcessing = true;
        setTimeout(function () {
            addToShoppingCartIsProcessing = false;
        }, 500);
        jQuery.ajax({
            type: "POST",
            url: "/layouts/TL/Ajax.aspx/AddToShoppingCart",
            data: "{productId:'" + productId + "', quantity:'" + quantity + "', uom :'" + uom + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Replace the div's content with the page method's return.
                LoadSublayout("TL/SubLayouts/TLWebshop/TLShoppingCartSpot", null, function (msg) {
                    var id = getClientSideId('shoppingCartSport');

                    jQuery("#" + id).find(".item").animate({ opacity: 0.3 }, 500, function (id) {

                        jQuery(this).replaceWith(jQuery(msg.d).find(".item"));
                        jQuery(this).css({ opacity: 0.3 });
                        jQuery(this).animate({ opacity: 1 }, 600);

                    });
                });
            }
        });
    }

}

function getClientSideId(ServersideID) {
    return jQuery("div[id$='" + ServersideID + "']").attr("id");
}
function show() {

}




function DeleteFromShoppingCart(productId, uom) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/damask/Ajax.aspx/DeleteFromShoppingCart",
        data: "{productId:'" + productId + "', uom :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            LoadSublayout("Damask/UserControls/ShoppingCartSpot", null, function (msg) {
                jQuery("#small_ShoppingCart_container").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                jQuery("#small_ShoppingCart_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);

            });
            LoadSublayout("Damask/UserControls/webshop/ShoppingCart/SmallCartInfo", null, function (msg) {
                jQuery("#SmallCartInfo_container").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                jQuery("#SmallCartInfo_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
        }
    });
}

function UpdateShoppingCart(productId, quantity, uom) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/damask/Ajax.aspx/UpdateShoppingCart",
        data: "{productId:'" + productId + "' quantity:'" + quantity + "', uom :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

function SwapImage(Elem_id, className) {
    var elem = document.getElementById(Elem_id);
    elem.setAttribute("class", className);
}


