$(document).ready(function() {
    // Configure menu
    $('nav li:has(ul)').mouseenter(function() {
        var t = $(this), p = t.position();
        $('ul', t)
            .css('left', p.left)
            .css('top', p.top + t.innerHeight())
            .stop(true, true)
            .slideToggle();
    }).mouseleave(function() {$(this).find('ul').hide()});

    // Enable collapsing
    $('.collapsible .trigger').click(function()
    {
        $(this).parent().find('.collapser').slideToggle();
    })

    // Home/Services window.
    if (!$('html').hasClass('oldie')) {
        $('#window li a').mouseenter(function() {
            var t = $(this);

            var offset = 15, direction = '+';
            switch (t.attr('class')) {
                case 'pane-top-right':
                case 'pane-top-left':
                    direction = '-';
                    break;
                case 'pane-left':
                case 'pane-right':
                    offset = 0;
            }
            var top = direction + '=' + offset + 'px';

            offset = 15; direction = '+';

            switch (t.attr('class')) {
                case 'pane-top-left':
                case 'pane-bottom-left':
                case 'pane-left':
                    direction = '-';
            }
            var left = direction + '=' + offset + 'px';

            var options = {
                backgroundColor: '#1183c6',
                borderColor: '#2e5d78',
                top: top, left: left
            };

            $(this).animate(options, {
                duration: 250,
                complete: function() {
                    $(this).css('box-shadow', '0 2px 15px 0 #999');
                }
            });

            $(this).find('h3').animate({
                color: '#ffffff'
            }, 250);

        }).mouseleave(function() {
            var t = $(this);

            var offset = 15, direction = '-';
            switch (t.attr('class')) {
                case 'pane-top-right':
                case 'pane-top-left':
                    direction = '+';
                    break;
                case 'pane-left':
                case 'pane-right':
                    offset = 0;
            }
            var top = direction + '=' + offset + 'px';

            offset = 15; direction = '-';

            switch (t.attr('class')) {
                case 'pane-top-left':
                case 'pane-bottom-left':
                case 'pane-left':
                    direction = '+';
            }
            var left = direction + '=' + offset + 'px';

            var options = {
                backgroundColor: '#ffffff',
                borderColor: '#dddddd',
                top: top, left: left
            };

            $(this).animate(options, {
                duration: 250,
                complete: function() {
                    $(this).css('box-shadow', '');
                }
            });

            $(this).find('h3').animate({
                color: '#1183c6'
            }, 250);

        });
    }

    // Transition in?
    $('#loadingbar').hide();
    $('#admineditor, nav').fadeIn(250);
    $('#container').fadeIn(250);
    $('footer').fadeIn(250, function() {this.style.display = 'block';});
    $('body').delay(125).animate({
        opacity: 1.0
    }, {
       step: function(now, fx) {
           var posY = ((52 - 0) * fx.pos) + 0 + 'px';
           fx.elem.style.backgroundPosition = 'center '+posY;
       }
    });
})
