

// code development by Tim Whelan www.timwhelan.net

// global variables

var img_folder = "i/"; // should probably be "/images/"
var vMenuOffset = 22;
var hMenuOffset = 0;
var loggedin = "false";

// basic sniffer:
var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
var DOM = ((document.getElementById) && (!IE4)) ? 1 : 0;   // ns6

// NS4 resize fix
if (NS4) {
	origWidth = innerWidth;
	origHeight = innerHeight;
}

function reDo() {
	if (innerWidth != origWidth || innerHeight != origHeight) {
		location.reload();
	}
}

if (NS4) onresize = reDo;


// ----------------------------------------------------------------
// image-swapping code
// ----------------------------------------------------------------

var imagesLoaded = false;

function loadImage(fileName) {
	newImage = new Image();
	newImage.src = fileName;
	return newImage;
}

function preloadImages() {
	if(document.images) {
	// menu components:
	spacerImg = loadImage(img_folder + "spacer.gif");
	indicator_off = loadImage(img_folder + "indicator_off.gif");
	indicator_on = loadImage(img_folder + "indicator_on.gif");
	menu_tl = loadImage(img_folder + "menu_tl.gif");
	menu_tr = loadImage(img_folder + "menu_tr.gif");
	menu_left = loadImage(img_folder + "menu_left.gif");
	menu_right = loadImage(img_folder + "menu_right.gif");
	menu_bl = loadImage(img_folder + "menu_bl.gif");
	menu_br = loadImage(img_folder + "menu_br.gif");
	menu_btm = loadImage(img_folder + "menu_btm.gif");
	menu_top = loadImage(img_folder + "menu_top.gif");

    imagesLoaded = true;
  }
}


function changeImage(imageIn, imageSrc) {
	if (!imagesLoaded || !document.images) return;
	currImage = eval("document." + imageIn);
 	if (!currImage) {
    	if (NS4) {
    		var foundImage = false;
			for (l = 0; l < document.layers.length; l++) {
				currImage = eval("document.layers[" + l + "].document." + imageIn);
				if (currImage) {
					foundImage = true;
					break
				}
			}
			if (!foundImage) return;
		}
		else return;
	}
	if (NS4) currImage.src = eval(imageSrc + ".src");
	else currImage.setAttribute("src", eval(imageSrc + ".src"));
}

// end image-swapping code




// ----------------------------------------------------------------
// Dropdown Menu Code
// ----------------------------------------------------------------


// initialize some variables
var isOver = false;
var timer = null;
var cmTop = 0; 
var cmLeft = 0;
var cmDelay = 950; // menu delay (in milliseconds)
var whichMenu;
var activeKey;
var headerImages = false; // are images used as menu headers (as opposed to text)?



if (DOM) onload = InitObj;
preloadImages();





// Menu Arrays:


// Array of menus:
// format entries as 'menuName'
// where menuName corresponds to menuItems_ arrays below
// Note: if using images for menu headers (rather than text), filenames should
//  follow the following nomenclature:
//    default image: 'menuName_off.gif'
//    mouseOver image: 'menuName_on.gif'
//	  image name: 'menuNameBtn'

var menuList = new Array('',
	'whoAreWe',
	'events',
	'tools',
	'news',
	'store'
);



// individual menu arrays:
// array names should be "menuItems_" + menu name(as shown in menuList array)
// format menu items as ['url','text','public*','target*']
// where:
//	url = url for link
//	text = text to appear in text link or alt tag for image
//	public (boolean) = is link available without login?
//	target = target attribute for link (i.e. "_blank") 
// * = optional


var menuItems_whoAreWe = new Array(
['http://www.myserenityhouse.com/serenityhouse.php','Serenity House','1'],
['http://www.myserenityhouse.com/joanmarieA.php','Joan Marie Ambrose','1'],
['http://www.myserenityhouse.com/joanmarieW.php','Joan Marie Whelan','1'],
['http://www.myserenityhouse.com/testimonials.php','Testimonials','1'],
['http://www.myserenityhouse.com/contactUs.php','Contact Us','1'],
['http://www.myserenityhouse.com/alliancesPartners.php','Alliances and Partners','1']
);

var menuItems_events = new Array(
['http://www.myserenityhouse.com/services.php','Services','1'],
['http://www.myserenityhouse.com/eventsLectures.php','Events, Lectures & Workshops','1'],
['http://www.myserenityhouse.com/inspirationalMessages.php','Inspirational Messages','1']
//['http://www.myserenityhouse.com/serenityForum.php','Forum and Q&A','1'],
//['http://www.myserenityhouse.com/peaceProject.php','Peace Project','1'],
//['http://www.myserenityhouse.com/10min_coaching.php','10 minute coaching','1']
);

