// JavaScript: scripts.js

// Determine browser function

function checkBrowser() {
	this.ver = navigator.appVersion
	this.dom = document.getElementById?1:0
	this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4 = (document.all && !this.dom)?1:0;
	this.ns5 = (this.dom && parseInt(this.ver) >= 5)?1:0;
	this.ns4 = (document.layers && !this.dom)?1:0;
	this.bw = (this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}

bw = new checkBrowser()

function upDown(div, nest) {
	obj = bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if (obj.display=='block') obj.display = 'none'
	else obj.display = 'block'
}

var useragent = navigator.userAgent.toLowerCase();

// CSS override function

if(useragent.indexOf('safari')!=-1) {
 document.write('<link rel="stylesheet" type="text/css" href="../styles/stylesSafari.css" />');
}
else if(useragent.indexOf('msie 6')!=-1 || useragent.indexOf('msie 5')!=-1) {
 document.write('<link rel="stylesheet" type="text/css" href="../styles/stylesIE.css" />');
}
else if(useragent.indexOf('msie 7')!=-1) {
 document.write('<link rel="stylesheet" type="text/css" href="../styles/stylesIE7.css" />');
}

// New window function

function openWindow(url, name, params) {
  if (params==undefined) params = 'width=520,height=500,scrollbars=yes,resizable=yes';
  popupWin = window.open(url, name, params);
  window.popupWin.focus();
}

// Display layers functions

function showHide(div, nest) {
	obj = bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if (obj.visibility=='visible' || obj.visibility=='show') obj.visibility = 'hidden'
	else obj.visibility = 'visible'
}

function show(div, nest) {
	obj = bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility = 'visible'
}

function hide(div, nest) {
	obj = bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility = 'hidden'
}

// Navigation menu functions

fFlag = 1;
function timeToDeleteNavMenu(x) {
	deleteMenuM = setTimeout("closeNavMenu('" + x + "');", 300);
	fFlag = 0;
}

function keepActiveNavMenu() {
	if (!fFlag) {
		clearTimeout(deleteMenuM);
	}
}

function closeNavMenu(x) {
	hide("n" + x);
	mc = document.getElementById("m" + x).className="menuTopItem";
}

function showHideDeleteNavMenu(x, y) {
	if (y) {
		for (i=0; i<menus.length; i++) {
			hide("n" + (i+1));
			document.getElementById("m" + (i+1)).className="menuTopItem";
		}
		if (menus[x-1].length>1) {
			show("n" + x);
		}
		document.getElementById("m" + x).className="menuTopItemOver";
	}
	else {
		timeToDeleteNavMenu(x);
	}
}

function navMenus() {
	for (i=0; i<menus.length; i++) {
		document.write('<td><div id="n'+(i+1)+'" class="menuDiv" onmouseover="keepActiveNavMenu()" onmouseout=showHideDeleteNavMenu('+(i+1)+',0)>');
		for (j=0; j<menus[i].length-1; j++) {
			document.write('<div class="menuItem" onmouseover=\'this.className="menuItemOver"\' onmouseout=\'this.className="menuItem"\' onMouseDown="javascript:document.location.href=\''+menus[i][j+1][1]+'\'"><nobr><a href="'+menus[i][j+1][1]+'" class="linkSubNav"><img src="../images/navArrow.gif" width="7" height="7" alt="" border="0" align="absmiddle" style="padding-right:5px;" />'+menus[i][j+1][0]+'</a></nobr></div>');
		}
		document.write('</div></td><td id="m'+(i+1)+'" onmouseover="keepActiveNavMenu();showHideDeleteNavMenu('+(i+1)+',1)" onmouseout="showHideDeleteNavMenu('+(i+1)+',0)" class="menuTopItem" onmousedown="document.location.href=\''+menus[i][0][1]+'\'">'+menus[i][0][0]+'<img src="../images/clr.gif" alt="" width="1" height="30" border="0" align="absmiddle" /></td>');
	}
}

function navSiteMapMenus() {
	for (i=0; i<menus.length; i++) {
		document.write('<tr><td><a href="'+menus[i][0][1]+'" class="linkMenuItem" style="font-size:105%;line-height:20px;">'+trimString(menus[i][0][0],"&nbsp;")+'</a></td></tr>');
		for (j=0; j<menus[i].length-1; j++) {
			document.write('<tr><td style="padding-left:12px;"><a href="'+menus[i][j+1][1]+'" class="linkMenuItem" style="font-size:100%;">'+trimString(menus[i][j+1][0],"&nbsp;")+'</a></td></tr>');
		}
	}
}

function navResources(name, listType, imgSrc, imgAlt, color) {
	if ((name=="Calendar")&&(listType=="Full Listing")) resources = calendar.length
	else if ((name=="Calendar")&&(listType=="Latest Listing")) {
		resources = Math.min(14, calendar.length);
		if (calendar[resources-1][1].length==0) resources = Math.max(1, resources-1);
	}
	else if ((name=="Newsletters")&&(listType=="Full Listing")) resources = newsletters.length
	else if ((name=="Newsletters")&&(listType=="Latest Listing")) {
		resources = Math.min(13, newsletters.length);
		if (newsletters[resources-1][1].length==0) resources = Math.max(1, resources-1);
	}
	else if ((name=="Worship Bulletins")&&(listType=="Full Listing")) resources = worshipBulletins.length
	else if ((name=="Worship Bulletins")&&(listType=="Latest Listing")) {
		resources = Math.min(21, worshipBulletins.length);
		if (worshipBulletins[resources-1][1].length==0) resources = Math.max(1, resources-1);
	}
	else if ((name=="Sermons")&&(listType=="Full Listing")) resources = sermons.length
	else if ((name=="Sermons")&&(listType=="Latest Listing")) {
		resources = Math.min(21, sermons.length);
		if (sermons[resources-1][1].length==0) resources = Math.max(1, resources-1);
	}
	for (i=0; i<resources; i++) {
		document.write('<tr>');
		if (((name=="Calendar")&&(calendar[i][1].length==0)) ||
			((name=="Newsletters")&&(newsletters[i][1].length==0)) ||
			((name=="Worship Bulletins")&&(worshipBulletins[i][1].length==0)) ||
			((name=="Sermons")&&(sermons[i][1].length==0))) {
			document.write('<td colspan="2" valign="top" style="padding-bottom:7px;">');
			document.write ('<span class="text" style="font-size:115%; font-weight:bold;"><span class="text'+color+'">');
			if (name=="Calendar") document.write(calendar[i][0])
			else if (name=="Newsletters") document.write(newsletters[i][0])
			else if (name=="Worship Bulletins") document.write(worshipBulletins[i][0])
			else if (name=="Sermons") document.write(sermons[i][0]);
			document.write('</span></span></a></td>');
		}
		else {
			document.write('<td width="15" valign="top">');
			document.write('<img src="../images/'+imgSrc+'.gif" alt="'+imgAlt+'" width="15" height="11" border="0" vspace="3" />');
			document.write('</td>');
			document.write('<td align="left" valign="top" style="padding-bottom:5px;">');
			document.write('<a href="');
			if (name=="Calendar") document.write(calendar[i][1])
			else if (name=="Newsletters") document.write(newsletters[i][1])
			else if (name=="Worship Bulletins") document.write(worshipBulletins[i][1])
			else if (name=="Sermons") document.write(sermons[i][1]);
			document.write ('" class="linkText'+color+'"><span class="text">');
			if (name=="Calendar") document.write(calendar[i][0])
			else if (name=="Newsletters") document.write(newsletters[i][0])
			else if (name=="Worship Bulletins") document.write(worshipBulletins[i][0])
			else if (name=="Sermons") document.write(sermons[i][0]);
			document.write('</span></a></td>');
		}
		document.write('</tr>');
	}
}

function Mod(X, Y) { return X - Math.floor(X/Y)*Y }

function repeatString(strInput, intCount) {
	var arrTmp = new Array(intCount+1);
	return arrTmp.join(strInput);
}

function trimString(strInput, strSearch) {
	while(strInput.indexOf(strSearch)==0) {
		strInput = strInput.substring(strSearch.length);
	}
	while(strInput.indexOf(strSearch, strInput.length-strSearch.length)>0) {
		strInput = strInput.substring(0, strInput.length-strSearch.length);
	}
	return strInput;
}

function navPhotoGallery(color) {
	for (i=0; i<photoGallery.length; i+=4) {
		for (j=0; j<2; j++) {
			document.write('<tr>');
			for (k=i; k<(Math.min(i+4, photoGallery.length)); k++) {
				if (j==0) {
					document.write('<td width="178" align="center" valign="bottom" style="background:no-repeat; background-position:top; background-image:url(../images/navPhotoGalleryTopBg.gif); margin:4px; padding-right:10px; padding-top:18px; padding-left:10px; padding-bottom:4px;">');
					document.write('<a href="'+photoGallery[k][2]+'" onClick="showFullScreen(this.href); return false" class="linkText'+color+'"><span class="text">');
					document.write('<img src="'+photoGallery[k][1]+'" alt="Slide Show: '+photoGallery[k][0]+'" class="img'+color+'" width="144" height="96" border="0" />');
				}
				else {
					document.write('<td width="178" align="center" valign="top" style="background:no-repeat; background-position:bottom; background-image:url(../images/navPhotoGalleryBotBg.gif); margin:4px; padding-right:10px; padding-top:4px; padding-left:10px; padding-bottom:18px;">');
					document.write('<a href="'+photoGallery[k][2]+'" onClick="showFullScreen(this.href); return false" class="linkText'+color+'"><span class="text">');
					document.write(photoGallery[k][0]);
				}
				document.write('</span></a></td>');
			}
			if (Mod((k-1),4)!=3) document.write(repeatString('<td>&nbsp;</td>',(4-Mod(k,4))));
			document.write('</tr>');
		}
	}
}

function navStoryGallery(color) {
	for (i=0; i<storyGallery.length; i++) {
		document.write('<a href="'+storyGallery[i][1]+'" class="linkText'+color+'">');
		document.write('<img src="'+storyGallery[i][2]+'"alt="My Glade Story: '+storyGallery[i][3]+'" class="img'+color+'"');
		if (i==0) document.write(' width="167"')
		else document.write(' width="49"');
		document.write(' height="64" style="margin-right:9px; margin-top:18px; margin-left:9px; margin-bottom:0px;" />');
		document.write('</a>');
	}
}

// Retreive and set cookies functions

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Change text size functions

function modRule(x) {
	if (!document.styleSheets) return;
	var mycss = new Array();
	if (document.styleSheets[0].cssRules)
		mycss = document.styleSheets[0].cssRules;
	else
		mycss = document.styleSheets[0].rules;
	mycss[0].style.fontSize = x + 'px';
}

getsize = getCookie("size")||10;
size = parseInt(getsize);
minimumFont = 10;
maximumFont = 13;

function changeTextSize(x) {
	size = (size < maximumFont) ? (size+x) : minimumFont;
	modRule(size);
	setCookie("size", size, "", "/");
}

if(getCookie("size")) modRule(getCookie("size"));

// Load functions

function onLoadFunctions() {
	if (!(navigator.userAgent.indexOf("Opera") != -1)) changeTextSize(0);
}
	
// No-spam email function

function email(a, b, c) {
	if (!b) b = "gladechurch.org";
	if (c) c = "?subject="+c; else c = "";
	document.location = "mailto:"+a+"@"+b+c;
}

// Pop-up windows functions

function centerWindow(u, n, w, h, p) {
	if (!p) p = "scrollbars=no,menubars=no,resizeable=no,toolbar=no,directories=no,status=no";
	wl = (screen.width-w)/2;
	wt = (screen.height-h)/2;
	pr = "width=" + w + ",height=" + h + ",left=" + wl + ",top=" + wt + "," + p;
	win = window.open(u, n, pr);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

function launchWebcast(u) {
	centerWindow(u, "webcast", "615", "520");
}

