
// function clipValues
// Returns the clip value designated by 'which'

function clipValues(obj,which) {
	if ((mac) && (is.ie4)) var clipv = obj.clip.split("rect(")[1].split(")")[0].split(" ")
	else var clipv = obj.clip.split("rect(")[1].split(")")[0].split("px")
	if (which=="t") return Number(clipv[0])
	if (which=="r") return Number(clipv[1])
	if (which=="b") return Number(clipv[2])
	if (which=="l") return Number(clipv[3])
}

// popMenu constructor
function popMenu(lyr, child) {
  var ref=this;
  var p;
  
  p = (ns4)? document.layers[lyr] : document.all[lyr];
  p.pstyle = (ns4)? p : p.style;
  p.name = lyr;

  p.tMenu = popMenu.toggleMenu;
  p.slideUp = popMenu.slideUp;
  p.slideDn = popMenu.slideDn;
  p.reWriteMenu = popMenu.reWriteMenu;		// ie-mac bug fix function
  
  ref.layer = p;
  p.ref = ref;
  
  p.height = (ns4) ? p.document.height : parseInt(p.offsetHeight);
  
  if ((p.pstyle.clip.bottom) || ((p.pstyle.clip != "") && (p.pstyle.clip != "auto") )){
 	 p.closedHeight = (ns4) ? p.pstyle.clip.bottom : clipValues(p.pstyle,'b');
 
 	 p.clipTop = (ns4) ? p.pstyle.clip.top : clipValues(p.pstyle,'t');
 	 p.clipBottom = (ns4) ? p.pstyle.clip.bottom : clipValues(p.pstyle, 'b');
 	 p.clipRight = (ns4) ? p.pstyle.clip.right : clipValues(p.pstyle, 'r');
 	 p.clipLeft = (ns4) ? p.pstyle.clip.left : clipValues(p.pstyle, 'l');
  }
  
  p.visibleheight = (p.clipBottom) ? p.clipBottom : p.height;
  
  if (child) p.child = (ns4) ? document.layers[child] : document.all[child];
  if (child) {
  	if (ns4) p.child.top = p.top + p.visibleheight + 2;
  	else p.child.style.top = p.pstyle.pixelTop + p.visibleheight + 2;
  }
  p.isOpen = false;		// is the menu open?
  p.pstyle.visibility = "visible";
}

// popMenu.toggleMenu
// Toggles a popMenu open or closed
popMenu.toggleMenu = function () {
	if ((is.ie401) && (is.mac)) return;
	var increment = 10;
	var lyr = this.ref.layer;
	if (lyr.isOpen == false) {
		if (lyr.clipBottom < lyr.height) {
			if (ns4) {
				lyr.clipBottom += increment;
				lyr.clip.bottom = lyr.clipBottom;
			}
			else {
				lyr.clipBottom += increment;
				lyr.style.clip = "rect("+lyr.clipTop+"px "+lyr.clipRight+"px "+lyr.clipBottom+"px "+lyr.clipLeft+"px)";
				if (mac) {lyr.style.pixelTop += 1; lyr.style.pixelTop -= 1;}
			}
			if (lyr.child) lyr.child.slideDn();
			if (ns4) setTimeout("document.layers[\'"+lyr.name+"\'].tMenu();",30);
			else setTimeout("document.all[\'"+lyr.name+"\'].tMenu();",30);
		}
		else {
			lyr.isOpen = true;
			if ((lyr.child) && (mac) && (ie)) { blurForms(); lyr.child.reWriteMenu();}	
			return;
		}
	}
	if (lyr.isOpen == true) {
		if (lyr.clipBottom > lyr.closedHeight) {
			if (ns4) {
				lyr.clipBottom -= increment;
				lyr.clip.bottom = lyr.clipBottom;
			}
			else {
				lyr.clipBottom -= increment;
				lyr.style.clip = "rect("+lyr.clipTop+"px "+lyr.clipRight+"px "+lyr.clipBottom+"px "+lyr.clipLeft+"px)";
				if (mac) {lyr.style.pixelTop += 1; lyr.style.pixelTop -= 1;}
			}
			if (lyr.child) lyr.child.slideUp();
			if (ns4) setTimeout("document.layers[\'"+lyr.name+"\'].tMenu();",30);
			else setTimeout("document.all[\'"+lyr.name+"\'].tMenu();",30);
		}
		else {
			lyr.isOpen = false;
			if ((lyr.child) && (mac) && (ie)) { blurForms(); lyr.child.reWriteMenu();}
			return;
		}
	}
}

// popMenu.slideUp
// Slides the popMenu object up 10 pixels
popMenu.slideUp = function () {
	var increment = 10;
	var lyr = this.ref.layer;
	lyr.pstyle.top = parseInt(lyr.pstyle.top) - increment;
	if (lyr.child) lyr.child.slideUp();
}

// popMenu.slideDn
// Slides the popMenu object down 10 pixels
popMenu.slideDn = function () {
	var increment = 10;
	var lyr = this.ref.layer;
	lyr.pstyle.top = parseInt(lyr.pstyle.top) + increment;
	if (lyr.child) lyr.child.slideDn();
}

// function reWriteMenu
// A function to fix an ie-mac problem where form fields don't 
// update correctly on a layer that changes it's absolute position.
popMenu.reWriteMenu = function () {
	var lyr = this.ref.layer;
	lyr.innerHTML = lyr.innerHTML;
	if (lyr.child) lyr.child.reWriteMenu();
}

function blurForms() {
	for (var i=0; i < document.forms.length; i++) {
		for (var j=0; j < document.forms[i].elements.length; j++) {
			document.forms[i].elements[j].blur();
		}
	}
}