/* ####################################
   ----- DD Nav
   #################################### */
function showMenu(id) {
	if(document.all && !document.compatMode) {
		getElement(id).style.display = 'block';
	} else {
		timer1ID = window.setTimeout(function(){showItNow(id);}, 150);
	}
}

function showItNow(id) {
	getElement(id).style.display = 'block';
	fixSelects('visible');
}

function hideMenu(id) {
	if(document.all && !document.compatMode) {
		getElement(id).style.display = 'none';
	} else {
		clearTimeout(timer1ID);
		timer2ID = window.setTimeout(function(){hideItNow(id);}, 150);
	}
}

function hideItNow(id) {
	getElement(id).style.display = 'none';
	clearTimeout(timer2ID);
	fixSelects('hidden');
}

function clearTimer2(id) {
	if(document.all && !document.compatMode) {
		showMenu(id);
	} else {
		clearTimeout(timer2ID);
	}
}

// Hide select form elements from IE
function fixSelects(visibility) {
	if (document.all) { // bug fix for IE drop-down menu rendering -- hide SELECT elements while menu is visible 
		var formvisibility = (visibility == 'visible') ? 'hidden':'inherit'; 
		for (var j=0; j<document.forms.length; j++) {
		  for (var k=0; k<document.forms[j].elements.length; k++){ 
			if(document.forms[j].elements[k].options) { 
			  document.forms[j].elements[k].style.visibility = formvisibility;
			  if(document.forms[j].elements[k].parentNode){ // if possible shaddow the SELECT elements with grey background
				if(document.forms[j].elements[k].parentNode.tagName!='SPAN' && document.forms[j].elements[k].parentNode.id!='ob_selShaddow_'+j+'_'+k) document.forms[j].elements[k].outerHTML='<span id="ob_selShaddow_'+j+'_'+k+'">'+document.forms[j].elements[k].outerHTML+'</span>';
				document.forms[j].elements[k].parentNode.style.cssText=(formvisibility=='hidden')? 'background-color:#eee;' : '';
			  }
			}
		  }
		}
	}
}

//************
//  helpers (generic implementations) - REVIEW
//************
//returns an object correctly referenced for Netscape4, IE4 & W3C compliant browsers
function getElement(id, path) {  //path only needed by netscape4
  var obj,i; if(!path)path=document;
  if(document.getElementById) obj=document.getElementById(id); //W3C DOM
  else if(document.all) obj=document.all[id]; //DHTML DOM
  else if (!(obj=path.id)&&document.layers) for(i=0;!obj&&path.layers&&i<path.layers.length;i++) obj=getElement(id,path.layers[i].document); //NS4 DOM
  if(!obj) for(i=0;!obj&&i<path.forms.length;i++)obj=path.forms[i].elements[id]; //Last resort: check form elements
  return obj;
}

// Sets the text of a layer for Netscape4, IE4 & W3C compliant browsers
function layerHTML(layer,html) {
	if(!(obj=getElement(layer))) return false;
	else with (obj)
		if (document.layers) { 
			document.writeln(html); 
			document.close();
		}else innerHTML = html;
	return true;
}

/* sets an element's css display property to block or none
 * usage: displayElement('elementId','off') */
function displayElement(elemId,v){
  if(v=='undefined' || v==null) v='1';
  dVal=(v=='hide' || v=='none' || v=='off' || ((v-0)<1) || v===false)?'none':'block';
  if(getElement(elemId)){ 
	var styleObj=getElement(elemId).style;
	styleObj.display=dVal; 
	return true; 
  }
  else return false;
}

function toggleDisplayElement(elemId){
  if(getElement(elemId)){ 
	var styleObj=getElement(elemId).style;
    if(styleObj.display!='none') displayElement(elemId,0);
	else displayElement(elemId,1);
	return true; 
  }
  else return false;
}

//positions layer (div) in relation to a marker graphic of same name+"Marker"
function positionToMarker(divId, offsetTop, offsetLeft) {
  marker=getElement(divId+'Marker','document.images');
  if (marker==null || marker+''=='undefined') return;  //exit if no marker;
  target=getElement(divId,'document.layers');
  if (document.layers) {  //NS4
    target.visibility='visible';
    target.top=marker.y+offsetTop;
    target.left=marker.x+offsetLeft;
  }
  else {  //W3C (test for IE4)
    while (marker.offsetParent) {  //add all parent offsets together to give total offset
      offsetTop+=marker.offsetTop;
      offsetLeft+=marker.offsetLeft;
      marker=marker.offsetParent;
    }
    target.style.visibility='visible';
    target.style.top=offsetTop;
    target.style.left=offsetLeft;
  }
}

