var objBrowse = navigator.appName;

function init(logged_in, menuID, alias) {
	registerEvents(document.links);
	if (logged_in) {
		showPreview(menuID, alias);
	}
	try {
		var accordion = new Fx.Accordion(h3s, divs, {
			opacity: false,
			start : false
		});
		if (typeof(showOnStart) == 'undefined') {
			showOnStart = 0;
		}
		accordion.showThisHideOpen(showOnStart);
	} catch (err) {}
	addTooltips();
	writeFooter();
}

function addTooltips() {
	eval(scriptToEval);
}

function debug() {
	var debugWindow = window.open("content/debug.html", "debug", "height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function ajaxComplete() {} // empty function - do not delete

function ajaxScrollToFeedbackForm() {
	var e = document.getElementById(ajax.element);
	ajax.elementObj.innerHTML = ajax.response;
	e.style.visibility="visible";
	var y;
	var objBrowse = navigator.appName;
	if (objBrowse == "Microsoft Internet Explorer") {
		y = document.body.scrollHeight;
	} else {
		y = document.documentElement.scrollHeight;
	}
	window.scrollTo(0, y);
	try {
		document.getElementById('realname').focus();
	} catch (err) {}
}

// registers click events against all of the links appearing within the left hand menu
function registerEvents(objColl) {
	var i;
	try {
		for ( i = 0; i < objColl.length; i++ ) {
			if (objColl[i].className.substr(0, 5) == 'menu_') { // adds tooltip events
				if (objColl[i].className.search('menu_overview') == -1 && objColl[i].className.search('menu_start_here_hack') == -1) {
					objColl[i].onclick = setDeactivateGuidedTourCookie;
				} else if (isNaN(getCookieVal('on_tour')) && objColl[i].className.search('menu_start_here_hack') == -1) {
					objColl[i].onclick = setActivateGuidedTourCookie;
				}
			} else if (objColl[i].parentNode.className == 'footer noprt') {
				objColl[i].onclick = setDeactivateGuidedTourCookie;
			} else if (objColl[i].href.search('p=overview') != -1 && isNaN(getCookieVal('on_tour'))) {
				objColl[i].onclick = setActivateGuidedTourCookie;
			}
		}
	}
	catch (err) {}
	/*tourstop = getCookieVal('on_tour');
	if (tourstop >= 0) {
		for ( i = 0; i < objColl.length; i++ ) {
			if (!objColl[i].onclick) {
				objColl[i].onclick = setDeactivateGuidedTourCookie;
			} // may need to add an old_onclick script to preserve on click events
		}
	}*/
}

/* assign a warning to the menu items that aren't part of the overview (right-hand nav)
to say that you are about to leave the overview.
Because a session cookie determines whether to show the guide (right-hand nav)
we need to reset the cookie when leaving the guide. */
function setDeactivateGuidedTourCookie(e) {
	if (!e) {
	var e = window.event;
	}
	var obj = e.srcElement;
	if (!obj) {
		var obj = e.target;
	}
	targetURL = obj.href;
	// hijack the href so as to reset session cookie first
	if (getCookieVal('on_tour') != 1 && !isNaN(getCookieVal('on_tour'))) {
		targetObj = obj;
		obj.href='javascript:void(0)';
		if(!confirm('You are about to leave the guide.')) {
			targetObj.href = targetURL;
			return false;
		}
	}
	// expire the cookie
	destroyCookie('on_tour');
	leavingTour();
}

function setActivateGuidedTourCookie() {
	setCookieVal('on_tour', 1);
}

function destroyCookie(cookieName) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (1000*3600*-1) ); // 1000ms*3600s*-1h = -1hr
	document.cookie = cookieName + '=; expires=' + (expdate.toGMTString()) + ';';
}

function leavingTour() {
	window.location = targetURL;
}

function showMenuDescription(tooltipContainer, isVisible) {
	try {
		tooltipContainer.style.height = quoteContainerHeight + 'px';
		tooltipContainer.style.width = quoteContainer.offsetWidth + 'px';
		tooltipContainer.style.visibility = isVisible;
		return true;
	}
	catch (err) { return false; }
}
	
function showTooltip(e, message) {
	var st = Math.max( document.body.scrollTop, document.documentElement.scrollTop );
	var bodyWidth = Math.max( document.body.clientWidth, document.documentElement.clientWidth ) - 20 ;
	if (objBrowse == "Microsoft Internet Explorer") {
		xpos = e.clientX;
		ypos = e.clientY + st - 10;
		targ = e.srcElement.parentElement;
	
	//try to avoid mouse over message Div problem by moving up the offset a bit
		if ( e.srcElement.offsetHeight > 20 ) {
			yoffset = e.srcElement.offsetHeight - 20 ;
		} else {
			yoffset = e.srcElement.offsetHeight;
		}
	}
	else {
		/*use prototype to get the position more correctly cross browsers*/
		var posArray = Position.positionedOffset( e.currentTarget );
		xpos = posArray[0] + 5;
		ypos = posArray[1] - 5;
	}
	document.getElementById("tooltip").style.left = xpos + "px";
	document.getElementById("tooltip").innerHTML = message;
	document.getElementById("tooltip").style.top = ypos - document.getElementById("tooltip").offsetHeight + "px";
	document.getElementById("tooltip").style.visibility = "visible";
}

