window.onload = initNav;

// m: function getObj(obj)
//    Global
//		References and obj

function getObj(obj){
    if(typeof obj == "string"){
        obj= document.getElementById(obj);
    }
    return obj;
}

function showObj(obj){
	getObj(obj).style.display="";
}
function hideObj(obj){
	getObj(obj).style.display="none";
}

//-----------------------------------------------------------------------------------
//TOP NAVIGATION start

var currentSection;
var timeoutNavitem;

function NavItem(id, fgImg, bgImg)
{
  this.id = id;
  this.fgImg = fgImg;
  this.bgImg = bgImg;
}


function getMenuBGImg(id)
{
  // arrNavItem is defined in MainMenu.xsl
  for(var i = 0; i < arrNavItem.length; i++)
  {
    if(arrNavItem[i].id == id)
      return arrNavItem[i].bgImg;
  }
}

function initNav(){

	// Some pages don't have the menu
	if (document.getElementById("nav-main") == null)
		return;

    if((document.getElementsByTagName("body")[0].getAttribute("id")) && document.getElementsByTagName("body")[0].className != "non-section"){
        currentSection = document.getElementsByTagName("body")[0].getAttribute("id");
        if(currentSection){
            hilite(currentSection, null);
        }
    }
}

function hilite(section, subSection){
    clearTimeout(timeoutNavitem);
    hideAll();
    
    // Highlight the menu section
    var sectionImgObj = document.getElementById("navitem-img-" + section);
    if(sectionImgObj != null)
      sectionImgObj.src = getMenuBGImg(section);
    
    // Show the menu sub section and highlight the one which corresponds to the
    // current page
    var subSectionObj = document.getElementById("navitem-" + section + "-subnav")
    if(subSectionObj != null)
    {
      subSectionObj.style.display = "block";
      
      // If there was no subSection passed into the function, the subSection
      // is the page currently being displayed
      if(subSection == null || subSection == "")
        subSection = document.getElementById("menuSelectedSection").value;

      var aTags = subSectionObj.getElementsByTagName('A');
      for(var i = 0; i < aTags.length; i++)
      {
        if(aTags[i] && aTags[i].id == ("subnav-" + subSection))
          aTags[i].className = "hilite";
        else
          aTags[i].className = "";
      }
    }
}

function lolite(){
	clearTimeout(timeoutNavitem);
	timeoutNavitem = setTimeout("lolite2()", 2000);
}

function lolite2(){
    hideAll();
    if(currentSection){
        // Highlight the menu section
        var sectionImgObj = document.getElementById("navitem-img-" + currentSection);
          if(sectionImgObj != null)
            sectionImgObj.src = getMenuBGImg(currentSection);
          
        // Show the menu sub section and highlight the one which corresponds to the
        // current page
        var subSectionObj = document.getElementById("navitem-" + currentSection + "-subnav");
        if(subSectionObj != null) {
          subSectionObj.style.display = "block";
          
          var subSection = document.getElementById("menuSelectedSection").value;
          var aTags = subSectionObj.getElementsByTagName('A');
          for(var i = 0; i < aTags.length; i++) {
            if(aTags[i] && aTags[i].id == ("subnav-" + subSection))
              aTags[i].className = "hilite";
            else
              aTags[i].className = "";
          }
        }
    }
}

function hideAll(){
    for(var i=0; i<arrNavItem.length; i++){
        var sectionImgObj = document.getElementById("navitem-img-" + arrNavItem[i].id);
        if(sectionImgObj != null)
          sectionImgObj.src = arrNavItem[i].fgImg;
        var subSectionObj = document.getElementById("navitem-" + arrNavItem[i].id + "-subnav");
        if(subSectionObj != null)
          subSectionObj.style.display = "none";
    }
}

//TOP NAVIGATION end
//-----------------------------------------------------------------------------------

// Get the absolute coords of an object that isn't absolute
function getPosOffset(what, offsettype){
	
	var totaloffset = (offsettype=="left") ? what.offsetLeft : what.offsetTop;	
	var parentEl = what.offsetParent;
		
	while (parentEl != null) {
		totaloffset = (offsettype=="left") ? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
		parentEl = parentEl.offsetParent;
	}

	return totaloffset;
}

// Returns the first form element above a given element
function parentForm(el) {
  thisEl = el;
  while ((thisEl.tagName.toLowerCase() != 'form') && (thisEl.parentNode != null)) {
    thisEl = thisEl.parentNode;
  }
  return thisEl;
}

// Move one div to the position of another
function alignDivs(fixed, dynamic) {
	dynamic.style.left = getPosOffset(fixed, "left") + "px";
	dynamic.style.top = getPosOffset(fixed, "top") + "px";	
}

