(function($) {
    current_section = -1;
    function getPageContent (section) {
        if ( section != current_section ) {
            $.ajax({
                url: 'index.php',
                method: 'POST',
                datatype: 'JSON',
                data: {
                    section_id: section,
                },
                success: function ( data ) {
                    if ( data.status == 200 ) {
                        $('#page-title').html(data.pageTitle);
                        $('#user-content').html(data.html);
                        if ( section == 3 ) {
                            $('#content-contact-form').click(function () { getPageContent (5) });
                        }
                    }
                    else {
                        console.log("[" + data.status + "] " + data.errorCode + ": " + data.errorText);
                    }
                    current_section = section
                },
                error: function (jqXHR, exception) {
                    console.log(jqXHR.status + ": " +  jqXHR.responseText + " | - | " + exception);
                },
            });
        }
    }
    $(function() {
        $('#navbar-home').click(function () { getPageContent (1) } );
        $('#navbar-description').click(function () { getPageContent (2) });
        $('#navbar-work').click(function () { getPageContent (3) });
        $('#navbar-ideas').click(function () { getPageContent (4) });
        $('#navbar-contact-form').click(function () { getPageContent (5) });
        $('#navbar-contact-email').click(function () { window.location.href = "mailto:bryanpedini@bryanpedini.it"; });
        $('ul.nav li.dropdown').hover(function () {
            $(this).find('.dropdown-menu').stop(true, true).fadeIn(500);
        }, function () {
            $(this).find('.dropdown-menu').stop(true, true).fadeOut(500);
        });
        getPageContent(0);
    });
})(jQuery);