
// navstyle.js

var UNDEF = 'undefined';

// initNav
//	- modifies visibility of left-nav-bar ul's based on curr page
//	- invokes adjustNav if function provided on the page
//		to do other custom adjustments

function initNav() { initPage(); }


function initPage()
{
        // adjust visibility
        //    of nested ul in left navbar based on URL of this page
	//
	var rc = initNavStyle();	// based on exact match
	if (!rc) adjustNavStyle();	// based on regex vs this folder

	// convert any CONTACT(...) into proper href
	//
	fmtContacts();

        // pull in any sub-template/page specific adjustment code
	//
        if (typeof adjustNav == 'function') adjustNav();

}


function initNavStyle(url)
{

// turn on debugging (only if using firfox/firebug)
var d = false; 
if (typeof console == 'undefined') d = false;
if(d)console.log('in initNav url is ', url);
	
	// Find the unordered list that contains the navigtion links
	var navUL = document.getElementById('navul');
	if (!navUL) return true;

	// loop through all hrefs under navUL 
	var alist = navUL.getElementsByTagName("a");
	var acount = alist.length;
	
	var tst= location.href;
	if (url) tst = url
	tst = unescape(tst);  // IIS problem 
				// sometimes converting _BETA becomes %5FBETA
	tst = tst.toLowerCase();


	// define regex to match trailing / or /index.htm or /index.html
	var rx = /\/$|\/(index|beta)\.(htm|html|php)$/;
	var tst2= tst.replace(rx,'');

if(d)console.log('tst: ', tst, ', ',tst2);
	for (i=0; i<acount; i++)
	{
		var aobj = alist[i];
		var href = unescape(aobj.href).toLowerCase();

if(d)console.log('href.',i,': ',href);

		if (tst == href ||
		    tst2 == href.replace(rx,'') ){
			walkStylePath(aobj,'navcurrentpage',navUL,d);
			return true;
		}
	}
	return false;
	
}



function adjustNavStyle()
{
	// called by page to force

var d = true; if (typeof console == 'undefined') d = false;

	var url= location.href.toLowerCase();

	// this is called when initNavStyle found no matches
	// for example, for 4-th or 5-th level files

	var key = '';

	     if (url.match('/beachwalk/')) 	key='beachwalk';  
	else if (url.match('/essays/')) 	key='essays';
	else if (url.match('/news/'))     	key='news';  
	else if (url.match('/monitoring/'))     key='monitor';
	else if (url.match('/seining/'))    	key='seining';
	else if (url.match('/estuaries/'))    	key='estuary';
	else if (url.match('/beachlog/'))    	key='beachlog';
	else if (url.match('/about/'))    	key='about';  ;

	if (key == '') return;

	var navUL = document.getElementById('navul');
	if (!navUL) return true;

	var el = document.getElementById('href_'+key);
	if (!el) return;

	var allULs = navUL.getElementsByTagName("ul");
	for (var i=0; i<allULs.length; i++)
	{
		allULs[i].style.display = "none";
	}


	walkStylePath(el,'navcurrentpage',navUL,d);

}





function walkStylePath(obj,style,root,d)
{
	// aobj is an a tag
	// walk up to mark any ul containers and their first a tags
	var tag = obj.tagName;
if(d)console.log('wsp ', tag);

	if (obj == root) return;

	if (tag == 'A'){
		obj.className = style;
if(d)console.log('... ',obj.href, ' --> ',style);
	}
	else if (tag == 'UL'){
		//obj.className = 'navparentpath';
if(d)console.log('... ',' --> ',style);

		// what if no a tag ?? -- does this wrap ??
		var a = obj.previousSibling;
		while( a && a.tagName != 'A' && a != obj) a = a.previousSibling;
		if (a && a.tagName == 'A') a.className = 'navparentpath';

if(d)console.log('... ',' --> style.display: block');

		obj.style.display = 'block';
	}
	else if (tag == 'LI'){
		var el  = obj.firstChild;
		while( el  && el.tagName != 'UL' && el != obj) el = el.nextSibling;
		if (el && el.tagName == 'UL') el.style.display = 'block';
	}

	walkStylePath(obj.parentNode,'navparentpath',root,d);
}

function showRightColumn(bool)
{
	if (bool) return;  // already handled by wsu style-sheet

	// modify style to hide secondary div, and expand main width

	var el=document.getElementById('secondary');
	if (el) el.style.display = 'none'

	var el=document.getElementById('main');
	if (el) el.style.width = '556px';  
}


// --------------------------------- functions to support trouble reporting

function reportIssue(basewin)
{
	var url="http://beachwatchers.org/bw/island/issues/entry.php?"
		+ "url=" + escape(basewin.location.href)
		+ "&domain=ezidweb";

	basewin.open(url);
}

// -------------------------------- functions to support DW img swap 


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// -------------------------------- functions to support contactRelay

function fmtContacts()
{
	var el = document.getElementById('main');
	if (!el) return;

	var txt = el.innerHTML;
	var txt2 = txt.replace( /CONTACT[(]([^)]+)[)]/g,  function (all,str){ return fmtOneContact(str);});
	el.innerHTML = txt2;
}
	


function fmtOneContact(txt)
{

	var url='http://beachwatchers.org/bw/island/contact/form.php';
	var arr = txt.split(/ +for +/);

	var con = arr[0]; var econ = escape(con); 

	var edom = dom = '';

	if (arr.length > 1){ 
		arr[0] = '';
		dom = ' for ' + arr[1];
		edom = escape(arr[1]);
	}

	var ans = '<a href=' + url + '?contact=' + econ
			+ '&domain=' + edom +'>' + con  + '</a>';

	if (arr.length >  1) ans +=  dom;

	return ans;

}

