/**
 * DocuShare Dropdown Action Menu ver.2
 * @author jdorsey
 */

// Global variables

	var curArrowBtn = null; // variable for active menu (if any)
	var rootPath = null; //retrieves the docushare absolute path

// ---------------------------------------------
// Detect if browser is I.E, and if it is 6 or 7
// ---------------------------------------------

function browserTypeChecker() {
  this.IE = false; //functions that just need to know if the browser is I.E.
	this.IE6 = false; //functions for IE6 alone
	this.IE7 = false; //functions for IE7 alone
	 
  if (document.compatMode && document.all && !window.opera) {
    this.IE = true;
    if(document.documentElement && typeof document.documentElement.style.maxHeight!='undefined'){
			this.IE7 = true;
			return;
		}
		this.IE6 = true;
		return;
  }
}
var detectIE = new browserTypeChecker();

// ----------------------
// Detect document events
// ----------------------

		if (detectIE.IE){
  			document.onmousedown = handleMousedown;
				document.onmouseup = handleMouseup;
		}else{
  			document.addEventListener("mousedown", handleMousedown, true);
				document.addEventListener("mouseup", handleMouseup, true);
		}	
		// Handles onmousedown events anywhere on the page (except menu buttons)
		// to turn off action menus
		function handleMousedown(event)
		{
			var clickedpageElement;
			if(curArrowBtn == null){return;}
			
			if(detectIE.IE){
				clickedpageElement = window.event.srcElement;
			}else{
				clickedpageElement = event.target;
			}
			if(clickedpageElement == curArrowBtn){
				return;
			}
			curArrowBtnPopup = getSibling(curArrowBtn);
			if(
			clickedpageElement.parentNode.parentNode.className != 'popup' &&
			clickedpageElement.parentNode.className != 'popup' && 
			clickedpageElement.className != 'popup')
			{
				if(curArrowBtn.parentNode.parentNode.parentNode.parentNode.id != 'pageheader_wrapper' ||
				curArrowBtn.parentNode.parentNode.parentNode.className != 'actionbar_wiki'){
					curArrowBtn.parentNode.parentNode.parentNode.parentNode.className = ''; //turn off collection row hilight
					}
				curArrowBtn.src = rootPath + 'images/action_arrow.gif';
				curArrowBtnPopup.style.display = 'none';
				curArrowBtn = null;
				

			}
		}
  	
		// When an action is clicked on in the menu, shut the menu onmouseup
		function handleMouseup(event){
			var clickedpageElement;
			if(curArrowBtn == null){return;}
			
			if(detectIE.IE){
				clickedpageElement = window.event.srcElement;
			}else{
				clickedpageElement = event.target;
			}
			if(clickedpageElement == curArrowBtn){
				return;
			}
		
		if(
			clickedpageElement.parentNode.parentNode.className == 'popup' &&
			clickedpageElement.nodeName == 'A')
			{
				curArrowBtn.parentNode.parentNode.parentNode.parentNode.className = ''; //turn off collection row hilight
				curArrowBtn.src = rootPath + 'images/action_arrow.gif';
				curArrowBtnPopup.style.display = 'none';
				curArrowBtn = null;
			} 
		
		}
			
  
