// Copyright 2003 Grace Fellowship of Puget Sound

//	alert(navigator.appCodeName+"*"+navigator.appName+"*"+navigator.userAgent+"*"+navigator.appVersion)

//USEFUL CONSTANTS
	menuWidth=125
	yOffset=180

	menuHiliteColor="#ccccee"
	menuBackgroundColor="#eeeef3"
	subMenuHiliteColor="#ccccff"
	

//==========================MENU SETUP=========================	

//MENU OBJECT DEFINITION
	function menu(id,title,href,sib,child){
		this.id=id
		this.title=title
		this.sib=sib
		this.child=child
		this.parent=null
		this.root=this
//		this.img="../images/"+id+"btn.gif"
		this.script=""
		if(href.indexOf("$")==0){
			this.script=href.substr(1,href.length-1)
			href="*"
		}
		this.href= (href=="")? "../"+id+"/":((href=="*")? "javascript:void(0)":href)	//* indicates that menu item is not a link
		this.extern=href.toLowerCase().indexOf("@")==0				//@ indicates that a new window needs to be opened
		if(this.extern) this.href=this.href.substr(1,this.href.length-1)
	}										
		var t2=document.cookie.indexOf("t2=")!=-1
		var ref=document.location+""	//This is the only way I could get document.location converted to a string so .indexOf() could be used with it in Mozilla!
		var adminURL=((ref.indexOf(":8080")!=-1)? "../../httpsdocs":("https://"+document.domain))+"/db/main.php?fromMenu=true"
		
		subMenuList=new Array()
//LOCATIONS SUBMENUS
//			chateaupacificMenu=new menu("chateaupacific","Chateau Pacific","","",""),
//		lynnwoodMenu=new menu("lynnwood","Lynnwood","*","",chateaupacificMenu),
//			gardencourtMenu=new menu("gardencourt","Garden Court","@http://www.agardencourt.com","",""),
//			elderhealthMenu=new menu("elderhealth","Elderhealth Northwest","",gardencourtMenu,""),
//		everettMenu=new menu("everett","Everett","*",lynnwoodMenu,elderhealthMenu),
//			avaloninnMenu=new menu("avaloninn","Avalon Inn","@http://www.avaloninn.com","",""),
//		edmondsMenu=new menu("edmonds","Edmonds","(",everettMenu,avaloninnMenu))
//MAIN MENU		
		mainMenuList=new Array(
//			linksMenu=new menu("links","Friends (links)","","",""),
//			albumMenu=new menu("album","Photo Album","",linksMenu,""),
//			contactMenu=new menu("contact","Contact Us","",albumMenu,""),
			supportMenu=new menu("support","Support Us","","",""),
//			applicationMenu=new menu("application","Application Form","../application/TitusIIApp.pdf",supportMenu,""),
			//slideshowMenu=new menu("slideshow","Slide Show","$openSlideShow()",supportMenu,""),
			changedlivesMenu=new menu("changedlives","Changed Lives","",supportMenu,""),
			beliefsMenu=new menu("beliefs","What We Believe","",changedlivesMenu,""),
			//endorsementsMenu = new menu("endorsements", "Endorsements", "", beliefsMenu, ""),
			//articlesMenu = new menu("articles", "Articles", "", beliefsMenu, ""),
			//churchesMenu = new menu("churches", "Churches involved","",articlesMenu, ""),
			peopleMenu=new menu("people","Our People","",beliefsMenu,""),
//			faqMenu=new menu("faq","FAQ","",beliefsMenu,""),
//			memoryMenu=new menu("memory","In Memory of...","",faqMenu,""),
//			testimoniesMenu=new menu("testimonies","Testimonies","",memoryMenu,""),
//			locationsMenu=new menu("locations","Locations","*",peopleMenu,edmondsMenu),
			aboutMenu=new menu("about","Ministry Overview","",peopleMenu,""),
//			historyMenu=new menu("history","History","",aboutMenu,""),
			//newsMenu = new menu("news","News","",aboutMenu,""),
			//forumMenu = new menu("forum","Forum","",newsMenu,""),
			startMenu=new menu("homepage","Home","",aboutMenu,""))

