
// ====================================================== 
// Copyright (C) Extend AS. All rights reserved.          
// For details on use and redistribution please refer to  
// the LICENSE.TXT file included with these sources.      
// ====================================================== 

// ======================================================
// Set some properties
//

var hideAllOptions = 1;
var hideMexOnClick = 1;
var showPosition   = 'right';

// ======================================================
// functions for getting objects position on page, 
// copied from http://www.quirksmode.org/
//

function mex_findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
	obj = obj.offsetParent;
    }
  }
  else if (obj.x) curleft += obj.x;
  return curleft;
}

function mex_findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
	obj = obj.offsetParent;
    }
  }
  else if (obj.y) curtop += obj.y;
  return curtop;
}

// ======================================================
// mex_showmenu function
//

function mex_showmenu(e, menuname, objname) {
    var cM = document.getElementById(menuname);
    var ob = document.getElementById(objname);

    if (hideMexOnClick == 1) {
      document.body.onclick = function() {
        mex_hideall();
      }
    }

    mex_hideall(menuname);

    if (cM) {
      if (!e) e = window.event;
      var xPos = 0;
      var yPos = 0;

      if (is_ie) {
	var oBody   = document.body;
        var yScroll = oBody.scrollTop;
        var xScroll = oBody.scrollLeft;
        xPos = e.clientX + xScroll;
        yPos = e.clientY + yScroll;

	if (ob) {
	  xPos = mex_findPosX(ob);
	  yPos = mex_findPosY(ob);
	}

	xPos = xPos + 3;
	yPos = yPos + 3;

      } else {
        xPos = e.pageX + 3;
        yPos = e.pageY + 3;
      }

      if (cM.style.display=='inline') {
        cM.style.display='none';
	mex_showall_options();
      } else {
        cM.style.display='inline';
	mex_hideall_options();
      }

      if (showPosition == 'left') {
	xPos = xPos - cM.offsetWidth;
        if (xPos < 1) {
          xPos = 1;
	}
      }

      cM.style.top = yPos+'px';
      cM.style.left = xPos+'px';

      if (is_ie || is_opera) {
        e.cancelBubble = true;
      } else {
        e.stopPropagation();
      }
    }
  }

// ======================================================
// mex_hideall function
//

  function mex_hideall(exception) {
    var allElements = document.getElementsByTagName("DIV");
    var counter = 0;
    for (counter = 0; counter < allElements.length; counter++) { 
      if (allElements[counter].id.indexOf("menu_") == 0) {
        if (exception != allElements[counter].id) {
          allElements[counter].style.display='none';
        }
      }
    }
    mex_showall_options();
  }

// ======================================================
// mex_hideall_options function
//

  function mex_hideall_options() {
    if (hideAllOptions == 1) {
      var allOptions = document.getElementsByTagName("SELECT");
      for (var counter = 0; counter < allOptions.length; counter++) {
        allOptions[counter].style.display='none';
      }
    }
  }

// ======================================================
// mex_showall_options function
//

  function mex_showall_options() {
    if (hideAllOptions == 1) {
      var allOptions = document.getElementsByTagName("SELECT");
      for (var counter = 0; counter < allOptions.length; counter++) {
        allOptions[counter].style.display='inline';
      }
    }
  }


