﻿var allProducts;
var allQuestions;
var allStaticQuestions;
var questions = [];
var products = [];
var custProducts = [];
var staticQuestions = [];
var wishLocation;

function refreshMyFuneralList() {
    $loading.show();
    loadProducts();
}

function loadProducts() {
    // get all products
    $.getJSON("/c-RootSite/Builtin_Service", { action: "getProducts" }, function (data) {
        if (!allProducts) {
            allProducts = data.products;
            allQuestions = data.questions;
            allStaticQuestions = data.staticQuestions;
            wishLocation = data.location;
            $("#productCount").text(data.count);
        }

        var uuids = GetProducts();
        products = []; // reset
        questions = [];
        staticQuestions = [];
        var index = 0;
        for (var i = 0; i < allProducts.length; i++) {
            var product = allProducts[i];
            if (uuids.indexOf(product["UUID"]) >= 0) { // add
                products[index] = product;
                index++;
            }
        }
        var answers = GetWishs();
        for (var j = 0; j < allQuestions.length; j++) {
            if (answers[allQuestions[j].UUID]) {
                var answer = answers[allQuestions[j].UUID];
                var title = allQuestions[j].Title;
                var ans = '';
                if (answer.Answer.indexOf('A') > -1)
                    ans += allQuestions[j].OptionA + "<br/>";
                if (answer.Answer.indexOf('B') > -1)
                    ans += allQuestions[j].OptionB + "<br/>";
                if (answer.Answer.indexOf('C') > -1)
                    ans += allQuestions[j].OptionC + "<br/>";
                if (answer.Answer.indexOf('D') > -1)
                    ans += allQuestions[j].OptionD + "<br/>";
                if (answer.Answer.indexOf('Last') > -1)
                    ans += answer.OptionLastInput + "<br/>";

                questions.push({ uuid: allQuestions[j].UUID, title: title, answers: ans, rule: allQuestions[j].ProductRule });
            }
        }
        var staticAnswers = GetStaticQuestions();
        for (var k = 0; k < allStaticQuestions.length; k++) {
            var title = allStaticQuestions[k].Title;
            var type = allStaticQuestions[k].EncodeTitle.toLowerCase();
            var ans = '';
            if (staticAnswers[allStaticQuestions[k].UUID]) {
                var answer = staticAnswers[allStaticQuestions[k].UUID];
                switch (type) {
                    case "crematie":
                        ans += staticLiterals.subtitle_crematie + '<br/>'
                        if (answer.Answer.indexOf('A') > -1)
                            ans += staticLiterals.option_a_crematie + '<br/>';
                        if (answer.Answer.indexOf('B') > -1)
                            ans += staticLiterals.option_b_crematie + '<br/>';
                        if (answer.Answer.indexOf('C') > -1)
                            ans += staticLiterals.option_c_crematie + '<br/>';
                        if (answer.Answer.indexOf('D') > -1)
                            ans += staticLiterals.option_d_crematie;
                        if (answer.Answer.indexOf('E') > -1)
                            ans += staticLiterals.option_e_crematie;
                        break;
                    case "begravenis":
                        ans += staticLiterals.subtitle_begravenis + '<br/>'
                        if (answer.Answer.indexOf('A') > -1)
                            ans += staticLiterals.option_a_begravenis + '<br/>';
                        if (answer.Answer.indexOf('B') > -1)
                            ans += staticLiterals.option_b_begravenis + '<br/>';
                        if (answer.Answer.indexOf('C') > -1)
                            ans += staticLiterals.option_c_begravenis + '<br/>';
                        if (answer.Answer.indexOf('D') > -1)
                            ans += staticLiterals.option_d_begravenis;
                        if (answer.Answer.indexOf('E') > -1)
                            ans += staticLiterals.option_e_begravenis;
                        break;
                    case "voorwerpen_bij_kist":
                        if (answer.Answer.indexOf('A') > -1) {
                            ans += staticLiterals.input_a_voorwerpen_bij_kist + '<br/>';
                            ans += answer.inputA + "<br/>";
                        }
                        if (answer.Answer.indexOf('B') > -1)
                            ans += staticLiterals.option_b_voorwerpen_bij_kist + '<br/>';
                        if (answer.Answer.indexOf('C') > -1)
                            ans += staticLiterals.option_c_voorwerpen_bij_kist + '<br/>';
                        if (answer.Answer.indexOf('D') > -1) {
                            ans += staticLiterals.option_d_voorwerpen_bij_kist + '<br/>';
                            ans += answer.inputD;
                        }
                        if (answer.Answer.indexOf('E') > -1) {
                            ans += staticLiterals.option_e_voorwerpen_bij_kist + '<br/>';
                        }
                        break;
                    case "religieuze_uitvaart":
                        if (answer.Answer.indexOf('A') > -1) {
                            ans += staticLiterals.option_a_religieuze_uitvaart + '<br/>';
                            if (answer.inputA1 != '')
                                ans += staticLiterals.input_a1_religieuze_uitvaart + " " + answer.inputA1 + "<br/>";
                            if (answer.inputA2 != '')
                                ans += staticLiterals.input_a2_religieuze_uitvaart + " " + answer.inputA1 + "<br/>";
                            if (answer.inputA3 != '')
                                ans += staticLiterals.input_a3_religieuze_uitvaart + " " + answer.inputA1 + "<br/>";
                        } else if (answer.Answer.indexOf('B') > -1) {
                            ans += staticLiterals.option_b_religieuze_uitvaart + '<br/>';
                            if (answer.inputB != '')
                                ans += staticLiterals.input_b_religieuze_uitvaart + " " + answer.inputB;
                        } else if (answer.Answer.indexOf('C') > -1) {
                            ans += staticLiterals.option_c_religieuze_uitvaart + '<br/>';
                        } else if (answer.Answer.indexOf('D') > -1) {
                            ans += staticLiterals.option_d_religieuze_uitvaart + '<br/>';
                        }
                        break;
                    case "overige_wensen":
                        if (answer.Answer.indexOf('A') > -1 && answer.inputA != '')
                            ans += answer.inputA;
                        break;
                }
                staticQuestions.push({ uuid: allStaticQuestions[k].UUID, title: title, answers: ans, rule: allStaticQuestions[k].ProductRule });
            } else if (type == "cremeren_of_begraven") {
                var wishType = GetWishType();
                if (wishType && wishType != "both") {
                    switch (wishType) {
                        case "begraaf":
                            ans = staticLiterals.option_a_cremeren_of_begraven + ".<br/>";
                            ans += staticLiterals.location_begraven_subtitle + ": " + wishLocation;
                            break;
                        case "crematie":
                            ans = staticLiterals.option_b_cremeren_of_begraven + ".<br/>";
                            ans += staticLiterals.location_cremeren_subtitle + ": " + wishLocation;
                            break;
                        case "decidelater":
                            ans = staticLiterals.option_c_cremeren_of_begraven + ".";
                            break;
                    }
                    staticQuestions.push({ uuid: allStaticQuestions[k].UUID, title: title, answers: ans, rule: allStaticQuestions[k].ProductRule });
                }
            }
        }
        custProducts = [];
        var cstProducts = GetCustProducts();
        for (var gid in cstProducts) {
            var title = '';
            var rule = '';
            var cid = '';
            for (var j = 0; j < allProducts.length; j++) {
                var group = allProducts[j]['ProductGroup'];
                if (group && group['UUID'].toLowerCase() == gid) {
                    title = group["Title"];
                    rule = group["ProductRule"];
                    cid = group["ContentId"];
                    break;
                }
            }
            custProducts.push({ id: cid, title: title, text: cstProducts[gid], rule: rule });
        }
        buildProductList();
    });
}

