/* GLOBAL VARIABLES */
var NGRef = '';
var GridLetter = ''
var Easting = 0;
var Northing = 0;
var osMap;
var osEast = 0;
var osNorth = 0;
var osContent = '';
var osPos;
var osSize;
var osMarker;
var osIcon;

function fncMapInit(zIn) {
	var zoom = 7;
	if (zIn != null) { zoom = zIn; }
	osMap = new OpenSpace.Map('map');
	osMap.setCenter(new OpenSpace.MapPoint(osEast,osNorth),zoom);
}

/* CamelCase String Method using Prototype Function */
String.prototype.toCamelCase = function() {
	var re = /\s/;
	var words = this.split(re);
	re = /(\S)(\S+)/;
	for (i = words.length - 1; i >= 0; i--) {
		re.exec(words[i]);
		words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
	}
	return words.join(' ');
}
function fncCamelCase(txt) {
	var inTxt = txt.toLowerCase();
	var on = /\s/;
	var words = inTxt.split(on);
	for (n = words.length - 1; n >= 0; n--) {
		var wn = new String(words[n]);
		var wc = wn.substring(0,1);
		var wr = wn.substring(1);
		wc = wc.toUpperCase();
		words[n] = wc + wr;
	}
	return words.join(' ');
}
/* write image link with rollover */
function fncWriteImgLink(imgno,href,alt,omover,omout,dim) {
	var xTxt="<a href='"+href+"'";
	xTxt+=" onmouseover='JavaScript:document.images["+imgno+'].src="'+omover+'";'+"'";
	xTxt+=" onmouseout='JavaScript:document.images["+imgno+'].src="'+omout+'";'+"'>";
	xTxt+="<img src='"+omout+"' alt='"+alt+"' border='0px' "+dim+"' /></a>";
	document.write(xTxt);
}

/* Get Ordnance Survey Coordinates from NGRef */
function fncGetCoords(ingr) {
	var NGR=ingr;
	var inLength=NGR.length;
	if (inLength == 2) { return; }
	var lcGridLetter=new String(NGR.slice(0,2));
	GridLetter=lcGridLetter.toUpperCase();
	var MidEast='', MidNorth='', EndBit='X';
	if (inLength==6) { MidEast=NGR.slice(2,4); MidNorth=NGR.slice(4,6); EndBit='000'; }
	if (inLength==8) { MidEast=NGR.slice(2,5); MidNorth=NGR.slice(5,8); EndBit='00'; }
	if (inLength==10) { MidEast=NGR.slice(2,6); MidNorth=NGR.slice(6,10); EndBit='0'; }
	if (inLength==12) { MidEast=NGR.slice(2,7); MidNorth=NGR.slice(7,12); EndBit=''; }
	if (EndBit=='X') { window.alert('Input NGR must be either 6, 8, 10 or 12 characters in length.'); }
	var StartEast='', StartNorth='';
	if (GridLetter=='SV') { StartEast='0'; StartNorth='0'; }
	if (GridLetter=='SW') { StartEast='1'; StartNorth='0'; }
	if (GridLetter=='SX') { StartEast='2'; StartNorth='0'; }
	if (GridLetter=='SY') { StartEast='3'; StartNorth='0'; }
	if (GridLetter=='SZ') { StartEast='4'; StartNorth='0'; }
	if (GridLetter=='TV') { StartEast='5'; StartNorth='0'; }
	if (GridLetter=='SR') { StartEast='1'; StartNorth='1'; }
	if (GridLetter=='SS') { StartEast='2'; StartNorth='1'; }
	if (GridLetter=='ST') { StartEast='3'; StartNorth='1'; }
	if (GridLetter=='SU') { StartEast='4'; StartNorth='1'; }
	if (GridLetter=='TQ') { StartEast='5'; StartNorth='1'; }
	if (GridLetter=='TR') { StartEast='6'; StartNorth='1'; }
	if (GridLetter=='SM') { StartEast='1'; StartNorth='2'; }
	if (GridLetter=='SN') { StartEast='2'; StartNorth='2'; }
	if (GridLetter=='SO') { StartEast='3'; StartNorth='2'; }
	if (GridLetter=='SP') { StartEast='4'; StartNorth='2'; }
	if (GridLetter=='TL') { StartEast='5'; StartNorth='2'; }
	if (GridLetter=='TM') { StartEast='6'; StartNorth='2'; }
	if (GridLetter=='SH') { StartEast='2'; StartNorth='3'; }
	if (GridLetter=='SJ') { StartEast='3'; StartNorth='3'; }
	if (GridLetter=='SK') { StartEast='4'; StartNorth='3'; }
	if (GridLetter=='TF') { StartEast='5'; StartNorth='3'; }
	if (GridLetter=='TG') { StartEast='6'; StartNorth='3'; }
	if (GridLetter=='SC') { StartEast='2'; StartNorth='4'; }
	if (GridLetter=='SD') { StartEast='3'; StartNorth='4'; }
	if (GridLetter=='SE') { StartEast='4'; StartNorth='4'; }
	if (GridLetter=='TA') { StartEast='5'; StartNorth='4'; }
	if (GridLetter=='NW') { StartEast='1'; StartNorth='5'; }
	if (GridLetter=='NX') { StartEast='2'; StartNorth='5'; }
	if (GridLetter=='NY') { StartEast='3'; StartNorth='5'; }
	if (GridLetter=='NZ') { StartEast='4'; StartNorth='5'; }
	if (GridLetter=='NR') { StartEast='1'; StartNorth='6'; }
	if (GridLetter=='NS') { StartEast='2'; StartNorth='6'; }
	if (GridLetter=='NT') { StartEast='3'; StartNorth='6'; }
	if (GridLetter=='NU') { StartEast='4'; StartNorth='6'; }
	if (GridLetter=='NL') { StartEast='0'; StartNorth='7'; }
	if (GridLetter=='NM') { StartEast='1'; StartNorth='7'; }
	if (GridLetter=='NN') { StartEast='2'; StartNorth='7'; }
	if (GridLetter=='NO') { StartEast='3'; StartNorth='7'; }
	if (GridLetter=='NF') { StartEast='0'; StartNorth='8'; }
	if (GridLetter=='NG') { StartEast='1'; StartNorth='8'; }
	if (GridLetter=='NH') { StartEast='2'; StartNorth='8'; }
	if (GridLetter=='NJ') { StartEast='3'; StartNorth='8'; }
	if (GridLetter=='NK') { StartEast='4'; StartNorth='8'; }
	if (GridLetter=='NA') { StartEast='0'; StartNorth='9'; }
	if (GridLetter=='NB') { StartEast='1'; StartNorth='9'; }
	if (GridLetter=='NC') { StartEast='2'; StartNorth='9'; }
	if (GridLetter=='ND') { StartEast='3'; StartNorth='9'; }
	if (GridLetter=='HY') { StartEast='3'; StartNorth='10'; }
	if (GridLetter=='HU') { StartEast='4'; StartNorth='11'; }
	if (StartEast=='') { window.alert('Input NGR letter not recognised.'); }

	Easting=StartEast+MidEast+EndBit;
	Northing=StartNorth+MidNorth+EndBit;
}

