/*
 *  file:       menu.js
 *  version:        002
 *  avtor:      Letshenko Dmitriy Vladimirovich
 */

/*******************************************/


/**********************************************/

var IE = ( document.all ) ? true : false;
var domEvent;


/**********************************************/

/* константы: */

/* тип менюхи */
var MENU_DEFAULT = 0;
var MENU_MAIN_H  = 1;
var MENU_MAIN_V  = 2;
var MENU_POPUP   = 4;

var MENU_STYLE   = "menuclass";
var MENU_HILITED = "hiliteditem";
var MENU_BLURED  = "blureditem";
var MENU_BSELECTED = "bselecteditem";
var MENU_HSELECTED = "hselecteditem"

var MENU_SEPARATOR = "+++";

var MENU_DELAY   = 400;
var MENU_READY   = true;

/**********************************************/

var initMenu;

function setInitMenu( func )
{
    MENU_READY = false;
    initMenu = func;
}



/* пункт менюхи */
function CMenuItem( menu, id )
{
    /* id пункта менюхи */
    this.id = id;
    
    /* менюха-контейнер */
    this.parentMenu = menu;
    
    /* заголовок, хинт */
    this.title = "";
    this.hint  = "";
    this.href = "";
    
    /* дочернее меню */
    this.childMenu = null;
    
    /* тут лежит ячейка таблицы */
    this.cell = null;
    
    this.hiliteItem = function() {
        if ( this.parentMenu.selected == this ){
            this.cell.className = MENU_HSELECTED;   
        }
        else {
            this.cell.className = MENU_HILITED;
        }
        this.parentMenu.nothide = true;
    }
    
    this.blurItem = function() {
        if ( this.parentMenu.selected == this ){
            this.cell.className = MENU_BSELECTED;   
        }
        else {
            this.cell.className = MENU_BLURED;
        }
        this.parentMenu.nothide = false;
    }
    
    this.setChild = function( menuid ) {
        this.childMenu = AllMenues.items[ menuid ];
        this.childMenu.parentMenu = this.parentMenu;
        this.parentMenu.children.append( this.childMenu );
        this.childMenu.type = MENU_POPUP;
            
        this.cell.onmouseover = showChildAfter;
        this.cell.onmouseout = hideChildAfter;
    }
    
    this.init = function( row, title, href, hint ){
        
        this.cell = row.insertCell(0);
        
        this.cell.id = this.id;
        this.cell.className = MENU_BLURED;
        
        if ( href.substr(0,6) == "child " ) {
            this.cell.innerHTML = ':: ' + title + ' ::';
            href = href.substr(6);
            this.setChild( hint );          
        } 
        else {

            this.cell.innerHTML = '&nbsp;&nbsp;&nbsp;' + title;
            this.cell.title = hint;
        }
        
        this.cell.onclick = itemClick;
            
        /* хэндлеры для всех */
        if ( IE ) {
            this.cell.attachEvent("onmouseover",hiliteCaller);
            this.cell.attachEvent("onmouseout",blurCaller);
        }
        else {
            this.cell.addEventListener("mouseover", hiliteCaller, false);
            this.cell.addEventListener("mouseout", blurCaller, false);
        }
        
        this.href = href;
        this.title = title;
        this.hint = hint;
        
    } 
    
}

/**********************************************/

function itemClick( e )
{
	
    domEvent = e;
    var cell = (IE) ? currentCell() : findCell( this.id );
    cell.cell.className = MENU_HSELECTED;
  
    if ( cell.parentMenu.selected != null ) {
        cell.parentMenu.selected.cell.className = MENU_BLURED;
    }

    unselectAll();
    
    cell.parentMenu.selected = cell;
    itemSelected = cell;
    
    if (cell.parentMenu.parentMenu != null )
    {
		cell.parentMenu.hide();
    }    
    
    if ( cell.href != "" ){    
        eval(cell.href);
    }
}

function hiliteCaller( e )
{
    domEvent = e;
    
    var cell = (IE) ? currentCell() : findCell( this.id );
    cell.hiliteItem();
    
    cell.parentMenu.hidesoon = false;
    
    if ( cell.parentMenu.parentMenu != null )
        cell.parentMenu.parentMenu.hidesoon = false;
}

function blurCaller( e )
{
    domEvent = e;
    var cell = (IE) ? currentCell() : findCell( this.id );
    cell.blurItem();
    
    cell.parentMenu.hidesoon = true;
    setTimeout( "hideParentOf('" + cell.id + "')", MENU_DELAY );
}

