addressWindow       = null;
editAddressWindow   = null;
navigatorWindow     = null;
thesaurusWindow     = null;
linkBrowserWindow   = null
whatsNewWindow      = null;
searchResultsWindow = null;

function format ()
{
	if (arguments.length <= 1 ) return arguments[0];
	re = /(^[^%]*)%%(.*)/;
	newString = arguments[0];
	for (i=1; i<arguments.length; ++i)
	{
		newString = newString.replace (re, "$1 " + arguments[i] + " $2")
	}
	return newString;
}


function preloadImages (aDocument, anArray)
{
	var  img = new Image;
	for (i=0; i<anArray.length; ++i)
	{
		img.src = anArray[i] + '.gif';
		img.src = anArray[i] + 'X.gif';
	}
	aDocument.imageNames = anArray;
}
function hilite (aDocument, name, index) { aDocument.images[name].src = aDocument.imageNames[index] + 'X.gif'; }
function fade (aDocument, name, index) { aDocument.images[name].src = aDocument.imageNames[index] + '.gif'; }


function openUrlInNewWindow (aParentWindow, aURL)
{
	aParentWindow.open (aURL);
}


function spawnWindow (aWindow, w, h)
{
	var scroll = 'scrollbars,';
	
	if (! w)
		w = 400;
	if (! h)
	{
		if (aWindow.document.body) 	// Only for IE
		{
			h = aWindow.document.body.scrollHeight;
			h += 70;
			if ((h+180) > screen.availHeight)
				h = screen.availHeight - 180;
			else
				scroll = '';
		}
		else
			h = 600;
	}

	url = aWindow.document.location.protocol + "//" + aWindow.document.location.hostname + aWindow.document.location.pathname + aWindow.document.location.search;
	url += (url.charAt (url.length-1) == '/') ? '?' : '&';
	url += 'spawned=true' + aWindow.document.location.hash;
	
	var  s = aWindow.name;
	if (s == "Contents") 
		s = "Contents1";
	else
		s = "Contents" + (parseInt(s[7]) + 1);
	
	aWindow.open (url, s, scroll+'resizable,status,width=' + w + ',height=' + h);
}


function tee (event, id, etc)
{
	if (this.MenuBar && this.MenuBar.linkOrMenu)  
		return this.MenuBar.linkOrMenu (event, id, etc);
	return true;
}


function displayAddress (id, noContent)
{
	addressWindow = executeObject ('Address', id, 'Display', addressWindow, 400, 300, 100, 100, noContent);
}

function newAddress (parentId)
{
	editAddressWindow = executeObject ('Address', null, 'Edit', editAddressWindow, 480, 410, null, null, null, '/?addTo=' + parentId);
}

function editAddress (id)
{
	editAddressWindow = executeObject ('Address', id, 'Edit', editAddressWindow, 480, 410);
}

function showLinkBrowser (id, field, noTree)
{
	if (noTree)
		field += "&part=content";
	linkBrowserWindow = executeObject ('Entry', id, 'LinkBrowser', linkBrowserWindow, noTree ? 470 : 800, 500, 0, 0, null, '/?field=' + field);
}

function silentExecuteObject (type, id, aCommand, aWindow, w, h, x, y, noContent)
{
	executeObject (type, id, aCommand, aWindow, w, h, x, y, noContent);
}

function executeObject (type, id, aCommand, aWindow, w, h, x, y, noContent, parameters)
{
	if (! w)  w = 400;
	if (! h)  h = 300;
	if (! x)  x = 100;
	if (! y)  y = 100;
	aWindow = popupSingleton
	(
		aWindow,	type + aCommand,
		'scrollbars=yes,resizable=yes,width='   + w + 
																 ',height=' + h + 
																 ',top='    + y + 
																 ',left='   + x
	);
	if (id == null)
		id = '';
	if (!noContent)
		aWindow.location.href = '/servlet/is/' + type + '.' + id + '.' + aCommand + (parameters ? parameters : '');
	return aWindow;
}


var thesaurus = 
{
	fat: { url: '/Thesaurus/Start.html', width: 715, height: 500 },
	slim: { url: '/Thesaurus/Start.html', width: 250, height: 500 }
};

thesaurus.defaults = thesaurus.fat;


function search ()
{
	var url = "/servlet/is/Entry..Search/";
	if (Contents.entry)
		url += "?subTree=" + Contents.entry.id;
	Contents.location.href = url;
}


function popupThesaurusFor (caller, w, h, url)
{
	if (!url)  url = thesaurus.defaults.url;
	if (! w)  w = thesaurus.defaults.width;
	if (! h)  h = thesaurus.defaults.height;

	thesaurusWindow = popupSingleton
	(
		thesaurusWindow,
		'Thesaurus',
		'scrollbars=yes,resizable=yes,width=' + w + ',height=' + h
	);
	thesaurusWindow.document.location.href = url;
}



function popupNavigator (depth, template)
{
	var id = Contents.entry ? Contents.entry.id : 1;
	popupNavigatorOn (id, depth, 420, 500, template);
}


function popupNavigatorOn (anId, depth, w, h, template)
{
	if (! depth)  depth = 2;
	if (! w)  w = 420;
	if (! h)  h = 500;

	navigatorWindow = popupSingleton
	(
		navigatorWindow,
		'Navigator',
		'scrollbars=yes,resizable=yes,width=' + w + ',height=' + h + ',status=no,screenX=0,screenY=0,left=0,top=0'
	);
	var ref = '/servlet/is/' + anId + '/?command=tree&maxLevel=' + depth;
	if (template)
		ref += "&template=" + template;
	navigatorWindow.location.href = ref;
}

function popupSearchResults (url)
{
	searchResultsWindow = popupSingleton
	(
		searchResultsWindow,
		'SearchResults',
		'scrollbars=yes,resizable=yes,width=480,height=550,status,screenX=0,screenY=0,left=0,top=0'
	);
	if (url != null)
		searchResultsWindow.document.location.href = url;
}


function popupWhatsNew ()
{
	whatsNewWindow = popupSingleton
	(
		whatsNewWindow,
		'News',
		'scrollbars=yes,resizable=yes,width=550,height=550,status,screenX=0,screenY=0,left=0,top=0'
	);
	whatsNewWindow.document.location.href = '/servlet/is/Global..ShowNews/?daysBack=14';
}


function popupSingleton (aWindow, windowName, windowParams)
{
	if (aWindow && !aWindow.closed && aWindow.document) 
	{ 
		aWindow.focus ();
		return aWindow;
	}
	var loc = navigator.appName.indexOf ("Opera") == -1 ? '' : window.frames[0].location;
	aWindow = window.open
	(
		loc,
		windowName,
		windowParams
	);

	if (aWindow && !aWindow.closed && aWindow.document) 
		aWindow.focus ();
	
	return aWindow;
}

function login ()
{
	var request = '/servlet/is/Grantee..login/?noNoAccessMessage=true';
	if (this.Contents && this.Contents.entry && this.Contents.entry.id)
		request += '&destination=' + this.Contents.entry.id;
	this.Contents.location.href = request;
}

