var $j = jQuery.noConflict();

/* Navigation */

function Tabs(parent, options)
{
    var tabs = $j('#'+parent).find('div.content');
    var navs = $j('#'+parent+' > .nav').find('li');

    var currentTab, currentNav, currentHref;

    var settings = jQuery.extend({
        'event': 'click'
    }, options);

    var historySupport = true;
    if ($j.browser.msie && $j.browser.version.substr(0,1)<=7) historySupport = false;

    initialize = function()
    {
        tabs.each(function(){
            $j(this).hide();
        });

        navs.each(function(){
            if(settings.event == 'hover')
            {
                $j(this).hover(function(){
                    show(this);
                });
            }

            if(settings.event == 'click')
            {
                $j(this).click(function(){
                    show(this);
                    return false;
                });
            }
            
        });

        hash = getHash();
        anchor = navs[0];
        if(hash !== false)
        {
            hash = $j(navs).find('a[href="' + hash + '"]').parent();
            if(hash.size() > 0)
            {
                anchor = hash;
            }
        }
        show(anchor);
        if(historySupport)
        {
            setInterval(checkHash, 500);
        }
    }

    show = function(element)
    {
        if(currentTab !== undefined && currentNav !== undefined)
        {
            currentNav.removeClass('current');
            currentTab.hide();
        }

        nav = $j(element);
        href = nav.find('a').attr('href');
        tab = $j(href);
        
        nav.addClass('current');
        tab.show();

        if(historySupport)
        {
            changeHash(href);
        }

        pageTracker._trackPageview('/tabs/'+href.substring(1));

        currentNav = nav;
        currentHref = href;
        currentTab = tab;
    }

    checkHash = function()
    {
        hash = getHash();
        if(hash !== currentHref)
        {
            show($j(navs).find('a[href="' + hash + '"]').parent());
        }
    }

    getHash = function()
    {
        hash = document.location.hash;
        if(hash !== undefined && hash !== '' && hash !== false && jQuery.trim(hash) !== '')
        {
            return hash;
        }
        else
        {
            var location = document.location.toString();
            if (location.match('#'))
            {
                return '#' + location.split('#')[1];
            }
        }
        return false;
    }

    changeHash = function(hash)
    {
        if(getHash() !== hash)
        {
            document.location.hash = hash;
            if(document.location.hash !== hash)
            {
                document.location.href = hash;
            }
        }
    }

    initialize();

}

$j(document).ready(function(){
    new Tabs('content', {'event': 'hover'});
});
