$(document).ready(function(event) {
    showContactBox();
    contactMenu();
    formExtras();
    validateForm();
});

function showContactBox() {
    var contactBtn = $('div.contact-container .contact-button');
    var contactWrapper = $('div.contact-wrapper');

    contactWrapper.data('open', false);

    contactBtn.click(function() {
        if (!contactWrapper.data('open')) {
            contactWrapper.animate({
                left: 0
            }, 500)
            .data('open', true);
        } else {
            contactWrapper.animate({
                left: -375
            }, 500, 'easeInBack')
            .data('open', false);
        }
    });
}

function contactMenu() {
    $('ul.contact-menu li').each(function(i) {
        $(this).click(function() {
            $('ul.contact-menu li').removeClass("selected");
            $('ul.contact-menu li').children('div').remove();
            $(this).addClass("selected");
            $(this).append('<div class="arrow"></div>');

            $("div.contact-body .inner").hide();
            $("div.contact-body .inner").eq(i).fadeIn(400);
        });
    });
}

function formExtras() {
    $('input#reseDag').datepicker({
        dateFormat: 'dd/mm/yy',
        dayNames: ['Söndag', 'Måndag', 'Tisdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lördag'],
        dayNamesMin: ['Sö', 'Må', 'Ti', 'On', 'To', 'Fr', 'Lö'],
        dayNamesShort: ['Sön', 'Mån', 'Tis', 'Ons', 'Tor', 'Fre', 'Lör'],

        monthNames: ['Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'],
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'Maj', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']
    });

    var availableDestinations = ["äventyrsresor", "gourmetresor", "sportresor", "folklore", "rekreation"];
    $("input#resemal").autocomplete({
        source: availableDestinations
    });

}

function validateForm() {
    jQuery.validator.setDefaults({
        debug: false,
        submitHandler: function() {
            $("div.total-error").hide();
            submitForm();
        }
    });

    $("#aspnetForm").validate({
        //element: "input#kontaktperson, input#telefon, input#epost",
        ignore: ".ignore",        
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
					? 'Du missade 1 fält. Det har markerats nedanför'
					: 'Du missade ' + errors + ' fält.  Dom har markerats nedanför';
                $("div.total-error span").html(message);
                $("div.total-error").fadeIn(400);
            } else {
                $("div.total-error").hide();
                $("input#submitForm").addClass('valid');
            }
        },
        rules: {
            kontaktperson: {
                required: true,
                minlength: 2
            },
            telefon: {
                required: true,
                digits: true
            },
            epost: {
                required: true,
                email: true
            }
        },
        messages: {
            kontaktperson: "Fyll i en kontaktperson",
            telefon: {
                required: "Fyll i ditt telefonnummer",
                digits: "Fältet får endast innehålla siffror"
            },
            epost: {
                required: "Fyll i din epostadress",
                email: "Ogiltig epostadress"
            }
        }
    });   
    
}

function submitForm() {

    // Skicka formuläret
    // All göttig Ajax hamnar här!
    alert("Nu ska formuläret skickas!!"); 
        
}