var pagePrefix = '';
var pageNumber = 1;
var lastPage = -1;
var atTop = true;
var eclipseLanded = false;
var reIncrement = 1;
var isIE = false, isNS6 = false, isNS7 = false, isNS = false, isOpera = false;
var appversion;
var ignoreResize = false;
var topDocument = document;
var degenerate = false;
var siteLoaded = false;

// Original:  Gregor (legreg@legreg.de) 


//
//Sort out whether I'm dealing with IE or Naviagator
//Determine the web browser type and version
//
var useragent = navigator.userAgent;
var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;
var pos = useragent.indexOf('MSIE');
var bVer = null;
	if (pos > -1) {
		bVer = useragent.substring(pos + 5);
		pos = bVer.indexOf(';');
		bVer = bVer.substring(0,pos);
		isIE = true;
	} else{
		pos = useragent.indexOf('Opera');
		if (pos > -1)	{
			bVer = useragent.substring(pos + 6);
			pos = bVer.indexOf(' ');
			bVer = bVer.substring(0, pos);
			isOpera = true;
		}else if (bName == "Netscape") {
			isNS = true;
			bVer = useragent.substring(8);
			pos = bVer.indexOf(' ');
			bVer = bVer.substring(0, pos);
			if (parseInt(navigator.appVersion) >= 5) {
				pos = useragent.lastIndexOf('/');
				bVer = useragent.substring(pos + 1);
			}
		}
	}
	appversion = parseInt( bVer );
	if( isNS ){
		isNS7 = (appversion >= 7);
		isNS6 = !isNS7 && (appversion >= 6);
	}
	if( !tofjsloaded ){
	  if( (isNS && !isNS7) || (isIE && appversion<5 ) ){
		var proceed = top.confirm( "This site is best viewed with IE5 (or greater) or Netscape 7.\n  It's appearance under other browsers is not guaranteed\nsince transparency effects may be lost.\n\nDo you wish to proceed?");		

		if( !proceed ){
			top.close();
		} else {
			degenerate = true;
			tofjsloaded = true;
		}
	  }
	}
