﻿/*
	Fellowship Church Menu Script
	(c) 2005 Fellowship Church
*/

    /* this is pretty dirty, but it works in both IE & FF */
	var timeouts = new Array();
	var activeitem = '';
	var activeitem2 = '';
	var activeitem3 = '';
	var interval = 10;
	var clearus2 = false;
	var clearus3 = false;
	var gotourl = '';
	var gotourl_timeout;

	/* these are for the sliding login box */
	var clipcounter = 200;
	var toplocation = 0;


	function writedebug(msg) {
		return true;
		if(document.getElementById('debug') && document.getElementById('debug').style.display == 'block') {
			//document.getElementById('debug').innerHTML = msg + '<br>' + document.getElementById('debug').innerHTML;
			document.getElementById('debug').innerHTML += msg + '<br>';
		}
		else alert(document.getElementById('debug').style.display);
	}

	function cleartimer(timeout) {
			clearTimeout(timeouts[timeout]);
			timeouts.splice(timeout,1);
			//var foo = timeouts;
			//timeouts = foo;
			//timeouts[timeout].destroy();
	}

	function isChild(pnode,cnode) {
		if(!cnode.parentNode) return false;
		var tnode = cnode.parentNode;
		while(tnode.nodeName != 'body' && tnode.nodeName != 'BODY') {
			if(tnode == pnode) return true;
			tnode = tnode.parentNode;
		}
		return false;
	}

	function setactive(item) {
		// prevent clearning when in third column
		//if(isChild(item,activeitem)) return true;
		if(activeitem2) activeitem3 = activeitem2;
		if(activeitem) activeitem2 = activeitem;
		activeitem = item;
		//writedebug('A1: ' + activeitem.id);
		//writedebug('A2: ' + activeitem2.id);
		//writedebug('A3: ' + activeitem3.id);
	}

	function clickItem() {
		if(this.getElementsByTagName('a').length > 0) this.getElementsByTagName('a')[0].click();
	}

	function column2_mouseover() {
		clearus2 = false;
		// unshade column 3 and unhover recent active items with leve 3 depth
		document.getElementById('shade_column3').style.display = 'none';
		if(activeitem2 && getLevel(activeitem2) == 3) {
				unshade(activeitem2.parentNode.parentNode.id);
				activeitem2.className = '';
			}

		if(activeitem3 && getLevel(activeitem3) == 3) {
				unshade(activeitem3.parentNode.parentNode.id);
				activeitem3.className = '';
		}

		if(activeitem && activeitem.id && timeouts['shade_' + activeitem.id]) {
			cleartimer('shade_'+activeitem.id);
		}
		if(activeitem && activeitem.id && timeouts['shade_' + activeitem.parentNode.parentNode.id]) {
			cleartimer('shade_'+activeitem.parentNode.parentNode.id);
		}
		if(activeitem && activeitem.id && timeouts['hover_' + activeitem.parentNode.parentNode.id]) {
			cleartimer('hover_'+activeitem.parentNode.parentNode.id);
		}
		if(activeitem && activeitem.id && timeouts['hover_' + activeitem.id]) {
			cleartimer('hover_'+activeitem.id);
		}
	}

	function column2_mouseout() {
		if(activeitem && activeitem.id && timeouts['shade_' + activeitem.id]) {
			var cmd = "unshade('" + activeitem.id + "')";
			timeouts['shade_'+activeitem.id] = setTimeout(cmd,interval);
		}
	}

	function column3_mouseover() {
		if(!activeitem3 || !activeitem3.id || !activeitem3.parentNode) return false;
		clearus3 = false;
		//alert(activeitem3.parentNode.parentNode.parentNode.parentNode.id);
		if(timeouts['hover_' + activeitem3.parentNode.parentNode.parentNode.parentNode.id]) {
			cleartimer('hover_'+activeitem3.parentNode.parentNode.parentNode.parentNode.id);
		}
		if(timeouts['hover_' + activeitem3.parentNode.parentNode.id]) {
			cleartimer('hover_'+activeitem3.parentNode.parentNode.id);
		}
		if(timeouts['hover_' + activeitem3.id]) {
			cleartimer('hover_'+activeitem3.id);
		}
		if(timeouts['shade_' + activeitem3.id]) {
			cleartimer('shade_'+activeitem3.id);
		}
		if(timeouts['shade_' + activeitem3.parentNode.parentNode.id]) {
			cleartimer('shade_'+activeitem3.parentNode.parentNode.id);
		}
		if(timeouts['shade_' + activeitem3.parentNode.parentNode.parentNode.parentNode.id]) {
			cleartimer('shade_'+activeitem3.parentNode.parentNode.parentNode.parentNode.id);
		}
		clearlevel3();
	}

	function column3_mouseout() {
		if(activeitem3 && activeitem3.id && timeouts['shade_' + activeitem3.id]) {
			var cmd = "unshade('" + activeitem.id + "')";
			timeouts['shade_'+activeitem.id] = setTimeout(cmd,interval);
		}
	}

	function getLevel(ele) {
		if(ele.parentNode.id == 'nav_main') return 1;
		if(ele.parentNode.parentNode.parentNode.id == 'nav_main') return 2;
		if(ele.parentNode.parentNode.parentNode.parentNode.parentNode.id == 'nav_main') return 3;
		return false;
	}
	
	function clearLevel(ele) {
		//alert(ele.id);
		var li = document.getElementById('nav_main').getElementsByTagName('li');
		var targetlevel = getLevel(ele);
		for(var i=0; i < li.length; i++) {
			if(getLevel(li[i]) != targetlevel) continue;
			if(li[i].id == ele.id) continue;

			if(li[i].getElementsByTagName('ul').length) li[i].firstChild.src = 'images/icons/arrow-white.gif';
			li[i].className = '';
		}
	}	

	function setHover(ele) {

		// used when hovering over an item that has a submenu
		if(ele && ele.id) {
			clearLevel(ele);
			setactive(ele);
			if(ele.className != 'fcHover') ele.className = 'fcHover';
			var thisitem = ele;
		}
		// used when hovering over an item that does not have a submenu
		else {
			clearLevel(this);
			setactive(this);
			if(this.className != 'fcHover') this.className = 'fcHover';
			if(timeouts['hover_' + this.id]) cleartimer('hover_'+this.id);
			if(getLevel(this) == 1) {
				document.getElementById('shade_column2').style.display = 'none';
			}
			var thisitem = this;
		}

	}

	function unsetHover(eid) {
		if(eid && document.getElementById(eid)) {
			var ele = document.getElementById(eid);
			ele.className = '';
			writedebug('UH: '+eid+'<br>');
		}
		else {
			this.className = '';
			writedebug('UH: '+this.id+'<br>');
		}

	}

	function unsetHoverTimer() {
		if(!this.id) {
			var ran_number=Math.round(Math.random()*1000);
			this.id = 'fcEid'+ran_number;
		}
		// we must use timeouts to prevent blinking
		var cmd = "unsetHover('" + this.id + "')";
		timeouts['hover_'+this.id] = setTimeout(cmd,interval);
	}

	function unshadeTimer() {
		if(!this.id) {
			var ran_number=Math.round(Math.random()*1000);
			this.id = 'fcEid'+ran_number;
		}
		// we must use timeouts to prevent blinking
		var cmd = "unshade('" + this.id + "')";
		timeouts['shade_'+this.id] = setTimeout(cmd,interval);
	}

	 
	 function addevents_shade() {
	 	var navmain_li = document.getElementById('nav_main').getElementsByTagName('li');
		for(var i=0;i<navmain_li.length;i++) {
			// add id #'s to all LI elements
			if(!navmain_li[i].id) {
				var ran_number=Math.round(Math.random()*1000);
				navmain_li[i].id = 'fcEid'+ran_number;
			}
			navmain_li[i].onclick = clicklink;

			if(navmain_li[i].getElementsByTagName('ul').length > 0) {
				var img = document.createElement('img');
				img.src = 'images/icons/arrow-white.gif';
				img.align = 'right';
				img.style.paddingRight = '4px';
				img.style.paddingTop = '4px';
				navmain_li[i].insertBefore(img,navmain_li[i].firstChild);
				navmain_li[i].onmouseover = shade;
				navmain_li[i].onmouseout = unshadeTimer;
			}
			else {
				navmain_li[i].onmouseover = setHover;
				navmain_li[i].onmouseout = unsetHoverTimer;
			}
		}
	 }

	 function shade(column) {
		if(this.id && timeouts['shade_' + this.id]) cleartimer('shade_'+this.id);

		//this.className = 'fcHover';
		//setactive(this);
		setHover(this);

		this.firstChild.src = 'images/icons/arrow-red.gif';
		// determine column to shade
		var foo = this.parentNode;
		for(var column=5; column < 12; column++) {
		   if(foo.id && foo.id == 'nav_main') break;
		   foo = foo.parentNode;				 
		}
		column=(column-1)/2;
		if(column == 3) {
			clearus3 = true;
		}

		// prevent blinking of column2
		if(column == 2) {
			clearus2 = true;
		}

		if(column == 2 && (activeitem == activeitem2)) {
			document.getElementById('shade_column3').style.display = 'none'; 
		}
		writedebug('SH: ' + column + ':' + this.id);

		var foo = document.getElementById('shade_column'+column);
		if(foo.style.display != 'block') foo.style.display = 'block';
		var foo = document.getElementById('promotext');
		if(foo.className != 'lighten') foo.className = 'lighten';
	 }


	 
	 function unshade(eid) {
		if(document.getElementById(eid)) ele = document.getElementById(eid);
		else ele = this;
		//ele.className = '';
		unsetHover(ele.id);
		ele.firstChild.src = 'images/icons/arrow-white.gif';

		// determine column to unshade
		var foo = ele.parentNode;
		for(var column=5; column < 12; column++) {
		   if(foo.id && foo.id == 'nav_main') break;
		   foo = foo.parentNode;				 
		}
		column=(column-1)/2;
		if(clearus3) {
			clearus3 = false;
			return true;
		}
		if(clearus2) {
			clearus2 = false;
			return true;
		}

		writedebug('US: ' + column);
		document.getElementById('shade_column'+column).style.display = 'none'; 
		document.getElementById('promotext').className = '';
	 }

	function fcClearAll() {
		document.getElementById('shade_column2').style.display = 'none'; 
		document.getElementById('shade_column3').style.display = 'none'; 
		document.getElementById('promotext').className = '';
		var navmain_li = document.getElementById('nav_main').getElementsByTagName('li');
		for(var i=0; i< navmain_li.length; i++) {
			navmain_li[i].className = '';
		}
	}

	/* manage clicks to li tags */
	/* onclick is automatically set for each tag */

	function sethref() {
		for(i=0;i<gotourl.childNodes.length;i++) {
			if(gotourl.childNodes[i].nodeName.toUpperCase() == 'A') {
				window.location.href = gotourl.childNodes[i].href;
				return false;
			}
		}
	}

	function clicklink() {
		if(!gotourl || (this != gotourl.parentNode.parentNode && this != gotourl.parentNode.parentNode.parentNode.parentNode)) gotourl = this;
		if(!gotourl_timeout) gotourl_timeout = setTimeout("sethref()",200);
		return true;
	}

	function clearlevel(level,except) {
		var nm = document.getElementById('nav_main').childNodes;
		for(var i=0; i < nm.length; i++) {
			if(nm[i].nodeName.toUpperCase() != 'LI') continue;
			var l2 = nm[i].childNodes
			for(var j=0; j < l2.length; j++) {
				if(l2[j].nodeName.toUpperCase() != 'LI') continue;
				else if(level == 2) {
					// clear all level3 items
					var l3 = l2.getElementsByTagName('li');
					for(var k=0; k<l3.length; k++) {
						l3[k].className = '';
					}
					if(l2[j] == except) continue;
					else l2[j].className = '';
				}
				else if(level == 3) {
					var l3 = l2[j].getElementsByTagName('li');
					for(var k=0; k<l3.length; k++) {
						if(l3[k] != except) l3[k].className = '';
					}
				}
			}
		}
	}

	function clearlevel3() {
		var nm = document.getElementById('nav_main').getElementsByTagName('li');
		for(var i=0; i < nm.length; i++) {
			if(getLevel(nm[i]) == 3) {
				nm[i].className = '';
			}
		}
	}

	function showhide_login() {
		var ele = document.getElementById('login');
		if(ele.style.display == 'block') {
			clipcounter = 0;
			clipbox(10);
		}
		else {
			// set hidden, then display block so we can get the dimensions and location
			ele.style.visibility = 'hidden';
			ele.style.display = 'block';
			ele.style.visibility = 'visible';
			if(!toplocation) { toplocation = ele.offsetTop; } 
			clipcounter = ele.offsetHeight;
			ele.style.top = toplocation-clipcounter + 'px';
			clipbox(-10);
		}

	}

	function clipbox(qty) {
		ele = document.getElementById('login');
		clipcounter+=qty;
		ele.style.top = toplocation - clipcounter + 'px';
		ele.style.clip="rect(" + clipcounter + "px, " + ele.offsetWidth + 'px, ' + ele.offsetHeight + "px, 0px)";
		if((qty < 0 && clipcounter > 0) || (qty > 0 && clipcounter <= ele.offsetHeight)) {
			// make drop down location precise to the original css
			if(qty < 0 && qty*-1 >  clipcounter) { qty = clipcounter*-1; } 
			setTimeout('clipbox('+qty+')',10);
		}
		else if(qty > 0) ele.style.display = 'none';
	}

	function changecampus(val,baseurl) {
		if(!val || val.length == 0) { return false; }
		else {
			//window.location.href = '/' + val + '/' + baseurl;
			window.location.href = "http://" + val + ".fellowshipchurch.com/" + baseurl;

		}
	}

	window.onload = addevents_shade;
