// Navigation Functions for Dogchurch.org

var baseNOM = "dogchurch";
var baseURL = "http://www.dogchurch.org/";
var baseDIR = "public_html/";
var blogURL = "http://dogchurch.blogspot.com/";

// implements the "Pick a Room" pulldown menu. assumes all URLs are absolute
function goRoom(theRoom){
	var newPage=theRoom.options[theRoom.selectedIndex].value;
	if(newPage == "" || newPage == "none" || newPage == self.location.href) { return; }
	newPage=makeRelative(newPage);
	if(newPage.indexOf("sitediag") > -1){ sitewin=window.open('http://www.dogchurch.org/narthex/sitediag.html','FloorPlan','toolbar=no,location=no,personalbar=no'); sitewin.focus(); return; }
	if(newPage.indexOf(".wav") > -1){ soundwin=window.open(newPage,'bark','width=102,height=17,scrollbars=no'); soundwin.focus(); return; }
	if(newPage.indexOf("http") > -1 && newPage.indexOf(baseURL) < 0 && newPage.indexOf(blogURL) < 0){ win2=window.open(newPage,'win2','toolbar=yes,location=yes,personalbar=yes,scrollbars=yes'); win2.focus; return; }
	self.location = newPage;
}

// converts absolute URLs to relative, called by goRoom
function makeRelative(newPage){
	var oldPage = self.location.href;
	newStart = newPage.indexOf(baseURL);
	if(newStart > -1) { // newPage is onsite
		oldStart = oldPage.indexOf(baseDIR);
		if(oldStart > -1) { // we are local, convert to relative
			oldPage = oldPage.substring(oldStart+baseDIR.length);
			newPage = newPage.substring(newStart+baseURL.length);
			oldRay = oldPage.split("/");
			newRay = newPage.split("/");
			istop = Math.min(oldRay.length, newRay.length);
			matches = -1;
			dirDepth = oldRay.length - 1;
			for(i=0;i<istop;i++){
				if(newRay[i] == oldRay[i]) matches++ ;
				else break;
			}
			if(matches > -1) {
				newPage = newPage.substring(indexOf(newRay[matches])+newRay[matches].length);
				dirDepth-=matches;
			}
			for(i=0;i<dirDepth;++i) newPage = "../" + newPage;
		}
	}
	return newPage;
}

// prints which Room user is in for page header
function getNav(){
	var myNav = ""; var myRoom = "";
	var myPage = document.location.href;
	var myStart = myPage.indexOf(baseDIR); // local
	if(myStart < 0) myStart = myPage.indexOf(baseNOM); // onsite
	myPage = myPage.slice(myStart,-1);
	var myRay = myPage.split("/");
	if(myRay.length < 2) {
		myNav = "<font size = '+2' color='#990033'><b>HOME</b></font><font size='+1' color='#990033'>, Virtual Church of the Blind Chihuahua</font>";
		return myNav;
	}
	myRoom = myRay[1].toUpperCase();
	if(myRoom.indexOf("INDEX")>-1 || myRay.length < 2) { // Page is the Home Page
		myNav = "<font size = '+2' color='#990033'><b>HOME</b></font><font size='+1' color='#990033'>, Virtual Church of the Blind Chihuahua</font>";
	} else {
		if(myRoom == "NARTHEX") { // Page is in Narthex
			myNav = "<font size = '+1' color='#990033'><b><a href="+baseURL+">HOME</a></b></font> > " + document.title;
		} else if((myRay[myRay.length-1].indexOf("index")>-1) || (myRay[myRay.length-1].indexOf(".")<0)) { // Page is Index page of a directory
			myNav = "<font size = '+2' color='#990033'><b>"+myRoom+"</b></font>"; 
		} else { // Page is in myRoom, with document.title
			myNav = "<font size='+1' color='#990033'><b><a href="+baseURL+myRay[1]+">"+myRoom+"</a></b></font> > "+document.title;
		}
	}
//	myNav="<p class='sidebar' style='padding-top: 16px;'>" + myNav + "</p>";
	return myNav;
}
