function closeAllMenus() {
    $('.open').find('ul').slideUp('fast');
    $('.open').removeClass('open');
}

function getPageURL() {
    return $('body').data('href') != null ? $('body').data('href') : location.href;
}

function entityDecode(str) {
    var $ta = $('<textarea></textarea>');
    $ta.html(str);
    return $ta.text();
}

$(document).ready(function(){
    $('#in-sharer,#fb-sharer').show();
    $('.unli').hide();
    $('.menuactive.menuparent').addClass('open').find('ul').show();

    $('#in-sharer').click(function(){
        window.open('http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(getPageURL())+'&title='+encodeURIComponent(document.title)+'&source=Design-ate','sharer','toolbar=1,status=0,width=626,height=436');
        return false;
    });

    $('#fb-sharer').click(function(){
        window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(getPageURL())+'&t='+encodeURIComponent(document.title),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
    });

    $('#primary-nav a').click(function() {
        // don't bother with ajax from front page
        if ($('#page-container').hasClass('front-page')) {
            return true;
        }

        // handle top-level sections
        if ($(this).hasClass('depth-1')) {
            if ($(this).hasClass('menuparent')) {
                $li = $(this).closest('li');
                if (!$li.hasClass('menuactive')) {
                    closeAllMenus();
                }
                $li.find('li.depth-2').closest('ul').slideDown('fast').addClass('open');
                $li.addClass('open');
            }
            else {
                closeAllMenus();
                $(this).closest('li').addClass('open menuactive');
                $(this).addClass('menuactive');
            }
        }

        if ($(this).hasClass('depth-2') && !$(this).hasClass('menuactive')) {
            $('.depth-2 ul').slideUp().removeClass('open');

            $li = $(this).closest('li');
            $li.find('ul').slideDown('fast').addClass('open');
            $li.addClass('open');
        }
/*
        if ($(this).hasClass('depth-3')) {
            $li = $(this).closest('li.depth-2');
            $li.addClass('menuactive open');
            $li.find('a.depth-2').addClass('menuactive open');
        }
*/
        $('.menuactive').removeClass('menuactive');
        $(this).addClass('menuactive');
        $(this).parentsUntil('#primary-nav').addClass('menuactive open').show();
        $('.menuactive > a').addClass('menuactive open');

        // store the munged page's direct address for use with facebook/linkedin sharer functions
        $('body').data('href', $(this).attr('href'));

        $.post($(this).attr('href'), {}, function(data){
            var tmp = data.split(/<!-- \w+ content -->/);
            if (tmp.length == 3) {
                $('#content').replaceWith(tmp[1]);
            }

            var tmp = data.split(/<[\/]?title>/i);
            if (tmp.length == 3) {
                document.title = entityDecode(tmp[1]);
            }

            tmp = data.match(/<div id="page-container" class="([^"]+)" style="background-image: (url\(.*\));">/i);
            if (tmp.length == 3) {
                $('#page-container').removeClass().addClass(tmp[1]).css('backgroundImage', tmp[2]);
            }

        }, "html");
        return false;
    });
});


