
window.JS_NAV_TAB = "JS_NAV_TAB";
window.FX_NAV_MENU;

window.addEvent
(
    'domready',
    
    function()
    {
        window.navList = $$('.mod_nav a');        
        window.MOD_NAV_REQ;
        
        navList.each
        (
            function($e, $ix)
            {
                $e.ix = $ix;
                $e.addEvent('click', navClick);
            }
        );
        
        var ix = Cookie.read( window.JS_NAV_TAB );
        
        if( location.href.indexOf('#') > -1 )
        {
            location.href.replace( /\#(.*)/, 
                function( $0, $1 )
                {
                    ix = $1;
                }
            );
        }
        
        ix = parseInt(ix, 10)||0;
        if( ix > navList.length ) ix = navList.length - 1;
        
        navList[ix].fireEvent('click');
        
        
        var mod_left = $$('dl.mod_nav');        
        if( mod_left && mod_left.length )
        {
            mod_left = mod_left[0];
            
            window.FX_NAV_MENU = XFixPosition.exec( mod_left, {} );
        }
        
         XTips.exec();
        
    }
);

function navClick($e)
{
    var url = this.href;
    var ix = this.ix;
    
    var column = initLoading(url);

    if( window.MOD_NAV_REQ )
    {
        window.MOD_NAV_REQ.cancel();
        window.MOD_NAV_REQ = null;
    }
                                    
    Cookie.write( window.JS_NAV_TAB, ix, {duration:3} );    
    location.href = location.href.replace(/\#[\s\S]*/, '') + '#' + ix;
    
    $$('.mod_nav dd').each
    (
        function($se)
        {
            $se.removeClass('on');
        }
    );
    
    this.getParent().addClass( 'on' );
    
    
    setTimeout( function(){ if( window.FX_NAV_MENU ) window.FX_NAV_MENU.fix(); }, 0 ); 
    if( column.data )
    {
        //initColumnData(column.data, column);
        return false;
    }
     
    window.MOD_NAV_REQ = new Request
                        ( 
                            {
                                url:url, 
                                method: 'get', 
                                onSuccess:
                                function( $t )
                                {
                                    initColumnData($t, column);
                                    
                                    window.MOD_NAV_REQ = null;
    
                                    var list = column.getElements('ul.itemList > li');
                                    if( list.length )
                                    {
                                        list.each
                                        (
                                            function($e, $ix)
                                            {
                                                $e.addEvent
                                                (
                                                    'mouseover',
                                                    function()
                                                    {
                                                        if( this.hasClass('important') )
                                                        {
                                                            this.exClass = 'important';
                                                            this.removeClass('important');
                                                        }
                                                        this.addClass( 'hover' );
                                                    }
                                                );
                                                
                                                $e.addEvent
                                                (
                                                    'mouseout',
                                                    function()
                                                    {
                                                        this.removeClass( 'hover' );
                                                        if( this.exClass )
                                                        {
                                                            this.addClass( this.exClass );
                                                        }
                                                    }
                                                );
                                            }
                                        );
                                        
                                        //alert(column.innerHTML)
                                        XTips.exec(column);
                                    }
                                }
                            } 
                        );
    window.MOD_NAV_REQ.send();
    
    return false;
}

function initLoading($url)
{
    var columnBox = $$('dl.mod_nav_content')[0];
    var columnList = $$('dl.mod_nav_content > dd');  
    
    var column;
    
    columnList.each
    (
        function($e, $ix)
        {
            if( $e.get('href') == $url )
            {
                column = $e;
            }
            $e.hide();
        }
    );
    
    if( column ) 
    {
        column.show();
        return column;
    }
    
    if( !column )
    {
        column = $(document.createElement('dd'));
        column.set('href', $url);
        column.set('html',  '<div class="mod_loading"></div>')
        columnBox.appendChild( column );
    }
    return column;
}

function initColumnData($t, $column)
{
    $column.data = $t;        
    $column.set( 'html', $t );
}

