$(document).ready(function()
{
    var $blog_list = $('#blog_list');
    var $list_elements = $('#blog_list li');
    var $intro_wrapper = $('#intro, #nav');
    var $intro = $('#intro');
    var $mouse_helper = $('#mouse_helper');
    var $mouse_highlight = $('#mouse_helper span');
    var $load_wrapper = $('#load_wrapper');
    var $load_width = 0;

    var has_scrolled = false;

    var pixel_regex = /([0-9]+)/;
    var regex_margin = pixel_regex.exec($blog_list.css('margin-top'));
    if(regex_margin)
        var margin_start = regex_margin[0];

    var regex_line = pixel_regex.exec($blog_list.children('li').css('height'));
    if(regex_line)
        var line_height = regex_line[0];

    var visible_elements = 10;

    var i = 0;
    $blog_list.css({ "marginLeft": "-500px", "opacity": 0});
    $('body').css('overflow-y', 'scroll');
    $('body.home #introheader a').attr('href', '#');
    $mouse_helper.hide();

    scrollto_list(0, false);


    $(window).bind('hashchange', function () {
        var hash = window.location.hash || '#blog';

        if(hash == "#about" || hash == "#blog")
        {
            if($load_wrapper.hasClass('content_active'))
            {
                $load_wrapper.animate({ 'margin-left': '-500px', 'opacity': 0 }, 200, null, function(){
                    $load_wrapper.html("").removeClass('content_active');
                });
            }
        }

        if(hash == "#about")
        {
            $intro.removeClass('inactive');
            $intro_wrapper.animate({ 'margin-left': '129px' }, 200);
            $blog_list.animate({ 'margin-left': '-500px', 'opacity': 0 }, 200).addClass('inactive');

            $('#intro a').show();
            $('#intro label').hide();

            return false;
        }
        if(hash == "#blog")
        {
            $intro.addClass('inactive');
            $intro_wrapper.animate({ 'margin-left': '570px' }, 200);
            $blog_list.animate({ 'margin-left': '0', 'opacity': 1 }, 200).removeClass('inactive');
            if( ! $mouse_helper.hasClass('has_scrolled'))
            {
                $mouse_helper.show("drop", { direction: "up" }, 200, function(){
                    $mouse_highlight.effect("pulsate", { times:3 }, 800);
                });
            }
            $('#intro a').hide();
            $('#intro label').show();

            return false;
        }
        var hash_match;
        if(hash_match = hash.match(/^\#blog\/([\w-]+)$/))
        {
            $load_wrapper.css({ "marginLeft": "-500px", "opacity": 0});
            $load_wrapper.load("/" + hash_match[1] + " #content", function(){
                $load_wrapper.addClass('content_active').animate({ 'margin-left': '0', 'opacity': 1 }, 200);

                $load_width = $('#content').hasClass('wide') ? 700 : 500;

                $('#post_title').wrapInner('<a href="/' + hash_match[1] + '" />');

                $intro.addClass('inactive');
                $intro_wrapper.animate({ 'margin-left': $load_width + 570 + 'px' }, 200);
                $blog_list.animate({ 'margin-left': $load_width + 20 + 'px', 'opacity': 1 }, 200);
                $('#loading_wrapper').hide();
                hljs.initHighlighting();
            });

            $('#loading_wrapper').show();
            $('#loading').css('width', '0px').animate({ 'width': '100px' }, 200);
            
            $('#intro a').hide();
            $('#intro label').show();
        }
    }).trigger("hashchange");

    $intro.click(function()
    {
        if($intro.hasClass('inactive'))
        {
            if($load_wrapper.hasClass('content_active'))
                window.location.hash = "#blog";
            else
                window.location.hash = "#about";
        }
    });

    $list_elements.each(function()
    {
        $(this).data('position', i);
        i++;

        $('a', this).attr('href', "#blog/" + $('a', this).attr('id').replace('post-', ''));
    });

    $list_elements.click(function()
    {
        if( ! $(this).hasClass('active'))
        {
            scrollto_list($(this).data('position'), true);
            return false;
        }
    });

    $blog_list.live('mouseover', function(){
        if( ! has_scrolled)
            $(this).addClass('scroll_hint');
    });

    $blog_list.live('mouseleave', function(){
        $('body').css('overflow-y', 'scroll');
    });

    $blog_list.mousewheel(function(event, delta)
    {
        $('body').css('overflow-y', 'hidden');
        $blog_list.removeClass('scroll_hint');
        has_scrolled = true;

        var count = $('#blog_list li.active').data('position');
        var scroll_to = count - delta;
        if(scroll_to < $list_elements.length && scroll_to > -1)
        {
            scrollto_list(scroll_to, false);
        }

        $('body').css('overflow-y', 'scroll');
    });
    
    function scrollto_list(position, effect)
    {
        var list_element = $list_elements.eq(position);
        if( ! list_element.hasClass('active'))
        {
            $list_elements.addClass('hidden')
                .removeClass('active');
            effect ? list_element.children('.excerpt').fadeIn('200') : list_element.children('.excerpt').show();
            list_element.addClass('active')
                .removeClass('hidden');

            var i = 0;
            $list_elements.each(function()
            {
                $(this).css('opacity', (1 - (Math.abs(i - position) / (visible_elements - 1))));
                i++;
            });

            var count;
            count = list_element.data('position');
            $blog_list.css('margin-top', (-margin_start - (count * line_height)) + 'px');
        }
        return false;
    }

    var $work_li = $('#work li img');
    $('#work li div').addClass('info').hide();

    $work_li.click(function(){
        $(this).siblings('div').toggle(100);
    });
});