var menuItems_tools = new Array(
['http://www.myserenityhouse.com/meditationTheory.php','Theory of Meditation','1'],
['http://www.myserenityhouse.com/cdMeditation.php','Meditation & Lecture CDs','1'],
['http://www.myserenityhouse.com/empowermentAffirmations.php','Empowerment Affirmations','1'],
['http://www.myserenityhouse.com/serenityGallery.php','Gallery of Images','1'],
['http://www.myserenityhouse.com/meditativePractices.php','Meditative Practices','1']
);

var menuItems_news = new Array(
['http://www.myserenityhouse.com/recentNews.php','Recent News','1'],
['http://www.myserenityhouse.com/newsletters.php','eMail Newsletters','1']
// ['pressReleases.php','Press Releases','1'],
// ['interviews.php','Interviews','1']
);

var menuItems_store = new Array(
['http://www.joanmariewhelan.com/Pages/Products.html','Books','1'],
['http://www.joanmariewhelan.com/Pages/Products.html','CDs','1'],
['http://www.joanmariewhelan.com/Pages/Products.html','eBooks','1'],
['http://www.joanmariewhelan.com/Pages/Products.html','Labyrinths','1'],
['http://www.joanmariewhelan.com/Pages/Products.html','Miscellaneous','1']
);



// build standard dropdowns
for ( i = 1 ; i < menuList.length ; i++ ) {
	var menuItems = eval("menuItems_" + menuList[i]); // list item menuList[i] references item in menuList array and [1] references the second element (eg. the menu name)
	var divNum = "cm_" + menuList[i]; // menu layer name
	var strDiv = '<div class="menuLayer" id="' + divNum + '" onMouseOver="OverLayer();" onMouseOut="OutLayer();">\n';
	strDiv = strDiv + '<table cellspacing=0 width=80 cellpadding=0 border=0>\n';
	strDiv = strDiv + '<tr><td><img src="' + menu_tl.src + '" height=3 width=3></td><td background="' + menu_top.src + '"><img src="' + spacerImg.src + '" height=1 width=1></td><td background="' + menu_top.src + '"><img src="' + menu_top.src + '" height=3 width=62></td><td align="right" valign="top"><img src="' + menu_tr.src + '" height=3 width=7></td></tr>\n';
	for ( j = 0 ; j < menuItems.length ; j++ ) {
		if ((menuItems[j][2]>0) || (loggedin>0)) {
			var indicatorName = "indicator_" + i + "_" + j;
			strDiv = strDiv + '<tr><td><img src="' + menu_left.src + '" width=3 height=18></td>';
			strDiv = strDiv + '<td><img src="' + indicator_off.src + '" name="' + indicatorName + '" width=12 height=18></td>';
			strDiv = strDiv + '<td class="menuItems" nowrap><a onMouseOver="changeImage(\'' + indicatorName + '\',\'indicator_on\')" onMouseOut="changeImage(\'' + indicatorName + '\',\'indicator_off\')" href="' + menuItems[j][0] + '"';			if (menuItems[j][3]) {				strDiv = strDiv + ' target="' + menuItems[j][3] + '"';			}
			strDiv = strDiv + '">' + menuItems[j][1] + '</a></td>\n';
			strDiv = strDiv + '<td align="right"><img src="' + menu_right.src + '" height=18 width=7></td></tr>\n';
		}
		else {
			strDiv = strDiv + '<tr><td><img src="' + menu_left.src + '" width=3 height=18></td>';
			strDiv = strDiv + '<td><img src="' + spacerImg.src + '" width=6 height=8></td>';
			strDiv = strDiv + '<td class="menuItemsUnav" nowrap>' + menuItems[j][1] + '</td>\n';
			strDiv = strDiv + '<td align="right"><img src="' + menu_right.src + '" height=18 width=7></td></tr>\n';
		}
	}
	strDiv = strDiv + '<tr><td align="left" valign="bottom"><img src="' + menu_bl.src + '" height=3 width=3></td><td background="' + menu_btm.src + '"><img src="' + spacerImg.src + '" height=1 width=1></td><td background="' + menu_btm.src + '"><img src="' + menu_btm.src + '" height=3 width=62></td><td align="right" valign="bottom"><img src="' + menu_br.src + '" height=3 width=7></td></tr>\n';
	strDiv = strDiv + '</table></div>';
	document.write(strDiv);
}



