/**
 * Scroll
 */
(function($) {
    // remove default links
    $('ul.nav-sub a').each(function(){

        // get the href of the links
        var href = $(this).attr('href');

        // split the href into parts
        var href_parts = href.split('#');

        // set the href to the last part of the href array
        $(this).attr('href', '#' + href_parts[1].replace('scroll-', ''));

        // store the parent href into a new attribute for use on detail pages
        $(this).attr('parent_href', href_parts[0] + '#' + href_parts[1]);
    });


    // set an event on all the sub navigation anchors
    $('ul.nav-sub a, #navigation .submit-button a').click(function(event) {
        // prevent default operation

        // get the href attribute
        var href = $(this).attr('href');

        // get the id of the element the href should point to
        var section = ($('#scroll-' + href.replace('#', '')));

        // if the id is not found, replace the href with the parent href and go to it.
        if(section.length < 1) {
            // replace the href with the parent_href
            $(this).attr('href', $(this).attr('parent_href'));
            return;
        }
        else {
            // prevent the link from executing and do jquery scroll stuff
            event.preventDefault();
        }

        // check vor jQuery object
        if ($(this).length == 0) {
            return;
        }

        // stop all the current animations
        $('html').stop();

        // set the top offset
        var top_offset = -50;

        // the first attribute should scroll all the way to the top
        if ('#' + $(".section-container:first").attr('id').replace('scroll-', '') == $(this).attr('href')) {
          top_offset = -200;
        }

        // scroll to
        $.scrollTo('#scroll-' + $(this).attr('href').replace('#', ''), 1000, {offset: {top: top_offset, left: 0}});

        // set selected sub menu item
        $('ul.nav-sub li').removeClass('sel');
        $(this).closest('li').addClass('sel');

    });

     // add scroll event handler
    $(window).scroll(function() {
        if (!$('html').is(':animated')) updateSelectedSection();
    });

    // update the selected states
    function updateSelectedSection() {
        // variables
        var current_top = $(window).scrollTop();
        var closest_offset = 0;
        var closest_element = false;

        // loop all the content sections
        $('.section-container').each(function() {

            // calculate the offsets
            var offset_top = $(this).position().top - current_top;

            // guard: check if the offsets are in range
            if (offset_top > 300) {
                return;
            }

            // check offset
            if (!closest_element || offset_top < closest_offset) {
                closest_element = $(this);
                closest_offset =  Math.abs(offset_top);
            }
        });

        // guard: check for closest element
        if (!closest_element || closest_element.attr('id') == undefined) {
            return;
        }

        // set selected
        $('ul.nav-sub li').removeClass('sel');
        $('a[href="#'+ closest_element.attr('id').replace('scroll-', '') +'"]').closest('li').addClass('sel');
    }

    // make #title scroll to #scroll-title
    $('a[href=\'' + location.hash +'\']').trigger('click');

    // update selected states
    updateSelectedSection();


})(jQuery);
