﻿$(document).ready(function() {
    
    //var thisServer = "http://thebeach"
    var thisServer = ""
    
    //Slideshow {

    $('#home_Intro').append("<div id='home_Intro_Foto2'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Toegang tot de mooiste wijnen uit Itali&euml;</span>De voordelen van bestellen via internet al ontdekt? Toegang tot mooie Toscaanse wijnen was nog nooit zo eenvoudig. U bepaalt zelf de gewenste afleverdag.</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto3'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Toegang tot de mooiste wijnen uit Itali&euml;</span>Wilt u de wijnen eerst proeven? Toscanewijn.nl organiseert diverse proeverijen op verschillende locatie in het land, schrijf u hiervoor in.</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto4'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Toegang tot de mooiste wijnen uit Itali&euml;</span>Op zoek naar acties en voordeel? Iedere maand zijn er diverse wijnen in promotie en probeer ook eens de combi wijndoos!</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto5'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Toegang tot de mooiste wijnen uit Itali&euml;</span>Gaat u binnenkort naar de Toscane? Toscanewijn.nl biedt u leuke restauranttips voor een goed glas wijn en een heerlijke maaltijd.</div></div></div>");
    $('#home_Intro').append("<div id='home_Intro_Foto6'><div class='home_Intro_Tekst'><div><span class='home_Intro_Header'>Toegang tot de mooiste wijnen uit Itali&euml;</span>Weer even dat “Toscane gevoel” terug krijgen in Nederland? Hier vindt u de wijnen terug die u daar in Toscane heeft gedronken. Salute!</div></div></div>");

    $('#home_Intro').cycle({
        fx: 'fade',
        timeout: 6000,
        speed: 2000
    });



    //Bestelformulier submitten    
    $("#bestellenSubmit").click(function() {

        $("#formulier_Standaard").submit();

        //$("#formulier_Standaard").fadeOut("slow", function() {
        //    this.submit();
        //});

        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", "#5c9701")
                $('#' + 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, '.');



        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");

        }

    }


});