/* write page headings and icons */
function fncCreateLynx(inCode,frontierInd,latin,type,name) {
	document.write("<div style='width:720px;margin-left:auto;margin-right:auto;'>");

	var ngrCode=inCode.substr(0,2);
	var FullNGR=inCode;
	var imgNo=0, hrefTxt='', altTxt=''. mOverImg='', mOutImg='', imgDim='';

	document.write("<div style='float:left;width:70px;height:140px;'><table width='100%'><tr valign='top'><td align='left'>");

	imgNo=0;
	hrefTxt='../index.htm';
	altTxt='RBO Main Menu';
	mOverImg='../images/_rbo_ani.gif';
	mOutImg='../images/_rbo_ico.gif';
	imgDim="width='64px' height='64px'";

	fncWriteImgLink(imgNo,hrefTxt,altTxt,mOverImg,mOutImg,imgDim);

	document.write("</td></tr><tr valign='top'><td align='left'>");

	/* Replace following with fncWriteImgLink() to /places/index.htm - HTM, ICO and ANI needed! */
	document.write("<img src='../images/_dummy.gif' alt='' width='64px' height='64px' />");

	document.write('</td></tr></table></div>');

	document.write("<div style='float:right;width:70px;height:140px;'><table width='100%'><tr valign='top'><td align='right'>");

	imgNo=2;
//	hrefTxt='../xml/ngr-layermap.htm?code='+ngrCode;
	hrefTxt='../cgi-bin/ngr-layermap.pl?code='+ngrCode;
	altTxt='NGR Map - Grid Letter '+ngrCode;
	mOverImg='../images/_ngr_ani.gif';
	mOutImg='../images/_ngr_ico.gif';
	imgDim="width='60px' height='57px'";

	if (ngrCode.substring(0,1)!='N') {
		mOutImg='../images/_' + ngrCode.toLowerCase() + '_i.gif';
	}

	fncWriteImgLink(imgNo,hrefTxt,altTxt,mOverImg,mOutImg,imgDim);

	document.write("</td></tr><tr valign='top'><td align='right'>");

	if (frontierInd!=null && frontierInd!='') {

		imgNo=3;
		hrefTxt='../frontiers/hw_menu.htm';
		altTxt='Hadrian&#39;s Wall Menu';
		mOverImg='../images/_'+frontierInd.toLowerCase()+'_ani.gif';
		mOutImg='../images/_'+frontierInd.toLowerCase()+'_ico.gif';
		imgDim=" width='64px' height='64px'";

		if (frontierInd=="AW") {
			hrefTxt='../frontiers/antonine.htm';
			altTxt='Antonine Wall Menu';
		}

		fncWriteImgLink(imgNo,hrefTxt,altTxt,mOverImg,mOutImg,imgDim);

	} else {

		document.write("<img src='../images/_dummy.gif' border='0px' alt='' width='64px' height='64px' />");

	}

	document.write('</td></tr></table></div>');

	/* Write Heading One Text */

	document.write("<h1><span class='rbo'>www.Roman-Britain.org</span>");

	var headNo=0;
	var xClass = '';

	if (latin!=null && latin!='') {
		xTxt="<br /><small><i class='rbo'>"+latin+'</i>';
		xClass=" class='rbo'";
		document.write(xTxt);
		headNo++;
	}
	if (type!=null && type!='') {
		xTxt='<br /><small><span>'+type+'</span>';
		document.write(xTxt);
		headNo++;
	}
	if (name!=null && name!='') {
		xTxt='<br /><small><span>'+name+'</span>';
		document.write(xTxt);
		headNo++;
	}
	for (var n=0; n<=headNo; n++) {
		document.write('</small>');
	} 

	document.write('</h1>');

	/* Write Hidden Form for Footer Script */

	document.write("<div style='display:none;'>");
	document.write("<form name='NGRForm' method='get' action='http://www.roman-britain.org/'>");
	document.write("<input type='hidden' name='NGRef' value='"+FullNGR+"' />");
	document.write("</form></div>");

	/* Write Navigation Links */

	var xTitle = document.title;
//	xTitle = xTitle.toCamelCase();
	xTitle = fncCamelCase(xTitle);

	document.write("<h5 class='navlist'>");
	document.write("<a href='../index.htm'>Index</a> ==> ");
	document.write("<a href='index.htm'>Places</a> ==> ");
	document.write("<a href='"+window.location+"'"+xClass+">"+xTitle+"</a>");
	document.write("</h5>");

	fncGetCoords(FullNGR);

	if (latin!=null && latin!='') { osContent = "<span class='rbo'>" + latin + '</span><br />'; }
	osContent += type + '<br />(' + name + ')';
	osNorth = Northing;
	osEast = Easting;

}

