// mainMenu - set on the page itself.  determines which type of resizing to handle
// currentPage - set on the page. determines which page we're on.

var heightChanged; //resize handler
var introToPage; //menu close, open page
var returnHome; // menu return
var defaultClickHandlers; //sets the nav default click handler events
var homeClickHandlers; //sets the nav home click handlers
var switchPage; //normal page switcher function
var blogLoad; //load the blog url into the iframe

window.addEvent('domready', function() {

    heightChanged = function() {
        var windowSize = window.getSize();
        
        if(windowSize.y < 594)
            $('footer').setStyle('top', '450px').setStyle('bottom', 'auto');
        else
            $('footer').setStyle('bottom', '0px').setStyle('top', 'auto');
        
        if(mainMenu) {
            var headerHeight;
            var latestTop;
            var bodyHeight;

            if(windowSize.y < 594) {
                headerHeight = 450;
                latestTop = 175;
                bodyHeight = 11;
            }
            else {
                var aboveAndBelow = windowSize.y - 594;
                var space = Math.floor( aboveAndBelow / 2 );
                headerHeight = 450 + space;
                latestTop = headerHeight - 275;
                bodyHeight = space;
            }
            $('header_inner').setStyle('height', headerHeight+'px');
            $('latest_title').setStyle('top', latestTop+'px');
            $('body').setStyle('height', bodyHeight+'px');
        }
        else {
            var bodyHeight;
            var contentHeight;

            if(windowSize.y < 594) {
                bodyHeight = 271;
                contentHeight = 231;
            }
            else {
                bodyHeight = windowSize.y - 322;
                contentHeight = bodyHeight - 40;
            }
            $('body').setStyle('height', bodyHeight+'px');
            $('content').setStyle('height', contentHeight+'px');
            $('blog_content').setStyle('height', contentHeight+'px');
            $('projects_content').setStyle('height', contentHeight+'px');
            $('sketchbook_content').setStyle('height', contentHeight+'px');
            $('about_content').setStyle('height', contentHeight+'px');
        }
    }
    
    introToPage = function(pageName) {
        mainMenu = false;
        currentPage = pageName;
        //if(currentPage == 'blog') blogLoad();
        defaultClickHandlers();
        //fade out title
        $('latest_title').fade('out');
        //shrink header
        (function() {
            $('header_inner').tween('height', '189px');
        }).delay(500);
        
        (function() {
            heightChanged();
            $(currentPage+'_content').setStyle('display', 'block').set('opacity', 1);
            $('content').setStyle('display','block').set('opacity', 0).fade('in');
            $(currentPage).addClass('active');
        }).delay(1000);
    }

    returnHome = function() {
        mainMenu = true;
        homeClickHandlers();
        $(currentPage).removeClass('active');
        $('content').fade('out');
    
        (function() {
            $('content').setStyle('display','none');
            $(currentPage+'_content').setStyle('display','none');
    
            var windowSize = window.getSize();
    
            var headerHeight;
            var latestTop;
            var bodyHeight;
    
            if(windowSize.y < 594) {
                headerHeight = 450;
                latestTop = 175;
                bodyHeight = 11;
            }
            else {
                var aboveAndBelow = windowSize.y - 594;
                var space = Math.floor( aboveAndBelow / 2 );
                headerHeight = 450 + space;
                latestTop = headerHeight - 275;
                bodyHeight = space;
            }
            $('latest_title').setStyle('top', latestTop+'px');
            $('body').setStyle('height', bodyHeight+'px');
    
            $('header_inner').tween('height', headerHeight+'px');
        }).delay(500);
        
        (function() {
            $('latest_title').setStyle('display', 'block').fade('in');
        }).delay(1000);
        
    }


    defaultClickHandlers = function() {
        $('projects').removeEvents('click');
        $('sketchbook').removeEvents('click');
        $('blog').removeEvents('click');
        $('about').removeEvents('click');
        
        $('projects').addEvent('click', function(e) {
            e.stop(); switchPage('projects');
        });
        $('sketchbook').addEvent('click', function(e) {
            e.stop(); switchPage('sketchbook');
        });
        $('blog').addEvent('click', function(e) {
            e.stop(); switchPage('blog');
        });
        $('about').addEvent('click', function(e) {
            e.stop(); switchPage('about');
        });

        $('banner').addEvent('click', function(e) {
            e.stop(); returnHome();
        });
    }

    homeClickHandlers = function() {
        $('banner').removeEvents('click');
        $('projects').removeEvents('click');
        $('sketchbook').removeEvents('click');
        $('blog').removeEvents('click');
        $('about').removeEvents('click');

        $('projects').addEvent('click', function(e) {
            e.stop(); introToPage('projects');
        });
        $('sketchbook').addEvent('click', function(e) {
            e.stop(); introToPage('sketchbook');
        });
        $('blog').addEvent('click', function(e) {
            e.stop(); introToPage('blog');
        });
        $('about').addEvent('click', function(e) {
            e.stop(); introToPage('about');
        });
    }

    switchPage = function(newCurrentPage) {
        $(currentPage+'_content').fade('out');
        $(currentPage).removeClass('active');
        (function() {
            $(currentPage+'_content').setStyle('display','none');
            currentPage = newCurrentPage;
            //if(currentPage=='blog') blogLoad();
            $(currentPage+'_content').setStyle('display','block').set('opacity', 0).fade('in');
            $(currentPage).addClass('active');
        }).delay(500);
    }

    blogLoad = function() {
        //var url = 'http://naturalfuture.tumblr.com/?rand='+ (Math.random() * 1000);
        //var url = 'test.html'; //'http://naturalfuture.tumblr.com?rand='+ (Math.random() * 1000);
        //$('blog_content').set('src', url);
    }
    
    
    // startup actions
    if(mainMenu) {
        $('latest_title').setStyle('display','block');
        homeClickHandlers();
    }
    else {
        $('content').setStyle('display', 'block');
        $(currentPage+'_content').setStyle('display', 'block');
        defaultClickHandlers();
    }

    window.addEvent('resize', heightChanged);
    heightChanged();

}); // end of window domready
