﻿$(document).ready(function () {

    //select all the a tag with name equal to modal
    $('a[rel=modal]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#overlay').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect		
        //$('#overlay').fadeIn(500);
        $('#overlay').fadeTo("slow", 0.6);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).fadeIn(1000);

        $('body').toggleClass('hide-overflow');

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        e.preventDefault();
        $('#overlay').hide();
        $('.window').hide();
        $('body').toggleClass('hide-overflow');
    });

    $(document).keyup(function (e) {
        if (e.keyCode == 27) {
            e.preventDefault();
            $('#overlay').hide();
            $('.window').hide();
            $('body').toggleClass('hide-overflow');
        }
    });


    $('#listitem-email').hide();

    $('#radio-telefon').change(function (e) {
        $('#listitem-email').hide();
        $('#listitem-telefon').show();
    });

    $('#radio-email').change(function (e) {
        $('#listitem-email').show();
        $('#listitem-telefon').hide();
    });

    //if mask is clicked
    $('#overlay').click(function () {
        $(this).hide();
        $('.window').hide();
        $('body').toogleClass('hide-overflow');
    });

    $('#sendmail').click(function () {

        var firmanavn = $('#firmanavn').val();
        var kontaktperson = $('#kontaktperson').val();
        var emne = $('#emne').val();
        var email = $('#email').val();
        var telefon = $('#telefon').val();

        var datastr = 'firmanavn=' + firmanavn + '&emne=' + emne + '&kontaktperson=' + kontaktperson + '&email=' + email + '&telefon=' + telefon;

        send(datastr);

        function send(datastr) {
            $.ajax({
                type: 'POST',
                url: 'mailer.php',
                data: datastr,
                cache: false,
                success: success
            });
        }

        function success(html) {
            $('#input').hide();
            $('#output').html(html).show();
        }

    });

});
