// JavaScript Document

Array.prototype.in_array = function(needle) {
    for(var i=0; i < this.length; i++) if(this[ i] === needle) return true;
    return false;
}

// initialise the jQuery requests
$(document).ready( function()
{

    var active_button = $('.button').first();
    active_button.addClass('active');
    var dashboard = $('#dashboard');
    var text_element = $("#text");

    function setNavigationDone()
    {
        var c = document.cookie;
        var values = c.split('; ');
        var values_length = values.length;
        var new_values = {};
        for (var i = 0; i < values_length; i++)
        {
            var splitted = values[i].split('=');
            if (splitted.length && splitted.length === 2)
            {
                new_values[splitted[0]] = splitted[1];
            }
        }
        // Motiv
        if (new_values.bg && new_values.bg !== '')
        {
            $('#nav_motiv').addClass('done');
        }
        else
        {
            $('#nav_motiv').removeClass('done');
        }
        // Fenster
        if (    (new_values.style && new_values.style!== '')
            || (new_values.muntin && new_values.muntin !== '')
            || (new_values.winWidth && new_values.winWidth !== '')
            || (new_values.winHeight && new_values.winHeight !== '')
            || (new_values.connector && new_values.connector !== '')
        )
        {
            $('#nav_fenster').addClass('done');
        }
        else
        {
            $('#nav_fenster').removeClass('done');
        }
        // Farbe
        if (   new_values.winColor && new_values.winColor !== ''
            && new_values.winColorName && new_values.winColorName !== ''
        )
        {
            $('#nav_farbe').addClass('done');
        }
        else
        {
            $('#nav_farbe').removeClass('done');
        }
        // Extras
        if (    (new_values.sh && new_values.sh !== '')
            || (new_values.insekten && new_values.insekten !== '')
            || (new_values.ws && new_values.ws !== '')
            || (new_values.luft && new_values.luft !== '')
            || (new_values.glas && new_values.glas !== '')
            || (new_values.schatten && new_values.schatten !== '')
        )
        {
            $('#nav_extras').addClass('done');
        }
        else
        {
            $('#nav_extras').removeClass('done');
        }
    }

    function setWindow()
    {
        // first set the cookies
        var expire = 60*60*24*7;
        var cookieString;
        var windowVars;
        document.cookie = "secondWin=" + variables_config.secondWin + "; max-age=" + expire;
        document.cookie = "bg=" + variables_config.background + "; max-age=" + expire;
        document.cookie = "style=" + variables_config.style + "; max-age=" + expire;
        document.cookie = "muntin=" + variables_config.muntin + "; max-age=" + expire;
        document.cookie = "w=" + variables_config.winWidth + "; max-age=" + expire;
        document.cookie = "h=" + variables_config.winHeight + "; max-age=" + expire;
        document.cookie = "winColor=" + variables_config.winColor + "; max-age=" + expire;
        document.cookie = "winColorName=" + variables_config.winColorName + "; max-age=" + expire;
        document.cookie = "connector=" + variables_config.connector + "; max-age=" + expire;
        document.cookie = "sh=" + variables_config.shutters + "; max-age=" + expire;
        document.cookie = "ws=" + variables_config.frame + "; max-age=" + expire;
        document.cookie = "luft=" + variables_config.luft + "; max-age=" + expire;
        document.cookie = "glas=" + variables_config.glas + "; max-age=" + expire;
        document.cookie = "schatten=" + variables_config.schatten + "; max-age=" + expire;
        document.cookie = "insekten=" + variables_config.insekten + "; max-age=" + expire;
        document.cookie = "einbruchschutz=" + variables_config.einbruchschutz + "; max-age=" + expire;
        document.cookie = "profil_geometrie=" + variables_config.profil_geometrie + "; max-age=" + expire;
        windowVars = "style=" + variables_config.style;
        windowVars += "&muntin=" + variables_config.muntin;
        windowVars += "&w=" + variables_config.winWidth;
        windowVars += "&h=" + variables_config.winHeight;
        windowVars += "&color=" + variables_config.winColor;
        windowVars += "&colorName=" + variables_config.winColorName;
        windowVars += "&connector=" + variables_config.connector;
        windowVars += "&sh=" + variables_config.shutters;
        windowVars += "&ws=" + variables_config.frame;
        windowVars += "&luft=" + variables_config.luft;
        windowVars += "&glas=" + variables_config.glas;
        windowVars += "&insekten=" + variables_config.insekten;
        windowVars += "&schatten=" + variables_config.schatten;
        windowVars += "&einbruchschutz=" + variables_config.einbruchschutz;
        windowVars += "&profil_geometrie=" + variables_config.profil_geometrie;
        text_element.load("scripts/textmaker.php?" + windowVars);
        // return "media/window.php?"+windowVars;
        $(".window img").attr("src", "media/window.php?" + windowVars);
        //setNavigationDone();
    }

    function bootDrawers()
    {
        $("#dashboard > *:visible .drawer:not(:first)").hide();
        $("#dashboard > *:visible .drawer:first").show();
        $("#dashboard > *:visible .tab:not(:first)").removeClass("active");
        $("#dashboard > *:visible .tab:first").addClass("active");
        setWindow();
    }

    /*** OPEN THE DASHBOARD ***/
    $("#dashboard > *:not(:first)").hide();
    // slide open the first dash
    $('.button').click( function(event)
    {
        event.preventDefault();
        if ($(this).hasClass('active'))
        {
            return;
        }
        active_button.removeClass('active');
        active_button = $(this);
        active_button.addClass('active');
        dashboard.animate({
            'left': -300
        },
        {
            'duration': 400,
            'queue': false,
            'complete': function()
            {
                var chosenDash = active_button.attr('value');
                $("#dash" + chosenDash).show().siblings().hide();
                bootDrawers();
                dashboard.animate({
                    'left': 0
                },
                {
                    'duration': 600,
                    'queue': false
                });
            }
        });
    });

    /*** TOGGLE THE DRAWERS ***/
    // hide all drawers after the first drawer
    bootDrawers();
    // toggle the drawer shown
    $(".tab").click( function(event)
    {
        event.preventDefault();
        $(this).next(".drawer").slideDown(500).siblings(".drawer:visible").slideUp(500)
        $(this).addClass("active");
        $(this).siblings(".tab").removeClass("active");
    });
    // set the value according to what was clicked

    // 1: Background switcher
    $("#dash1 .swatch a").click( function(event)
    {
        event.preventDefault();
        var clickValue =  $(this).attr("id");
        $(".background img").attr("src", "hintergrunde/" + clickValue);
        //variables.find('#background').text(clickValue);
        variables_config.background = clickValue;
    });
    // 1b: Toggle one or two windows
    $("#dash1 .controls a").click( function(event)
    {
        event.preventDefault();
        var toggleValue = $(this).attr('id').split('_')[2];

        if (toggleValue == '1')
        {
            $(this).text("Zwei Fenster zeigen");
            $(this).attr('id', 'show_window_0');
            $('#window_2').fadeOut();
            variables_config.secondWin = 0;
        }
        else
        {
            $(this).text("Nur ein Fenster zeigen");
            $(this).attr('id', 'show_window_1');
            $('#window_2').fadeIn();
            variables_config.secondWin = 1;
        }
        setWindow();
    });
    // 2a: Window shape switcher
    $("#dash2 #windowStyle a").click( function(event)
    {
        event.preventDefault();
        variables_config.style = $(this).attr("data-id");
        setWindow();
    });
    // 2b: Window muntin switcher
    $("#dash2 #muntinStyle a").click( function(event)
    {
        event.preventDefault();
        variables_config.muntin = $(this).attr("data-id");
        setWindow();
    });
    // 2c: Change connector type
    $("#dash2 #connector a").click( function(event)
    {
        event.preventDefault();
        variables_config.connector = $(this).attr('data-id');
        setWindow();
    });
    // 2d: Change window size
    $("#dash2 button").click( function(event)
    {
        event.preventDefault();
        var newWidth = $("form #winWidth").val();
        var newHeight = $("form #winHeight").val();
        variables_config.winWidth = newWidth;
        variables_config.winHeight = newHeight;
        setWindow();
    });

    $("#dash2 #profil_geometrie a").click( function(event)
    {
        event.preventDefault();
        variables_config.profil_geometrie = $(this).attr('data-id');
        setWindow();
    });

    // 3: Colour switcher
    $("#dash3 .swatch a").click( function(event)
    {
        event.preventDefault();
        variables_config.winColor = $(this).attr('class');
        variables_config.winColorName = $(this).attr('id');
        setWindow();
    });
    // 4a: Change shutters
    $("#dash4 #shutters a").click( function(event)
    {
        event.preventDefault();
        variables_config.shutters = $(this).attr('data-id');
        setWindow();
    });
    // 4a: Change shutters
    $("#dash4 #glas a").click( function(event)
    {
        event.preventDefault();
        variables_config.glas = $(this).attr('data-id');
        setWindow();
    });
    // 4a: Change shutters
    $("#dash4 #insekten a").click( function(event)
    {
        event.preventDefault();
        variables_config.insekten = $(this).attr('data-id');
        setWindow();
    });
    // 4a: Change shutters
    $("#dash4 #luft a").click( function(event)
    {
        event.preventDefault();
        variables_config.luft = $(this).attr('data-id');
        setWindow();
    });


    /*
     * show / hide sub_aufsatz options
     */
    if(['aufsatz', 'aufsatz_mit_pt', 'aufsatz_ohne_pt'].in_array(variables_config.schatten))
    {
        $('#sub_aufsatz').css({ display: 'block' });
    }
    else
    {
        $('#sub_aufsatz').hide();
    }

    /*
     * show / hide shutter options
     *
    if(['aufsatz', 'vorbau', 'aufsatz_mit_pt', 'aufsatz_ohne_pt'].in_array(variables_config.schatten))
    {
        $('#shutters').hide();
    }
    else
    {
        $('#shutters').show();
    }
     */


    // 4a: Change shutters
    $("#dash4 #schatten a").click( function(event)
    {
        event.preventDefault();
        variables_config.schatten = $(this).attr('data-id');



        if(['aufsatz', 'aufsatz_mit_pt', 'aufsatz_ohne_pt'].in_array(variables_config.schatten))
        {
            $('#sub_aufsatz').css({ display: 'block' });
        }
        else
        {
            $('#sub_aufsatz').hide();
        }

        /*
        // show/hide schutters options
        if(['aufsatz', 'vorbau', 'aufsatz_mit_pt', 'aufsatz_ohne_pt'].in_array(variables_config.schatten))
        {
            $('#shutters').hide();
        }
        else
        {
            $('#shutters').show();
        }
        */

        setWindow();
    });
    // 4b: Change frame width
    $("#dash4 #frames a").click( function(event)
    {
        event.preventDefault();
        variables_config.frame = $(this).attr('data-id');
        setWindow();
    });
    $("#dash4 #einbruchschutz a").click( function(event)
    {
        event.preventDefault();
        variables_config.einbruchschutz = $(this).attr('data-id');
        setWindow();
    });


    /*** OUTPUT BUTTONS ***/
    // Print button
    $("#print_button").click( function(event)
    {
        event.preventDefault();
        window.open('print.php', 'print', 'resizable=yes, scrollbars=yes, status=no, toolbar=no, menubar=yes, width=600, height=400');
    });
    /*** OUTPUT BUTTONS ***/
    // Print button
    $("#mail_button").click( function(event)
    {
        event.preventDefault();
        var user_email = window.prompt('Bitte geben Sie Ihre E-Mail-Adresse ein:');
        $.ajax({
            'url': 'mail.php',
            'type': 'POST',
            'dataType': 'json',
            'cache': false,
            'data': {
                'email': user_email
            },
            'success': function(result)
            {
                if (result && result.status && result.status == true)
                {
                    alert('Ihre Konfiguration wurde erfolgreich an ' + user_email + ' verschickt.');
                }
                else if (result && result.message)
                {
                    alert(result.message);
                }
            }
        });
    });
});
