/*
* switches menu between showing and hiding
* menu item status lives in a cookie
* so same state is retained over pages
*/

function showHide(theid){
    if (document.getElementById) {
    var switch_id = document.getElementById(theid);
        if(readCookie(theid) != 'show') {
           switch_id.className = 'show';
		   createCookie(theid,'show',30);
        }else{
           switch_id.className = 'hide';
			createCookie(theid,'hide',30);
        }
		//setBodyColHeight();
    }
}

/*
* generic function to create or update a cookie
*
*/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/*
* generic function to read a cookie
*
*/

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
* generic function to delete a cookie
*
*/

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*
* sets a spacer height so overall layout doesn't break
* when menu is being expanded and contracted
*
*/

function setBodyColHeight() {
	if (document.getElementById) {
		menuHeight = getColHeight('menu_top','menu_bottom');
		bodyHeight = getColHeight('body_top','body_bottom');
		if (menuHeight > bodyHeight) {
			document.getElementById('body_spacer').height = menuHeight + 250;
			//alert('menu: ' + menuHeight + ' body: ' + bodyHeight);
		}

	}
}

/*
* gets the distance (height in pixels) between 2 divs
*
*/
function getColHeight(idTop,idBot) {
	contentTopDiv = document.getElementById(idTop);
	contentBotDiv = document.getElementById(idBot);
	contentTop = getPixelsFromTop(contentTopDiv);
	contentBottom = getPixelsFromTop(contentBotDiv);
	return contentBottom - contentTop;
	//contentHeight = contentBottom - contentTop;
	//alert("The cell height is " + contentHeight);
}

/*
* gets the position of a div
*
*/
function getPixelsFromTop(obj){
	objFromTop = obj.offsetTop;
	while(obj.offsetParent!=null) {
		objParent = obj.offsetParent;
		objFromTop += objParent.offsetTop;
		obj = objParent;
	}
	return objFromTop;
}

//init menu items
//this is set off AFTER the page has loaded
//see onLoad in img in index.cfm
function initMenu() {
	if (document.getElementById) {
		for (i=0;i<all_menuItems.length;i++) {
			xid = document.getElementById(all_menuItems[i]);
			if (readCookie(all_menuItems[i]) == 'show') {
				xid.className = 'show';
			} else {
				xid.className = 'hide';
			}
		}
		//setBodyColHeight();
	}			
}