// ----------------------------
// Menu functions
// ----------------------------		

		function openActionMenu(event, clickedButton, path)
		{	
			var currentButton;
			rootPath = path;
			if (detectIE.IE){
   				currentButton = window.event.srcElement;
  			}else{
    			currentButton = event.currentTarget;
			}
			
			if(curArrowBtn != null){
				var row = curArrowBtn.parentNode.parentNode.parentNode.parentNode;
				row.className = 'backgroundTab';
				curArrowBtn.src = path + 'images/action_arrow.gif';
				curArrowBtnPopup = getSibling(curArrowBtn);
				curArrowBtnPopup.style.display = 'none';
				curArrowBtn = currentButton;
			}else{
				curArrowBtn = currentButton;
			}
			
			var popup = getSibling(currentButton);
			var row = currentButton.parentNode.parentNode.parentNode.parentNode;
			var container = currentButton.parentNode.parentNode.parentNode.className; //detect if container is a wiki page header
			currentButton.src = path + 'images/action_arrow_over.gif';
			currentButton.blur();
			popup.style.display = 'block';
			if(row.id != 'pageheader_wrapper' && container != 'actionbar_wiki'){//only hilight if in a collection
				row.className = 'currentTab';
			}
			var wHeight = document.body.offsetHeight;
			if(detectIE.IE){
				wHeight = wHeight -(wHeight - document.documentElement.clientHeight);
			}else{
				wHeight = wHeight -(wHeight - window.innerHeight);
			}
			pHeight = popup.offsetHeight;
			
			//find the x coordinate of the arrow button
			var bLocation_y = findPos(currentButton);
			var bLocation_x = findPos_left(currentButton) + currentButton.offsetWidth;
			var bodyWidth = document.body.clientWidth;
			var buttonRightEdge = (bodyWidth - bLocation_x) - 8;
			
			//find the y coordinate of the arrow button
			var pBottom = bLocation_y + pHeight;
			
			//if the pop-up goes off the top of the screen, fudge this
			var pUpHeight; 
			if(detectIE.IE){
				pUpHeight = pHeight - 5; // IF I.E, Give less spacing to the popup menu when it is above the arrow button
			}else{
				pUpHeight = pHeight + 18; //Give more spacing to the popup menu when it is above the arrow button
			}
			
			var pDistancetoBottom = wHeight - bLocation_y;
			//alert(wHeight + ' - ' + bLocation_y + ' = ' + pDistancetoBottom);
			
			var menuOffset = 0;
			if(detectIE.IE){
					menuOffset = (pHeight - pDistancetoBottom) - 5;
				}else{
					menuOffset = (pHeight - pDistancetoBottom) + 15;
				}
				
			if(pBottom > wHeight){
				//if the menu drops below the screen - appear to left of arrow
				
				if(pDistancetoBottom < (pHeight + 10)){
					popup.style.marginTop= '-' + menuOffset + 'px';
					popup.style.right= (buttonRightEdge + 25) + 'px';
					popup.style.borderTop= '1px solid #4269b1';
					popup.style.borderBottom= '1px solid #4269b1';
					popup.style.borderRight= '2px solid #e18336';
 
				}else{
				//if menu drops above the screen - appear to left of arrow too
					popup.style.marginTop= '-' + pUpHeight + 'px';
					popup.style.right= buttonRightEdge + 'px';
					popup.style.borderTop= '1px solid #4269b1';
					popup.style.borderBottom= '2px solid  #e18336';
					popup.style.borderRight= '1px solid #4269b1';
				}
			}
			// otherwise, just popup below the arrow
			else if(pBottom < wHeight){
				if(detectIE.IE7){popup.style.marginTop = '15px';}else{popup.style.marginTop = '';}
				popup.style.right= buttonRightEdge + 'px';
				popup.style.borderTop= '2px solid #e18336';
				popup.style.borderBottom= '1px solid #4269b1';
				popup.style.borderRight= '1px solid #4269b1';
			}
		}

		// If we encounter whitespace in Firefox,(Dom node type 1), then move to the next
		// node in the DOM tree.
		function getSibling(node)
		{
			nextChild = node.nextSibling;
			while(nextChild.nodeType != 1)
			{
				nextChild = nextChild.nextSibling;
			}
			return nextChild;
		}
		
		function findPos(obj) {
			var curtop = 0;
			if (obj.offsetParent) {
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) {
					curtop += obj.offsetTop
				}
			}
			//find the height of the hidden scrolled area at top
			var scrollTopAmt = document.documentElement.scrollTop; //document.body.scrollTop;

			curtop = curtop - scrollTopAmt; //Get the y cord (take into accout scrolling region and window size) ;
			curbottom = curtop + 10; //Now looking for the bottom y cord of button (add height of arrow button)
			
			return curbottom;
		}
	
	function findPos_left(obj) {
			var curleft = 0;
			if (obj.offsetParent) {
				curleft = obj.offsetLeft
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft
				}
			}
			return curleft;
	}