//=======================UTILITY FUNCTIONS======================

	function test(){
		alert("test")
	}
	
	function getMenu(id){
		if(!id) return false
		return checkMenu(id.replace(/[A-Z][a-z0-9-_]*$/,""),startMenu)
	}
	
	function checkMenu(id,start){
		var menu, result
		for(menu=start;menu!="";menu=menu.sib){
			if(menu.id==id) return menu
			if((menu.child!="")&&(result=checkMenu(id,menu.child))) return result
		}
		return false
	}
	
	function setParents(start,parent){
		var menu
		for(menu=start;menu!="";menu=menu.sib){
			menu.parent=parent
			if(menu.child!="") setParents(menu.child,menu)
		}
	}

	function openSlideShow()
	{
		var w=window.open("../slideshow/index.html","slides","height=600,width=800")
		w.focus()
	}
//=====================MISC STUFF=======================
	
//BROWSER SNIFFING	
	msie=navigator.userAgent.indexOf("MSIE")!=-1
	ns=navigator.userAgent.indexOf("Netscape")!=-1
	mac=navigator.userAgent.indexOf("Mac")!=-1
	gecko=navigator.userAgent.indexOf("Gecko")!=-1
	popup=document.getElementById// && !(msie&&mac)
	
//DISPLAY REFERENCE PASSAGES IN NEW WINDOW
	function verseLink(passage){
		pr=escape(passage.replace(/\ +/g,"+"))
		ref="http://biblegateway.com/cgi-bin/bible?language=english&version=NIV&passage="+pr+"&interface=print&showxref=no&showfn=no"
		document.write("<a href='"+ref+"' target='_blank' class='scriptRef'>"+passage+"</a>")
	}
//=======================MENU EVENT HANDLERS=====================
	
	lastRoot=null
	subList=new Array(10)
	for(i=0;i<10;i++) subList[i]=null
	subIndex=0
	lastElmnt=null
	lastParent=null

	var op
//MANAGE THE MENU STRUCTURE ON MOUSEOVER
	function inMenu(elmnt){
		if(!popup) return								//only works with popup menus
		var m=getMenu(elmnt.id)							//find the menu in the menu list
//		op.value=""
	//TAKE CARE OF PENDING MENU CLEARING EVENTS	
		if(delayTime){									//if there has been a mouseOut command
			if(m.root==lastRoot){						//and we are in the same menu we were previously
				clearTimeout(delayTime)					//cancel the menu clear timeout
				delayTime=false							//and indicate that we are not waiting for a menu clear
			}
			else{
				clearMenu()								//this is a new menu, so we need to clear the old one
			}
		}
		lastRoot=m.root									//remember which main menu item we are servicing
		if(elmnt.id.search(/Item$/)==-1) return			//don't need to do more if it is a submenu
		if(elmnt==lastElmnt) return
	//EXPAND menuDiv
		var md=document.getElementById("menuDiv");
		md.style.width="100%"
	//HIGHLIGHT THE ACTIVE MENU ITEM
		var inMain=(m.root==m)										
		var parentSub=null
		var parentItem=null
		if(m.parent){
			parentSub=document.getElementById(m.parent.id+"Sub")
			parentItem=document.getElementById(m.parent.id+"Item")
			if(lastParent){
				if((parentItem!=lastParent)&&(parentItem!=lastElmnt)){
					lastParent.style.backgroundColor=menuBackgroundColor
				}
				if((parentItem==lastParent)||(elmnt==lastParent)){
					lastElmnt.style.backgroundColor=menuBackgroundColor
				}
//				op.value=parentItem.id+"*"+lastParent.id+"*"+lastElmnt.id+"*"+elmnt.id
			}
			lastParent=parentItem
		}
		else{
			if(lastParent&&(elmnt==lastParent)){
				lastElmnt.style.backgroundColor=menuBackgroundColor
			}
			lastParent=null
		}
		elmnt.style.backgroundColor=menuHiliteColor			//hilite the menu item
		lastElmnt=elmnt
		
	//ELIMINATE OLD SUBMENU IF THERE IS ONE	
		if(subList[subIndex]&&(parentSub!=subList[subIndex])){		//if this is not the main menu item and belongs to a new submenu
			subList[subIndex].style.display="none"		//get rid of the old submenu
			subList[subIndex]=null
			if(subIndex) subIndex--
		}
		
	//SET UP THE NEW SUBMENU IF THERE IS ONE
		if(m.child=="")return;							//don't need to process further if there is no submuen
		var sub=document.getElementById(m.id+"Sub")		//find the new submenu
		if(sub==subList[subIndex]) return;				//don't need to process this submenu if it is already being displayed
		if(subList[subIndex]) subIndex++
		subList[subIndex]=sub

	//ADJUST SUBMENU SIZE AND POSITION
		sub.style.left=(inMain? elmnt.offsetWidth-1:parentSub.offsetLeft+parentSub.offsetWidth-1)+"px"

		var t=(elmnt.offsetTop+(inMain? 0:parentSub.offsetTop)) 	//calculate top to be even with parent item

		var h=sub.offsetHeight
		if(window.innerHeight){							//find top and height of screen (browser-dependent)
			var wh=window.innerHeight					//mozilla-style
			var wt=window.pageYOffset
		}
		else{
			var wh=document.body.clientHeight			//MSIE-style
			var wt=document.body.scrollTop
		}
		if((diff=(t+h+yOffset)-(wt+wh))>0){				//is bottom of submenu below the bottom of screen?
			sub.style.top=(t-diff)+"px"					//if so, adjust up
		}
		else{
			sub.style.top=t+"px";						//otherwise set even with top of parent item
		}
		sub.style.display="block"							//display submenu
	}
	
	delayTime=false