//
//Load the new body header and page contents
//Sensitive to whether header is null.  If so only the body is loaded.
function loadBody( header,contents ){
	bodyContentDownload.document.getElementById("content").innerHTML = "<b>Content loading ... Please wait</b>";
	if( header!=null ){
		bodyHeaderDownload.location.replace(header);
	}
	bodyContentDownload.location.replace(contents);
}
//Load a body from a button
function acloadBody( dir,header,contents ){
		loadBody( (header==null)?null:(dir + '/' + header) ,dir + '/' + contents );
}
//Tne equivalent fuinctions for the sidebar	   
function loadSidebar( header,contents ){
		sidebarContentDownload.document.getElementById("content").innerHTML = "<b>Sidebar loading ... Please wait</b>";
		sidebarHeaderDownload.location.replace(header);
		sidebarContentDownload.location.replace(contents);
}
function acloadSidebar( dir,header,contents ){
		loadSidebar( dir + '/' + header,dir + '/' + contents );
}
//Navagate individual pages in a multipage body
//Show/hide the naviagaion graphics
function showNavigation( target,visibility ){
	topDocument.getElementById( target ).style.visibility = visibility;
}
//Hide the navigation graphics
function clearNavigation(){
	showNavigation( "nextImage","hidden" );
	showNavigation( "prevImage","hidden" );
	lastPage = -1;
	pageNumber = 1;
}
//Set the navigation prefix string
function navPrefix( dir,prefix ){
	pagePrefix = dir + '/' + prefix + '_p';
}
//Hide/show the navigation buttons depending on the current page and how many pages there are
function checkNavigation(){
	if(lastPage<=1 ){
		clearNavigation();
	}
	else if( pageNumber==1 ){
		showNavigation( "nextImage","visible" );
		showNavigation( "prevImage","hidden" );
	} else if ( pageNumber==lastPage ){
		showNavigation( "nextImage","hidden" );
		showNavigation( "prevImage","visible" );
	} else {
		showNavigation( "nextImage","visible" );
		showNavigation( "prevImage","visible" );
	}
}
//Navigate to the first page of a multipage body
function gotoPage1( dir, header, prefix, limit){
	acloadBody( dir,header,prefix + '_p1.htm' );
	navPrefix(dir, prefix);
	pageNumber = 1;
	lastPage = -1;
	if( limit>=1 ){
		lastPage = limit;
		checkNavigation();
	}
}
//Go to the next page
function nextPage(){
	location.hash = '#cvpage' + pageNumber + '_top';
	pageNumber++;
	if( lastPage>0 ){
		if( pageNumber>lastPage ){
			pageNumber = lastPage;
		}
		checkNavigation();
	}
	loadBody( null,pagePrefix + pageNumber + '.htm' );
}
//Go to the previous page
function prevPage(){
	location.hash = '#cvpage' + pageNumber + '_top';
	pageNumber--;
	if( pageNumber<1 ){
		pageNumber = 1;
	}
	if( lastPage>0 ){
		checkNavigation();
	}
	loadBody( null,pagePrefix + pageNumber + '.htm' );
}
//Float an area in the browser window
//If there is enough room on the page center the content horizontally and 
//leave some space at the top
function floatIt( eid, minwidth, minheight ){
	var target = document.getElementById( eid );
	var winW, winH;
	
 	if ( isIE ) {
  		winW = document.body.offsetWidth;
  		winH = document.body.offsetHeight;
 	} else {
  		winW = window.innerWidth;
  		winH = window.innerHeight;
 	}
	if( ignoreResize ){
		ignoreResize = false;
	} else {
		target.style.left = (winW>=minwidth)?((winW-900)/2) + "px":"0px";
	}
}
//
//-- Manage ancilllary windows
// Create an array of 15 null values
var otherWindows = [null,null,null,null,null,null,null,null,null,null,null,null,null,null];
//
//Close an individual window.
function closeIt( awindow ){
	if( awindow!=null){
		if(isIE) awindow.location = "blank.htm";
		awindow.close();
	}
}
//Close all open windows  - called on exit
function closeAll(){
	for(var i=0;i<otherWindows.length;i++){
		closeIt(otherWindows[i]);
	}
}
//Under NS6 loading divisions does not seem to work - so don't
function changeContentDiv(){
//place holder
}

