
///////////////////////////////////////////////////////////////////////////////
//
// Browser Attributes
//

function getBrowserVersion() {
	return parseInt(navigator.appVersion);
}

function getBrowserName() {
	var myBrowser = navigator.appName;

	if (myBrowser.indexOf("Microsoft") != -1) {
		return "IE";
	}
	else if (myBrowser.indexOf("Netscape") != -1) {
		return "NS";
	}
	else {
		return "??";
	}
}

// global browser attributes
//--------------------------------------------
browserName    = getBrowserName();
browserVersion = getBrowserVersion();
myWindowHeight = screen.availHeight;
myWindowWidth  = screen.availWidth;
isNS4          = (browserName == "NS" && browserVersion < 5.0);
windowIsHuge   = false;

// default setting
if (myWindowWidth < 600) {
	myWindowWidth = 800;
}
else if (myWindowWidth > 1024) {
	myWindowWidth = 1024;
	windowIsHuge  = true;
}

// default setting
if (myWindowHeight < 400) {
	myWindowHeight = 600;
}
else if (myWindowHeight > 768) {
	myWindowHeight = 768;
	windowIsHuge   = true;
}

function resizeThisWindow() {
	if (windowIsHuge) {
		top.window.moveTo( (screen.width-myWindowWidth)/2,
				(screen.height-myWindowHeight)/2 - 40 );
	}
	else {
		top.window.moveTo(0,0);
	}

	// IE
	if (browserName == "IE") {
		top.window.resizeTo(myWindowWidth, myWindowHeight);
	}
	// NS
	else if (document.layers || document.getElementById) {
		if (top.window.outerHeight < myWindowHeight  ||
				top.window.outerWidth < myWindowWidth  ) {
			top.window.outerHeight = myWindowHeight;
			top.window.outerWidth  = myWindowWidth;
		}
	}
}


///////////////////////////////////////////////////////////////////////////////
//
// Calendar
//

function openCalendarWindow( /* String */ id ) {

	var winLeft = (screen.width - 600) / 2;
	var winTop  = (screen.height - 350) / 2 - 50;

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	popUpWindow = window.open( '/tcbc/calendar/displayevent.jsp?id=' + id,
			'event' + parseInt(Math.random()*100),
			'toolbar=0,location=0,scrollbars=1,resizable=1,status=1,width='+ 600 +',height=' + 350 + ',top=' + winTop + ',left=' + winLeft );
}


///////////////////////////////////////////////////////////////////////////////
//
// Email
//

// Opens e-mail window for Tri-City
// NOTE:  Assumes valid email address
function openDefaultEmailWindow( /* String */ email ) {
	openEmailWindow("", email);
}

// Opens e-mail window for 'pageTitle' (i.e. TCBC, Re:vive, etc.)
// Assumes valid email address
function openEmailWindow( /* String */ pageTitle,
						  						/* String */ email     ) {

	var winLeft = (screen.width - 600) / 2;
	var winTop  = (screen.height - 450) / 2 - 50;

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	popUpWindow = window.open( '/tcbc/email/emailform.jsp?sendToAddress='+email+'&pageTitle='+pageTitle,
			'email'+parseInt(Math.random()*100),
			'toolbar=0,location=0,scrollbars=0,resizable=1,status=1,width='+ 600 +',height=' + 450 + ',top=' + winTop + ',left=' + winLeft );
}


///////////////////////////////////////////////////////////////////////////////
//
// Window Operations
//

var MAX_WINDOW_WIDTH  = 1024;
var MAX_WINDOW_HEIGHT =  750;


function resizeAndRecenter( /* int */     widthUnit,
														/* int */     heightUnit,
														/* boolean */ usePercent ) {
	if (usePercent) {
		_resizeAndRecenterPercent(widthUnit, heightUnit);
	}
	else {
		_resizeAndRecenter(widthUnit, heightUnit);
	}
}

/* private */
function _resizeAndRecenter( /* int */ newWidth,
														 /* int */ newHeight ) {

	/*
	if (screen.width > maxWidth) {
		var p = maxWidth / screen.width;

		newWidth  = maxWidth;
		newHeight = newHeight * p;
	}
	*/
	
	if (newWidth > MAX_WINDOW_WIDTH) {
		newWidth = MAX_WINDOW_WIDTH;
	}
	if (newHeight > MAX_WINDOW_HEIGHT) {
		newHeight = MAX_WINDOW_HEIGHT;
	}

	var winLeft = (screen.width - newWidth) / 2;
	var winTop  = (screen.height - newHeight) / 2 - (newHeight * .05);

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	top.window.moveTo(winLeft, winTop);
	top.window.resizeTo(newWidth, newHeight);
}

