/*
 * (c) Ambientia Ltd.
 * dhtml menu IV 4.0
 *
 * Netcape 4+, Internet Explorer 4+
 * Mozilla 5,  Opera 5+
 * 
 *
 * required:
 *  dhtml.js
 *
 * @author J Löf
 */
/* static */
var UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
 
/* ------ Preferences ----- */
var LAYER_PREFIX           = 'menu'       //string div id prefix
var MENU_TIMEOUT           = 1000;        //milliseconds to hide
var HIDE_ALL               = 1;           //0 = dont hide rootlevel, 1 = hide all levels on mouseout
var OVERRIDE_COORDS        = 0;           //boolean alwasy override given coordinates
var MOUSE_POSITION         = 0;           //boolean always use mouse positioning
var ROOT_DIRECTION         = DOWN;        //direction to open root
var FIRSTCHILD_DIRECTION   = RIGHT;        //direction to open firstchild
var DEFAULT_CSS            = 'jvalikko';  //default class name
var TABLE_PADDING          = 2;           //layout table padding
var DEFAULT_WIDTH          = 150;         //default width, if dynamic check fails (isNS4)
var DEFAULT_HEIGHT         = 20;          //default height, if dynamic check fails (isNS4)
var AVERAGE_FONT_WIDTH     = 5;           //for approximating menu sizes when can't read element size (isNS4)
var ITEM_MARKER = '<img src="pics/n.gif" alt="" border="0" vspace="8" hspace="4" align="right">';
/* ------ /Preferences ----- */
var jMenuS = new Array();
var jmenuTimeout = null;