/* Create Map Markers */
function fncCreateMarker(ingr,itxt) {
	fncGetCoords(ingr);
	osContent = itxt;
	osNorth = Northing;
	osEast = Easting;
	osPos = new OpenSpace.MapPoint(osEast,osNorth);
	osSize = new OpenLayers.Size(240,120);
	osMarker = osMap.createMarker(osPos, null, osContent, osSize);
}
function fncDetailedMarker(ingr,icon,itxt) {
	fncGetCoords(ingr);
	osContent = itxt;
	osNorth = Northing;
	osEast = Easting;
	osPos = new OpenSpace.MapPoint(osEast,osNorth);
	osSize = new OpenLayers.Size(240,120);
	switch (icon) {
		case 'Fort':
			var size = new OpenLayers.Size(11,9);
			var offset = new OpenLayers.Pixel(-5,-4);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_F.gif', size, offset);
			break;
		case 'Fortress':
			var size = new OpenLayers.Size(15,13);
			var offset = new OpenLayers.Pixel(-7,-6);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_L.gif', size, offset);
			break;
		case 'Vexillation':
			var size = new OpenLayers.Size(15,13);
			var offset = new OpenLayers.Pixel(-7,-6);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_X.gif', size, offset);
			break;
		case 'Fortlet':
			var size = new OpenLayers.Size(7,7);
			var offset = new OpenLayers.Pixel(-3,-3);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_R.gif', size, offset);
			break;
		case 'Camp':
			var size = new OpenLayers.Size(11,9);
			var offset = new OpenLayers.Pixel(-5,-4);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_E.gif', size, offset);
			break;
		case 'Station':
			var size = new OpenLayers.Size(5,5);
			var offset = new OpenLayers.Pixel(-2,-2);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_Y.gif', size, offset);
			break;
		case 'Works':
			var size = new OpenLayers.Size(9,7);
			var offset = new OpenLayers.Pixel(-4,-3);
			osIcon = new OpenLayers.Icon('http://www.roman-britain.org/images/_W.gif', size, offset);
			break;
		default:
			osIcon = null;
	}
	osMarker = osMap.createMarker(osPos, osIcon, osContent, osSize);
}