/* private */
function _resizeAndRecenterPercent( /* int */ widthPercent,
																		/* int */ heightPercent ) {
	if (widthPercent >= 1) {
		widthPercent = widthPercent / 100;
	}
	if (heightPercent >= 1) {
		heightPercent = heightPercent / 100;
	}

	var newWidth  = widthPercent * screen.availWidth;
	var newHeight = heightPercent * screen.availHeight;

	_resizeAndRecenter(newWidth, newHeight, MAX_WINDOW_WIDTH);
}

// Opens window to specified percent of window space
function openWindowPercent( /* String */     url,
					        				  /* int,double */ percent,
					        					/* String */     parms  ) {

	if (percent > 1) {
		percent = percent/100;
	}

	// sample parms
	// "toolbar=0,location=0,scrollbars=0,resizable=1"

	var winLeft = (screen.width - percent*width) / 2;
	var winTop  = (screen.height - percent*height) / 2 - 50;

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	popUpWindow = window.open( url,
							   "title" + parseInt(Math.random()*100),
							   parms + "," + "width="+ width +",height=" + height + ",top=" + winTop + ",left=" + winLeft );
}


// Opens window to desired size
function openWindowSize( /* String */ url,
						 /* int */    width,
						 /* int */    height,
						 /* String */ parms  ) {

	// sample parms
	// "toolbar=0,location=0,scrollbars=0,resizable=1"

	var winLeft = (screen.width - width) / 2;
	var winTop  = (screen.height - height) / 2 - 50;

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	popUpWindow = window.open( url,
							   "title" + parseInt(Math.random()*100),
							   parms + "," + "width="+ width +",height=" + height + ",top=" + winTop + ",left=" + winLeft );
}

function openDefaultWindow( /* String */ url,
							/* int */    width,
							/* int */    height ) {

	openWindowSize(url, width, height, "toolbar=0,location=0,scrollbars=0,resizable=1,status=1");
}

///////////////////////////////////////////////////////////////////////////////
//
// Picture Stuff
//

function openBigPic( /* String */ windowName,
                     /* int */    photoNum,
                     /* String */ key          ) {

	var windowWidth  = 10;
	var windowHeight = 10;

	var winLeft = (screen.width - windowWidth) / 2;
	var winTop  = (screen.height - windowHeight) / 2 - 50;

	if (winLeft < 0) {
		winLeft = 0;
	}
	if (winTop < 0) {
		winTop = 0;
	}

	popUpWindow = window.open("/tcbc/_util/display.jsp?key=" + key + "&num=" + photoNum,
														"popUp" + parseInt(Math.random()*100),
														"status=1,toolbar=0,location=0,scrollbars=0,resizable=1,width=" + windowWidth + ",height=" + windowHeight + ",top=" + winTop + ",left=" + winLeft);
}


///////////////////////////////////////////////////////////////////////////////
//
// Sermon and Bible stuff
//

function sermon( /* String */ id ) {
	openWindowSize("/sermons/sermonPlayer.jsp?id=" + id + "&ministryId=1",
			320, 200,
			"toolbar=0,location=0,scrollbars=0,resizable=1,status=1");
}

function sermonInfo( /* String */ id ) {

	openWindowSize("/tcbc/sermons/sermon.jsp?id=" + id,
			500, 400,
			"toolbar=0,location=0,scrollbars=0,resizable=1,status=1");
}

function bible( /* String */ bookId,
				/* String */ chapter ) {

	openWindowSize("/sermons/biblePlayer.jsp?book=" + bookId + "&chapter=" + chapter,
			320, 200,
			"toolbar=0,location=0,scrollbars=0,resizable=1,status=1");
}


///////////////////////////////////////////////////////////////////////////////
//
// Show / Hide Display
//

function hide( /* String */ id ) {
	if (document.getElementById(id) ) {
		document.getElementById(id).style.display = "none";
	}
}

function show( /* String */ id ) {
	if (document.getElementById(id) ) {
		document.getElementById(id).style.display = "";
	}
}



// Redirect URL based off list's value
function goHere( /* form */ thisForm ) {
	var act = thisForm.quicklinks.options[thisForm.quicklinks.selectedIndex].value;

	if (act != "nada") {
		window.location.href = act;
	}
}
