
/* Browser check vars */
var is_ie, is_ie5, is_opera, is_khtml;

is_ie = ( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) );

is_ie5 = ( is_ie && /msie 5\.0/i.test(navigator.userAgent) );

// detect Opera browser
is_opera = /opera/i.test(navigator.userAgent);

// detect KHTML-based browsers
is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);

//get scroll coord
function MeMSOGetScrollPosition() {
   var coordinates = new Object();
	var curposx = 0, curposy = 0;

   if (typeof(window.pageXOffset) == 'number') {
      // Netscape compliant
      curposx = window.pageXOffset;
      curposy = window.pageYOffset;

   } else {
      if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
         // DOM compliant
         curposx = document.body.scrollLeft;
         curposy = document.body.scrollTop;

      } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
         // IE6 standards compliant mode
         curposx = document.documentElement.scrollLeft;
         curposy = document.documentElement.scrollTop;

      }

   }

   coordinates.x = curposx;
   coordinates.y = curposy;
	return coordinates;
}

function getMousePosition(e) {
   var coordinates = new Object();
	var mouseX = 0, mouseY = 0;
	var scrolldata;
	
	if (is_ie) {
		scrolldata = MeMSOGetScrollPosition();
		mouseY = event.clientY + scrolldata.y;
		mouseX = event.clientX + scrolldata.x;

	} else {
		mouseY = parseInt(e.pageY);
		mouseX = parseInt(e.pageX);

	}

   coordinates.x = mouseX;
   coordinates.y = mouseY;
	return coordinates;
}


//courtesy of http://www.quirksmode.org/js/findpos.html
function 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;
}

//courtesy of http://www.quirksmode.org/js/findpos.html
function 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;
}

function getElementHeight(Elem) {
   var height;
   height = 0;

	//if (ns4) {
	//	var elem = getObjNN4(document, Elem);
	//	return elem.clip.height;
	//} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		//if (op5) { 
		//	height = elem.style.pixelHeight;
		//} else {
			height = elem.offsetHeight;
		//}
		return height;
	//} 
}

function getElementWidth(Elem) {
   var width;
   width = 0;
   
	//if (ns4) {
	//	var elem = getObjNN4(document, Elem);
	//	return elem.clip.width;
	//} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		//if (op5) {
		//	width = elem.style.pixelWidth;
		//} else {
			width = elem.offsetWidth;
		//}
		return width;
	//}
}


/* Page justify constants */
var isPageLeft, isPageCenter, isPageRight;
isPageLeft = 1;
isPageCenter = 2;
isPageRight = 3;
var isPopupTopLeft, isPopupTopRight, isPopupLowerLeft, isPopupLowerRight, isPopupCenter, isPopupPositioned;
isPopupTopLeft = 1;
isPopupTopRight = 2;
isPopupLowerLeft = 3;
isPopupLowerRight = 4;
isPopupCenter = 5;
isPopupPositioned = 6;

/* Mouse vars */
var pageAlign = null;
var popupAlign = null;
var isMouseIn = false;
var curPopupID = null;
var curPopupAdjX = null;
var curPopupX = null;
var curPopupY = null;
var curPopupWidth = null;
var curPopupHeight = null;

function divPopups_OnMouseOver() {
   isMouseIn = true;
}

function divPopups_OnMouseOut() {
   isMouseIn = false;
}

function setupInpagePopups(setPageAlign, setPopupAlign) {
   if (setPageAlign != '') {
      pageAlign = setPageAlign;
   } else {
      pageAlign = isPageLeft;
   }
   if (setPopupAlign != '') {
      popupAlign = setPopupAlign;
   } else {
      popupAlign = isPopupLowerRight;
   }
}

function checkInpagePopups_OnClick(e) {
   if (curPopupID != null) {
      var curPopupName = 'divPopup' + curPopupID;     //get name of current popup div
      var isInDiv = false;    //keeps track if click target found in current popup div
      var testNode;     //current node being tested
      
      if (e.srcElement == null) {
         //for netscape
         testNode = e.target;
      } else {
         //for ie
         testNode = e.srcElement;
      }
      
      while ((testNode != null) && !(isInDiv)) {
         //search backward from click's target
         if (testNode.id == curPopupName) {
            //found! current target resides in current popup
            isInDiv = true;
         }
         if (testNode != null) {
            //if current node's parent is null, stop search
            testNode = testNode.parentNode;
         }
      }
      
      if (!isInDiv) {
         //if click outside current div, close it
	      inpagePopupCloseByID(curPopupID);  
      }
   }
}

