﻿jQuery(function($jQuery) {
    $jQuery("input[value='disabled']").each(function() {
        $jQuery(this).parent().parent().hide();
    });

    $jQuery("[name='HideThisSection'] :checkbox:not(:checked)").map(function() {
        $jQuery(this).parent().parent().nextAll("div").each(function() {
            $jQuery(this).find("input[id$jQuery='_ecstate']").each(function() {
                $jQuery(this).parent().parent().hide();
                $jQuery(this).val("disabled");
            });
        });
    });

 

    $jQuery("[name='HideThisSection'] input").click(function() {
        var checked = $jQuery(this).attr("checked");
        $jQuery(this).parent().parent().nextAll("div").each(function() {
            $jQuery(this).find("input[id$jQuery='_ecstate']").each(function() {
                if (!checked) {
                    $jQuery(this).parent().parent().hide();
                    $jQuery(this).val("disabled");
                } else {
                    $jQuery(this).parent().parent().show();
                    $jQuery(this).val("");
                }
            });
        });
    });


    $jQuery("input").keypress(function(e) {
        var isFirstmatch = true;
        if (e.keyCode == 13) {
            $jQuery(this).parents().each(function() {
                if (isFirstmatch) {
                    $jQuery(this).find("input[type='submit'],input[type='image']").map(function() {
                        isFirstmatch = false;
                        $jQuery(this).trigger('click');
                        return false;
                    });
                }
            });
            return false;
        }
    });

    $jQuery(".modalinfo").click(function() {
        var maxWidth = 800;
        var maxHeight = 800;

        if ($jQuery("#modalinfo-overlay").html() == null) {
            $jQuery(this).after("<div id='modalinfo-overlay'></div><div id='modalinfo-content'></div>");
        }

        var w = ($jQuery(window).width() < maxWidth) ? $jQuery(window).width() * 0.7 : maxWidth;
        var h = ($jQuery(window).height() < maxHeight) ? $jQuery(window).height() * 0.7 : maxHeight;
        var lPos = (($jQuery(document).width() - w) / 2); // +$jQuery(document).scrollLeft();
        var tPos = (($jQuery(window).height() - h) / 2) + $jQuery(document).scrollTop();

        $jQuery("#modalinfo-overlay").css({ 'position': 'absolute', 'top': '0px', 'left': '0px', 'z-index': '0', 'background': '#000' });
        $jQuery("#modalinfo-overlay").css({ width: $jQuery(document).width() + 'px', height: $jQuery(document).height() + 'px', opacity: 0.7 }).show(); //.fadeIn();

        $jQuery("#modalinfo-content").css({ position: 'absolute', top: '200px', left: '200px', display: 'none', width: '600px', 'z-index': '1', background: '#fff', padding: '10px', overflow: 'auto' });
        $jQuery("#modalinfo-content").css({ 'left': lPos + 'px', 'width': w + 'px', 'top': tPos + 'px', 'height': h + 'px', 'background': '#fff' }).fadeIn();

        var href = $jQuery(this).attr("href").replace(/\s/g, '%20');
        $jQuery("#modalinfo-content").load(href + ' .content');

        $jQuery("#modalinfo-overlay").click(function() {
            $jQuery(this).fadeOut();
            $jQuery("#modalinfo-content").fadeOut();
        });

        $jQuery(window).resize(function() {
            $jQuery("#modalinfo-overlay:visible").map(function() {
                $jQuery(".modalinfo").trigger("click");
            });
        });
        return false;
    });


});


function LoadRendering(rendering, placeholder) {
    var id = $jQuery("#scID").attr("content");

    $jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/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/ecommerce/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/ecommerce/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/ecommerce/Ajax.aspx/AddReview",
        data: "{title:'" + title + "', text :'" + text + "', rate:'" + rate + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


function AddToBasket(productId, quantity, uom) {
    $jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/AddToBasket",
        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("F60 Samenvatting winkelwagen", null, function(msg) {
                $jQuery("#small_basket_container")./*animate({ opacity: 0.3 }, 200).*/replaceWith(msg.d);
                //$jQuery("#small_basket_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
        }
    });
}


function DeleteFromBasket(productId, uom) {
    $jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/DeleteFromBasket",
        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("F60 Samenvatting winkelwagen", null, function(msg) {
                $jQuery("#small_basket_container")./*animate({ opacity: 0.3 }, 200).*/replaceWith(msg.d);
                //$jQuery("#small_basket_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
        }
    });
}

function UpdateBasket(productId, quantity, uom) {
    $jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/UpdateBasket",
        data: "{productId:'" + productId + "' quantity:'" + quantity + "', uom :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


