/*
    Navigation Animations / JS Tweaks
    Steven Bower 2008
*/

$(function() {
    // Handle external links
    $('a[rel="external"]').click(function() {
        window.open($(this).attr('href'),'_new'+(Math.random()*11));
        return false;
    });

    var url = window.location.pathname;
    var page = url.substring(url.lastIndexOf('/') + 1);
    
    // Show sub navs on the appropriate pages
    $('#navigation li a[href=/'+page+']').each(function() {
        $(this).addClass('active').parents('ul').show(); // For sub nav
        $(this).siblings('ul').show(); // For parent nav
    });
    
    // Show subnav for why weeks?
    $('#whyweeks a[href=/'+page+']').each(function() {
        
        $('#whyweeks span').hide();
        
        $(this).addClass('active').parents('ul').show(); // For sub nav
        $(this).siblings('ul').show(); // For parent nav
    });
    
    // Arrows
    $('#navigation li').hover(function() {
            var curr = $(this);
            var offset = 0;
            
            // Prevent duplicate arrows from showing up if hover is called multiple times
            if (curr.children('span').is('.arrow')) return;
            
            curr.prepend('<span class="arrow">&gt;&gt;</span>');
            
            if ($.browser.msie || curr.children('ul').length < 1) offset = 8;
            else offset = 4;
            
            curr.children('a').css({marginLeft: offset+'px'});
        },
        function() {
            var curr = $(this);
            
            curr.find('.arrow').remove();
            curr.children('a').css({marginLeft: '25px'});
        }
    );
    
    // Arrows
    $('#whyweeksnav li').hover(
        function() {
            var curr = $(this);
            var offset = 3;
            
            curr.prepend('<span class="arrow">&gt;&gt;</span>');
            
            curr.children('a').css({marginLeft: offset+'px'});
        },
        function() {
            var curr = $(this);
            curr.find('span:first').remove();
            curr.children('a').css({marginLeft: '20px'});
        }
    );
});