                                           /*         locus.js 
This file required for all the Arjay scripts
It has location variables and a function to relocate files to their proper aframes

version 1.1 2007 02 02
version 1.2 2007 03 07 fixed local testing by adding tryLocal and altering loc2aFrame tests
version 1.3 2007 03 26 another fix, this time to add a parameter so trailing numbers on file names are not automatically sequences.
version 1.4 2007 09 05 change test vs production site logic as now using a different kind of local server
version 1.5 2007 10 30 remove some redundant code
version 1.6 2007 11 19 added autoAdjustLocalLinks and code to support
*/

	// variables relating to paths; some sniffers
	
	// note that in the two urls following the trailing slash is not optional; it must be there
var base = location.host; //top of directory hierarchy 
var baseURL = 'http://'+ base+'/'
var pathToChainLinkFolder = "scripts/chainlinks/"	// unless you change things, this is "scripts/chainlinks/"
var nameOfDefaultCSSFile = "chainlinkswnh.css" // this is for chainlinks
	// what to do if browser incompatible
var tryAltSiteIfOldBrowser = true	// use the path below if defined to access an older and non dynamic site; else if set to false, all you get is a nagging error alert saying you have to upgrade your browser
var pathToAltNonDynamicSite = "www0/index.htm"	// could be an absolute path or relative to the base set above
var deftargetId = "aFrame0"; // name of default aFrame to direct top level files into
var defAFrameFolder = "/"; // name of top level folder for local testing
var tryDefaultFile = false;
var autoAdjustLocalLinks = true; //set to true to adjust ALL local links in loaded files located in a folder to point inside the folder. Set to false if you want to call the adjust proc manually by selector.
var defaultUrl = "defcontents.html"; // global default for aframe contents; can be overridden by passing a parameter
var fileSuffix = ".html";
var printAlerts = true;
var printSelErrors = false;
var embedErrorMsgs = false; // if set to false, no error messages displayed in id'd box for content
var cantCreateAjax = "Sorry, but I couldn't create an XMLHttpRequest.  Perhaps your browser is too old and needs updating (pre IE5.5 or Mozilla 6)."
var ajaxCompErr = "<span>There was a problem completing the AJAX request (not yet at step 4). Your browser may be too old or too incompartible to run Ajax pages.</span> "
var tooOld = "Sorry, your browser is too old to run this Ajax page. Please upgrade your browser.";
var fileProb1 = "There was a problem reading the file "
var fileprob2 = ".<BR>The HTML error is "
var badAFrameFileName = "File names transferring into aFrames must have at least one nondigit before any concluding numbers, and letters as a file extension.";
var selErrorMsg = "There are no items with the selector "

 // Call this first before trying to do rounded corners
function roundSniff()
{
	if(!document.getElementById || !document.createElement)
		return(false);
	var b=navigator.userAgent.toLowerCase();
	if(b.indexOf("msie 5")>0 && b.indexOf("opera")==-1)
		return(false);
	return(true);
}

 // Call this before using aFrames. The aFrame script requires various methods so will do nothing in ie 5 for instance.
function aFrameSniff()
{
	if(!document.documentElement.innerHTML || !document.getElementById) {if (printAlerts) window.alert (tooOld); return(false)}; // ||!test.push // replaced push to extend some functionality
	return(true);
}

/* 	loc2aFrame munges urls to force browsers going directly to a file intended to be framed back to main where the file is placed into an aFrame
	if the location filename is index, does nothing (safety)
		If a host path (relative to the main url) is given, the transfer is made to it.
	if the file is numbered, like "file23.xxx", transfer to baseURL with query str ?file = num; receiver will treat this as part of a sequence
		where the part "file" is the same as the id of the target, so this really amounts to ?targetId=number
	 	if the file is not numbered, transfer to baseURL with query 
	 			1. ?pHostFrameId=filename if a pHostFrameId is supplied
				2. ?dir=filename; here for file = file.xxx, query is ?dir = file.xxx	 		
			assumption: class of aFrame box is "dir" and it will presumably load the filename
			 if there is no directory in the location, fall back to 1 using ?defAFrameId=filename and the receiving code tries to know what to do.
*/

function loc2aFrame (pHostFile, pHostFrameId, isSeq)
{	
	if(!aFrameSniff) return;
	var path = location.pathname;
	var re, str1, str2, queryString, newURL;
	var hostFile = (pHostFile)? pHostFile : ""
	var fileInDir =  path.indexOf ('/') != -1// if true, file is down from at least one local directory
	var isTopIndex = (!fileInDir && path.substr (0,5) == "index.") // test if supplied file is top level index
		// prepare to slice location into dir and filename
	var loc = path.lastIndexOf ('/'); // get start of bare filename
		// but in order of failure, use the supplied id initially as a dir, then a dir name before the file, then the default aFrameFolder set above
	var frameName = (pHostFrameId)? pHostFrameId: ((loc != -1)? path.slice (0,loc) : defAFrameFolder); // won't be a slash if using an id, but...
	var fileName = path.slice (loc + 1); 
	if (fileName.length == 0 || isTopIndex) return // no filename or was at top level already ==> do nothing
			// slice filename into name and number (in case this is a numbered sequence of files)
	re = /^(.*\D)(\d*)(\.[a-zA-Z]*)$/; // [1] = bare filename; [2] = digit; [3] = suffix
	var uArray = re.exec(fileName);
	if (!uArray) {if (printAlerts) window.alert (badAFrameFileName); return} // give up if can't slice it at all
	if (!isTopIndex) // don't do this if at top level index
		{	
			if (uArray[2].length > 0 &&isSeq) {str1 = uArray[1]; str2 = uArray[2]} // only use a trailing number as part of a sequence if told to
				// this latter gives us the query string ?barefileName=suffixdigit
				// otherwise, we get the query string with ?frameName=localPath (the latter including any directories)
			else {str1 = frameName; str2 = path}
			queryString = "?"+str1+"="+str2;
			newURL = baseURL+hostFile+queryString; // piece together a new location and go there
			window.location.replace (newURL);
		}
}
