// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/dropdown.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.


// ***** Popup Control *********************************************************


/***********************************************
* AnyLink Drop Down Menu- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function iecompattest(){
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function clearbrowseredge(obj, dropmenuobj, whichedge) {
  // p = obj, c = dropmenuobj
  var ie4=document.all
  var ns6=document.getElementById&&!document.all

  var edgeoffset=0;
  if (whichedge=="rightedge"){

    // Find the right edge
    var windowedge=ie4 && !window.opera ? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;

    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
    //alert(whichedge + ": " + windowedge + " " +  findPosX(dropmenuobj) + " " + dropmenuobj.contentmeasure + " " + obj.offsetWidth); // Testing

    // rightedge: 580 -39 434 96
    // rightedge: 580 255 434 96

    if (windowedge-findPosX(dropmenuobj) < dropmenuobj.contentmeasure) {
      //edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth;
      edgeoffset = dropmenuobj.contentmeasure - (windowedge - findPosX(dropmenuobj));
    }
  } else {

    // Find the top edge (page may be scrolled down)
    var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset

    // Find the bottom edge
    var windowedge=ie4 && !window.opera ? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18

    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;

    //alert(whichedge + ": " + windowedge + " " + topedge + " " + findPosY(dropmenuobj) + " " + dropmenuobj.contentmeasure + " " + obj.offsetHeight); // Testing

    // bottomedge: 922 482 547 390 14
    // bottomedge: 1462 482 547 390 14

    // (922 - 547) 375 < 390
    if (windowedge-findPosY(dropmenuobj) < dropmenuobj.contentmeasure) { //move up?
      // 390 + 14
      //edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight;

      // Move the window up by: (height of the content to display) - (bottom edge of window - top edge of content to display)
      edgeoffset = dropmenuobj.contentmeasure - (windowedge - findPosY(dropmenuobj));
      edgeoffset = (edgeoffset < obj.offsetHeight) ? 0 : edgeoffset;

      // (547 - 482) 65 < 390
      if ((findPosY(dropmenuobj) - edgeoffset) > topedge) { //up no good either?
      //if ((findPosY(dropmenuobj)-topedge) < dropmenuobj.contentmeasure) { //up no good either?
        // 547 + 14 - 482 = 79
        //edgeoffset=findPosY(dropmenuobj)+obj.offsetHeight-topedge;

        //edgeoffset = edgeoffset - ((findPosY(dropmenuobj) - edgeoffset) - topedge);

      }
    }
  }
  //alert(whichedge + ": " + edgeoffset); // Testing
  return edgeoffset
}


// ***** at_show_aux *****

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );

  if (c["absolute_position"] == "absolute") {
    //alert("Absolute offset"); // Testing
    var top  = c["at_offsetHeight"];
    var left = c["at_offsetWidth"];
  } else if (c["at_offsetWidth"] != undefined) {
    //alert("Within Parent offset"); // Testing
    // Allow the child object to be placed above the parent
    var top  = (c["at_offsetHeight"]) ? p.offsetHeight-c["at_offsetHeight"] : 0;
    var left = (c["at_offsetWidth"]) ? p.offsetWidth -c["at_offsetWidth"] : 0;
  } else {
    //alert("Default offset"); // Testing
    // Allow the child object to be placed to the right or below the parent
    var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
    var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;
  }

  if (c["absolute_position"] != "absolute") {
    for (; p; p = p.offsetParent) {
      top  += p.offsetTop;
      left += p.offsetLeft;
    }
  }

  if (c.style.visibility != "visible") {
    //c.style.left=findPosX(c)-clearbrowseredge(p, c, "rightedge")+"px"
    //c.style.top=findPosY(c)-clearbrowseredge(p, c, "bottomedge")+p.offsetHeight+"px"

    c.style.position   = "absolute";

    // Position the dropdown 3 times, the first is to allow the dropdown to
    // settle properly in IE6 which otherwise functions quite well.
    c.style.left = left - clearbrowseredge(p, c, "rightedge") + "px"
    c.style.top = top - clearbrowseredge(p, c, "bottomedge") + "px"

    // The second time we record the results to check for zero values.
    // Zero values mean no edges were in range. We do not always trust this.
    var clearLeft = clearbrowseredge(p, c, "rightedge");
    var clearTop = clearbrowseredge(p, c, "bottomedge");
    c.style.left = left - clearLeft + "px"
    c.style.top = top - clearTop + "px"

    // Double check the readjusted window which may need moving again.
    if (clearLeft == 0) c.style.left = left - clearbrowseredge(p, c, "rightedge") + "px"
    if (clearTop == 0) c.style.top = top - clearbrowseredge(p, c, "bottomedge") + "px"

    //alert("top: "+top); // Testing
    //alert("left: "+left); // Testing
    //c.style.top        = top +'px';
    //c.style.left       = left+'px';
    c.style.visibility = "visible";
  }

}

// ***** at_show *****

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  at_show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);
}

// ***** at_hide *****

function at_hide()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  // Setting timeout to 0 achieves a better look
  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 0);
  //c.style.visibility = "hidden";
}


function at_hide_manual(id_child)
{
  var c = document.getElementById(id_child);
  c.style.visibility = "hidden";
}


// ***** at_click *****

function at_click()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  if (c.style.visibility != "visible") at_show_aux(p.id, c.id); else c.style.visibility = "hidden";
  return false;
}

// ***** at_attach *****

// PARAMETERS:
// parent    - id of the parent html element
// child     - id of the child  html element that should be droped down
// showtype  - "click" = drop down child html element on mouse click
//             "hover" = drop down child html element on mouse over
// position  - "x" = display the child html element to the right
//             "y" = display the child html element below
// cursor    - omit to use default cursor or specify CSS cursor name
// usagetype - "menu" = use child for "hover" and "click" states
//             "display" = do not use child for "hover" and "click" states
//             "noshadow" = no drop shadow
// hide      - "auto" = to hide onmouseout
//             "manual" = use link in child to close
//             "nolink" = no close link
// offsetWidth  - omit for child object to be placed outside of the parent
// offsetHeight - height placement within the parent object
// positiontype - "absolute" = position absolute
//                "relative" = position relative to the parent

function at_attach(parent, child, showtype, position, cursor, usagetype, hide, offsetWidth, offsetHeight, positiontype)
{
  var p = document.getElementById(parent);
  var c = '';

  if (cursor != undefined) p.style.cursor = cursor;
  if (usagetype == undefined) usagetype = "menu";
  if (hide == undefined) hide = "auto";

  // Look for the child element, if not found create it from JavaScript data
  if (document.getElementById(child)) {
    c = document.getElementById(child);
  } else {
    // Skip the first and last events which are just place holders
    for (var bioCnt=1; bioCnt < bioListArray.length; bioCnt++) {
      if ( bioListArray[bioCnt][0] == child ) {
        //alert("Found: "+ child); // Testing
        c = document.createElement('div');
        c.id = child;

        p.parentNode.insertBefore(c,p.nextSibling);
        //alert(c.offsetWidth); // Testing
        //<div id=\"shadow-container\"><div class='shadow1'><div class='shadow2'><div class='shadow3'>

        co = document.createElement('div');
        co.className = 'qvContent ' + bioListArray[bioCnt][1];
        co.innerHTML = bioListArray[bioCnt][2];
        c.appendChild(co);

        c = document.getElementById(child);

      } // if ( bioListArray[bioCnt][0] == child )
    } // for (var bioCnt=1; bioCnt < bioListArray.length; bioCnt++)
  } // if (document.getElementById(child))


  if (usagetype.search(/(noshadow)/i) == -1) {
    // Auto generate a transparent CSS drop shadow
    sa = document.createElement('div');
    sa.className = 'shadow-container';
    c.appendChild(sa);

    s1 = document.createElement('div');
    s1.className = 'shadow1';
    sa.appendChild(s1);

    s2 = document.createElement('div');
    s2.className = 'shadow2';
    s1.appendChild(s2);

    s3 = document.createElement('div');
    s3.className = 'shadow3';
    s2.appendChild(s3);

    s4 = document.createElement('div');
    s4.className = 'shadow4';
    s3.appendChild(s4);

    s5 = document.createElement('div');
    s5.className = 'shadow5';
    s4.appendChild(s5);

    s6 = document.createElement('div');
    s6.className = 'shadow6';
    s5.appendChild(s6);

    so = document.createElement('div');
    so.className = 'container';
    s6.appendChild(so);
  } // if (usagetype.search(/(noshadow)/i) == -1)

  if (hide.search(/(nolink)/i) == -1) {
    cb = document.createElement('a');
    cb.className = 'close-box';
    cb.href = "javascript:at_hide_manual('"+c.id+"');";
    c.appendChild(cb);

    cbi = document.createElement('img');
    cbi.src = 'images/stock-close.gif';
    cb.appendChild(cbi);
  } // if (hide.search(/(nolink)/i) == -1)

  // For Safari and Firefox
  c.style.cssFloat = 'left';
  // For IE6 & 7
  c.style.styleFloat = 'left';

  var currentWidth = c.offsetWidth;
  //var currentHeight = c.offsetHeight;
  var currentHeight = c.offsetHeight;

  // mdavignon 11/21/2008: Specifically setting the width seems to have the
  // best results especially in IE6
  c.style.width = currentWidth +'px';

  // mdavignon 11/21/2008: No idea why we need the -12, but we do
  if (usagetype.search(/(noshadow)/i) == -1) so.style.height = currentHeight - 12 +'px';
  // End of auto generate a transparent CSS drop shadow


  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = position;
  c["at_position"]   = position;
  c["absolute_position"] = "relative";
  if (positiontype == "absolute") c["absolute_position"] = "absolute";

  if (offsetHeight == undefined && offsetHeight == "") offsetHeight = 0;
  if (offsetWidth != undefined && offsetWidth != "") {
    //alert('offsetWidth is set to: ' + offsetWidth); // Testing
    c["at_offsetWidth"] = offsetWidth;
    c["at_offsetHeight"] = offsetHeight;
  }

  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      if (hide.search(/(manual)/i) == -1) p.onmouseout  = at_hide;
      if (usagetype.search(/(display)/i) == -1) {
        c.onmouseover = at_show;
        if (hide.search(/(manual)/i) == -1) c.onmouseout  = at_hide;
      }
      break;
    case "hover":
      p.onmouseover = at_show;
      if (hide.search(/(manual)/i) == -1) p.onmouseout  = at_hide;
      if (usagetype.search(/(display)/i) == -1) {
        c.onmouseover = at_show;
        if (hide.search(/(manual)/i) == -1) c.onmouseout  = at_hide;
      }
      break;
  }
}


/**
 * http://blog.firetree.net/2005/07/04/javascript-find-position/
 */
function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1)
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}


function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