function checkScrollPanePosition() {
    var top = $('.jScrollPaneDrag').position().top;
    if (top == 0)
        return 0;
    var height = $('.jScrollPaneDrag').height();
    if (top + height >= $('.jScrollPaneTrack').height())
        return 3;

    return 2;
}

function buildProductList() {

    var selectedCount = products.length + questions.length + staticQuestions.length;
    if (selectedCount < 1) {
        $('#yesWish').hide();
        $('#noWish').show();
    }
    else {
        $('#noWish').hide();
        $('#yesWish').show();
    }
    var list = [];

    for (var i = 0; i < products.length; i++) {
        var product = products[i];
        var productHtml = "<li><a href='" + wlink + "?rule=" + product["ProductGroup"]["ProductRule"] + "&id=" + product["ProductGroup"]["ContentId"] + "'><h4 class='name-category'>" + product["Title"] + "</h4><input type='hidden' id='uuid' value='" + product["UUID"] + "' /><img src='" + product["Thumbnail"] + "' alt='' width='40' /><span class='name-product'>" + product["IntroText"] + "</span></a></li>";
        list.push(productHtml);
    }

    for (var j = 0; j < questions.length; j++) {
        var question = questions[j];
        var questionHtml = "<li><a href='" + wlink + "?rule=" + question.rule + "&id=" + question.uuid + "'><h4 class='name-category'>" + question["title"] + "</h4><input type='hidden' id='uuid' value='" + question["UUID"] + "' /><span class='name-product'>" + question["answers"] + "</span></a></li>";
        list.push(questionHtml);
    }

    for (var k = 0; k < staticQuestions.length; k++) {
        var question = staticQuestions[k];
        var questionHtml = "<li><a href='" + wlink + "?rule=" + question.rule + "&id=" + question.uuid + "'><h4 class='name-category'>" + question["title"] + "</h4><input type='hidden' id='uuid' value='" + question["UUID"] + "' /><span class='name-product'>" + question["answers"] + "</span></a></li>";
        list.push(questionHtml);
    }

    for (var l = 0; l < custProducts.length; l++) {
        var cst = custProducts[l];
        var questionHtml = "<li><a href='" + wlink + "?rule=" + cst.rule + "&id=" + cst.id + "'><h4 class='name-category'>" + cst["title"] + "</h4><input type='hidden' id='uuid' value='" + cst["UUID"] + "' /><span class='name-product'>" + cst["text"] + "</span></a></li>";
        list.push(questionHtml);
    }

    $("#productList").html(list.join(""));

    $('#productList li').click(function () { document.location = $(this).children('a').attr('href'); });


    $('#countChosen').text(selectedCount);

    if (selectedCount > 4) {
        $("#wishList-holder").height("267px")
            .mouseenter(function () {
                var pos = checkScrollPanePosition();
                if (pos != 3)
                    $("#downButton").show();

                if (pos != 0)
                    $('#upButton').show();
            })
            .mouseleave(function (evt) {
                if (!$(evt.relatedTarget).hasClass('arrow-down') && !$(evt.relatedTarget).hasClass('arrow-up')) {
                    $("#downButton").hide();
                    $('#upButton').hide();
                }
            });
        $("#wishList-holder").jScrollPane({ showArrows: true });
    }
    else {
        $("#wishList-holder").height("auto");
        $("#downButton").hide();
        $('#upButton').hide();
        var itemHeight = $("#productList > li").height();
        if (itemHeight > 0) {
            itemHeight = itemHeight + 15;
        }
        var containerHeight = itemHeight * products.length;
        $(".jScrollPaneContainer").height(containerHeight);
    }

    $loading.hide();
}

var $loading;
$(document).ready(function () {
    $loading = $("#loading");

    loadProducts();
    subscribe("wishchanged", loadProducts);
    $('#downButton')
        .click(function () {
            $("#wishList-holder")[0].scrollBy(65);
        });
    $('#upButton')
        .click(function () {
            $("#wishList-holder")[0].scrollBy(-65);
        });
});