// load popup window
function popUp(url, scrollbars) {
	l = ((window.outerWidth - 500) / 2);
	t = ((window.outerHeight - 500) / 2);
  
  if(scrollbars == null) {scrollbars = 'no';}
  
  
	window.open(url, "popup", 
	
	// Size
	"width=500px," +
	"height=500px," +
	
	// Position
	"left=" + l + "px," +
	"top=" + t + "px," +
	"screenX=" + l + "px," +
	"screenY=" + t + "px," +
	
	// Remove bars etc.
	"menubar=no," +
	"location=no," +
	"status=no," +
	"directories=no," +
	"toolbar=no," +

	// Sizing issues
	"scrollbars="+scrollbars+"," +
	"resizable=no," +
	
	// No minimise/maximise button?
	"dialog=yes," +

	"");
}


// load popup window
function sizedPopUp(url, width, height) {
	l = ((window.outerWidth - width) / 2);
	t = ((window.outerHeight - height) / 2);

	window.open(url, "popup", 
	
	// Size
	"width="+width+"px," +
	"height="+height+"px," +
	
	// Position
	"left=" + l + "px," +
	"top=" + t + "px," +
	"screenX=" + l + "px," +
	"screenY=" + t + "px," +
	
	// Remove bars etc.
	"menubar=no," +
	"location=no," +
	"status=no," +
	"directories=no," +
	"toolbar=no," +

	// Sizing issues
	"scrollbars=no," +
	"resizable=no," +
	
	// No minimise/maximise button?
	"dialog=yes," +

	"");
}

// load popup window
function sizedPopUpScrollbars(url, width, height) {
	l = ((window.outerWidth - width) / 2);
	t = ((window.outerHeight - height) / 2);

	window.open(url, "popup", 
	
	// Size
	"width="+width+"px," +
	"height="+height+"px," +
	
	// Position
	"left=" + l + "px," +
	"top=" + t + "px," +
	"screenX=" + l + "px," +
	"screenY=" + t + "px," +
	
	// Remove bars etc.
	"menubar=no," +
	"location=no," +
	"status=no," +
	"directories=no," +
	"toolbar=no," +

	// Sizing issues
	"scrollbars=yes," +
	"resizable=no," +
	
	// No minimise/maximise button?
        "dialog=yes");

}
function toggleDisplay(idName) {
	var obj = document.getElementById(idName).style;
	
	if (obj.display == 'none')
		obj.display = '';
	else
		obj.display = 'none';
}

function toggleRadio(radio) {
	var hidden = eval('radio.form.'+radio.name.substring(0, radio.name.length - 6));
	
	if (hidden.value == 'on') {
		hidden.value = 'off';
		radio.checked = false;
	}
	else {
		hidden.value = 'on';
		radio.checked = true;	
	}
}

var debugWindow = -1;

function debug(msg) {

	if (!debugOn)
		return;

	if (debugWindow == -1 || debugWindow.closed) {
		debugWindow = window.open("", "debug", "width=500px,height=500px,menubar=no,toolbar=no,location=no,status=no");
		debugWindow.document.title = "OpenJaw Javascript Debug";
		debugWindow.document.write("<span style='font-size: 10px; font-family: Verdana; color: red;'>Accessing Window - " + location + "</span><hr/>");			
	}

	debugWindow.document.write("<span style='font-size: 10px; font-family: Verdana;'>" + msg + "</span><hr/>");
}

// This code is used for "popup" layers, where the show/hide is affected by the mouse being
// over the original link and the "popup" layer itself
//
// Suggested use:
//		<a href="" onMouseOver="showIn('popup');" onMouseOut="hideIn('popup', 300)">....</a>
//		<div id="popup" onMouseOver="showIn('popup');" onMouseOut="hideIn('popup', 300)">....</div>

var timeoutIndicators = new Array();

function hideIn(divName, timeout) {
	if (timeoutIndicators[divName] == null) {
		timeoutIndicators[divName] = setTimeout("hide('"+divName+"')", timeout);
	}
}

function showIn(divName) {
	if (timeoutIndicators[divName] != null) {
		clearTimeout(timeoutIndicators[divName]);
		timeoutIndicators[divName] = null;
	}
	var div = document.getElementById(divName);
  
  // If this is an IE browser
  if (document.all)
  {
    // Put an iFrame behind the div, so that select boxes don't show through
    // in Internet Explorer
    var iframe = document.getElementById('divBackgroundIframe');
    iframe.style.top = div.style.top;
    iframe.style.left = div.style.left;
    iframe.style.width = div.clientWidth + 'px';
    iframe.style.height = div.clientHeight + 'px';
    iframe.style.display = '';
    
    if(iframe.style.zIndex == '')
      iframe.style.zIndex = 2;
    div.style.zIndex = iframe.style.zIndex + 1;
  }

  div.style.visibility = "visible";
}

