/** 
 *  ================================================
 *    Initialisation on various jQuery plugins
 *    and some globally used jQuery functions
 *
 *    TABLE OF CONTENTS
 *    01 - Main slider on the frontpage
 *    02 - Main navigation
 *    03 - Sliding down login box
 *    04 - inFieldLabels plugin for form field lables
 *    05 - PrettyPhoto plugin
 *    06 - Tabbed widget in the sidebar
 *    07 - Contact forms
 *  ================================================
 */

 
/**
 *  ================================================
 *  01 - Initialise the main slider on the frontpage
 *  ================================================
 */
jQuery(window).load(function() {
    setTimeout(function(){
        $('#slider').nivoSlider({
          effect:'random',
          slices:12,
          animSpeed:500,
          pauseTime:5000,
          startSlide:0,
          directionNav:true,
          controlNav:true,
          keyboardNav:false,
          pauseOnHover:false
        });
    }, 300);
});


$(document).ready(function(){

    /**
     *  ================================================
     *  02 - Main navigation
     *  ================================================
     */
    $('#main-nav').pragmaNavigation();


    /**
     *  ================================================
     *  03 - Slide-down login box
     *  ================================================
     */
    var formHide;
    $('#login-box').hover(
        function(){
            clearTimeout(formHide);
            $(this)
                .stop().animate( {top: -15}, 300 )
                .find('#login-box-slide-btn').removeClass('down').addClass('up');
    },
        function(){
            formHide = setTimeout(function(){
            $('#login-box')
                .stop().animate( {top: -65}, 800 )
                .find('#login-box-slide-btn').removeClass('up').addClass('down');
            }, 300);
        }
    );
    $('#login-box-slide-btn').click(function(){
        $(this)
            .removeClass('up').addClass('down')
            .parent().stop().animate( {top: -65}, 300 );
    });


    /**
     *  ================================================
     *  04 - Initialise inFieldLabels plugin on 
     *       form field lables with class "infield"
     *  ================================================
     */
    $("label.infield").inFieldLabels();


    /**
     *  ================================================
     *  05 - Initialise prettyPhoto plugin
     *  ================================================
     */
    $("a.prettyPhoto").prettyPhotoHover().prettyPhoto({
        theme:'neutrino_light',
        opacity:0.60
    });


    /**
     *  ================================================
     *  06 - Tabbed widget in the sidebar
     *  ================================================
     */
    var tabContainers = $('div.tabs > div');
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show()

        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();


    /**
     *  ================================================
     *  07 - CONTACT FORMS
     *  ================================================
     */

    /**
     *    Main contact form on the contact page
     */

    /* Initial jQuery Validation */
    $("#contact-form").validate({
        rules: {
            name_field: { required: true },
            subject_field: { required: true },
            email_field: { required: true, email:true },
            message_field: { required: true }
        },
        messages: {
            name: "Bitte geben Sie Ihren Namen ein",
            subject: "Bitte füllen Sie den Betreff Ihrer Nachricht ein",
            message: "Bitte geben Sie hier Ihre Nachricht ein",
            email: {
                required: "Wir benötigen Ihre E-Mail Adresse um Sie zu kontakieren",
                email: "Ihre E-Mail Adresse muss folgendem Format entsprechen: name@domain.com"
            }
        }
    });

    /* Submit for server side validation and processing */
    $(function(){
        $("#contact-form").submit(function(){
            $.post("../contact.php", $("#contact-form").serialize(),
            function(data){
                if(data.emailsent == 'yes'){
                    $('#contact-form').fadeOut(1000, function(){$("#form-info").html("Ihre Nachricht wurde übermittelt.<br />Danke.").fadeIn();});
                } else {
                    $("#form-info").html("Sorry there was a problem with your email. Please try again.");
                }
            }, "json");
            return false;
        });
    });

    /**
     *    Site-wide contact form in the footer
     */

    /* Initial jQuery Validation */
    $("#footer-contact-form").validate({
        rules: {
            email: { required: true, email:true },
            message: { required: true }
        },
        messages: {
            message: "Bitte geben Sie hier Ihre Nachricht ein",
            email: {
                required: "Wir benötigen Ihre E-Mail Adresse um Sie zu kontakieren",
                email: "Ihre E-Mail Adresse muss folgendem Format entsprechen: name@domain.com"
            }
        }
    });

    /* Submit for server side validation and processing */
    $(function(){
        $("#footer-contact-form").submit(function(){
            $.post("../contact-footer.php", $("#footer-contact-form").serialize(),
            function(data){
                if(data.emailsent == 'yes'){
                    $('#footer-contact-form').fadeOut(1000, function(){$("#footer-form-info").html("Ihre Nachricht wurde übermittelt.<br />Danke.").fadeIn();});
                } else {
                    $("#footer-form-info").html("Sorry there was a problem with your email. Please try again.");
                }
            }, "json");
            return false;
        });
    });

});