//
//Functions to load content
//
//--DISCLAIMER
//Load the disclaimer page.  This happens if a user clicks on the copyright notice
//The sidebar is unchanged.  homeWindow indicates whether the function was invoked from an IFRAME
//child button (homeWindow = false) or the main panel (homeWindow = true).
function disclaimer( homeWindow ){
	var dt = (homeWindow)?'Home/':'./';
	
	clearNavigation();
	loadBody( dt + 'disclaimer_bodyheader.htm', dt + 'disclaimer_bodycontent.htm' );
}
//--ABOUT THIS SITE
//From a click on the about this site string in the main window.  Text has a link to disclaimer
function aboutThisSite(){
	clearNavigation();
	loadBody( 'Home/aboutsite_bodyheader.htm','Home/aboutsite_bodycontent.htm' );
}
//--HOME PAGES
//Load the home page
//Sensitive to location.hash.  If set to '#eclipse' then the SET Editor detail page pops up on entry.  This
//happens only on the first call.
function home(){
	if( !siteLoaded ){
		siteLoaded = true;
		document.getElementById( "sitetext" ).outerHTML = "<div style='TOFObject'>&nbsp;</div>";
	}
	loadSidebar( 'Home/home_sidebarheader.htm', (location.hash=='#eclipse')?'Home/eclipse_sidebarcontent.htm':'Home/home_sidebarcontent.htm' );
	atTop = true;
	theObjectForge( true );
	atTop = false;
	//If someone gets here via the Eclipse link then pop up the ACP editor stuff on the first time through
	if( location.hash=='#eclipse' && !eclipseLanded ){
		var myX, myY;
		
		eclipseLanded = true;
		if( isIE ){
			myX = self.screenLeft;
			myY = self.screenTop -100;
		}else{
			myX = self.screenX;
			myY = self.screenY;
		}
		MOJOacpeditor( myX+150, myY+200 );
		return true;
	}
}
//Service the buttons on the home page sidebar
//--The Object Forge
//homeWindow reflects where the call came from, ie. true => the top panel, false => an IFRAME
function theObjectForge( homeWindow ){
	var dt = (homeWindow)?'Home':'.';

	clearNavigation();
	acloadBody(dt,'home_bodyheader.htm','home_bodycontent.htm');
}	
//--Background
function companyBackground(){
	clearNavigation();
	acloadBody( '.','background_bodyheader.htm','background_bodycontent.htm' );
}
//--Products & Service
//This function is called from a sidebar button but navigation occurs from the main panel.  The call
//to navPrefix sorts this out.
function companyProds(){
	gotoPage1( '.','services_bodyheader.htm','services_bodycontent',2);
	//Reset the prefix so I can navigate from the top page
	navPrefix( 'Home','services_bodycontent' );	
}
//--Contact
function contact(){
	clearNavigation();
	acloadBody( '.','contact_bodyheader.htm','contact_bodycontent.htm' );
}
//--MASCOT 2002 PAGES
//
//--Load the MASCOT 2002 sidebar and pages
function mascot2002(){
	clearNavigation();
	loadSidebar( 'M2002/m2002_sidebarheader.htm','M2002/m2002_sidebarcontent.htm');
	atTop = true;
	m2002intro( true );
	atTop = false;
}
//--Introduction
function m2002intro( homeWindow ){
	var dt = (homeWindow)?'M2002':'.';

	gotoPage1( dt,'m2002_intro_bodyheader.htm','m2002_intro_bodycontent',2 );
	navPrefix( 'M2002','m2002_intro_bodycontent');
}
//--ACP Diagrams	
function m2002ACP(){
	gotoPage1( '.','m2002_acp_bodyheader.htm','m2002_acp_bodycontent',3 );
	navPrefix( 'M2002','m2002_acp_bodycontent');
}
//--System Element Templates
function m2002SET(){
	gotoPage1( '.','m2002_set_bodyheader.htm','m2002_set_bodycontent',2 );
	navPrefix( 'M2002','m2002_set_bodycontent');
}
//--The MASCOT Machine
function m2002MM(){
	gotoPage1( '.','m2002_mm_bodyheader.htm','m2002_mm_bodycontent',2 );
	navPrefix( 'M2002','m2002_mm_bodycontent');
}
//
//--MOJO Pages
//
//Load the MOJO pages and sidebar
function MOJO(){
	clearNavigation();
	loadSidebar( 'MOJO/MOJO_sidebarheader.htm','MOJO/MOJO_sidebarcontent.htm');
	atTop = true;
	MOJOintro( true );
	atTop = false;
}
//--Introducing MOJO
function MOJOintro( homeWindow ){
	var dt = (homeWindow)?'MOJO':'.';
	
	gotoPage1( dt,'MOJO_intro_bodyheader.htm','MOJO_intro_bodycontent',1 );
	navPrefix( 'MOJO','MOJO_intro_bodycontent');
}
//--The SETs Editor
function MOJOseteditor(){
	gotoPage1('.','MOJO_acpeditor_bodyheader.htm','MOJO_acpeditor_bodycontent',1);
	navPrefix( 'MOJO','MOJO_acpeditor_bodycontent');
}
//--The MOJO Mascot Machine
function MOJOmm(){
	gotoPage1('.','MOJO_config_bodyheader.htm','MOJO_config_bodycontent',1);
	navPrefix( 'MOJO','MOJO_config_bodycontent');
}
//--MOJO Devices
function MOJOdevice(){
	gotoPage1('.','MOJO_device_bodyheader.htm','MOJO_device_bodycontent',2);
	navPrefix( 'MOJO','MOJO_device_bodycontent');
}
//
//--Download Pages
var termsAccepted = false;	//a boolean to indicate whether a user has accpeted the terms and conditions
//
//--Indicate acceptance of terms and conditions.  Accept button on the Download page
//I attempt to reap the user's email address.  This function does some rudimentry checking for the proper form 
//of an email address.  Requires 'xx@bb.cc'.  Once t&cs are accpeted the t&c page is not displayed any more.
function acceptTerms(){
	var emform = bodyContentDownload.document.emailAddress;
	var emadd = emform.useremail.value;
	var proceed = false;
	
	if( emadd!="" ){
		var atindex = emadd.indexOf( "@" );
		
		if(atindex>1){
			proceed = ((emadd.lastIndexOf( ".")>atindex) && (emadd.length>(emadd.lastIndexOf( ".")+1)));
		}
	}
	if( !proceed ){
		var prompt = bodyContentDownload.document.getElementById( "formPrompt" );
		
		prompt.innerHTML = '<b><i>Please enter a valid emal address (someone@somewhere.xxx):';
		prompt.style.color = "#ff0000";
		emform.reset();
		return;
	}
	termsAccepted = true;
	emform.action = '../MailAddresses/mail_log.cgi';
	emform.target = "formResult";
	emform.submit();
	downloadMojo( false );
}
//--Decline button the the t&c page.
function declineTerms(){
	acloadBody('.',null,'downloads_declined_bodycontent.htm');
}
//--Show the Download pages.
//Displays t&c page if terms not accepted, otherwiae the MOJO R1.0 page.
function downloads(){
	clearNavigation();
	loadSidebar('Download/downloads_sidebarheader.htm','Download/downloads_sidebarcontent.htm');
	atTop = true;
	downloadDocs( true );
	atTop = false;
}
//--MOJO R1.0 - will not display until t&c accepted.
function downloadMojo( ){
	if(!termsAccepted){
		acloadBody('.','downloads_tandc_bodyheader.htm','downloads_tandc_bodycontent.htm');
	} else {
		acloadBody('.','downloads_mojo_bodyheader.htm','downloads_mojo_bodycontent.htm');
	}
}
//--Documents - will not display until t&c accepted.
function downloadDocs( homeWindow ){
	var dt = (homeWindow)?'Download':'.';

	acloadBody(dt,'downloads_docs_bodyheader.htm','downloads_docs_bodycontent.htm');
}
// Access ancillary windows
//
//--MASCOT 2002 - Detailed Example ACP Diagram
var copen = "height=640,width=820";