// create menu layer objects (cm's)
function InitObj() {
	for (i = 1; i < menuList.length; i++) { 
		whichEl = "cm_" + menuList[i];
		if (DOM) {
			whichEl = document.getElementById(whichEl);
		}
		else {	
			whichEl = (NS4) ? document.layers[whichEl] : document.all[whichEl].style;
		}
		whichEl.onmouseover = OverLayer;
		whichEl.onmouseout = OutLayer;
	}
}
InitObj();




// make whichMenu visible (separated for delay in NS)
function makeVisible() {
	if (DOM) {
		whichMenu.style.visibility = "visible";
	}
	else {
		whichMenu.visibility = "visible";
	}
	
}






// activate menus by name
function ShowLayer(showEl) {
	clearTimeout(timer);
	if ((activeKey != showEl) && (headerImages)) changeImage(activeKey + "Btn",activeKey + "_off");
	activeKey = showEl;
	HideAllLayers();
	var menuDiv = showEl + "Div";
	if (headerImages) {
		changeImage(activeKey + "Btn",activeKey + "_on");
	}
	var whichAnchor = showEl + "A";
	showEl = "cm_" + showEl;

	if (DOM) {
		var whichEl = document.getElementById(showEl);
	}
	else {	
		var whichEl = (NS4) ? document.layers[showEl] : document.all[showEl].style;
		
	}
	if (IE4) {
		if (document.all[whichAnchor]){ 
			cmTop = findy(document.all[whichAnchor]) + vMenuOffset;
			cmLeft = findx(document.all[whichAnchor]);
		}
	}
	if (NS4) {
		for (i = 1; i < document.layers.length; i++) { 
			var divString = new String(document.layers[i].name);
			if (divString.indexOf(menuDiv) != -1) {
				if (document.layers[menuDiv].document.anchors[whichAnchor]) {
					cmTop = document.layers[menuDiv].y;
					cmLeft = document.layers[menuDiv].x;
					cmTop = cmTop + document.layers[menuDiv].document.anchors[whichAnchor].y + vMenuOffset;
					cmLeft = cmLeft + document.layers[menuDiv].document.anchors[whichAnchor].x;
				}
			}
			else {
				if (document.anchors[whichAnchor]) {
					cmTop = document.anchors[whichAnchor].y + vMenuOffset;
					cmLeft = document.anchors[whichAnchor].x;
				}
			}
		}
	}
	if (DOM){	
		if (document.anchors[whichAnchor]){
			cmTop = findy(document.anchors[whichAnchor]) + vMenuOffset;
			cmLeft = findx(document.anchors[whichAnchor]);
		}
	}
	if (DOM) {
		whichEl.style.top = cmTop;
		whichEl.style.left = cmLeft;
		whichMenu = whichEl;
		mvis = setTimeout('makeVisible()',1);
	}
	else {	
		whichEl.top = cmTop;
		whichEl.left = cmLeft;
		whichMenu = whichEl;
		if (NS4) mvis = setTimeout('makeVisible()',1);
		else whichEl.visibility = "visible";
	}
}



// find x and y of anchor calling ShowLayer function
function findy(item) {
	if (item.offsetParent) {
		return item.offsetTop + findy(item.offsetParent);
	}
	else {
		return item.offsetTop;
	}
}


function findx(item) {
	if (item.offsetParent) {
		return item.offsetLeft + findx(item.offsetParent);
	}
	else {
		return item.offsetLeft;
	}
}




// deactivate menus
function HideAllLayers() {
	for (i = 1; i < menuList.length; i++) { 
		whichEl = "cm_" + menuList[i];
		if (DOM) whichEl = document.getElementById(whichEl);
		else whichEl = (NS4) ? document.layers[whichEl] : document.all[whichEl].style;
		if (!isOver) {
			if (DOM) whichEl.style.visibility = "hidden";
			else whichEl.visibility = "hidden";
			if (headerImages) changeImage(activeKey + "Btn",activeKey + "_off");
		}		
	}
}



// maintain layer visibility
function OverLayer() {
	clearTimeout(timer);
	isOver = true;
}

// initiate deactivation clock
function OutLayer() {
	clearTimeout(timer);
	isOver = false;
	timer = setTimeout("HideAllLayers()",cmDelay);
}



// End of Dropdown Menu code





// ----------------------------------------------------------------

// ----------------------------------------------------------------


// Open featureless popup window of fixed, specified size 50px from top left
function openPopup(url,width,height) {
  popupWin = window.open(url, 'popupWin', 'menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,width=620,height=675,left=0,top=0');
}



