var menu = {
    oldHeight: 0,
    lyricsState: new Array(),
    lasthref: null,
    init: function() {

        $.address.change(menu.loadPage);

        $("ul.menu li").mouseover(function() {
            menu.changeBg('/' + $(this).find('a').attr('id').split('-')[1]);
        });

        $(".song_lirycs_toggle").live("click", function(){

            var trackId = $(this).attr('id').split('_')[1];
            if (typeof(menu.lyricsState[trackId]) == 'undefined' || !menu.lyricsState[trackId]) {
                $("#song_" + trackId).show('slow', function(){
                    $(".content_block").height($(".content_block").height() + $("#song_" + trackId).height());
                });

                menu.lyricsState[trackId] = true;
            } else {
                $("#song_" + trackId).hide('slow', function(){
                    $(".content_block").height($(".content_block").height() - $("#song_" + trackId).height());
                });

                menu.lyricsState[trackId] = false;
            }
            return false;
        });

        $(".drag_up").live('click', function(){
            menu.lasthref = $('div.drag_up').parent('a').attr('href');
            $('div.drag_up').parent('a').attr('href', '/#');

            $('div.content_block').animate({height: 0, "padding-bottom": 0}, function(){
                $('div.drag_up').removeClass('drag_up').addClass('drag_down');
                $('div.drag_down').parent('a').attr('href', menu.lasthref);
            });
        });
    },

    setActive: function(page) {
        $("ul.menu li").removeClass('active');
        $('#menu-' + page).parent().addClass('active');
    },

    pages: {
        '/main': {
            'top': 0,
            'bottom': 0,
            'height': 508,
            'scripts': function(){
                $("#intro").show();
            }
        },
        '/music': {
            'top': -223,
            'bottom': -510,
            'height': 490
        },
        '/events': {
            'top': -446,
            'bottom': -1002,
            'height': 685
        },
        '/video': {
            'top': -669,
            'bottom': -1693,
            'height': 772
        },
        '/photo': {
            'top': -892,
            'bottom': -2466,
            'height': 668,
            'scripts': function(){
                $('a[rel=lightbox]').lightBox();
            }
        },
        '/about': {
            'top': -1115,
            'bottom': -3135,
            'height': 682
        },
        '/contacts': {
            'top': -1338,
            'bottom': -3820,
            'height': 450
        }
    },
    pageLoaded: function(result) {
        $('div.drag_up').removeClass('drag_up').addClass('drag_down');

        $result = $(result);


        // Block split
        $('#container').replaceWith($result.filter('#container'));

        $('#bottom').replaceWith($result.filter('#bottom'));
        $('title').replaceWith($result.filter('title'));
       
        // Animate
        var $contentBlock = $('div.content_block');
        var currentPage = menu.lastEvent.path;
        
        // Tricky way to animate to height: auto        
        $contentBlock.height('auto');
        var newHeight = $('div.content_block').height();
        $('div.content_block').height(menu.oldHeight);

        $("div.content_block").animate({
            "height": newHeight,
            "padding-bottom": '50px'
        }, 1000,
            function() {
                if (!menu.pages[currentPage]) {
                    menu.pages[currentPage] = {};
                }
                menu.pages[currentPage].cache = result;
                $('div.drag_down').removeClass('drag_down').addClass('drag_up');
                menu.oldHeight = newHeight;
        });
        
        menu.changeBg('/' + currentPage.split('/')[1]);
        menu.setActive(currentPage.split('/')[1]);

        $("#intro").hide();

        if (typeof (menu.pages['/' + currentPage.split('/')[1]].scripts) != 'undefined') {
            menu.pages['/' + currentPage.split('/')[1]].scripts();
        }
    },

    loadPage: function(event) {
        menu.lastEvent = event;
        
        if (event.value != '/') {
            if (menu.pages[event.path] && menu.pages[event.path].cache) {

                var pageContent = menu.pages[event.path].cache;
                menu.pageLoaded(pageContent, "cached");

            } else {

                $.get(event.path, menu.pageLoaded, 'html');
            }

            $("ul.menu li").unbind('mouseover');
        }
    },

    changeBg: function(pageLink) {

        $("div.bottom").html($("#" + pageLink.replace('/', '')).html());
        
        var sizes = menu.pages[pageLink];
        var top = sizes.top;
        var bottom = sizes.bottom;
        var height = sizes.height;

        if (undefined !== top && NaN !== parseInt(top)) {
            $('#top').css({
                "backgroundPosition": 'center ' + top + 'px'
            });
        }

        if (undefined !== bottom && NaN !== parseInt(bottom)) {
            $('#bottom').css({
                "backgroundPosition": 'center ' + bottom + 'px'
            });
        }

        if (undefined !== height && NaN !== parseInt(height)) {
            $('#bottom').height(height);
        }

        if (pageLink == '/main') {
            $("#intro").show();
        } else {
            $("#intro").hide();
        }

        
    }
};

$(document).ready(function() {

    menu.init();

});

