/*----------------------------------------------------------------------
  layerAPI.js - crossplatform compatibility layer routines   

  history:
      4.12.2003 Tomas Kolousek
          - basic rough imlementation, tested in IE 5.5, Mozilla 1.5, NS4.51
     20.12.2003 Tomas Kolousek
          - onXXX actions added for elements, rewritten to object oriented model                    
      3.1.2004 Tomas Kolousek
          - added function "execMethod" for NS4 compatibility and delayed auto hiding
  ---------------------------------------------------------------------*/


// -- cross-browser element accessing -- //

function getElem(id,par)
// returns object referenced by it's 'id'
{
  var element = null;
  if (par==null)
    par = document;
  else
    par = (isNS?par.document:par);	// NS4 references layer objects as doc.layer.doc.XXX

  if (isDOM)  
    element = document.getElementById(id)
  else if (isIE)
    element = document.all[id]
  else if (isNS)
    element = document.layers[id]
  return (element)   
}
// ---------------= SUPPORTING FUNCTIONS =----------------- //
function execMethod(instanceName, methodName)
// Netscape 4 compatibility function for calling object methods in events handlers
{ var index;
    switch (methodName) {
      case "show":          window[instanceName].show();
                            break;
      case "hide":          window[instanceName].hide();
                            break;
      case "elemMouseOut":  window[instanceName].elemMouseOut();
                            break;
      case "elemMouseOver": window[instanceName].elemMouseOver();
                            break;
    }
}

function showElem(_id)
// creates code for automatic opening menu with it's ID
{
  execMethod(_id,'show');  
}
function hideElem(_id)
// creates code for automatic hiding menu with it's ID
{
  
  window[_id].parentTimer = setTimeout('execMethod("'+_id+'","hide")',elemHideTime);  
}

// --------= GENERAL LAYER OBJECT =--------- //

function elemTimeoutHide()
// caled after 'hideTime' in miliseconds and 'canHide' enabled
// if no layer is active (ie. no layer with mouse over it) it hides the layer
{
  if (this.onHide)
    this.onHide();
  if (this.canHide)
    this.hide();
}

function elemMouseOut()
// called immediatelly after moving mouse out from the created layer
{
//  window.status='Mouse out '+this.elemName;
  if (this.canHide)   // automatical hiding is in use
  {
    if (this.hideTimeout == 0)
      this.hide();
    else
      this.hideTimer=setTimeout(this.elemName+'.elemTimeoutHide();',this.hideTimeout)
  }
  if (this.evtMouseOut)   // user defined MouseOut handler
    this.evtMouseOut();
}

function elemMouseOver()
// called immediatelly after moving mouse over the created layer
{
//  window.status='Mouse over '+this.elemName+' '+this.parentTimer;
  elemActive = this.elemName;
  if (this.parentTimer)
    {  clearTimeout(this.parentTimer);this.parentTimer = 0 };
  if (this.hideTimer)
    {  clearTimeout(this.hideTimer);this.hideTimer = 0 };    
  elemActive = this.elemName;   // update 
  if (this.evtMouseOver)          // user defined MouseOut handler
    this.evtMouseOver();
}

function elemShow()
// shows the element
{
  var elem = getElem(this.elemName);
  if (elem)                     // if there's element found
    if (isNS)
      elem.visibility = visible;        // NS4 Layer object property
    else 
      elem.style.visibility = visible;  // otherwise DOM style property
  if (this.hideTimer)
  {
    clearTimeout(this.hideTimer);
    this.HideTimer = 0;
  }
}

function elemHide()
// hides the element
{
  var elem = getElem(this.elemName);
  if (elem)                      // if there's element found
    if (isNS)
      elem.visibility = hidden;        // NS4 Layer object property
    else 
      elem.style.visibility = hidden;  // otherwise DOM style property
  if (this.evtHide)
    this.evtHide();
}

function elemInsert()
// inserts element content into HTML page code
{
  document.write (this.content);
}

function CSSelem(_id,_left,_top,_width,_height,_bgcolor,_class,_content)
// creates <DIV> or <LAYER> container with specified parameters
// background color should be specified due to nestandard CSS background element specification
{var html_code='';

   if (isNS)	// <layer> container required for mouseout events
  {
    html_code = '<layer name="'+_id+'" _id="'+_id+'"'
                          +(_left?' left="'+_left+'"':'')
                          +(_top?' top="'+_top+'"':'')
                          +(_width?' width="'+_width+'"':'')
                          +(_height?' height="'+_height+'"':'')
                          +(_bgcolor?' bgColor="'+_bgcolor+'"':'')
                          +(_class?' class="'+_class+'"':'')
                          +' visibility = "hidden"'
                          +' onmouseover="execMethod(\''+_id+'\',\'elemMouseOver\');return(true)"' 
                          +' onmouseout="execMethod(\''+_id+'\',\'elemMouseOut\');return(true)"' 
                          +'>'
                          +(_content?_content:'')
                          +'</layer>';
  }
  else		// <div> container for DOM standard or IE/Opera
  {
     html_code = '<div name="'+_id+'" id="'+_id+'" style="position:absolute; '
                          +(_left?' left:'+_left+'px;':'')
                          +(_top?' top:'+_top+'px;':'')
                          +(_width?' width:'+_width+'px;':'')
                        //  +(_height?' height:'+_height+'px;':'')
                          +(_bgcolor?' background:'+_bgcolor+';':'')
                          +' visibility:hidden;'
                          +'"'
                          +(_class?' class="'+_class+'"':'')
                          +' onmouseover="execMethod(\''+_id+'\',\'elemMouseOver\');return(true)"' 
                          +' onmouseout="execMethod(\''+_id+'\',\'elemMouseOut\');return(true)"' 
                          +'>'
                          +(_content?_content:'')
                          +'</div>';
  }
  
  // ---- properties ---- //
  this.content = html_code;
  this.elemName = _id;
  this.canHide = true;
  this.hideTimeout = 100;     
  this.parentTimer = 0;       // TimerID for canceling Timer operation of opener 
  this.hideTimer = 0;         // TimerID when performing delayed autohiding
  // ---- methods ---- //
  CSSelem.prototype.insert = elemInsert;
  CSSelem.prototype.show = elemShow;
  CSSelem.prototype.hide = elemHide;
      //--- private methods for internal use only ---//
  CSSelem.prototype.elemMouseOut = elemMouseOut;
  CSSelem.prototype.elemMouseOver = elemMouseOver;
  CSSelem.prototype.elemTimeoutHide = elemTimeoutHide;
  // ---- events ---- //
  this.evtMouseOut = null;
  this.evtMouseOver = null;
  this.evtHide = null;
}

// ---- Startup: browser detection variables ---- //

var isDOM = (document.getElementById?true:false);	// indicates DOM compatible browser
var isIE = (document.all?true:false);			        // indicates IE family browser
var isNS = (document.layers?true&&!isDOM:false);	// indicates Netscape DOM incompatible browser 
var isOpera = (window.opera?true:false);		// indicates Opera family browser

var visible = (isDOM?'visible':'show');			// variables for visibility display
var hidden = 'hidden';

var elemActive = '';                        // ID of last active element
var elemHideTime = 100;                     // delay for hidding elemnt via hideElem