//*************
//  Header
//*************
//checks form field - used in Header Search form
function IsString(sVal,sMsg){ if(sVal==""){ alert(sMsg); return false; } return true; }

//***************
// window Pop-ups
//***************
function screenOffset(axis,pixels){
  var percentOffset=(10/100); // percent offset from axis
  var axisDimension=new Number;
  if(axis=='x')axisDimension=screen.availWidth;
  if(axis=='y')axisDimension=screen.availHeight;
  return Math.ceil((axisDimension-pixels)*percentOffset);
}

function popUpWin(URL,winWidth,winHeight,winType,strWinName){
  var winOpts=new String,oNewWin=new Object,oDate=new Date;
  var xPos=new Number,yPos=new Number;
  (!winWidth)?winWidth=680:winWidth-=0;
  (!winHeight)?winHeight=520:winHeight-=0;
  switch(winType){
    case "console":
      winOpts="resizable";
      if(!strWinName)strWinName='newConsoleWin';
      break;
    case "fixed":
      winOpts="status";
      if(!strWinName)strWinName='newFixedWin';
      break;
    case "noFeatures":
      winOpts="scrollbars";
      if(!strWinName)strWinName="noFeatures";
      break;
    case "elastic":
      winOpts="menubar,scrollbars,resizable,status";
      if(!strWinName)strWinName='newElasticWin';
      break;
    default:
      winOpts="toolbar,menubar,scrollbars,resizable,location,status,directories";
      if(!strWinName)strWinName='ourbrisbaneWin';
      break;
  }
  if(window.screenX){
    xPos=screenOffset('x',window.screenX)+window.screenX;
    yPos=screenOffset('y',window.screenY)+window.screenY;
  }else if(window.screenLeft){
    xPos=screenOffset('x',window.screenLeft)+window.screenLeft;
    yPos=screenOffset('y',window.screenTop)+window.screenTop;
  }
  if(screen.availWidth<(xPos+winWidth))xPos=screen.availWidth-winWidth;
  if(screen.availHeight<(yPos+winHeight))yPos=screen.availHeight-winHeight;
  winOpts+=",x="+xPos+",left="+xPos+",y="+yPos+",top="+yPos+",height="+winHeight+",width="+winWidth;
  oNewWin=window.open(URL,strWinName,winOpts);
  oNewWin.focus();
  return oNewWin;
}
// onMouseOver="self.status=this.title;return true;" onMouseOut="self.status='';return true;" 
function titleStatus(){ window.status=this.title; return true; }
/**
 * Link title to status
 *
 * Sets the status bar message (displayed on hover) for links to their title attribute
 *
 * @author Christopher Hopper <christopher.hopper@brisbane.qld.gov.au>
 * @param mixed id The id of the container object or the container object itself
 * @param object path optional The DOM path to the container object i.e. document.forms
 * @return boolean true on success, false on failure setting focus
 */
function linkTitle2Status(id, path){
	if(!path)path=document;
	var debug=false;
	var i,j,f=false; 
	if(debug)alert(typeof(id));
	var o=(typeof(id)=='object')? id : ((typeof(id)=='string')? getElement(id): null);
	if(o&&o.title&&o.tagName=='A'){ o.onmouseover=titleStatus; o.onmouseout=clearStatus; }
	if(o&&document.getElementById){ //W3C DOM
		for(i=0;o.hasChildNodes()&&i<o.childNodes.length;i++){
			if(debug){if(o.childNodes[i].nodeType==1)alert('<'+o.childNodes[i].tagName+(o.childNodes[i].id? ' id='+o.childNodes[i].id: '')+'>'); else alert('text');}
			if(o.childNodes[i].nodeType==1){ 
				if(o.childNodes[i].title&&o.childNodes[i].tagName=='A'){ o.childNodes[i].onmouseover=titleStatus; o.childNodes[i].onmouseout=clearStatus; }
				else if(o.childNodes[i].hasChildNodes()) linkTitle2Status(o.childNodes[i]);
			}
		} 
	}else if(o&&document.all){ //DHTML DOM
		for(i=0;o.all.length&&i<o.all.length;i++){ 
			if(debug){if(o.all[i].nodeType==1) alert('<'+o.all[i].tagName+(o.all[i].id? ' id='+o.all[i].id: '')+'>'); else alert('text');}
			if(o.all[i].nodeType==1){ if(o.all[i].title&&o.all[i].tagName=='A'){ o.all[i].onmouseover=titleStatus; o.all[i].onmouseout=clearStatus; } }
		}
	}else if(o&&!f&&document.layers){ //NS4 DOM 
		for(i=0;o.document.links&&i<o.document.links.length;i++){ if(o.document.links[i].title){ o.document.links[i].onmouseover=titleStatus; o.document.links[i].onmouseout=clearStatus; } } //traverse links
		for(i=0;o.document.layers&&i<o.document.layers.length;i++) focusFirstChild(id,o.document.layers[i].document); //traverse child layers
	}
	return;
}

