/**
 * Navigation javascript
 */
(function($){

    /**
     * Logo anchor
     */
    var logo = $('a.logo');

    /**
     * Navigation div
     */
    var navigation = $('div#navigation');

    // guard: check for navigation element
    if ( navigation.length == 0 )
    {
        return false;
    }

    /**
     * Navigation offset
     */
    var navigationOffset = navigation.offset();

    /**
     * Calculate the height of the main navigation
     */
    var navigation_height = navigation.outerHeight() + navigationOffset.top;

    /**
     * The viewport height
     */
    var viewport_height = false;

    /**
     * Guard for scrolled state
     */
    var scrolled = false;

    // initial update call
    updateNavigation();

    /**
     * Update the navigation
     */
    function updateNavigation() {

        // first get the viewport height
        viewport_height = $(window).height();

        // get the document height        
        document_height = $(document).height();

        // get the scroll height
        scroll_height = $(window).scrollTop() ;

        // check whether the navigation should be scrollable...
        // The first condition checks if the navigation is too large to be fixed positioned
        if (viewport_height < navigation_height) {

//            scrolled = false;

            // set css
//            navigation.css('position', 'absolute');
            navigation.css({'top':'auto','bottom': 1});

//            if (scroll_height > navigation_height - viewport_height) {
//
//                scrolled = true;
//                navigation.css('left', $('#center').offset().left+'px').prependTo(document.body);
//                navigation.stop().animate({                
//                    'top':(scroll_height+viewport_height-navigation.height())+'px'
//                }, {
//                    duration: 100,
//                    easing: 'linear'
//                });
//            }
        }

        // otherwise set it fixed
        else {

//            scrolled = false;
//            $('#center').prepend(navigation.css('left', 'auto'));

            // set css
            navigation.css({'top': 101,'bottom':'auto'});
        }
    }

    /**
     * Add viewport change handler
     */
    $(window).resize(function() {

        // update the navigation
        updateNavigation();
    });

    /**
     * Add scroll handler
     */
    $(window).scroll(function() {

//        // get the scroll height
//        scroll_height = $(window).scrollTop() ;
//
//        if (scroll_height < navigation_height - viewport_height) {
//            navigation.stop().animate({
//                'top': navigationOffset.top
//            }, 250);
//        }

        // update the navigation
        updateNavigation();
    });
})(jQuery);
