﻿$(document).ready(function () {

    //var thisServer = "http://thebeach"
    var thisServer = ""

    //Slideshow {

    $('#home_Intro').append("<div id='home_Intro_Foto3'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Ontdek onze champagne</span>Wist je dat <i>crémants</i> Franse bubbelwijnen zijn die op precies dezelfde wijze champagnes worden gemaakt als champagne? La Champenoise biedt <i>crémants</i> aan uit de Bourgogne. Perfecte kwaliteit: de smaak van champagne en de prijs van een cava!</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto4'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Ontdek onze champagne</span>Wil je onze champagnes en crémants eerst leren kennen? La Champenoise nodigt je graag uit voor een champagneproeverij, zie onze agenda. Of nodig ons uit voor het organiseren van een sprankelende wijnproeverij op jouw verzoek! Stuur ons een e-mail en wij sturen je een offerte.</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto5'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Ontdek onze champagne</span>Op zoek naar een echt bijzonder relatiegeschenk? La Champenoise helpt met de juiste selectie champagne en een stijlvolle verpakking. Wat te denken van de combinatie tussen een sprankelend rode crémant met een tienjarige schitterende millésimé champagne uit 1999?</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto6'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Ontdek onze champagne</span>Het is mooi als wij vertellen bij welke gerechten onze champagnes mooi passen. Maar als je nou op zoek bent naar een mooie champagne bij je gerecht? Onze virtuele sommelier Richard Champagne helpt je graag bij het kiezen van de juiste wijn!</div></div></div>");


    $('#home_Intro').cycle({
        fx: 'fade',
        timeout: 7000,
        speed: 2000
    });



    //Bestelformulier submitten    
    $("#bestellenSubmit").click(function () {

        $("#formulier_Standaard").submit();

        //$("#formulier_Standaard").fadeOut("slow", function() {
        //    this.submit();
        //});

        return false;

    });


    //Thumbnail link
    $(".thumbLink").click(function () {

        var theThumb = $(this);

        $("#productFoto").fadeTo(200, 0, function () {
            $("#productFoto").attr("src", $(theThumb).next('.zoomImage').val()).fadeTo(600, 100);
        });

        return false;

    });



    //Factuur gegevens kopieeren > verzend
    $("#VerzendAdres_Identiek").click(function () {

        if ($(this).is(":checked")) {
            $("#AdresContactBedrijfsnaam_verzend").val($("#AdresContactBedrijfsnaam").val());
            $("#AdresContactAanhef_verzend").val($("#AdresContactAanhef").val());
            $("#AdresContactVoornaam_verzend").val($("#AdresContactVoornaam").val());
            $("#AdresContactAchternaam_verzend").val($("#AdresContactAchternaam").val());
            $("#AdresStraatnaam_verzend").val($("#AdresStraatnaam").val());
            $("#AdresHuisnummer_verzend").val($("#AdresHuisnummer").val());
            $("#AdresRegel2_verzend").val($("#AdresRegel2").val());
            $("#AdresPostcode_verzend").val($("#AdresPostcode").val());
            $("#AdresPlaats_verzend").val($("#AdresPlaats").val());
            $("#AdresLand_verzend").val($("#AdresLand").val());
        }
        else {
            $("#AdresContactBedrijfsnaam_verzend").val("");
            //$("#AdresContactAanhef_verzend").val("");
            $("#AdresContactVoornaam_verzend").val("");
            $("#AdresContactAchternaam_verzend").val("");
            $("#AdresStraatnaam_verzend").val("");
            $("#AdresHuisnummer_verzend").val("");
            $("#AdresRegel2_verzend").val("");
            $("#AdresPostcode_verzend").val("");
            $("#AdresPlaats_verzend").val("");
            //$("#AdresLand_verzend").val("");  

        };

    });


    //Tooltip lijsten
    $('.productInListZoom, .button_Vergroot').tooltip({
        track: true,
        delay: 0,
        fade: 250,
        showURL: false,
        top: -175,
        left: 15,
        bodyHandler: function () {
            return $("<img/>").attr("src", $(this).next('.productInListFoto_zoom').val());
            //return '<img src='"  + "' />';
            //alert($(this).next('.productInListFoto_zoom').val())
        }
    });

    //Tooltip betaalland
    $('.tooltipNormal').tooltip({
        track: true,
        delay: 0,
        fade: 250,
        extraClass: "tooltipNormalClass",
        showURL: false,
        top: 0,
        left: 15
    });


    //Product in winkelmandje
    $(".button_InWinkelmandje, .button_InWinkelmandje2").click(function () {

        productid = $(this).attr('href');

        var button = $(this);
        var preloader = $(this).next('img');

        button.css("display", "none");

        preloader.fadeIn('slow', function () {

            $.ajax({
                url: thisServer + "/inc/inc_functies_ajax.asp",
                type: "POST",
                data: "f=addproduct&productid=" + productid + "&aantal=1",
                dataType: 'jsonp',
                cache: false,
                success: function (rvAdd) {

                    //alert(rvAdd);


                    button.css("display", "block");
                    preloader.css("display", "none");

                    $.ajax({
                        url: thisServer + "/inc/inc_functies_ajax.asp",
                        type: "POST",
                        data: "f=calculatecart",
                        dataType: 'jsonp',
                        cache: false,
                        success: function (rv) {

                            reloadHeaderMandje(rv);

                            var win_width = $(window).width();
                            var scrollToLeft = $(window).scrollLeft();
                            var win_height = $(window).height();
                            var scrollToBottom = $(window).scrollTop();
                            var box_width = 381;
                            var box_height = 132;


                            //$('body').append("<div id='feedbackMsg'><img src='/images/icon_success.gif'><div>Het product is toegevoegd aan uw <a href='/winkelmand'>winkelmandje</a></div></div>");
                            $('body').append(rvAdd);
                            $('#feedbackMsg').css({ left: (((win_width / 2 - box_width / 2)) + scrollToLeft) + 'px', top: (((win_height / 2 - box_height / 2)) + scrollToBottom) + 'px' });

                            $('#feedbackMsg').fadeIn('slow').animate({ opacity: 1.0 }, 3000).fadeOut('slow', function () {
                                $(this).remove();
                            });



                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.responseText);
                        }
                    });


                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.responseText);
                }
            });

        });



        return false;


    });

    //Winkelmandje aantal bijwerken
    $(".winkelmand_ProductAantal").change(function () {

        var aantal = $(this).val();
        var id = $(this).next("input[type='hidden']").val();
        var prijs = $(this).next("input[type='hidden']").next("input[type='hidden']").val();

        $.ajax({
            url: thisServer + "/inc/inc_functies_ajax.asp",
            type: "POST",
            data: "f=updateproduct&WinkelmandjeBijwerken=True&productaantal" + id + "=" + aantal,
            dataType: 'jsonp',
            cache: false,
            success: function (rv) {
                //alert(rv);
                $('#' + id).find("td").css("background-color", "#bd932d")
                $('#' + id).find("td").fadeTo(500, 0.8, function () {
                    $(this).css("background-color", "#efefef")
                    var newprijs = (prijs * aantal)
                    $(".vProductPrijsTotaal" + id).empty()
                    $(".vProductPrijsTotaal" + id).append(newprijs.toFixed(2));

                    $(this).fadeTo("slow", 1, function () {

                    });

                });


                reloadHeaderMandje(rv);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
            }
        });

    });


    //Product verwijderen
    $(".winkelmand_ProductVerwijderen").click(function () {

        id = $(this).attr('href');

        $.ajax({
            url: thisServer + "/inc/inc_functies_ajax.asp",
            type: "POST",
            data: "f=deleteproduct&WinkelmandjeBijwerken=True&deleteProduct" + id + "=True",
            dataType: 'jsonp',
            cache: false,
            success: function (rv) {

                $('#' + id).find("td").css("background-color", "red");
                $('#' + id).find("td").fadeOut(500, function () { $(this).parent().remove(); });
                $('#' + id).fadeOut(500, function () { $(this).css("display", "none"); });

                reloadHeaderMandje(rv);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
            }
        });

        return false;

    });


    function reloadHeaderMandje(vMandje) {

        var aRV = vMandje.split("#")

        $("#header_Winkelmand").fadeOut("fast", function () {

            $(".vAantalproducten").empty().append(aRV[0]);

            $(".vTotaalbedrag").empty().append(aRV[1]);

            if (aRV[0] == 0) {
                $("#winkelmand_Leeg").css("display", "inline");
                $("#winkelmand_Gevuld").css("display", "none");
                $("#header_Winkelmand_Button").css("display", "none");
            }
            else {
                $("#winkelmand_Leeg").css("display", "none");
                $("#winkelmand_Gevuld").css("display", "inline");
                $("#header_Winkelmand_Button").css("display", "inline");
            }
        });

        $("#header_Winkelmand").fadeIn("fast");

        $(".vSubtotaal").empty().append(aRV[1]);

        $(".vShipping").empty().append(aRV[2]);

        $(".vBTW").empty().append(aRV[3]);

        $(".vGrandTotal").empty().append(aRV[4]);

        var subtotal = aRV[1];

        subtotal = subtotal.toString().replace(/\,/g, '.');


        //Minimum bestelwaarde: voorlopig n.v.t.
        //if (subtotal < 50) {

        //            $("#message_Besteding").css("display", "block");
        //$("#button_Betalen").css("display", "none");

        //}
        //else {

        //$("#message_Besteding").css("display", "none");
        //$("#button_Betalen").css("display", "block");

        //        }

        if (aRV[0] == 0) {

            $(".vTekstMandGevuld").css("display", "none");

            $(".vTekstMandLeeg").css("display", "block");

            $("#bestellen_Block").css("display", "none");

        }

    }

    //Nieuwsbrief aanmelding
    $("#NieuwsbriefLink").click(function () {

        var mail = $("#NieuwsbriefAanmelding").val();

        $.ajax({
            url: thisServer + "/inc/inc_functies_ajax.asp",
            type: "POST",
            data: "f=addaanmelding&v=" + mail,
            dataType: 'jsonp',
            cache: false,
            success: function (rv) {
                
                if (rv == "success") {
                    $("#NieuwsbriefBlock").empty().append("<h2>Hartelijk dank!</h2>U bent nu aangemeld voor onze nieuwsbrief!");
                }
                else {
                    $("#NieuwsbriefAanmelding").css("background-color", "red").css("color", "white");
                }
                
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
            }
        });

        return false;

    });



});
