Navigator = {
    
    currentModule : null,
    url : null,
    routing : {
        homepage: '',
        e401: '',
        e404: '',
        e500: ''
        // and more ...
    },
    
    init : function(expr) {
        //if(0)
        $(expr).live('click', function() {
            
            if($(this).hasAttr('href')) {
                
                Navigator.go($(this).attr('href'));

                return false;
            }
        });
        
        $('form.ajaxized').live('submit', function() { // fixme
            
            var form = $(this).closest('form');
            
            form.ajaxSubmit({
                target: '#sf_content'
            });
            
            Navigator.onComplete();
            
            return false;
        });
        
        $.fn.hashchange.src = Navigator.routing.homepage;

        $(window).hashchange(function() {
            
            var hash = location.hash;
            var url = hash.replace(/^#/, '');
            
            if(Navigator.url != null && hash == '') {
                
                url = $.fn.hashchange.src;
            }
            //document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
            
            
            if(url && url.match('^/') == '/') {

                Navigator.load(Navigator.url = url);
                
            } else {
                
                Navigator.initUrl(Navigator.url = window.location.pathname);
            }
        })

        $(window).hashchange();
    },

    go : function(url) {
        
        url = url.replace('#', '');
        location.hash = '#' + url;
    },

    load : function(url) {
        
        this.onLoad(url);
        
        //$.get(url, {}, function(responseText, textStatus, XMLHttpRequest) {
        $('#sf_content').load(url, function(responseText, textStatus, XMLHttpRequest) {

            if(XMLHttpRequest.status == 401) {

                /*Navigator.go(this.routing.e401);

                if(BuyStuff.user.isAuthenticated)
                    BuyStuff.welcomeBox.logout();*/

            } else if(XMLHttpRequest.status == 404) {

                //Navigator.go(BuyStuff.routing.e404);
            }

            Navigator.initUrl(url);
            Navigator.onComplete(url, XMLHttpRequest);
        })
    },

    onLoad : function(url) {
        
    },

    onComplete : function(url, xhr) {
        
    },

    onInitUrl : function(url) {
        
    },
    
    initUrl : function(url) {
        
        //url = url.replace(new RegExp('(.*/galeria/show)(.+)'), '$1Image');
        
        switch(url) {

            default:
                //debug('default url:' + url);
                break;

            case Navigator.routing.e401:
            case Navigator.routing.e404:

                break;

            /*case BuyStuff.routing.event_show:
            case BuyStuff.routing.galeria_show:
            case BuyStuff.routing.galeria_showImage:
                
                BuyStuff.modules.comment.init();

                break;

            case BuyStuff.routing.szakosztaly_show:

                BuyStuff.currentModule = BuyStuff.modules.szakosztaly;
                BuyStuff.currentModule.init();

                break;*/
        }
        
        Navigator.onInitUrl(url);
    }
}

