/* --------------------------------------------------------------------------------------------------------
AUTHOR:			Ryan Marincovich
DATE CREATED:	2009.09.24
Client:         Archdiocese of Saint Paul/Minneapolis
File Purpose:	Common Javascript File
-------------------------------------------------------------------------------------------------------- */




/* ---------- OVERLAY AND MEDIA DATA AND FNS ------------------------------------------------ */
var isOverlayLoaded = false;

// build overlay objects (run onload)
function buildOverlay() {
	
	// global overlay elements
	overlayMainObj = document.getElementById('mainOverlay');
	overlayBodyObj = document.getElementById('container');
	overlayBGObj = document.getElementById('overlayBG');
	overlayGalleryObj = document.getElementById('photoGallery');
	overlayVideoPlayerObj = document.getElementById('videoPlayer');
	overlayAudioPlayerObj = document.getElementById('audioPlayer');
	
	// calc height
	windowHeight = overlayMainObj.offsetHeight;
	contentHeight = overlayBodyObj.offsetHeight;
	overlayBGObj.style.height = Math.max(windowHeight, contentHeight) + "px";
	
	// set var to note objects are built and ready
	isOverlayLoaded = true;
}

// function to open flash photo gallery
function galleryOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayGalleryObj.style.visibility = 'visible';
}

// function to open photo gallery 
function photoGalleryOpen(intResourceID, strGalleryTitle, strFlashSRC, strDetailURL) {
	// open the overlay
	galleryOpen();
	
	// get frame objects
	var objGalleryContainer = document.getElementById('photoGalleryFlash');
	overlayGalleryTitleObj = document.getElementById("galleryTitle");
	overlayGalleryLinksObj = document.getElementById("galleryLinks");
	overlayGalleryAddThisObj = document.getElementById('photoGalleryAddThis');
	overlayGalleryTitleObj.innerHTML = strGalleryTitle;
	overlayGalleryLinksObj.innerHTML = '<a href="/resources/photo-galleries-detail.php?intResourceID=' + intResourceID + '">Details</a> &nbsp;|&nbsp; <a href="/resources/photo-galleries.php">View All Photo Galleries</a>';
	overlayGalleryAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strGalleryTitle + ' (Archdiocese of Saint Paul and Minneapolis)');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Photo Gallery ' + intResourceID + '</div>';
	strHTML += '<script type="text/javascript">';
	strHTML += '	buildFlashMovie(500, 400, \'' + strFlashSRC + '\', 9, \'\', "photoGalleryFlash");';
	strHTML += '<\/script>';
	strHTML += '<noscript>';
	strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="500" height="400" title="Photo Gallery">';
	strHTML += '		<param name="movie" value="' + intResourceID + '" />';
	strHTML += '		<param name="quality" value="high" />';
	strHTML += '		<param name="bgcolor" value="#000000" />';
	strHTML += '		<embed src="' + intResourceID + '" quality="high" bgcolor="#000000" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="500" height="400"></embed>';
	strHTML += '	</object>';
	strHTML += '</noscript>';
	// set frame html
	objGalleryContainer.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objGalleryContainer.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to close flash photo gallery (clears innerHTML of video frame)
function closeGallery() {
	overlayMainObj.style.visibility = 'hidden';
	overlayGalleryObj.style.visibility = 'hidden';
	var objGallery = document.getElementById('photoGalleryFlash');
	objGallery.innerHTML = '';
}


// function to open video overlay
function videoPlayerOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayVideoPlayerObj.style.visibility = 'visible';
}

// define function to start playing a video in a div obj without reloading the page
function openVideoDetail(intResourceID, strVideoTitle, strFlashSRC, strDetailURL) {
	// open the overlay
	videoPlayerOpen();
	// get frame objects
	var objVideoFrame = document.getElementById('videoFrame');
	overlayVideoTitleObj = document.getElementById("videoTitle");
	overlayVideoLinksObj = document.getElementById("videoLinks");
	overlayVideoPlayerAddThisObj = document.getElementById('videoPlayerAddThis');
	overlayVideoTitleObj.innerHTML = strVideoTitle;
	overlayVideoLinksObj.innerHTML = '<a href="/resources/videos-detail.php?intResourceID=' + intResourceID + '">Details</a> &nbsp;|&nbsp; <a href="/resources/videos.php">View All Videos</a>';
	overlayVideoPlayerAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strVideoTitle + ' (Archdiocese of Saint Paul and Minneapolis)');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Playing: ' + strFlashSRC + '</div>';
	strHTML += '<script type="text/javascript">';
	strHTML += '	buildFlashMovie(400, 345, \'' + strFlashSRC + '\', 9, \'\', "videoFrame");';
	strHTML += '<\/script>';
	strHTML += '<noscript>';
	strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="400" height="345" title="Video Player">';
	strHTML += '		<param name="movie" value="' + strFlashSRC + '" />';
	strHTML += '		<param name="quality" value="high" />';
	strHTML += '		<param name="wmode" value="transparent" />';
	strHTML += '		<embed src="' + strFlashSRC + '" quality="high" wmode="transparent" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="400" height="345"></embed>';
	strHTML += '	</object>';
	strHTML += '</noscript>';
	// set video frame html
	objVideoFrame.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objVideoFrame.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to stop a video (clears innerHTML of video frame)
function stopVideo() {
	overlayMainObj.style.visibility = 'hidden';
	overlayVideoPlayerObj.style.visibility = 'hidden';
	var objVideoFrame = document.getElementById('videoFrame');
	objVideoFrame.innerHTML = '';
}

// function to open audio overlay
function audioPlayerOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayAudioPlayerObj.style.visibility = 'visible';
}