//RESPOND TO MOUSEOUT	
	function  outMenu(elmnt){								//delay clearing the menu to avoid screen glitches
//		op.value=elmnt.id
		if(!popup) return								//only need to do this if we have popup menus
		if(delayTime)clearTimeout(delayTime)			//if a delay timeout is already set, clear it
		delayTime=setTimeout("clearMenu()",100)			//restart the delay timer to 0.5 seconds
//		var item=document.getElementById(elmnt.id.replace(/[A-Z][a-z]*$/,"Item"))
//		item.style.borderColor=menuBorderColor;
	}
	
//CLEAR MENU HILITING
	function clearMenu(){
//		op.value="*"+subIndex+'*'
		var item,sub,m
		for(i=0;i<mainMenuList.length;i++){				//walk through menu list
			m=mainMenuList[i]
			if(m.id=="") continue						//skip blank menu items (used as spacers)
			item=document.getElementById(m.id+"Item")
			
			item.style.backgroundColor="transparent"		//reset background color to match body
		}
		for(i=0;i<subMenuList.length;i++){
			m=subMenuList[i]
			item=document.getElementById(m.id+"Item")
			item.style.backgroundColor=menuBackgroundColor
		}

		for(i=0;i<10;i++){
//			op.value+='['+i+"]"
			if(!subList[i]) continue
//			op.value+=subList[i].id
			subList[i].style.display="none"					//hide submenus
			subList[i]=null
		}
		subIndex=0

		lastElmnt=null
		lastParent=null
		
		if(delayTime) clearTimeout(delayTime)			//reset 
		delayTime=false
		lastRoot=null

	//CONTRACT menuDiv
		var md=document.getElementById("menuDiv");
		md.style.width="150px"

		return 
	}
		
//MAIN MENU GENERATION
	function generateMenu(){
		var m
		document.write("<table class='mainMenu' cellpadding='0' cellspacing='0' width='152'>")
		for (m=startMenu;m;m=m.sib){
			document.write("<tr>")
			document.write("<td>")
			if(m.id!=""){
				if(popup){
					document.write("<div class='mainItem' id='"+m.id+"Item' onmouseover=\"inMenu(this)\" onmouseout=\"outMenu(this)\">")
				}
				else{
					document.write("<div class='mainItem' style:'padding: 3px'>")
				}
				if(m.href!="") document.write("<a class='mainLink' href='"+m.href+"' "+((m.extern)? "target='_blank'":"")+((m.script)? " onclick='"+m.script+"'":"")+">")
				document.write("<img src='../images/"+m.id+"Btn.gif' alt='' style='border: 0px;'>")
				if(m.href!="") document.write("</a>")
			}
			else{
				if(popup) document.write("<div style='height: 12px; border: 0px; margin: 0px; padding: 0px;'>")
			}
			if(popup||(m.id!="")) document.write("</div>")
//SUBMENU GENERATION FOR NON-POPUPS			
			if((!popup)&&(m.child!="")){
				document.write("<table cellspacing='4' cellpadding='0'>")
				for(n=m.child;n!="";n=n.sib){
					document.write("<tr><td width='10'>&nbsp;</td>")
					document.write("<td >")
					if(n.href!="") document.write("<a class='mainLink' href='"+n.href+"' "+((n.extern)? "target='_blank'":"")+">")
					document.write(n.title)
					if(n.href!="") document.write("</a>")
					document.write("</td></tr>")
				}
				document.write("</table>")
			}
			
			document.write("</td>")
			document.write("</tr>")
		}
		document.write("</table>")
	}
	
	
