function initNav(){
	
	//First Tier - Top Nav
    if ( document.getElementById )
    {
        var nav = document.getElementById('nav');
        var navlis = nav.getElementsByTagName('li');
        
        for ( var i=0; i<navlis.length; i++ )
        {            
            if ( navlis.item(i).getElementsByTagName('ul')[0] )
            {
				addAnEvent(navlis.item(i),'keyup',visible);
                addAnEvent(navlis.item(i),'mouseover',visible);
                addAnEvent(navlis.item(i),'mouseout',hidden);
                addAnEvent(navlis.item(i),'blur',hidden);
                addAnEvent(navlis.item(i),'focus',visible);
            					
				var navlisul = navlis.item(i).getElementsByTagName('ul');
			
			}
        }
		
		//turns current subnav item on
		if (!document.getElementById('bodyhome')){
		//alert("meow");
			var subnav = document.getElementById('subnav');
			var subnavas = subnav.getElementsByTagName('a');
			
			for (var a=0; a<subnavas.length; a++){
				
				var currenthref=String(subnavas[a].href);
				//currenthref=currenthref.split("?");
				
				var currentloc=String(document.location);
				//currentloc=currentloc.split("?");
	
				if (unescape(currenthref)==unescape(currentloc)){
					subnavas[a].className="subnavon"
				}
			}
		}
	}

	//Second Tier - Top Nav
    if ( document.getElementById )
    {
        var nav = document.getElementById('navbottom');
        var navlis = nav.getElementsByTagName('li');
       
        for ( var i=0; i<navlis.length; i++ )
        {            
            if ( navlis.item(i).getElementsByTagName('ul')[0] )
            {	 
				addAnEvent(navlis.item(i),'keyup',visible);
                addAnEvent(navlis.item(i),'mouseover',visible);
                addAnEvent(navlis.item(i),'mouseout',hidden);
                addAnEvent(navlis.item(i),'blur',hidden);
                addAnEvent(navlis.item(i),'focus',visible);
            					
				var navlisul = navlis.item(i).getElementsByTagName('ul');
			}
        }
		
		//turns current subnav item on
		if (!document.getElementById('bodyhome')){
			var subnav = document.getElementById('subnav');
			var subnavas = subnav.getElementsByTagName('a');
			
			for (var a=0; a<subnavas.length; a++){
				
				var currenthref=String(subnavas[a].href);
				//currenthref=currenthref.split("?");
				
				var currentloc=String(document.location);
				//currentloc=currentloc.split("?");
	
				if (unescape(currenthref)==unescape(currentloc)){
					subnavas[a].className="subnavon"
					
				}
			}
		}
	}
	
	 if (document.all) sfTarget(); initGlossary(); pageFunc(); initPDFWindow(); initPopup(); initPopup2();
	if (document.getElementById('Photo')) {
	document.getElementById('Photo').onload = initFade;
	loadAPI();
	}
	if (document.getElementById('randphoto')) homePhoto();
	
	if (document.getElementById('randphoto2')) sectionPhoto();
}

function addAnEvent( target, eventName, functionName){
        eval('target.on'+eventName+'=functionName');
}

function hidden(){
	this.getElementsByTagName('ul')[0].className = "";
}

function visible(){

    this.getElementsByTagName('ul')[0].className = "block";
}
    
function genRand(x) {
 var range = x;
 return Math.floor(Math.random() * range) + 1;
}

function homePhoto() {
	var ph = genRand(4);
	document.getElementById("randphoto").innerHTML = "<img src=\"images/h-ph"+ph+".jpg\" width=\"380\" height=\"250\" alt=\"Providing leadership for the Bay Area legal community and general public since 1872.\" />"
}


function sectionPhoto() {
	var ph = genRand(6);
	document.getElementById("randphoto2").innerHTML = "<img src=\"images/s-photo"+ph+".jpg\" width=\"160\" height=\"88\" alt=\"Providing leadership for the Bay Area legal community and general public since 1872.\" />"
}



if (location.href.indexOf("print=1") > 0){
	for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
	if(a.getAttribute('title').indexOf("printfriendly") !=-1)
		a.disabled = false;
	}
}

function switchtomain() {
	var url = location.href;
	if ((location.href.indexOf("#") > 0)) {
		var tempurl = url.split("#");
		url = tempurl[0]
	}
	url = (location.href.indexOf("?print=1") < 0) ? url.split("&print=1") : url.split("?print=1");
	this.href = url[0];
}