function moveInpagePopups_OnResize() {
   var mouseX, pageWidth;
   
   if (curPopupID != null) {
      //Set adjustments, for use on resize to set "relative" position
      //Get page width
	   if (is_ie) {
         pageWidth = document.body.clientWidth;
	   } else {
         pageWidth = window.innerWidth;
      }
      //Get adjustment
      if (pageAlign == isPageLeft) {
         mouseX = curPopupAdjX;
      } else if (pageAlign == isPageCenter) {
         mouseX = ((pageWidth / 2) * (curPopupAdjX - 1)) / curPopupAdjX;
      } else if (pageAlign == isPageRight) {
         mouseX = pageWidth - curPopupAdjX;
	   }

	   divPopup = document.getElementById('divPopup' + curPopupID);  
    	curPopupX = (mouseX - 5);
      //move popup based on mouseclick and where how it is aligned
      switch(popupAlign)
      {
         case isPopupCenter:
            divPopup.style.left = (mouseX - (curPopupWidth / 2))+ 'px';
            break;
         case isPopupTopLeft:
            divPopup.style.left = (mouseX - curPopupWidth)+ 'px';
            break;
         case isPopupTopRight:
            divPopup.style.left = (mouseX - 5)+ 'px';
            break;
         case isPopupLowerLeft:
            divPopup.style.left = (mouseX - curPopupWidth)+ 'px';
            break;
         default:
         //case isPopupLowerRight:
            divPopup.style.left = (mouseX - 5)+ 'px';
            break;
      }
	   //divPopup.style.top = (mouseY - 5) + 'px';
   }
}


function inpagePopupOpen(me, e, id, parentObject) {
   var divPopup, mousedata, mouseX, mouseY, pageWidth;
   
   if (curPopupID != null) {
      inpagePopupCloseByID(curPopupID);
   }
   
   //Setup popup div
	divPopup = document.getElementById('divPopup' + id);  
	
	//Get mouse's X and Y coordinates
	mousedata = getMousePosition(e);
	mouseX = mousedata.x;
	mouseY = mousedata.y;

   //Set adjustments, for use on resize to set "relative" position
   //Get page width
	if (is_ie) {
      pageWidth = document.body.clientWidth;
	} else {
      pageWidth = window.innerWidth;
   }
   //Get adjustment
   if (pageAlign == isPageLeft) {
      curPopupAdjX = mouseX;
   } else if (pageAlign == isPageCenter) {
      curPopupAdjX = (pageWidth / 2) / ((pageWidth / 2) - mouseX);
   } else if (pageAlign == isPageRight) {
      curPopupAdjX = (pageWidth - mouseX);
	}
	//Move popup div to click spot
	divPopup.style.visibility = 'visible';
	divPopup.onmouseover = divPopups_OnMouseOver;
	divPopup.onmouseout = divPopups_OnMouseOut;
   //Get popup width
	if (is_ie) {
	   curPopupWidth = divPopup.offsetWidth;
	   curPopupHeight = divPopup.offsetHeight;
	} else {
	   curPopupWidth = divPopup.offsetWidth;
	   curPopupHeight = divPopup.offsetHeight;
   }

   //move popup based on mouseclick and where how it is aligned
   switch(popupAlign)
   {
      case isPopupCenter:
         divPopup.style.left = (mouseX - (curPopupWidth / 2))+ 'px';
         divPopup.style.top = (mouseY - (curPopupHeight / 2)) + 'px';
         break;
      case isPopupTopLeft:
         divPopup.style.left = (mouseX - curPopupWidth)+ 'px';
         divPopup.style.top = (mouseY - curPopupHeight) + 'px';
         break;
      case isPopupTopRight:
         divPopup.style.left = (mouseX - 5)+ 'px';
         divPopup.style.top = (mouseY - curPopupHeight) + 'px';
         break;
      case isPopupLowerLeft:
         divPopup.style.left = (mouseX - curPopupWidth)+ 'px';
         divPopup.style.top = (mouseY - 5) + 'px';
         break;
      default:
      //case isPopupLowerRight:
         if (parentObject == null) {
            divPopup.style.left = (mouseX - 5)+ 'px';
            divPopup.style.top = (mouseY - 5) + 'px';
         } else {
            //get object to popup the menu below
            var obj = document.getElementById(parentObject);
            
            divPopup.style.left = findPosX(obj) + 'px';
            divPopup.style.top = (findPosY(obj) + getElementHeight(parentObject)) + 'px';
         }
         break;
   }
	curPopupX = (mouseX - 5);
	curPopupY = (mouseY - 5);

   isMouseIn = true;
	curPopupID = id;
   e.cancelBubble = true;
};


function inpagePopupCloseByID(id) {
   var divPopup;
	divPopup = document.getElementById('divPopup' + id);  
	divPopup.style.visibility = 'hidden';
	divPopup.onmouseover = null;
	divPopup.onmouseout = null;
   isMouseIn = false;
	curPopupID = null;
   curPopupAdjX = null;
};

function inpagePopupCloseByVar(divPopup) {
	divPopup.style.visibility = 'hidden';
	divPopup.onmouseover = null;
	divPopup.onmouseout = null;
   isMouseIn = false;
	curPopupID = null;
   curPopupAdjX = null;
};