function acpExample(){
	otherWindows[0] = window.open( '../Other_Pages/ExampleACPDiagram/ExampleACP_page.htm','acpdiagram',copen);
}
//-- MASCOT 2002 - SET Document Detail
function setDetail(){
	otherWindows[1] = window.open('../Other_Pages/SETSDetail/SETS_page.htm','setdetail',copen);
}
//-- MASCOT 2002 - MASCOT Machine Detail
function MMDetail(){
	otherWindows[2] = window.open('../Other_Pages/MMDetail/MMDetail_page.htm','mmdetail',copen);
}
//-- MOJO - SETs Editor Detail and #eclipse enrty
function MOJOacpeditor( myLeft,myTop){
	var posit = copen;
	
	if( myLeft!=null && myTop!=null ){
		posit += ',left=' + myLeft + ',top=' + myTop;
	}
	otherWindows[3] = window.open('../Other_Pages/SETEditor/SETEditor_page.htm','mojoacpe',posit);
}
//-- MOJO - Timers Example
function MOJOtimersexample(){
	otherWindows[4] = window.open('../Other_Pages/TimersExample/TimersExample_page.htm','mojotimex',copen);
}
//-- MOJO - Dining Philosophers Example
function MOJOphilex(){
	otherWindows[5] = window.open('../Other_Pages/DiningPhils/DiningPhils_page.htm','mojodiningphils',copen);
}
//
//Manage downloads and documents on the Downloads pages
//
//--Download/Documents - The MOJO API
function mojoAPI(){
	otherWindows[6] = window.open('../Release1.0/api/index.html','mojoAPI');
}
//--Download/Documents - view MASCOT for the Modern World
function mftmw(){
	otherWindows[7] = window.open('../Release1.0/docs/mftmw.pdf','mftmw');
}
//--Download/Documents - view Abbreviated Reference
function abbref(){
	otherWindows[8] = window.open('../Release1.0/docs/abbref.pdf','abbref');
}
//
//--Other pages
//
//--Home/Products & Services - pop up a link to the Arcitech website
function Arcitech(){
	otherWindows[9] = window.open('http://www.arcitech2.com','arcitech');
}
//--About this Site - the Example site page
function jsExample(){
	otherWindows[10] = window.open('../ExamplePanel/ExamplePanel_page.htm','jsexample');
}
