/*
 Flyout Code

 Alex Fahey - 0419 47 9898

*/

var adjustTop = 0;                   // Set if flyout top not aligned
var adjustLeft = 0;                  // Set if flyout not aligned on right side of menu bar
var flyoutMenu = null;               // Selected menu reference
var highlighted = null;              // The top level menu item selected
var offsetX = 0;                     // Flyout left offset
var offsetY = 0;                     // Flyout top offset
var timeout = null;                  // Reference to timeout event

/*
Calculators the X and Y coordination offsets for the flyout
from the root of the document xml tree.
*/
function calculateOffsets(obj)
{
  var tagObject = obj;

  offsetY = adjustTop;
  offsetX = obj.offsetWidth;

  while (tagObject) // Calculate x,y offsets
  {
    offsetY = offsetY + tagObject.offsetTop;
    offsetX = offsetX + tagObject.offsetLeft;
    tagObject = tagObject.offsetParent;
  }

  // Perform adjustments
  offsetY += adjustTop;
  offsetX += adjustLeft;
}

/*
Cancels the current window timeout event
*/
function cancelTimeout()
{
  window.clearTimeout(timeout);
  timeout = null; // Remove reference
}

/*
Displays the flyout menu at the specified X and Y coordination
*/
function display(xPos, yPos, stayOpen)
{
  flyoutMenu.style.display = "block";
  flyoutMenu.style.visibility = "visible";

  flyoutMenu.style.left = xPos + "px";
  flyoutMenu.style.top =  yPos + "px";

  // turn highlight on
  highLightOn();
}

/*
Hides the currently displayed flyout menu
*/
function hide()
{
  if((flyoutMenu != null) && (flyoutMenu != undefined))
  {
    flyoutMenu.style.display="none";
    flyoutMenu.style.visibility="hidden";

    // Reset offsets to zero
    flyoutMenu.style.left =  0 + "px";
    flyoutMenu.style.top = 0 + "px";

    // Reset references
    flyoutMeanu = null;

    // turn highlight off
    highLightOff();
  }
}

/*
Starts the display of flyout menu by determining the select menu
*/
function mouseOver(group)
{
  if(!(typeof(group) == "string")) // guard
  {
    // The group must be a string value of the id attribute of the menu group DIV tag
    return;
  }

  var stayOpen = false;
  var group = document.getElementById(group) // Get the DIV menu_group

  if((flyoutMenu != null) && (flyoutMenu.parentNode.id == group.id))
  {
    stayOpen = true;
    cancelTimeout();
  }
  else // Menu group selected
  {
    if((flyoutMenu != null) && (flyoutMenu != undefined))
    {
      // A flyout menu currently open
      cancelTimeout();
      hide(); // Hide now
    }

    // Get the first unordered list in the menu_group DIV tag
    flyoutMenu = group.getElementsByTagName("ul")[0];

    // Calculate the x,y position from the menu_grop
    calculateOffsets(group);

    // Get the first A child tag of the DIV within the menu_group
    highlighted = group.getElementsByTagName("div")[0].firstChild;
  }

  if((flyoutMenu != null) && (flyoutMenu != undefined))
  {
    display(offsetX, offsetY, stayOpen); // display the flyout
  }
}

/*
Closes the currently display flyout menu in a delayed time period
*/
function shut()
{
  if((flyoutMenu != null) && (flyoutMenu != undefined))
  {
    timeout = window.setTimeout("hide();", 400);
  }
}

function highLightOff()
{
	if(highlighted.parentNode.id != "selected")
	{
		//Restore original colours if item is not selected
		highlighted.style.backgroundColor = '#FFFFFF';
		highlighted.style.color = "#000000";
	}
}

function highLightOn()
{
  // Set highlight colours
  highlighted.style.backgroundColor = '#FF0000';
  highlighted.style.color = "#FFFFFF";
}


// More Options  over functions
/*
Starts the display of flyout menu by determining the select menu
*/
function showMoreOptions(group)
{
  if(!(typeof(group) == "string")) // guard
  {
    // The group must be a string value of the id attribute of the more_option_list DIV tag
    return;
  }

  var stayOpen = false;
  var group = document.getElementById(group) // Get the DIV menu_group

//  if((flyoutMenu != null)) //  && (flyoutMenu.parentNode.id == group.id)
//  {
//    stayOpen = true;
//    cancelTimeout();
//  } else {// Menu group selected  
//    if((flyoutMenu != null) && (flyoutMenu != undefined))
//    {
//      // A flyout menu currently open
//      cancelTimeout();
//      hide(); // Hide now
//    }

//    // Get the first unordered list in the menu_group DIV tag
    flyoutMenu = group.getElementsByTagName("ul")[0];

    // Calculate the x,y position from the menu_grop
    calculateMoreOptionsOffsets(group);

    // Get the first A child tag of the DIV within the menu_group
    //highlighted = group.getElementsByTagName("div")[0].firstChild;
//  }

//  if((flyoutMenu != null) && (flyoutMenu != undefined))
//  {
    displayMoreOptions(offsetX, offsetY, stayOpen); // display the flyout
//  }
}

