// Maintain state, JavaScript
// Version: 21 October 2007
// Copyright 2004-2007, Louis W. G. Barton

/* Abstract:
 * Invoked by index.html (or other, language-specific index page) only.
 * Maintains state variables for the current session.
 * Holds 'global' references to right-frame objects.
 * The 'global' variables should be accessed or changed via the functions provided;
 *   they should not be read or written directly.
 * Changes the browser's document title [different to that of search engine indices?].
 *
 * Implementation Notes:
 * - JS "for (i in xxx)" is buggy; use "(i=0; i < xxx.length; i++)".
 */

/* GLOBAL CONSTANTS: */
var state_debug=false;  // Hard-coded: true=generate JavaScript errors; false=suppress all errors
var state_pageTitle='The NEUMES Project';  // Replacement text for page title

/* GLOBAL VARIABLES: */
var state_lang='en';  // URL parm: user's natural language; default='en'
var state_level='0';  // URL parm: for exit site, depth of navigation
var state_opened='no';  // URL parm: this site has been opened (viz., this isn't first access
			// in navigation), esp. for Announcements; default='no'
var state_frames='yes';  // URL parm: whether to show frames; default='yes'
var state_page='';  // URL parm: jump-to page, for put direct accesses into frames; ''=none
var state_searchStr=self.location.search;  // URL parameters string
var state_params='';  // temp Array for params

// Objects in right-hand frame [refer to these vars and methods by 'top.']:
var rulerObj=null;  // Frame Ruler object
var rulerStyle=null;  // style property of the Frame Ruler object
var rulerDocument=null;  // handle for document containing the ruler


/* FUNCTIONS */

/*	FN: Get Debug State:
 *
 * state_debug is hard-coded in this; there is no setter fn.
 */
function getDebugState() {
	return (state_debug);
}


/*	FNS: Screen Ruler:
 *
 */
function register_rulerDoc(document_with_ruler) {
	rulerDocument=document_with_ruler;
}
function register_rulerObj(ruler_obj) {
	rulerObj=ruler_obj;
}
function register_rulerStyle(style_obj) {
	rulerStyle=style_obj;
}


/*	FN: Get User-specified Language:
 */
function getLangState() {
	return (state_lang);
}


/*	FN: Set User-specified Language:
 */
function setLangState(newLang) {
	state_lang=newLang;
}


/*	FN: Depth of Navigation:
 *
 */
function getLevelState() {
	return state_level;
}


/*	FN: Get Opened State:
 *
 * From URL parm: specifies whether this site has been opened in the current session
 * ("opened" means, this is not the first incident of access to this site).
 * This information is used especially for Announcements (display in first access only).
 * Default value = 'no'.
 */
function getOpenedState() {
	return state_opened;
}


/*	FN: Set Opened State:
 *
 * Called to notify maintain_state that the site has been opened in the current session
 * ("opened" means, this is not the first incident of access to this site).
 */
function setOpenedState(newOpenedState) {
	if (null == newOpenedState) {
	  state_opened='yes';
	  return;
	}
	if (newOpenedState == 'no') {
	  state_opened='no';
	} else {
	  state_opened='yes';
	}
}  //end, setOpenedState()


/*	FN: Get Frames State:
 */
function getFramesState() {
	return state_frames;
}


/*	FN: Set Frames State:
 *
 * To-do: Assertions.
 */
function setFramesState(newState) {
	state_frames=newState;
}


/*	FN: Get Page State:
 */
function getPageState() {
	return state_page;
}


/*	FN: Set Page State:
 */
function setPageState(newPage) {
	state_page=newPage;
}


// TO DO: change fn to getParmsState();
// in left.htm and main_X.htm, change fn call to getStateParms()
function getParmsState() {
	return('?lang=' + state_lang +  '&level=' + state_level +  '&opened=' + state_opened +
		'&page=' + state_page);
}
function getParms() {  /* DEPRECATED */
	return('?lang=' + state_lang +  '&level=' + state_level +  '&opened=' + state_opened +
		'&page=' + state_page);
}

// TO DO: in left.htm and main_X.htm, change fn getParmsNew() to
// setStateOpened('no'); getStateParms();
// force first access in navigation:
// Caller passes the language code for the language of the page being opened.
// Q: Why doesn't address bar show the current URL for pages opened by Left menu or bottom menu?
function getParmsNewState(new_lang) {
	if (new_lang == '') {
		new_lang = state_lang;
	}
	return('?lang=' + new_lang +  '&level=' + state_level +  '&opened=no' +  '&page=' +
		state_page);
}
function getParmsNew() {  /* DEPRECATED */
	return('?lang=' + state_lang +  '&level=' + state_level +  '&opened=no' +  '&page=' +
		state_page);
}


/* INLINE CODE: */

// Get state variables from searchStr:
state_params=state_searchStr.split('&');
for (i=0; i < state_params.length; i++) {
	if(state_params[i].indexOf('lang=') != -1) {
		state_lang=state_params[i].substring(state_params[i].indexOf('=')+1,
			state_params[i].length);
	}  //else use default
}
for (i=0; i < state_params.length; i++) {
	if(state_params[i].indexOf('level=') != -1) {
		state_level=state_params[i].substring(state_params[i].indexOf('=')+1,
			state_params[i].length);
	} else {
		if(history.length) {
			var levelInt=history.length;
			state_level=levelInt + '';
		}
	}
}
for (i=0; i < state_params.length; i++) {
	if(state_params[i].indexOf('opened=') != -1) {
		state_opened=state_params[i].substring(state_params[i].indexOf('=')+1,
			state_params[i].length);
	}  //else use default
}
for (i=0; i < state_params.length; i++) {
	if(state_params[i].indexOf('frames=') != -1) {
		state_frames=state_params[i].substring(state_params[i].indexOf('=')+1,
			state_params[i].length);
	}  //else use default
}
for (i=0; i < state_params.length; i++) {
	if(state_params[i].indexOf('page=') != -1) {
		state_page=state_params[i].substring(state_params[i].indexOf('=')+1,
			state_params[i].length);
	}  //else use default
}

/* Override page title: */
if(document.title) {
	top.document.title=state_pageTitle;
}
/*[end, maintain_state.js]*/