function showChildAfter( e )
{
    domEvent = e;
    var cell = (IE) ? currentCell() : findCell( this.id );
    cell.childMenu.hidesoon = false;
    cell.childMenu.showsoon = true;
    setTimeout( "showChildOf('" + cell.id + "')", MENU_DELAY );
}

function hideChildAfter( e )
{
    domEvent = e;
    var cell = (IE) ? currentCell() : findCell( this.id );
    cell.childMenu.hidesoon = true;
    cell.childMenu.showsoon = false;
    setTimeout( "hideChildOf('" + cell.id + "')", MENU_DELAY );
}

function scrollbreak()
{
    var key;
    for ( key in AllMenues.items )
    {
        if ( AllMenues.items[ key ].type == MENU_POPUP )
            AllMenues.items[ key ].hide();
    }
}
    
/**********************************************/
        
function showChildOf( cellid )
{
    var cell = findCell( cellid );
    var cx = 0, cy = 0;
    var td = cell.cell;
    
    if ( cell.parentMenu.type == MENU_MAIN_H ) {
        cx = td.offsetLeft 
           + td.offsetParent.offsetParent.offsetLeft;
        
        if (cell.parentMenu.frame == cell.childMenu.frame)
        {
            cy = td.offsetParent.offsetParent.offsetTop
               + td.offsetParent.offsetParent.offsetHeight;
        }
        else
        {
            if (IE) {
                cx -= cell.childMenu.frame.screenLeft 
                    - cell.parentMenu.frame.screenLeft;
            }
            else {
                cx -= cell.childMenu.frame.screen.left 
                    - cell.parentMenu.frame.screen.left;
            }
            cy += cell.childMenu.layer.parentNode.scrollTop;
        }
    }
    else {
        cy = td.offsetTop + 
            td.offsetParent.offsetParent.offsetTop - 1;
        
        if (cell.parentMenu.frame == cell.childMenu.frame)
        {
            cx = td.offsetParent.offsetParent.offsetLeft
               + td.offsetParent.offsetParent.offsetWidth + 2;
        }
        else
        {
            if (IE) {
                cy -= cell.childMenu.frame.screenTop 
                    - cell.parentMenu.frame.screenTop;
                cy += cell.childMenu.layer.parentNode.scrollTop - 2;    
            }
            else {
                cy -= cell.childMenu.frame.screen.top
                    - cell.parentMenu.frame.screen.top;
                cy += cell.childMenu.frame.scrollY;    
            }
        }
    }
    
    if ( cell.childMenu.showsoon ) cell.childMenu.show( cx, cy );
}

function hideChildOf( cellid )
{
    var menu = findCell( cellid ).childMenu;
    if ( menu.hidesoon ) menu.hide();
}       

function hideParentOf( cellid )
{
    var menu = findCell( cellid ).parentMenu;
    if ( ( menu.type == MENU_POPUP ) && menu.hidesoon )
    { 
        menu.hide();
        if ( menu.parentMenu != null )
        {
            if ( !menu.nothide ){
                menu.parentMenu.hidesoon = true;

                setTimeout( "hideParentOf('" + 
                    menu.parentMenu.id 
                    + MENU_SEPARATOR + "0"
                    + "')", MENU_DELAY );
            }
        }
    }
}

/**********************************************/