/*
Calculators the X and Y coordination offsets for the flyout
from the root of the document xml tree.
*/
function calculateMoreOptionsOffsets(obj)
{
  var tagObject = obj;
  tagObject = tagObject.offsetParent;

  offsetY = 0;
  //offsetX = obj.offsetWidth;
  offsetX = obj.offsetWidth;

  while (tagObject) // Calculate x,y offsets
  {
    offsetY = offsetY + tagObject.offsetTop;
    offsetX = offsetX + tagObject.offsetLeft;
    tagObject = tagObject.offsetParent;
  }

  // Perform adjustments
  offsetY += 18;
  offsetX += -180;
}

/*
Displays the flyout menu at the specified X and Y coordination
*/
function displayMoreOptions(xPos, yPos, stayOpen)
{
  flyoutMenu.style.display = "block";
  flyoutMenu.style.visibility = "visible";

  flyoutMenu.style.left = xPos + "px";
  flyoutMenu.style.top =  yPos + "px";

}

function shutMoreOptions()
{
  if((flyoutMenu != null) && (flyoutMenu != undefined))
  {
    //timeout = window.setTimeout("hide();", 400);
  }
}

/*
############################# Code for more options ##############################
*/

var boxMoreOptions = null;               // Selected menu reference
var startMoreOptionsX = -180;                     // MoreOptoins left offset
var startMoreOptionsY = 18;                     // MoreOptoins top offset

/*
Starts the display of More Options Box by determining the select page element
*/
function showMoreOptions(group)
{
	if(!(typeof(group) == "string")) // guard
	{
		// The group must be a string value of the id attribute of the more_option_list DIV tag
		return;
	}
	// Get the DIV more_option_list
	var group = document.getElementById(group) 

	//Only procced if we have found the More Options Box
	if((group != null) && (group != undefined))
	{
		if((boxMoreOptions != null) && (boxMoreOptions.parentNode.id == group.id))
		{
			cancelMoreOptionsTimeout();
		} else {
			if((boxMoreOptions != null) && (boxMoreOptions != undefined))
			{
				// Another More Options Box is visable so we need to hide it
				cancelMoreOptionsTimeout();
				hideMoreOptions(); // Hide now
			}
	
			// Get the first unordered list in the more_option_list DIV tag
			boxMoreOptions = group.getElementsByTagName("ul")[0];
	
			// Calculate the x,y position from the More Options Box
			calculateMoreOptionsOffsets(group);
		}

		//Only proceed if we have found someting to show
		if((boxMoreOptions != null) && (boxMoreOptions != undefined))
		{
			displayMoreOptions(offsetMoreOptionsX, offsetMoreOptionsY); // display the flyout
		}
	}
}
/*
Calculator the X and Y coordination offsets for the More Options Box
from the root of the document xml tree.
*/
function calculateMoreOptionsOffsets(obj)
{
	var tagObject = obj;
	tagObject = tagObject.offsetParent;

	//Set up the inital offset values
	offsetMoreOptionsY = startMoreOptionsY;
	offsetMoreOptionsX = startMoreOptionsX + obj.offsetWidth;

	while (tagObject) // Calculate x,y offsets
	{
		offsetMoreOptionsY = offsetMoreOptionsY + tagObject.offsetTop;
		offsetMoreOptionsX = offsetMoreOptionsX + tagObject.offsetLeft;
		tagObject = tagObject.offsetParent;
	}
}

/*
Displays the More Options Box at the specified X and Y coordination
*/
function displayMoreOptions(xPos, yPos)
{
	boxMoreOptions.style.display = "block";
	boxMoreOptions.style.visibility = "visible";

	boxMoreOptions.style.left = xPos + "px";
	boxMoreOptions.style.top =  yPos + "px";

}

/*
Hides the More Options Box
*/
function shutMoreOptions()
{
	if((boxMoreOptions != null) && (boxMoreOptions != undefined))
	{
		timeout = window.setTimeout("hideMoreOptions();", 400);
	}
}

/*
Hides the currently displayed More Options Box
*/
function hideMoreOptions()
{
	if((boxMoreOptions != null) && (boxMoreOptions != undefined))
	{
		boxMoreOptions.style.display="none";
		boxMoreOptions.style.visibility="hidden";

		// Reset offsets to zero
		boxMoreOptions.style.left =  0 + "px";
		boxMoreOptions.style.top = 0 + "px";

		// Reset references
		boxMoreOptions = null;
	}
}

/*
Cancels the current window timeout event
*/
function cancelMoreOptionsTimeout()
{
	window.clearTimeout(timeout);
	timeout = null; // Remove reference
}
