/*
 * Dynamically create a link to update the current page. 
 * Each template file should have a line like this:
 *
 * <!-- com.omniupdate.ob --><!-- /com.omniupdate.ob -->
 *
 * That line should go just before the closing body tag.
 *
 * Omniupdate will dynamically replace this tag with a link, wrapped
 * around a 1px transparent gif. The line of code containing the link is
 * customizable in Setup > Web Sites. This script dynamically replaces
 * our copyright link's href with the href that OU inserts for us. 
 */

if (!JEJ)
	var JEJ = {};

if (!JEJ.doubledelegate) {
	JEJ.doubledelegate = function(f1, f2) {
		return function() {
			if (f1)
				f1();
			if (f2)
				f2();
		}
	}
}

var HOOK = {};

HOOK.switchhook = function () {

	var h = document.getElementById('ouhook');
	if (!h)
		return;

	/* convert the url into the correct location on the web express
 	 * server.
 	 */
	var u = window.location.href;

	/* ALERT: If the path has already been welded into this line, we don't
   * modify it.
   */
	if (u.indexOf('path=') != -1) 
		return;
	if (u.indexOf('http://www.lib.uchicago.edu') == 0)
		u = u.substring(27);
	if (u.indexOf('http://convert.lib.uchicago.edu') == 0) 
		u = u.substring(31);
 /* 
  * HACK. remove double slashes, from alumnifriends redirected url.
  * see http://www.lib.uchicago.edu/alumnifriends/libsoc/ =>
  *     http://www.lib.uchicago.edu/e/alumnifriends//libsoc/
  */
	u = u.replace('//', '/');
	if (u.indexOf('/e') == 0)
		u = u.substring(2);
	if (u.indexOf('/staffweb') == 0)
		u = u.substring(9);
	if (u.lastIndexOf('/') == u.length - 1)
		u = u + 'index.html';

 	/* ALERT: I assume that a path paramter hasn't been already specified
 	 * here.
   */
	u = h.href + '&path=' + encodeURIComponent(u);

	if (window.location.href.indexOf('http://convert.lib.uchicago.edu/e/') == 0)
		u = u.replace('site=Library', 'site=Convert_Library')
	if (window.location.href.indexOf('http://convert.lib.uchicago.edu/staffweb/') == 0)
		u = u.replace('site=LibraryIntranet', 'site=Convert_Staffweb')

	/* replace the link embedded in the copyright symbol. */
	var lnks = document.links;
	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].id == "copylink") {
			lnks[i].href = u;
		}
	}
}
window.onload = JEJ.doubledelegate(window.onload, HOOK.switchhook);