/* DEPRECATED FUNCTIONS - delete when all references are removed
 * used on some pages - find and remove */
function popWin(URL,winWidth,winHeight,winType,strWinName){ return !popUpWin(URL,winWidth,winHeight,winType,strWinName);}
function popUp(URL, height, width, name, properties) { if (!name)name=false; return !popUpWin(URL,width-0,height-0,'',name); }
function MM_openBrWindow(URL, name, properties){ return !popUpWin(URL,0,0,'',name); }
function OB_openBrWindow(URL, name, properties){ return !popUpWin(URL,0,0,'',name); }
function MM_setTextOfLayer(objName,x,newText) { layerHTML(objName,unescape(newText)); }
function MM_findObj(n, d) { return getElement(n, d); }

//***************
//  MM Functions
//***************
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

/**
 * HTML Entities lookup class
 * 
 * An object for the encoding and un-encoding of special characters to their 
 * HTML entities or simple character equivalents. Usage example:
 *
 *		// output '&amp;' to the browser window.
 *		var ents=new EntitiesLookup;
 *		var s=ents.encode('&');
 *		document.write(s);
 *
 * @package global
 * @subpackage scripts
 * @version 1.0
 * @author Christopher Hopper <christopher.hopper@team.ourbrisbane.com>
 * @copyright Copyright © Brisbane City Council 2005
 * @access  public
 *
 *
 */

/**
 * Entities Lookup
 *
 * Constructor function for the Entities Lookup object class
 *
 * @author Christopher Hopper <christopher.hopper@team.ourbrisbane.com>
 * @param bool debug optional Value to indicate whether debug mode is on.
 * @return object EntitiesLookup 
 * @access public
 */