function hide(divName) {
  if (document.all)
  {
    var iframe = document.getElementById('divBackgroundIframe');
    iframe.style.display = 'none';
  }
	document.getElementById(divName).style.visibility = "hidden";
	timeoutIndicators[divName] = null;
}

// Div Popup Dialog

function showDialog(divName, e, offsetX, offsetY) {
	if (timeoutIndicators[divName] == null) {	
		var d = document.getElementById(divName);
    
    // If the div is already showing, don't move it
    if(d.style.visibility == "visible")
      return;
    
    // Work out the browser specific coordinates
    var xPos = 0;
    var yPos = 0;
    var pageBottom = 0;
    var pageRight = 0;
    if (document.all) {
      if (document.documentElement)
      {
        xPos = document.documentElement.scrollLeft + e.clientX;
        yPos = document.documentElement.scrollTop + e.clientY;
        pageBottom = document.documentElement.scrollTop + document.documentElement.clientHeight;
        pageRight = document.documentElement.scrollLeft + document.documentElement.clientWidth;
      }
      else
      {
        xPos = document.body.scrollLeft + e.clientX;
        yPos = document.body.scrollTop + e.clientY;
        pageBottom = document.body.scrollTop + document.body.clientHeight;
        pageRight = document.body.scrollLeft + document.body.clientWidth;
      }
    }
    else {
      xPos = e.pageX;
      yPos = e.pageY;
      pageBottom = window.innerHeight + window.pageYOffset;
      pageRight = window.innerWidth + window.pageXOffset;
    }
    
    var leftPos = xPos + offsetX - parseInt(d.style.width);
    var topPos = yPos + offsetY;
    
    // Make sure the div isn't partly off the bottom of the screen
    var divBottom = topPos + d.clientHeight;
    if(divBottom > pageBottom)
    {
      topPos -= divBottom - pageBottom + 20;
    }
    
    // Make sure the div isn't partly off the right side of the screen
    var divRight = leftPos + d.clientWidth;
    if(divRight > pageRight)
      pageRight -= divRight - pageRight + 20;
    
    
    // Set the div position
    d.style.left = leftPos + 'px';
    d.style.top = topPos + 'px';
	}
	showIn(divName);
}

// Div Popup

function divPopup(popupId)
{
  var p = document.getElementById('divPopup_'+popupId);
  
  if (window.innerWidth) {
	  iw = window.innerWidth;
	  ih = window.innerHeight;
  }
  else {
  	  iw = screen.availWidth;
  	  ih = screen.availHeight;
  }
  
  var l = ((iw - parseInt(p.style.width)) / 2);
  var t = ((ih - parseInt(p.style.height)) / 2);

  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    t = scrOfY = window.pageYOffset +t;
  }
  else if (document.documentElement) {
	t = document.documentElement.scrollTop + t;
  }
  else {
	t = document.body.scrollTop + t;
  }  

  p.style.left = l + 'px';
  p.style.top = t + 'px';
  
  p.style.display = "block";
  
  if (document.all)
  {
    // Put an iFrame behind the div, so that select boxes don't show through
    // in Internet Explorer

    var tipsIFrame = document.getElementById('divPopup_'+popupId+'_iframe');

    tipsIFrame.style.top = p.style.top;
    tipsIFrame.style.left = p.style.left;
    tipsIFrame.style.display = "block";
  }
  p.style.visibility = "visible";
  
  return false;
}

function closeDivPopup(popupId)
{
  var p = document.getElementById('divPopup_'+popupId);
  
  if (document.all)
  {
    var tipsIFrame = document.getElementById('divPopup_'+popupId+'_iframe');
    tipsIFrame.style.display = "none";
  }

  p.style.display = "none";
  return false;
}

// City List

var cityListCallback = "";
var cityListDescCallback = "";

function openCityList(callback) {
	cityListCallback = callback;
	showCityList();
}

function openCityList2(callback, descCallback) {
	cityListCallback = callback;
	cityListDescCallback = descCallback;
	showCityList();
}

function showCityList()
{
	divPopup('CityList');
	
	//Focus the city list unless the browser is Safari, in which it doesn't work properly.
	if (!document.childNodes || document.all || navigator.taintEnabled || accentColorName) {
		setTimeout("document.cityList.cityListBox.focus()", 0);
	}
}

function returnCityList() {
	var lb = document.cityList.cityListBox;
	if (cityListCallback != "") {
		var element = document.getElementById(cityListCallback);
		element.value = lb.options[lb.selectedIndex].value;
	}
	if (cityListDescCallback != "") {
		var element2 = document.getElementById(cityListDescCallback);
		element2.value = lb.options[lb.selectedIndex].text;
	}
	closeDivPopup('CityList');
}