/* МЕНЮХА */
function CMenu( id, frame, type )
{
    /* id для менюхи */
    this.id = id;
    
    /* тип менюхи */
    this.type = type;
    
    /* фрейм, где менюха лежит */
    if ( typeof(frame) == "undefined" ) {
        frame = window;
    }
    this.frame = frame;
    
    
    /* таблица - представление менюхи */
    this.layer = this.frame.document.createElement('DIV');
    this.layer.style.visibility = "hidden";
    this.layer.style.position = "absolute";
    this.layer.style.left = 0;
    this.layer.style.top = 0;

    this.frame.document.body.appendChild( this.layer );
    
    this.table = this.frame.document.createElement('TABLE');
    this.table.id = id;
    this.table.cellSpacing = 0;
    this.table.cellPadding = 0;
    this.layer.appendChild( this.table );
    this.table.className = MENU_STYLE;
    
    this.frame.document.body.onscroll = scrollbreak;
    
    
    /* пункты менюхи */
    this.entryes = new CCollection();
    
    if ( this.type == MENU_MAIN_H ) 
        this.row = this.table.insertRow(0);
    
    /* добавление пункта */
    this.addItem = function( title, href, hint ) {
        var itemid = this.id + MENU_SEPARATOR + this.entryes.count;
        if (this.full) return;
        var item = new CMenuItem( this, itemid );
        
        if ( this.type != MENU_MAIN_H )
            this.row = this.table.insertRow( 
                this.entryes.count );
        
        item.init( this.row, title, href, hint );
        
        this.entryes.append( item );
    }
    this.full = false;
    
    /* показуха */
    
    this.visible = false;
    this.x = 0;
    this.y = 0;
    this.hidesoon = false;
    this.showsoon = false;
    this.nothide = false;
    
    this.show = function( x, y ) {
        this.x = x;
        this.y = y;
        this.ontop();
    }
    
    this.ontop = function() {
        this.visible = true;
        this.layer.style.left  = this.x;
        this.layer.style.top   = this.y;
        this.layer.style.visibility = "visible";
    }
    
    /* ныкает менюху */
    this.hide = function() {
        if (this.nothide) return;
        this.visible = false;
        this.layer.style.visibility = "hidden";
    }
    
    /* детишки */
    this.children = new CCollection;
    
    /* родитель */
    this.parentMenu = null;
    
    this.isradio = true;
    this.selected = null;
    this.unselect = function(){
        if (!this.selected) return;
        this.selected.cell.className = MENU_BLURED;
        this.selected = null;
    }
    
    /* суем себя в коллекцию менюх */
    AllMenues.append( this );
    
}

/**********************************************/

/* коллекция */
function CCollection()
{
    this.items = new Array();       
    this.count = 0;
    
    this.append = function( item ) {
        this.items[ item.id ] = item;
        this.count++;       
    }
    
    this.findItemLike = function( pattern ) {
        var itemid;
        
        var pos = pattern.indexOf( MENU_SEPARATOR ) ;
        itemid = ( pos == -1 ) ? pattern : pattern.substring( 0, pos );
        
        return this.items[ itemid ];
    }
}

var AllMenues = new CCollection();
var MainMenu  = null;

var itemSelected = null;

function unselectAll()
{
    if (!itemSelected) return;
    itemSelected.parentMenu.selected = null;
    itemSelected.cell.className = MENU_BLURED;
    itemSelected = null;
}

/**********************************************/

function findEvent()
{
    if ( window.event != null ) return event;
    
    var i;
    var frames = window.parent.frames;
    
    for ( i = 0; i < frames.length; i++ ){
        if ( frames[i].event != null ) return frames[i].event;
    }
    
    return null;
}

function currentCell()
{
    return findCell(findEvent().srcElement.id);
}

function findCell( cellid )
{
    return AllMenues.findItemLike( cellid ).entryes.items[ cellid ];
}

/**********************************************/

function createMenu( menuid, frame )
{
    var menu = new CMenu( menuid, frame, MENU_DEFAULT );
    return menu;
}

function createMainMenu( menuid, frame, horiz )
{
    var type = horiz ? MENU_MAIN_H : MENU_MAIN_V; 
    var menu = new CMenu( menuid, frame, type );
    menu.isradio = true;
    MainMenu = menu;
    return menu;
}

function showMenu( menuid, x, y )
{
    var menu = AllMenues.items[ menuid ];
    if ( typeof(x) == "undefined" ){
        menu.ontop();   
    }
    else {
        menu.show( x, y );
    }
}

function showMainMenu( x, y )
{
    MainMenu.show( x, y );
}

function hideMenu( menuid )
{
    var menu = AllMenues.items[ menuid ];
    menu.hide();
}

function hideAllMenues()
{
    var key;
    for ( key in AllMenues.items )
    {
        AllMenues.items[ key ].hide();
    }
    MainMenu.ontop();
}

function killAllMenues()
{
    var key, b;
    for ( key in AllMenues.items )
    {
        b = AllMenues.items[ key ].layer.parentNode;
        if (b) b.removeChild(AllMenues.items[key].layer);
    }
    delete AllMenues;
    AllMenues = new CCollection;
}

var _was;
var _frame;

function loadinto( frame, url )
{
    MENU_READY = false;
    _frame = frame;
    _was = frame.document.body;
    if (IE) frame.location.href = url
    else frame.location = url;
//    setTimeout( "testChanged()", MENU_DELAY );
}

function testChanged()
{
    if ( _was == _frame.document.body)
    {
        setTimeout( "testChanged()", MENU_DELAY );
    }
    else
    {
        rebuildMenues();
    }
}

function rebuildMenues()
{
    if (MENU_READY) return;
    killAllMenues();
    initMenu();
//    MENU_READY = true;
}



/* konez */