function JValikko( _xset, _yset, _width, _height, _css, _open )
{ 
    this._xset = _xset || null;
    this._yset = _yset || null;
    this._xoffset = 0;
    this._yoffset = 0;
    this._width = _width || 0;
    this._height = _height || 0;
    this._x = 0;       //internal calls only
    this._y = 0;       //internal calls only
    this._css = _css || DEFAULT_CSS; 
    this._open = false; 
    this._parent = false;
    this._layer = LAYER_PREFIX + jMenuS.length;
    this.self = "jMenuS[" + jMenuS.length + "]";
    this._id = "jM"+jMenuS.length;
    jMenuS[jMenuS.length] = this;
    this.itemS = new Array();
    this.subMenuS = new Array();
    this.positioning = function ()
    {    
        this._x = window.event.x;
        this._y = window.event.y;
        if (this._parent && isNS4)
        {
            this._x += this._parent._x;
            this._y += this._parent._y;       
        }
    }
    
    this.lisaaItem = function (_name, _url, _target, _onmouseover, _onmouseout)
    {
        var p = new Object();
        p._name = _name;
        p._url = _url;
        p._id = this._id + 'i' + this.itemS.length;
        p._mouseover = this.self + ".hideChildren()";
        p._target = _target;
        p._onmouseover = _onmouseover;
        p._onmouseout = _onmouseout;
        this.itemS[this.itemS.length] = p;
        return p;
    }
    
    this.lisaaMenu = function (_name, _url, _target, _onmouseover, _onmouseout)
    {   
        var p = this.lisaaItem(_name, _url, _target, _onmouseover, _onmouseout);
        var t = new JValikko(null, null, this._width, this._height, this._css, false);
        t._parent = this;
        p._onmouseover += ";" + t.self + ".swz(this);";
        p._onmouseout += ";" + t.self + ".swz(this);";
        p._submenu = t;
        this.subMenuS[this.subMenuS.length] = t;
        return t;
    }    
    
    this._look = function (n)
    {
        if (this._customlook) return this._customlook(n);
        var html = "";
        if (this.itemS.length)
        {
            if (isNS4) html += '<table cellpadding="1" cellspacing="0" border="0"><tr><td bgcolor="#000000">';
            html += '<table cellpadding="'+TABLE_PADDING+'" cellspacing="0" border="0"';
            if (!isNS4 && this._width) html += ' width="'+this._width+'"';
            if (isNS4) html += ' width="100%"';
            html += '>';
            for (i = 0; i < this.itemS.length; i++)
            {
                html += '<tr class="'+this._css+'"><td class="'+this._css+'" onmouseover="hoverIn(this)" onmouseout="hoverOut(this)">';
                if (this.itemS[i]._submenu && this.itemS[i]._submenu.itemS.length) html += ITEM_MARKER;
                html += '<a';
                if (this.itemS[i]._url)       html += ' href="'+this.itemS[i]._url+'"';
                if (this.itemS[i]._target)    html += ' target="'+this.itemS[i]._target+'"';
                html += ' onmouseover="'+this.itemS[i]._onmouseover+'"';
                html += ' onmouseout="'+this.itemS[i]._onmouseout+'"';
                html += ' class="'+this._css+'">';
                html += this.itemS[i]._name;
                html += '</a></td></tr>';
            }
            html += '</table>';
            if (isNS4) html += '</td></tr></table>';
        }
        return html;
    }
    
    this.create = function ()
    {
        if (this._open) var v = "visible"; else var v = "hidden";
        createLayer( this._layer, this._look(), this._xset, this._yset, v );      
        return;
    }
    
    this.show = function (_fromAnchor )
    {
        if ( this._open ) return; //on jo näkyvillä
       
        if ( (this._xset || this._yset) && !OVERRIDE_COORDS)
        {
          this._x = this._xset;
          this._y = this._yset;
          /*paikannus ennalta määriteltyyn koordinaattiin */
        } //end if
        else if ( MOUSE_POSITION || !_fromAnchor )
        {
           /*paikannus hiiren koordinaattiin */
          this.positioning();
        } //end else if
        else           
        {
            /*paikannus parent-elementin koordinaatin perusteella */
            if (this._parent) //alitason valikolle
            {
                if (_fromAnchor.parentNode)
                    var a = getGeometry(_fromAnchor.parentNode);
                else
                  var a = getGeometry(_fromAnchor);
                
                this._x = a.x;
                this._y = a.y;     
                if (isNS4) //netscape 4
                {
                    var xl = 0;
                    for (var i = 0; i < this._parent.itemS.length; i++)
                      if (this._parent.itemS[i]._name.length > xl) xl = this._parent.itemS[i]._name.length;
                   this._x += parseInt(this._parent._x) + parseInt(xl * AVERAGE_FONT_WIDTH);
                   this._y += parseInt(this._parent._y);            
                   var height = this.itemS.length * DEFAULT_HEIGHT; //guess height          
                   if ( (this._y + height) > bodyHeight() )
                      this._y = (bodyHeight() - height);
                   if (this._y < 0)
                    this._y = 0;
                }
                else //paremmat selaimet
                {  
                  var own = getGeometry(getLayer(this._layer));
                  if (FIRSTCHILD_DIRECTION==RIGHT || this._parent._parent)
                  {     

                    if ((a.x + a.width + own.width) > bodyWidth())
                    {
                      this._x -= own.width;
                      if (this._x < 0) this._x = 0;
                    }
                    else
                        this._x += a.width;
                  }    
                  
                  if ( (this._y + own.height) > bodyHeight() )
                      this._y = (bodyHeight() - own.height);
                  if (this._y < 0) this._y = 0;
                  
                  if (FIRSTCHILD_DIRECTION==DOWN && !this._parent._parent)this._y += a.height;
                }
            } //end if
            else if (!this._parent) //juuritason valikoille
            {
                var a = getGeometry(_fromAnchor);
                this._x = a.x;
                this._y = a.y;
                if (ROOT_DIRECTION == RIGHT)
                    this._x += a.width;
                else
                    this._y += a.height;
                var own = getGeometry(getLayer(this._layer));
                if (own.width < a.width)
                {
                    var l = getLayer(this._layer);
                    l.style.width = a.width;
                    l.firstChild.style.width = a.width;   
                } //endif
                own = getGeometry(getLayer(this._layer));
                if (isNS4) //netscape 4
                {
                    var xl = 0;
                    for (var i = 0; i < this.itemS.length; i++)
                      if (this.itemS[i]._name.length > xl) xl = this.itemS[i]._name.length;
                    own.width = (xl * AVERAGE_FONT_WIDTH);
                }
                if ((this._x + own.width) > bodyWidth())
                  this._x = bodyWidth()-own.width;
            } //endif
        } //endif
        setLayerPosition( this._layer, (parseInt(this._x) + parseInt(this._xoffset)), (parseInt(this._y) + parseInt(this._yoffset)), this._align);
        visibility( this._layer, "on");
        this._open = true;
    }
    
    this.hide = function ()
    {
        if (!this._open) return;
        visibility( this._layer, "off" )
        for( var s = 0; s < this.subMenuS.length; s++ )
            if (this.subMenuS[s]._open)
                this.subMenuS[s].hide();
        this._open = false;
    }
    
    this.hideChildren = function ()
    {
        for (c = 0; c < this.subMenuS.length; c++)
          this.subMenuS[c].hide();
    }
    
    this.findRoot = function()
    {
        var thisRoot = false;
        var level = this;
        while (!thisRoot)
        {
            if (level._parent) level = level._parent;
            else thisRoot  = level.self;
        }
        return thisRoot;
    }
    
    this.swz = function ( _fromAnchor )
    {
        if (!this.itemS.length) return;
        var root = this.findRoot();
        for( var a = 0; a < jMenuS.length; a++ )
            if ( !jMenuS[a]._parent && ( jMenuS[a].self != root ) )
                jMenuS[a].hide();
        if (this._parent)
            for( var u = 0; u < this._parent.subMenuS.length; u++ )
                this._parent.subMenuS[u].hide();
        this.show(_fromAnchor);
    } 
}

function jValikkoSuljeKaikki()
{
    jMenuHideAll();
}

function jMenuHideAll()
{
    for (n = 0; n < jMenuS.length; n++)
    {
        if (HIDE_ALL) jMenuS[n].hide();
        else jMenuS[n].hideChildren();
    }
}

function JValikkoAlusta()
{   
    for( var h = 0; h < jMenuS.length; h ++ )
    {
        window.status = "generating dynamic navigation [" + h + "]";
        jMenuS[h].create();
        window.status = "done:" + h;
    }
    window.status = "ready";
}

if (isNS4) window.document.captureEvents( Event.MOUSEOUT | Event.MOUSEOVER | Event.MOUSEDOWN |  Event.MOUSEMOVE );

document.onmouseout = function (e)
{
    setupEventObject(e)
    if ( (element = findProp(window.event.srcElement, "id")) != null )
       if ( element.id.match(LAYER_PREFIX) )
       {
           clearTimeout(jmenuTimeout);
           jmenuTimeout = setTimeout( "jMenuHideAll()", MENU_TIMEOUT );
       }
}
document.onmouseover = function (e)
{
    setupEventObject(e)
    if ( (element = findProp(window.event.srcElement, "id")) != null )
       if ( element.id.match(LAYER_PREFIX) )
        clearTimeout(jmenuTimeout);
}
document.onmousedown = function (e)
{
    setupEventObject(e)
    if (window.event.srcElement.tagName == "INPUT") return;
    if ( findProp(window.event.srcElement, "href") == null)
        jMenuHideAll();

}