function generateTooltip(targetElement, cont, myTitle, options) {
	eval("new Tip('" + targetElement + "', '" + cont + "', { title : '" + myTitle + "'," +	options + "});");
}

function hideDiv() {
	document.getElementById("tooltip").innerHTML = "";
	document.getElementById("tooltip").style.visibility = "hidden";
}

function showflash(filelocation, mywidth, myheight) {
	showPopup('content/flash_popup.html', (mywidth/1 + 55), (myheight/1 + 110), new Array('filelocation', 'width', 'height'), new Array(filelocation, mywidth, myheight));
}

function showPopup(myLocation, w, h, attribKeys, attribValues) {
	try {
		var attribString = (attribKeys.length > 0)? "?" : "";
		var i;
		for (i = 0; i < attribKeys.length; i++) {
			attribString += attribKeys[i] + "=" + attribValues[i] + "&";
		}
		attribString = attribString.replace(/&$/g, '');
	} catch (err) { var attribString = ""; }
	if (typeof(myWindow) == "undefined" || myWindow.closed) { // second condition is a workaround for a problem in Mozilla
		myWindow = window.open(myLocation + attribString, null,'scrollbars=yes,toolbar=no,status=no,location=no,menubar=yes,resizable=yes,top=130,left=130,width=' + w + ',height=' + h + '');
	}
	myWindow.location = myLocation + attribString;
	myWindow.focus();
}

function writeFooter() {
	var height = Math.max(document.getElementById("content").clientHeight, document.getElementById("content").scrollHeight);
	if (height > 900 && isNaN(getCookieVal('on_tour'))) {
		document.getElementById("content_footer").innerHTML = "<br /><a href=\"#top\" class=\"top\" style=\"clear:both\" title=\"top of page\">top</a><br /><br />";
	}
}

//#######################################################
// ##### functions relating to the side navigation ######
// #####                                           ######

var expandoArr = new Array();
var expandoString = (getCookieVal("expando="));
expandoArr = expandoString.split(",");
if (isNaN(expandoArr[0]/1)) {
	expandoString = "";
	expandoArr = new Array();
}
function showHideNav(imgObj, ident) {
	if (imgObj.nodeName == "A") {
		imgObj = (imgObj.parentNode.previousSibling.nodeName == '#text')? imgObj.parentNode.previousSibling.previousSibling.firstChild : imgObj.parentNode.previousSibling.firstChild;
		if (imgObj.id != "plus" && imgObj.id != "plus_last") {
			return;
		}
	}
	switch (imgObj.id) {
		case ("plus") :
			imgObj.id = "minus";
			imgObj.src = "images/tree_menu_minus.gif";
			updateExpandoArr("add", ident);
			break;
		case ("minus") :
			imgObj.id = "plus";
			imgObj.src = "images/tree_menu_plus.gif";
			updateExpandoArr("remove", ident);
			break;
		case ("plus_last") :
			imgObj.id = "minus_last";
			imgObj.src = "images/tree_menu_minus_last.gif";
			updateExpandoArr("add", ident);
			break;
		case ("minus_last") :
			imgObj.id = "plus_last";
			imgObj.src = "images/tree_menu_plus_last.gif";
			updateExpandoArr("remove", ident);
			break;
	}
	var previousElement = imgObj;
	while (true) {
		previousElement = previousElement.parentNode;
		if (previousElement.nodeName == "TABLE") {
			var parentTableElement = previousElement;
			break;
		}
	}
	var spanElement = (parentTableElement.nextSibling.nodeName == "#text")? parentTableElement.nextSibling.nextSibling : parentTableElement.nextSibling;
	if (spanElement.style.display == "none") {
		//spanElement.id = "shown";
		spanElement.style.display = "block";
	}
	else {
		//spanElement.id = "hidden";
		spanElement.style.display = "none";
	}
}

function updateExpandoArr(methid, ident) {
	var identFound = false;
	var i;
	for (i = 0; i < expandoArr.length; i++) {
		if (expandoArr[i] == ident) {
			identFound = true;
			break;
		}
	}
	if (!identFound && methid == "add") {
		expandoArr.push(ident);
	}
	else if (identFound && methid == "remove") {
		expandoArr.splice(i, 1);
	}
	expandoString = expandoArr.toString();
	setCookieVal("expando", expandoString);
}

// #####                                           ######
//#######################################################


// cookie handling methods
function getCookieVal(offset) {
	var start = document.cookie.indexOf(offset)+offset.length+1;
	var end = document.cookie.indexOf(";", start);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(start, end));
}


// path should be set to whatever subfolder the site will eventually be contained within
function setCookieVal(cookieName, cookieValue) {
	//alert(cookieName + "=" + cookieValue);
	//var expdate = new Date();
	//expdate.setTime(expdate.getTime() + (1000*3600*3) ); // 1000ms*3600s*3h = 3hr
	document.cookie = cookieName + '=' + escape(cookieValue) + ';';
}

function dosearch() {
	document.getElementById('secondarysearch').submit()
}