//SUBMENU GENERATION FOR POPUPS	
	
	
	function subMenus(){
		if(popup){
			for (var m=startMenu;m!="";m=m.sib){
				subMenu(m)
			}
			setParents(startMenu,null)
		}
	}
	
	function subMenu(menu){
		if(menu.child=="") return
		document.write("<table cellpadding='0' cellspacing='0' id='"+menu.id+"Sub' class='subMenu'>")
		var n
		for(n=menu.child;n!="";n=n.sib){
			n.root=menu.root
			document.write("<tr><td nowrap>")
				document.write("<div id='"+n.id+"Item' class='subItem' onmouseover='inMenu(this)' onmouseout='outMenu(this)' onclick='"+(n.extern? ("window.open(\""+n.href+"\",\""+n.id+"\")'>"):("window.location=\""+n.href+"\"'>")))
					if(n.href!="") document.write("<a class='subLink' id='"+n.id+"LINK' href='"+n.href+"' "+(n.extern? "target='"+n.id+"'>":">"))
					document.write(n.title)
					if(n.href!="") document.write("</a>")
				document.write("</div>")
			document.write("</td></tr>")
		}
		document.write("</table>")
		for(n=menu.child;n!="";n=n.sib){
			subMenu(n)
		}
	}
			
//MAIN TOP		
	function mainTop(){
//		document.write("<input type='text' id='op' width='50' style='margin-left: 200px'>")
//		op=document.getElementById("op")
//		op.value=""
		document.write("<table width='100%' cellspacing='0' cellpadding='0'>")
			document.write("<tr>")
				document.write("<td style='background: url(../images/topbkg.jpg); height: 185px'>")
					document.write("<img src='../images/mission.gif' alt='' style='display: block; position: relative; top: 30px; left: 400px;'>")
			if(t2)	document.write("<div style='position: relative; top: 56px; text-align: right; font-size: 10pt;'><a href='"+adminURL+"' style='color: white' target='_blank'>ADMIN</a>&nbsp;</div>")
				document.write("</td>")
			document.write("</tr>")
		document.write("</table>")
		document.write("<div id='menuDiv' style='position: absolute; width: 150px'>")
					generateMenu();
					if(popup){
						subMenus()
					}
		document.write("</div>")
		document.write("<table width='100%' cellspacing='0' cellpadding='0' style='position: static;'>")
			document.write("<tr>")
				document.write("<td rowspan='2' valign='top' width='150'>")
					document.write("<img src='../images/darkmenu.jpg' alt=''>")	//Menu Cell
				document.write("</td>")
				document.write("<td height='100'>")
					document.write("<img src='hdr.jpg'>")
				document.write("</td>")
			document.write("</tr>")
			document.write("<tr>")
				document.write("<td id='content' class='bodyStyle' valign='top' align='left'>")
//					document.write("<div style='position: relative;'>") //Content Cell
	}

//MAIN BOTTOM	
	function mainBottom(){
//					document.write("</div>")
				document.write("</td>")
			document.write("</tr>")
		document.write("</table>")
		document.write("<hr>")
		document.write("<center><font size=\"-1\" face=\"times,'times new roman',serif\"><i>")
		document.write("Titus II Ministries &bull; 18605 49th Pl W, Lynnwood, WA 98037<br>")
		document.write("E-mail: <a href='mailto:info@titusII.org'>info@titusII.org</a>")
		document.write("</i></font></center>")
	}
		