function EntitiesLookup(){
  if(arguments.length>=1) this.debug=Boolean(arguments[0]);
  else this.debug=false;
  this.chars=[
     '&','<','>','"','“','”','„',"'",'‘','’','‚'
    ,'«','»','‹','›'
    ,'–','—','­','…','•','·','°','‰'
    ,'¢','£','¥','€'
    ,'™','®','©'
    ,'¿','¡','†','‡','¶','¦','§','ƒ'
    ,'¼','½','¾'
    ,'À','Á','Â','Ã','Ä','Å','Æ','Ç','Ð'
    ,'à','á','â','ã','ä','å','æ','ç','ð'
    ,'È','É','Ê','Ë'
    ,'è','é','ê','ë'
    ,'Ì','Í','Î','Ï','Ñ'
    ,'ì','í','î','ï','ñ'
    ,'Ò','Ó','Ô','Õ','Ö','Ø','Œ','Š'
    ,'ò','ó','ô','õ','ö','ø','œ','š'
    ,'Ù','Ú','Û','Ü','Ý','Ÿ'
    ,'ù','ú','û','ü','ý','ÿ'
  ];
  this.simpleHTML=[
     'and','&lt;','&gt;','&quot;','&quot;','&quot;','&quot;','&#39;','&#39;','&#39;','&#39;'
    ,'&lt;&lt;','&gt;&gt;','&lt;','&gt;'
    ,'-','-','-','...','*','*','degrees','permil'
    ,'cents','P','Y','E'
    ,'(TM)','(R)','(C)'
    ,'?','!','*','**','(paragraph)','|','s','f'
    ,'one quarter','one half','three quarters'
    ,'A','A','A','A','A','A','AE','C','DE'
    ,'a','a','a','a','a','a','ae','c','de'
    ,'E','E','E','E'
    ,'e','e','e','e'
    ,'I','I','I','I','N'
    ,'i','i','i','i','n'
    ,'O','O','O','O','O','O','OE','S'
    ,'o','o','o','o','o','o','oe','s'
    ,'U','U','U','U','Y','Y'
    ,'u','u','u','u','y','y'
  ];
  this.HTML=[
     '&amp;','&lt;','&gt;','&quot;','&#8220;','&#8221;','&#8222;','&#39;','&#8216;','&#8217;','&#8218;'
    ,'&laquo;','&raquo;','&#8249;','&#8250;'
    ,'&ndash;','&mdash;','&shy;','&hellip;','&bull;','&middot;','&deg;','&permil;'
    ,'&cent;','&pound;','&yen;','&euro;'
    ,'&#8482;','&reg;','&copy;'
    ,'&iquest;','&iexcl;','&dagger;','&Dagger;','&para;','&brvbar;','&sect;','&fnof;'
    ,'&frac14;','&frac12;','&frac34;'
    ,'&Agrave;','&Aacute;','&Acirc;','&Atilde;','&Auml;','&Aring;','&AElig;','&Ccedil;','&ETH;'
    ,'&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&eth;'
    ,'&Egrave;','&Eacute;','&Ecirc;','&Euml;'
    ,'&egrave;','&eacute;','&ecirc;','&euml;'
    ,'&Igrave;','&Iacute;','&Icirc;','&Iuml;','&Ntilde;'
    ,'&igrave;','&iacute;','&icirc;','&iuml;','&ntilde;'
    ,'&Ograve;','&Oacute;','&Ocirc;','&Otilde;','&Ouml;','&Oslash;','&OElig;','&Scaron;'
    ,'&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&oslash;','&oelig;','&scaron;'
    ,'&Ugrave;','&Uacute;','&Ucirc;','&Uuml;','&Yacute;','&Yuml;'
    ,'&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&yuml;'
  ];
}

/**
 * Entities Lookup encode
 * 
 * Prototype function for method to encode HTML entities found in a string
 * 
 * @author Christopher Hopper <christopher.hopper@team.ourbrisbane.com>
 * @param string es optional String with special characters to encode into HTML entities or convert to simple character equivalents
 * @param bool mode optional Modes are: true = use HTML entities; false = use mix of HTML entities and character equivalents
 * @return string The string with all HTML entities properly encoded or converted to equivalent characters
 * @access private
 */
function EntitiesLookup_encode(){ 
  var es=null, re=new RegExp, mode;
  var msg='', r=new Array;
  switch(arguments.length){
	  case 0:
		  return es;
	  case 1:
		  es=String(arguments[0]);
		  mode=true; 
		  break;
	  case 2:
	  default:
		  es=String(arguments[0]);
		  mode=Boolean(arguments[1]); 
		  break;
  }
  for(var i=0; i<this.chars.length; i++){
    re=new RegExp(this.chars[i], 'g');
	if((r=es.match(re))!= null){
		msg+="["+String(i+1)+"] "+r.toString()+"\n\n";
		es=mode==true? es.replace(re, this.HTML[i]): es.replace(re, this.simpleHTML[i]);
	}
  }
  if(this.debug) document.write('<div><pre>'+msg+'</pre></div>');
  return es;
}

/**
 * Entities Lookup un-encode
 * 
 * Prototype function for method to un-encode HTML entities found in a string
 * 
 * @author Christopher Hopper <christopher.hopper@team.ourbrisbane.com>
 * @param string es optional String with HTML entities to un-encode to special character equivalents
 * @return string The string with all HTML entities properly un-encoded to equivalent special characters
 * @access private
 */
function EntitiesLookup_unencode(){ 
  var es=null, re=new RegExp;
  var msg='', r=new Array;
  if(arguments.length<1) return es;
  else es=arguments[0].toString();
  for(var i=0; i<this.HTML.length; i++){
	re=new RegExp(this.HTML[i], 'g');
	if((r=es.match(re))!= null){
		msg+="["+String(i+1)+"] "+r.toString()+"\n\n";
		es=es.replace(re, this.chars[i]);
	}
  }
  if(this.debug) document.write('<div><pre>'+msg+'</pre></div>');
  return es;
}

// Add methods to the object class
EntitiesLookup.prototype.encode=EntitiesLookup_encode;
EntitiesLookup.prototype.unencode=EntitiesLookup_unencode;