// function to play audio file through flash player controls
function openAudioDetail(intResourceID, strAudioTitle, strSource, strDetailURL) {
	audioPlayerOpen();
	var objAudioFrame = document.getElementById('audioFrame');
	var overlayAudioTitleObj = document.getElementById('audioTitle');
	overlayVideoLinksObj = document.getElementById("audioLinks");
	overlayAudioPlayerAddThisObj = document.getElementById('audioPlayerAddThis');
	overlayAudioTitleObj.innerHTML = strAudioTitle;
	overlayVideoLinksObj.innerHTML = '<a href="/resources/audio-detail.php?intResourceID=' + intResourceID + '">Details</a> &nbsp;|&nbsp; <a href="/resources/audio.php">View All Audio</a>';
	overlayAudioPlayerAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strAudioTitle + ' (Archdiocese of Saint Paul and Minneapolis)');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Playing: ' + strSource + '</div>';
	strHTML += '<script type="text/javascript">';
	strHTML += '	buildFlashMovie(300, 80, \'' + strSource + '\', 9, \'\', "audioFrame");';
	strHTML += '<\/script>';
	strHTML += '<noscript>';
	strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="300" height="80" title="Audio Player">';
	strHTML += '		<param name="movie" value="' + strSource + '" />';
	strHTML += '		<param name="quality" value="high" />';
	strHTML += '		<param name="wmode" value="transparent" />';
	strHTML += '		<embed src="' + strSource + '" quality="high" wmode="transparent" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="300" height="80"></embed>';
	strHTML += '	</object>';
	strHTML += '</noscript>';
	// set container html
	objAudioFrame.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objAudioFrame.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to open audio player error message (for IE6)
function openAudioError(strAudioTitle, strFilename) {
	audioPlayerOpen();
	var objAudioFrame = document.getElementById('audioFrame');
	var overlayAudioTitleObj = document.getElementById('audioTitle');
	overlayAudioTitleObj.innerHTML = strAudioTitle;
	// init html var and fill with content
	var strHTML = '';
	strHTML += 'Sorry, streaming audio is not compatible with older versions of Internet Explorer (version 6 and earlier).<br /><br />';
	strHTML += 'If possible, we recommend upgrading your browser (try <a href="http://www.getfirefox.com" target="_blank">FireFox</a>).';
	// set container html
	objAudioFrame.innerHTML = strHTML;
}

// function to stop audio (clears innerHTML of audio frame)
function stopAudio() {
	overlayMainObj.style.visibility = 'hidden';
	overlayAudioPlayerObj.style.visibility = 'hidden';
	var objAudioFrame = document.getElementById('audioFrame');
	objAudioFrame.innerHTML = '';
}




/* ---------- WINDOW SIZE DATA AND FNS -------------------------------------------------------------------------------------- */

// get window total width
var windowTotalWidth = 0;
var setOriginalWidth = function(boolCheckSmall) {
	windowTotalWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	if (boolCheckSmall && (windowTotalWidth <= 1024)) {
		window.location = "/utilities/change-size.php?strSiteSize=small";
	}
}


// check to window size
var checkWindowSize = function() {
	
	objCurrentWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	
	var strSiteSize = '';
	
	// modify width of site based on window
	if (windowTotalWidth > 1024) {
		if (objCurrentWidth <= 1024) {
			strSiteSize = 'small';
			window.location = "/utilities/change-size.php?strSiteSize=" + strSiteSize;
		}
	} else {
		if (objCurrentWidth > 1024) {
			strSiteSize = 'wide';
			window.location = "/utilities/change-size.php?strSiteSize=" + strSiteSize;
		}
	}
}




/* ---------- UTILITY FNS ----------------------------------------------------------------------------------------------------------- */

// set directories session data
var setDirectoriesSession = function() {
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.open("GET", "/directories/ajax-set-session.php", true);
	xmlHttp.send(null);	
}

// set careers session data
var setCareersSession = function() {
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.open("GET", "/careers/ajax-set-session.php", true);
	xmlHttp.send(null);	
}


// toggle object menu
var toggleMenu = function(which, strType) {
	var objLink = document.getElementById(which);
	var objMenu = document.getElementById(which + 'Menu');
	
	if (objMenu.style.display == 'block') {
		objMenu.style.display = 'none';
		objLink.innerHTML = 'More Options';
	} else {
		objMenu.style.display = 'block';
		objLink.innerHTML = 'Hide Options';
	}
	if (strType == 'career') {
		setCareersSession();
	} else {
		setDirectoriesSession();
	}
};


// function to generate an addthis button
var buildAddThisButton = function(strAltURL, strAltTitle) {
	// set alt params for a tag
	var strAltURLParam = '';
	var strAltTitleParam = ''
	if (strAltURL) strAltURLParam = ' addthis:url="' + strAltURL + '"';
	if (strAltTitle) strAltTitleParam = ' addthis:title="' + strAltTitle + '"';
	// set alt params for old mouseover that is needed to work in an overlay
	if (!strAltURL) strAltURL = '[URL]';
	if (!strAltTitle) strAltTitle = '[TITLE]';
	return '<a href="http://addthis.com/bookmark.php?v=250&amp;username=bicycletheoryasm" class="addthis_button"' + strAltURLParam + strAltTitleParam + ' onmouseover="return addthis_open(this, \'\', \'' + strAltURL + '\', \'' + strAltTitle + '\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0" class="share" /></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=bicycletheoryasm"></script>';
}

// form field functions
function setHeaderFields() {
	var ffGlobalSearch = document.forms.frmGlobalSearch.strSearchText;
	if (!ffGlobalSearch.value) {
		ffGlobalSearch.style.color = "#999";
		ffGlobalSearch.value = " - Keyword Search - ";
	}
}
function clearField(which) {
	if (which.value.indexOf('Search') >= 0) {
		which.value = "";
		which.style.color = "#000";
	}
}