function findFirstFocus() {
	if (document.getElementById('first-focus')) {
		document.getElementById('first-focus').focus();
	}
}

// ================================================================ //
// Waiting/Processing Functionality (e.g. spinner, event blocker)   //
// ================================================================ //

// Event blocker shows and cursor shows processing is going on
function lockPage() {

  var blocker = document.getElementById('waiting-blocker');
  var norm = document.getElementById('normal-content');

  // Stretch the blocker div over the page content
  blocker.style.width = norm.clientWidth + "px";
  blocker.style.height = norm.clientHeight + "px";  
}

// This function will probably never be used, but best to implement it
function unlockPage() {

  var blocker = document.getElementById('waiting-blocker');
  var norm = document.getElementById('normal-content');

  // Stop the blocker div from covering the page content
  blocker.style.width = "0px";
  blocker.style.height = "0px";
}

// Move the cursor image to where the mouse is
function updateCursor(e) {
	
	var	cursorImg = document.getElementById('waiting-blocker-cursor');
	
	cursorImg.style.left = (e.clientX + 1) + 'px';
	cursorImg.style.top = (e.clientY + 1) + 'px';
}

// Shows a tooltip explaining the situation if a user tries to click something
function showBlockerTip() {

	var bTip = document.getElementById('waiting-blocker-tip');

	bTip.style.visibility = 'visible';
	setTimeout('hideBlockerTip();', 2000);
}

// Hides the tip when we're ready
function hideBlockerTip() {
	var bTip = document.getElementById('waiting-blocker-tip');

	bTip.style.visibility = 'hidden';
}


// Starts the Waiting Spinner
function startSpinner() {

  var spinner = document.getElementById('waiting-spinner');
  var content = document.getElementById('waiting-spinner-content');
  var body = document.body;
  
  var contentTop = body.scrollTop;
  if (document.documentElement)
    contentTop = document.documentElement.scrollTop;

  // Stretch the spinner div over the page content
  spinner.style.width = body.clientWidth + "px";
  spinner.style.height = "2000px"; //body.clientHeight + "px";
  content.style.left = ((body.clientWidth - content.clientWidth) / 2) + "px";
  content.style.top = (contentTop + 100) + "px";

  // If this is an IE browser
  if (document.all)
  {
    // Put an iFrame behind the div, so that select boxes don't show through
    // in Internet Explorer
    var iframe = document.getElementById('divBackgroundIframe');
    iframe.style.top = 0;
    iframe.style.left = 0;
    iframe.style.width = spinner.style.width;
    iframe.style.height = spinner.style.height;
    iframe.style.display = '';
    
    if(iframe.style.zIndex == '')
      iframe.style.zIndex = 2;
    spinner.style.zIndex = iframe.style.zIndex + 1;
    content.style.zIndex = spinner.style.zIndex + 1;
  }
}

// ================================================================ //
// Site Buttons   //
// ================================================================ //

function disableButton(id) {
	var but = document.getElementById(id);	
	if (but.className == 'nav-button')
		but.className = 'nav-button-disabled';
	if (but.className == 'action-button')
		but.className = 'action-button-disabled';
	but.disabled = true;
}

function enableButton(id) {
	var but = document.getElementById(id);	
	if (but.className == 'nav-button-disabled')
		but.className = 'nav-button';
	if (but.className == 'action-button-disabled')
		but.className = 'action-button';
	but.disabled = false;
}

function toggleButton(id) {
	var but = document.getElementById(id);	
	if (but.disabled) {
		if (but.className == 'nav-button-disabled')
			but.className = 'nav-button';
		if (but.className == 'action-button-disabled')
			but.className = 'action-button';
		but.disabled = false;
	}
	else {
		if (but.className == 'nav-button')
			but.className = 'nav-button-disabled';
		if (but.className == 'action-button')
			but.className = 'action-button-disabled';
		but.disabled = true;
	}
}


// m: Used for toggling expand/collapse image of link
function toggleExpandLink(obj){
	if(getObj(obj).className=='expandable-closed'){		
		getObj(obj).className = 'expandable-open';		
	}else{
		getObj(obj).className = 'expandable-closed';
	}
}


// m: Used for changing the className of an object
function toggleClass(obj,newClass){
	getObj(obj).className=newClass;
}

function expandParagraph(id) {
    var image = document.getElementById('expand-icon-'+id);
    var text = document.getElementById('expand-text-'+id);
    image.style.display = 'none';
    text.style.display = 'inline';
}

function collapseParagraph(id) {
    var image = document.getElementById('expand-icon-'+id);
    var text = document.getElementById('expand-text-'+id);
    image.style.display = 'inline';
    text.style.display = 'none';
}
