
$(document).ready(function() {

    $('.sidenav ul').hide();

    function callback(hash)  
    {
        var hash = window.location.hash.substr(1);
        var toLoad
        $('#menu li a, a.ajax').each(function(){
            var href = $(this).attr('href');
            if(hash==href){
                toLoad = hash;
            }                                            
        });          

        $('div#content').load(toLoad);            
    }

    var origContent = "";

    function selected() {   
        $("a").each(function() {
            var hreflink = $(this).attr("href");
            if (hreflink==window.location.hash.substr(1)) {
                $(this).addClass("selected");
            }
            else
                {
                $(this).removeClass("selected");
            }
        });
    }

    function load(hash) {
        selected();
        _gaq.push(['_trackPageview', location.pathname + location.search + location.hash]);
        if(hash != "") {
            $('div#content').prepend('<span id="load"><img src="/images/ajax-loader.gif"> Nahrávám obsah stánky...</span>');
            $('#load').fadeIn('slow');  
            if(origContent == "") {
                origContent = $('div#content').html();

            }
            $('div#content').load(hash);
        } 
        else if(origContent != "") 
            {
            $('div#content').html(origContent);    
            $('#load').fadeOut('slow');  
        }
    }                 


    $.history.init(function(url){
        load(url)}, {unescape: "/"
    });

    $('#menu a,.sidenav a,a.ajax').live('click', function(e) {   
        var url = $(this).attr('href');
        if(url=="#")
            {
            var x = $(this).parents().length; 
            var checkElement = $(this).next();
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                checkElement.slideUp('normal');
                return false;
            }
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                checkElement.slideDown('normal');
                return false;
            }

            if((!checkElement.is('ul')) && x<11) {
                $('.sidenav ul:visible').slideUp('normal');
                return false;
            } 

        }
        else
            {
            url = url.replace(/^.*#/, '');      
            $.history.load(url);
            if($(this).parents().length<11)
                {
                $('.sidenav ul:visible').slideUp('normal');

            }
            return false;
        }
    });

});	

