/*
  Casualties Union Custom Scripts - Version 2.3
  (C)2008-2009 Stuart Aust
  Contents:
    01  Global Variable Definitions
    02  Menu Tab Highlighter Script
    03  Dropdown Menu Handling Functions
		04  Derive Top and Left Position Function
    05  Page Initialisation Script
*/



/*
  [01] Global Variable Definitions
*/
var timerID = null;	 // Initialize the timer identity variable
var timerMenu = null;  // Initialize the timer menu variable



/*
  [02] Menu Highlighter - Version 1.1
  (C)Stuart Aust 2008
  Changes the tab background colour and text colour of a menu tab.
  Required parameters:
    boxTarget - Object to change the background of
    schemeIndex - Reference to the style scheme to use, where 0 is the default page menu tab style
*/
function menuHighlight(boxTarget,schemeIndex){
  switch(schemeIndex){
	  // Red Highlight Tab
	  case 1: boxTarget.style.background = "#ffff00 url(http://www.casualtiesunion.org.uk/images/fade_red.jpg) repeat-x";
		        break; 
	  // Yellow Highlight Tab
	  case 2: boxTarget.style.background = "#ffff00 url(http://www.casualtiesunion.org.uk/images/fade_yellow.jpg) repeat-x";
		        break;
		// Default Grey Menu Tab
	  default: boxTarget.style.background = " #808080 url(http://www.casualtiesunion.org.uk/images/fade_grey.jpg) repeat-x";
	}
}



/*
  [03] Dropdown Menu Handling Function Collection - Version 1.2
  (C)Stuart Aust 2008-2009
	  idSubMenu - works out the numerical id of the submenu associated with the header
	  menuOn - Displays submenu
    subMenuOn - Clears the timeOut if set
		menuOff - Sets a timeout to call the clear function
		clearSubMenu - Hides the submenu and unhighlights the header tab
  Required parameters:
    menuObj - Menu Object triggering the handler function
*/
function idSubMenu(headerMenu){
  var subMenuId = null;
	var menuList = document.getElementById('sitemenu').getElementsByTagName('div');  // Get Menu Header Elements
	for(m=2;m<menuList.length;m++){
	  if(menuList[m].id == headerMenu.id){
		  subMenuId = m - 1;
		}
	}
  return subMenuId;
}

//

function menuOn(menuObj){
  if(timerMenu != null){  // If there is already a menu open then close it
	  clearSubMenu();
		if(timerID != null){  // If the timer is set then clear it
		  clearTimeout(timerID);
			timerID = null;
		}
	}
  menuHighlight(menuObj,2);  // Highlight the Menu Tab
	timerMenu = menuObj.id.valueOf();  // Identify the open menu
  if(idSubMenu(menuObj) != null){  // Detect subMenu and display it
  	subID = 'menu' + idSubMenu(menuObj);
	  document.getElementById(subID).style.visibility = 'visible';  // Display Sub-Menu
	}
}

//

function subMenuOn(){
  if(timerID != null){
	  clearTimeout(timerID);
		timerID = null;
	}
}

//

function menuOff(){
  var delay = 800; // Set delay for menu persistence
  timerID = setTimeout('clearSubMenu();',delay);
}

//

function clearSubMenu(){
  var smObj = document.getElementById(timerMenu);
	(smObj.className == "activesite clickable") ? menuHighlight(smObj,1) : menuHighlight(smObj,0);  // Un-highlight the Menu Tab
  if(idSubMenu(smObj) != null){
  	subID = 'menu' + idSubMenu(smObj);
  	document.getElementById(subID).style.visibility = 'hidden';  // Hide Sub-Menu
	}
	timerMenu = null;  // Nullify the timerMenu pointer
}



/*
  [04] Top and Left Function - Version 1.1 (C)2009 Stuart Aust
	Return the Top (y) or Left (x) position of an object on the rendered page
	depending on the selected return parameter.
	Required Parameters:
	  container - The object to work on
		rParam - Return Parameter: x or y
*/		
function TopLeft(container,rParam){
  var x = y = 0;  // Initialise the x and y co-ordinates
  if(container.offsetParent){  // Check for presence of a parent element
    do{
      x += container.offsetLeft;
			y += container.offsetTop;
		} while (container = container.offsetParent);				
	}
  // DEBUG: alert(x + ' / ' + y);
	if(rParam == 'x'){
  	return x;
	}
	else{
  	return y;
	}
}



/*
  [05] Page Initialisation Script - Version 1.2
  (C)Stuart Aust 2008-2009
  Sets the CSS widths of menu elements to reflect number of sub-menu's present,
  dynamically positions sub-menu dropdowns, and highlights the active site tab
  Required parameters:
    none
*/
function initialisePage(){
	var nOm = document.getElementById('sitemenu').getElementsByTagName('div');  // Get Menu Header Elements
	var mWidth = 100 / nOm.length;  // Set width as a percentage of available space
	for(i=0;i<nOm.length;i++){
	  nOm[i].style.width = mWidth + '%';  // Assign equal widths to all header menus
		if(i>1 && i<(nOm.length - 2)){  // Ignore blanks at beginning and end of header row and home tab
      // Dynamically Position Sub-Menus
      var smId = 'menu' + (i - 1);
		  var subMenu = document.getElementById(smId);
      subMenu.style.width = mWidth + '%';
			subMenu.style.top = TopLeft(nOm[i],'y') + nOm[i].offsetHeight + 1 + 'px';
			subMenu.style.left = TopLeft(nOm[i],'x') + 'px';
		}
  }
	// Detect presence of a Gallery and format to fit page
	var gal = document.getElementById('picturegallery');
	if(gal != null){
    var dW = parseInt(gal.offsetWidth / 4) - 8;  // Calculate image width based on available display area
		var iCount = 1;
		while(document.getElementById('picture'+iCount) != null){
		  document.getElementById('picture'+iCount).style.width = dW + 'px';
		  iCount++;  
		}
	  gal.style.visibility = 'visible';
	}
}
