﻿var configuratieGuid = null;

function updateConfiguratie(updateKleuren, updateBekledingen) {
    loadBegin();
    $("#calculator").load("../../../Configurator/Parts/Calculator.aspx?ConfiguratieGuid=" + configuratieGuid, "", function (response, status, xhr) {
        if (status == "error") {
            var msg = "<strong>Er is een fout opgetreden in de calculator.</strong><br /><br />";
            $("#calculator").html(msg + xhr.status + " " + xhr.statusText);
        }

        loadEnd();
    });
    if (updateKleuren) {
        loadBegin("#toolboxKleuren");
        $("#toolboxKleuren").load("Parts/ToolboxKleuren.aspx?ConfiguratieGuid=" + configuratieGuid, "", function() { loadEnd("#toolboxKleuren"); });
    }
    if (updateBekledingen) {
        loadBegin("#toolboxBekledingen");
        $("#toolboxBekledingen").load("Parts/ToolboxBekledingen.aspx?ConfiguratieGuid=" + configuratieGuid, "", function() { loadEnd("#toolboxBekledingen"); });
    }
}

var loadcount = 0;

function loadBegin(id) {
    loadcount++;
    $(id).fadeTo(1, 0.25);
    $('#loading').show();
}

function loadEnd(id) {
    loadcount--;
    $(id).fadeTo(50, 1);
    if (loadcount == 0) {
        $('#loading').hide();
        if (iframeIntegratie) resizeTop();
        setupTranslator();
    }
}

$(document).ready(function() {
    updateConfiguratie(document.getElementById("toolboxKleuren"), document.getElementById("toolboxBekledingen"));
});

function updateUitvoering(guid, uitvoeringId) {
    loadBegin();
    var kleurId = 0;
    var bekledingId = 0;
    Configurator.SetUitvoering(guid, uitvoeringId, onSuccess, onFailed, null);
}

function updateKleur(guid, kleurId) {
    loadBegin();
    Configurator.SetKleur(guid, kleurId, onKleurenSuccess, onFailed, null);
    updateKleurImages(kleurId);
    showKleur();
}

function updateKleurImages(kleurId) {
    var img = document.getElementById("imgKleur");
    img.src = pictureKleur + "" + kleurId;
    img = document.getElementById("imgKleurThumb");
    img.src = pictureKleurThumb + "" + kleurId;
}

function updateBekleding(guid, bekledingId) {
    loadBegin();
    Configurator.SetBekleding(guid, bekledingId, onBekledingenSuccess, onFailed, null);
    updateBekledingImages(bekledingId);
    showBekleding();
}

function OnAanbetalingChange(sender, args) {
    updateFinancieringAanbetaling(configuratieGuid, sender.get_value());
}

function OnLooptijdChange(sender, args) {
    updateFinancieringLooptijd(configuratieGuid, sender.get_value());
}

function updateFinancieringAanbetaling(guid, aanbetaling) {
    loadBegin();
    Configurator.SetFinancieringAanbetaling(guid, aanbetaling, onFinancieringSuccess, onFailed, null);
}

function updateFinancieringLooptijd(guid, looptijd) {
    loadBegin();
    Configurator.SetFinancieringLooptijd(guid, looptijd, onFinancieringSuccess, onFailed, null);
}

function onFinancieringSuccess() {
    updateConfiguratie(false, false);
    loadEnd();
}

function updateBekledingImages(bekledingId) {
    var img = document.getElementById("imgBekleding");
    img.src = pictureBekleding + "" + bekledingId;
    img = document.getElementById("imgBekledingThumb");
    img.src = pictureBekledingThumb + "" + bekledingId;
}

function onBekledingenSuccess() {
    updateConfiguratie(false, true);
    loadEnd();
}

function onKleurenSuccess() {
    updateConfiguratie(true, false);
    loadEnd();
}

function onOptieSuccess() {
    updateConfiguratie(false, false);
    loadEnd();
}

function onSuccess(response, context, method) {
    if (response.KleurChanged) {
        updateKleurImages(response.KleurId);
    }
    if (response.BekledingChanged) {
        updateBekledingImages(response.BekledingId);
    }
    updateConfiguratie(true, true);
    loadEnd();
}

function onFailed() {
    updateConfiguratie(true, true);
    loadEnd();
}

function showKleur() {
    $("#divBekledingImage").hide();
    $("#divKleurImage").show();

}

function showBekleding() {
    $("#divKleurImage").hide();
    $("#divBekledingImage").show();
}

function updateOptie(guid, optieid, selected) {
    loadBegin();
    Configurator.SetOptie(guid, optieid, selected, onOptieSuccess, onFailed, null);
}


