(function($){
    $(".carousel").carouFredSel({
        items:{
            visible:1
        },
        scroll:{
            fx:"crossfade",
            easing:"easeInQuad",
            duration: 400
        },
        auto: 8000
    });

    $('.colorbox').colorbox();
    $('.colorbox.gmap_zg').colorbox({
        inline: true,
        href:   "#gmap_zg"
    });
    $('.colorbox.gmap_os').colorbox({
        inline: true,
        href:   "#gmap_os"
    });

    // FAQ toggler
    $('#faq .faq_item').each(function() {
        $(this).find('p').addClass('visuallyhidden');
    });

    $('#faq .faq_item h2').click(function(e) {
        e.preventDefault();
        $(this).siblings('p').toggleClass('visuallyhidden');
    });

    // Main nav
    $("ul.subnav").parent().append("<span></span>");

    $("nav ul li a")
        .click(function(e) {
            var subnav = $(this).parent().find('ul.subnav');
            if (subnav.length > 0) {
                // Don't click on menu item if there are subnavs
                e.preventDefault();
            }

            // Close all subnavs
            $('nav ul.subnav').hide();

            subnav
                // Open the one we need
                .slideDown('fast').show()
                .hover(
                    function() {},
                    function() {
                        $(this).slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
                    }
                );
        })
        .hover(
            function() {

                var subnav  = $(this).parent().find('ul.subnav');
                if (subnav.length > 0) {
                    $(this).click();
                }

            },
            function() {}
        );

    if ($('.form').length > 0) {
        if ($('.error').length > 0) {
            $('.error:eq(0)').siblings(':input').focus();
        } else {
            $(':input:eq(0)').focus();
        }
    }

})(jQuery);
