
jQuery(function( $ ){

    //Add 'hidden' to a few elements to keep them from
    //Showing up before we can start jQuery
    $('#header, #content').addClass('hidden');	

	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	
	/**
	 * Restart the scroll position to ( 0, 0 ) (Firefox doesn't reset it)
	 * could use $(target).scrollTo( 0, {axis:'xy'));
	 * but this needs to be quick(synchronous), to reset before $.localScroll.hash() begins
	 */
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#content', //could be a selector or a jQuery object too.
		axis:'y',//the default is 'y'
		queue:true,
		duration:2000
	});
	
	var $last = $([]);//save the last link
	
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
     $.easing.easeInOutExpo = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
	$.localScroll({
		axis:'y', //the default is 'y'
		queue:true,
        duration:1500,
        offset:-35,
        easing:'easeInOutExpo'
    });
    
    function animateIn(domEl, callback){
        var t = $(domEl); 
        this.width = t.width();
        this.height = t.height();
        
        t.css({'margin-left':3, 'margin-top':3});
        t.animate({marginLeft:0, marginTop:0, opacity:0.33}, 100, 'linear', function(){

        t.parent().parent().parent().parent().dequeue(); 
       
        t.animate({opacity:1}, 200, 'linear');
        });
    };

    function checkTop(page, scrollT){
        if(Math.abs($(page).offset().top - scrollT) < 500){
            activatePage(page);
        }
    }

    var activePage = undefined;


    function shrinkBorder(domEl){
        $(domEl).animate({borderLeftWidth:0, marginLeft:"50px"}, 1000);
    }

    function activatePage(page){
        if(activePage == page)return;
        if(activePage)deactivatePage(activePage);
        activePage = page;
        $(page).find('img.header').fadeIn(1500);
    }
    function deactivatePage(page){
       if(activePage)activePage = undefined;
       $(page).find('img.header').hide();
    }

    var oldScrollT = 0;
    var scrollInterval;
    function checkStop(){
        var scrollT = $(this).scrollTop();
        if(scrollT == oldScrollT){
                $('.page').each(function(){
                    checkTop(this, scrollT);
                });
                clearInterval(scrollInterval);
            }
            oldScrollT = scrollT;
     }

    $(document).ready(function(){

        $(document).pngFix();
        $('.page img.header').hide();

        $('#header').removeClass('hidden').hide();
        $('#header').fadeIn(1000);
        $('#content').removeClass('hidden');	

        oldScrollT = $(window).scrollTop();
        
        $('#nav-holder li, #header h1').each(function(){
            $(this).bind('mouseover', function(){
                    $(this).stop(true, true).animate({opacity:0.7}, 200).animate({opacity:1}, 500);
                });
        });
        $(window).scroll(function(){
            clearInterval(scrollInterval);
            scrollInterval = window.setInterval(function(){checkStop();}, 250);
                 //Just check if the scrolling has stopped ...
       });

        $('#toc a').css('opacity', 0).each(function(){
            var that = this;
            $('#toc').queue(function(){
                animateIn(that);
                $(that).bind('mouseover', function(){
                    $(that).stop(true, true).animate({opacity:0.7}, 200).animate({opacity:1}, 500);
                });
            });
        });
    });
});
