// 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 *********************************************************

// ***** at_show_aux *****

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );
  var parentWidth = p.offsetWidth;
  
  var top  = (c["at_position"] == "y") ? p.offsetHeight : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth  : 0;

  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;
  }

  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
  c.style.width = parentWidth + "px";

}

// ***** at_show *****

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);
  var a = document.getElementById(this["at_current"]);
  if (a) { a.style.visibility = 'hidden'; }
  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" ]);
  var a = document.getElementById(this["at_current"]);
  if (a && a.style.visibility == "hidden") { at_show_aux(p.id, a.id); }
  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 222);
}

function at_hide_current() {
  var a = document.getElementById(this["at_current"]);
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 222);
  if (a && a.style.visibility == "hidden") { at_show_aux(p.id, a.id); }

 // alert("at_hide_current done!");
}

// ***** 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

function at_attach(parent, child, active, showtype, position, cursor)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  var a = document.getElementById(active);

  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.style.position   = "absolute";
  c.style.visibility = "hidden";
 
  if (a) {
    p["at_current"]    = a.id;
    c["at_current"]    = a.id;
    a["at_position"] = position;
    
    a.style.position = "absolute";
  	a.style.visibility = "hidden";
  }

  if (cursor != undefined) p.style.cursor = cursor;

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
    case "hover":
      p.onmouseover = at_show;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide_current;
      break;
  }
}

function at_stretch(parent, child, current, itemCount)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );
  var a = document.getElementById(current);
  var parentWidth = p.offsetWidth;
  var pn = p.parentNode;
  
  var menuNodes = filterNodes(pn, 'menu_parent');
  var menuSize = menuWidth(menuNodes);

  var totalSize = 550;
  var marginLeft = 22;
  var paddingLeft = 10;

  var marginOffset = 4 * (itemCount -1);
  var availableSize = totalSize - marginLeft - marginOffset;
  var remainingSize = availableSize - menuSize;
	//alert("Freie Pixel: " + (availableSize - menuSize));
 
  var menuSizeButLast = menuWidthButLast(menuNodes, 1);
  var marginRight4LastItem = menuSizeButLast + marginOffset;
 
  var newPadding = remainingSize + paddingLeft;
  // auf nummer sicher gehen und gegebenfalls 1px verschiebung akzeptieren
  newPadding -= 1;
  var addOne = true;
  if (navigator.appVersion.indexOf("Mac")!=-1) addOne = false;
  
  if (addOne) {
  	newPadding ++;
  }
  
  if (p == null) return;
  var inner = searchClass(p, 'inner');
  
 // alert("inner classname: " + inner.className);
  inner.style.paddingLeft = newPadding + 'px';
  //p.style.marginLeft = '0px';

  //p.style.paddingLeft = newPadding +8 +'px';
  setPaddingMenuChild(p.nextSibling, newPadding);
  
  if (a == null) return;
  // set padding of inner of current 
  searchClass(a, 'inner').style.paddingLeft = newPadding + 'px';
   
}


// Funktion searchClass liefert das erste element aus den Kinderknoten von
// parent welches der Klasse className angehört!
function searchClass(parent, className) {
	// alert("searching class: " + className + " in " + parent.id);
	var childNodes = parent.childNodes;
	for (i = 0; i < childNodes.length; i++) {
		var child = childNodes[i];
		//alert ("child: " + child.className);
		if (!child.className) continue;
		if (child.className == className) {
		 return child;
		}
	}
}

// Liefert die Größe in Pixel des Menues
function menuWidth(menuNodes) {
	var sizeX = 0;
	for (i=0; i < menuNodes.length; i++)
		sizeX += menuNodes[i].offsetWidth;
	return sizeX;
}

function menuWidthButLast(menuNodes, minusLast) {
	var sizeX = 0;
	for (i = 0; i < (menuNodes.length - minusLast); i++) {	
		if (!menuNodes[i].id) continue;
		//alert(i + " - ID: " + menuNodes[i].id + " - size: " + menuNodes[i].offsetWidth);
		sizeX += menuNodes[i].offsetWidth;
	}

	return sizeX;
}

function printChildNodes(father) {
	alert("printing childnodes of: " + father.id);
	var s = "";
	for (i=0; i < father.childNodes.length; ++i) {
		var child = father.childNodes[i];
		if (!child.id) continue;
		s+= child.id + ", ";
	}

	alert("Nodes: " + s);
}

// Setzt das Padding des Links ausgerichteten menu_child (A Element)
function setPaddingMenuChild(menuChild, paddingLeft) {
	while (!menuChild.id) menuChild = menuChild.nextSibling;
	var value = menuChild.id.substring(10,0);
	if (value == "menu_child") {
		var childNodes = menuChild.childNodes;
		var size = childNodes.length;
		for (i = 0; i < size; i++) {
			var a = childNodes[i];
			if (a.nodeName == 'A') {
				a.style.paddingLeft = paddingLeft + 'px';
			}
		}		
	}
}

// Funktion filterNodes(parent, idPrefix) liefert eine Liste
// von Knoten aus den direkten Kinderknoten von parent zurück,
// welche eine ID besitzen die mit idPrefix beginnen!
function filterNodes(parent, idPrefix) {
	var nodeList = parent.childNodes;
	var size = nodeList.length;
	var retVal = new Array();
	var arrayPointer = 0;
	for (i = 0; i < size; ++i) {
		var child = nodeList[i];
		if (!child.id) continue;
		var value = child.id.substring(idPrefix.length, 0);
		if (value == idPrefix) {
			retVal[arrayPointer++] = child;
		}
	}
	return retVal;
}

function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

