/**
 * Contains the jquery calls to execute the navigation for the wrapper
 */
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

$(document).ready(function(){

    $("#nav li span.downArrow").hoverIntent(function() { //When trigger is clicked...
        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
    }, function() {});

    $("#nav li").hover(function() {
        $(this).find("span.downArrow").addClass("showDownArrow");
    },
    function() {
        $(this).find("ul.subnav").slideUp('slow');
        $(this).find("span.downArrow").removeClass("showDownArrow");
    }
    );

    $('#back2top').click(function() {
        $('html, body').animate({
            scrollTop:0
        }, 'slow');
        return false;
    });
	
    $('div#default-donationLink-href').each(function() {
        var val = $.trim($(this).text());
        if (val.indexOf('http') != -1 || val.indexOf("/") == 0) {
            $('a.donationLink').attr('href', val);
            if (($(this).hasClass('top-no-change'))) {
            //do nothing
            } else {
                $('a.top-donationLink').attr('href', $(this).text().trim());
            }
        }
    });
	
    $(".scroll").click(function(e){
        //prevent the default action for the click event

        //get the full url - like mysitecom/index.htm#home
        var full_url = this.href;

        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];

        //get the top offset of the target anchor
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;
        if(typeof adjustScrollHeight == 'function') {
            target_top = adjustScrollHeight(target_top);
        }

        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({
            scrollTop:target_top
        }, 'slow');
    });

    // sticky nav adapted from http://blog.hartleybrody.com/2011/05/creating-sticky-sidebar-widgets-that-scrolls-with-you/

    function isScrolledTo(elem) {
        var docViewTop = $(window).scrollTop(); //num of pixels hidden above current screen
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top; //num of pixels above the elem
        var elemBottom = elemTop + $(elem).height();

        return ((elemTop <= docViewTop));
    }

    var catcher = $('#page-top');
    var sticky = $('#page-nav');

    $(window).scroll(function() {
        if(isScrolledTo(sticky)) {
            sticky.css('position','fixed');
            sticky.css('top','0px');
        }
        var stopHeight = catcher.offset().top + catcher.height();
        if ( stopHeight > sticky.offset().top) {
            sticky.css('position','absolute');
            sticky.css('top',stopHeight);
        }
    });

});