function switchtoprint() {
	var url = location.href;
	if ((location.href.indexOf("#") > 0)) {
		var tempurl = url.split("#");
		url = tempurl[0]
	}
	url += (location.href.indexOf("?") < 0) ? "?print=1" : "&print=1";
	this.href = url;
}

function sendit() {
	FormWindow = window.open ("/inc/sendit.asp?loc="+escape(document.location) + "&title=" + escape(document.title),"formWindow","scrollbars=1,width=400,height=560,status=0,menubar=0,location=0,toolbar=0,screenX=0,screenY=0,left=0,top=0, resizable");
	FormWindow.focus ();
	return false;
        
}

function initPDFWindow(){
	var links=document.getElementsByTagName("a");
	var linky;
	for(var i=0;i<links.length;i++){
		linky = links[i];
		if(linky.href.indexOf(".wpd") > 0 || linky.href.indexOf(".pdf") > 0 || linky.href.indexOf(".hpd") > 0 || linky.href.indexOf(".doc") > 0 || linky.href.indexOf("getdoc.aspx?dl=0") > 0){
			linky.title = "This document will open in a new window.";
			linky.innerHTML += "<img src=\"https://www.sfbar.org/img/icon-newwin.gif\" width=\"23\" height=\"13\" alt=\"\" />";
			addAnEvent(linky,'click',PDFWindow);
		}
	}
}

function PDFWindow(){
 		appWindow = window.open (this.href,"appWindow","width=600,height=300,toolbar=0,resizable=1");
		appWindow.focus ();
		return false;        
}

function initPopup(){
	var links=document.getElementsByTagName("a");
	var linkp;
	for(var i=0;i<links.length;i++){
		linkp = links[i];
		if(linkp.className == "popup"){
			linkp.title = "This document will open in a new window.";
			linkp.innerHTML += "<img src=\"https://www.sfbar.org/img/icon-newwin.gif\" width=\"23\" height=\"13\" alt=\"\" />";
			addAnEvent(linkp,'click',Popup);
		}
	}
}

function Popup(){
 		appWindow = window.open (this.href,"appWindow","width=600,height=400,toolbar=1,scrollbars=1,resizable=1");
		appWindow.focus ();
		return false;        
}

//For Judicial Forum Videos
function initPopup2(){
	var links=document.getElementsByTagName("a");
	var linkp;
	
	for(var i=0;i<links.length;i++){
		linkp = links[i];
		if(linkp.className == "popup2"){
			linkp.innerHTML;
			addAnEvent(linkp,'click',Popup2);
		}
	}
}

function Popup2(){
 		appWindow = window.open (this.href,"appWindow","width=500,height=480,toolbar=0,scrollbars=1,resizable=1");
		appWindow.focus ();
		
		return false;        
}


function initGlossary() {
	if (document.getElementById('maincontent')){
		var mc = document.getElementById('maincontent');
		var gas = mc.getElementsByTagName('a');
			for ( var z=0; z<gas.length; z++ ){            
           		if ( gas[z].className == "glossary" ){
					gas[z].setAttribute("title", "Define this term");
					addAnEvent(gas.item(z),'click',glossary);
				}
        	}
		}
}

function glossary(){
 		GlossaryWindow = window.open (this.href,"glossaryWindow","width=300,height=200,scrollbars=1,resizable=1");
		GlossaryWindow.focus ();
		return false;        
}

function pageFunc(){
	if (document.getElementById('printfriendly')){
		var pflink = document.getElementById('printfriendly');
		addAnEvent(pflink,'click',switchtoprint);
	}

	if (document.getElementById('webfriendly')){
		var wflink = document.getElementById('webfriendly');
		addAnEvent(wflink,'click',switchtomain);
	}
		
	if (document.getElementById('send')){
		var sclink = document.getElementById('send');
		addAnEvent(sclink,'click',sendit);
	}
}

function sfTarget () {
	var sfEls=document.getElementsByTagName("div");
	var aEls = document.getElementsByTagName("a");
	document.lastTarget = null;
	for (var i=0; i<sfEls.length; i++) {
		if (sfEls[i].id) {
			if (location.hash==("#" + sfEls[i].id)) {
				sfEls[i].className+=" sftarget";
				document.lastTarget=sfEls[i];
			}
			for (var j=0; j<aEls.length; j++) {
				if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
					if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
					if (this.targetEl) this.targetEl.className+=" sftarget"; document.lastTarget=this.targetEl;
					return true;
				}
			}
		}
	}
}

window.onload=initNav;


