var contPages = new Array();
contPages[0] = 'dane-adresowe';
contPages[1] = 'formularz-kontaktowy';
contPages[2] = 'mapa-dojazdu';

$(document).ready(

    function() {
        $('#contact-menu').children('.position').each(
            //
            function(i) {
                $(this).click (
                    function () {
                        //$(this).blur ();
                        $('#contact-menu').children('.position').each(
                            function(i) {
                                $(this).removeClass('select');
                                $(this).children('a').blur();
                            }
                        )

                        $('#contact-pages').children('div').each( function(i) { $(this).hide(); } )
                        $('#contact-load').show();

                        hash   = $(this).children('a').attr('href');
                        tmpId  = $(this).attr('id');
                        tmpId  = tmpId.split('-');
                        thisId = tmpId[1];
                        location.hash = hash;
                        hash = hash.replace("#", "");

                        if (contPages[thisId]!= null) {
                            $('#subpage-' + thisId + '').addClass('select');
                            $('#contact-load').hide();
                            if (thisId == 2) { 
                                tmp = $('#contact-' + thisId + '').html();
                                $('#contact-' + thisId + '').html(tmp);
                            }
                            $('#contact-' + thisId + '').show();
                            return false;
                        } else {
                            $('#subpage-0').addClass('select');
                            $('#contact-load').hide();
                            $('#contact-0').show();
                            return false;
                        }

                        return false;
                    }
                )
            }
        )

        $('#contact').submit (function() {

            var validForm = function () {
                msg  = '';
                send = true;
                if ($('#name').val().trim() == '') {
                    msg  += 'Uzupełnij pole imię!<br />';
                    send = false;
                }
                if ($('#surname').val().trim() == ''  ) {
                    msg  += 'Uzupełnij pole nazwisko!<br />';
                    send = false;
                }
                if ($('#email').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1 ) {
                    msg  += 'Wprowadź poprawny adres e-mail!<br />';
                    send = false;
                }
                if ($('#mobile').val().trim() == ''  ) {
                    msg  += 'Uzupełnij pole telefon!<br />';
                    send = false;
                }
                if ($('#mesage').val().trim() == ''  ) {
                    msg  += 'Wprowadź treść wiadomości!<br />';
                    send = false;
                }
                if ($('#confirm:checked').val() == null ) {
                    msg  += 'Musisz zaakceptować warunki!';
                    send = false;
                }
                if (!send) {
                    $('#message').html(msg);
                    $('#message').show();
                }
                return send;
            }

            var startSending = function () {
                send = validForm();
                if (!send) {
                    return false;
                } else {
                    $('#contact-pages').children('div').each( function(i) { $(this).hide(); } )
                    $('#contact-load').show();
                    return true;
                }
            }
            var backToDataPage = function () {
                $('#contact-pages').children('div').each( function(i) { $(this).hide(); } )
                $('#contact-menu').children('.position').each( function(i) { $(this).removeClass('select'); } )
                $('#contact-load').show();
                location.hash = '#' + contPages[0] + '';
                $('#subpage-0').addClass('select');
                $('#contact-load').hide();
                $('#contact-0').show();
            }

            var callBackFunc = function(data, textStatus) {
                if (textStatus == 'success') {
                    if (data.search('error') != -1) {
                        $('#contact-load').hide();
                        $('#contact-notsend').show();
                        window.setTimeout(backToDataPage, 5000);
                    } else {
                        $('#contact-load').hide();
                        $('#contact-send').show();
                        window.setTimeout(backToDataPage, 5000);
                    }
                } else {
                    $('#contact-load').hide();
                    $('#contact-notsend').show();
                    window.setTimeout(backToDataPage, 5000);
                }
            }

            var submitOptions = {
                beforeSubmit:  startSending,  // pre-submit callback
                success:       callBackFunc,  // post-submit callback
                clearForm: true               // clear all form fields after successful submit
            };

            $(this).ajaxSubmit(submitOptions);
            return false;
        });

/*
        $('#contact').submit (
            function () {
                msg  = '';
                send = true;

                if ($('#name').val() == '') {
                    msg  += 'Uzupełnij pole imię!<br />';
                    send = false;
                }

                if ($('#surname').val() == ''  ) {
                    msg  += 'Uzupełnij pole nazwisko!<br />';
                    send = false;
                }

                if ($('#email').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1 ) {
                    msg  += 'Wprowadź poprawny adres e-mail!<br />';
                    send = false;
                }

                if ($('#mobile').val() == ''  ) {
                    msg  += 'Uzupełnij pole telefon!<br />';
                    send = false;
                }
                if ($('#mesage').val() == ''  ) {
                    msg  += 'Wprowadź treść wiadomości!';
                    send = false;
                }

                if ($('#confirm:checked').val() == null ) {
                    msg  += 'Musisz zaakceptować warunki!';
                    send = false;
                }
                if (!send) {
                    $('#message').html(msg);
                    $('#message').show();
                } else {
                    $('#contact-pages').children('div').each( function(i) { $(this).hide(); } )
                    $('#contact-load').show();

                    var postData  = $("#contact").serialize();
                    var postURL   = $("#contact").attr('action');

                    var backToDataPage = function () {
                        $('#contact-pages').children('div').each( function(i) { $(this).hide(); } )
                        $('#contact-menu').children('.position').each( function(i) { $(this).removeClass('select'); } )
                        $('#contact-load').show();
                        location.hash = '#' + contPages[0] + '';
                        $('#subpage-0').addClass('select');
                        $('#contact-load').hide();
                        $('#contact-0').show();
                    }

                    var callBackFunc = function(data, textStatus) {
                        if (textStatus == 'success') {
                            $('#contact-load').hide();
                            $('#contact-send').show();
                            $("#contact").reset();
                            window.setTimeout(backToDataPage, 5000);
                        } else {
                            $('#contact-load').hide();
                            $('#contact-notsend').show();
                            window.setTimeout(backToDataPage, 5000);
                        }
                    }

                    $.post(postURL, postData, callBackFunc);
                    //
                }
                return false;
            }
        )

        /* * /
        $('#contact-load').hide();
        $('#contact-2').show();
        $('#contact-2').hide();
        $('#contact-load').show();
        /* */

        if  (location.hash != '') {
            hash   = location.hash;
            thisId = 0;
            $('#contact-menu').children('.position').each(
                function(i) {
                    mHash   = $(this).children('a').attr('href');
                    mTmpId  = $(this).attr('id');
                    mTmpId  = mTmpId.split('-');
                    mThisId = mTmpId[1];
                    if (mHash == hash) { thisId = mThisId; }
                }
            )
            hash = hash.replace("#", "");

            if (hash == contPages[thisId]) {
                $('#subpage-' + thisId + '').addClass('select');
                $('#contact-load').hide();
                $('#contact-' + thisId + '').show();
            } else {
                urlToLoad = location.href;
                urlToLoad = urlToLoad.replace(".html", "");
                urlToLoad = urlToLoad.replace("#", "/");

            }
        } else {
            //location.hash = '#' + contPages[0] + '';
            $('#subpage-0').addClass('select');
            $('#contact-load').hide();
            $('#contact-0').show();
        }
        /* */
    }





);


function send_mail () {

}



