/* ===== Body onload function ===== */
function body_onload() {
    if ( typeof( window.addEventListener ) != "undefined" ) {
	    window.addEventListener("load", body_onload, false);
    } else if ( typeof( window.attachEvent ) != "undefined" ) {
	    window.attachEvent("onload", body_onload);
    } else {
	    MyFunction();
    }
}

/* ===== Start Common Site Tools ===== */
function getRootUrl() {
    pathlen = location.href.indexOf('.org/');
    return location.href.substring(0, pathlen + 5);
}

function basePageUrl() {
    var pathlen = location.href.indexOf('.aspx');

    // Make sure that we're not on the homepage
    if (pathlen == -1)
        return location.href;
    else
        return location.href.substring(0, pathlen + 5);
}

function pageQueryString() {
    var qs = $("__printPage__");
    var url = '';

    if (qs == null)
        return "";
    else
        return qs.value;
}           

function bookmarkPage() { 
    var url;
    var qs = pageQueryString();
    
    if (qs == "")
        url = basePageUrl()
    else
        url = basePageUrl() + "?" + qs;

	if (window.sidebar) // Mozilla Firefox Bookmark
		window.sidebar.addPanel(document.title, url,"");
	else if (window.external)  // IE Favorite
		window.external.AddFavorite(url, document.title); 
	else if(window.opera && window.print) // Opera Hotlist
		return true; 
} 
	
	
function printPage() {
    var qs = pageQueryString();
    var url;
    
    if (qs == "")
        url = basePageUrl() + '?elca_printpage=1';
    else
        url = basePageUrl() + "?elca_printpage=1&" + qs;
    
    newWindow = window.open(url, '','');
	/*
	// Fires the print dialog window within a browser
	if(window.attachEvent) {
		newWindow.attachEvent('onload',function Print(){
			newWindow.print()
			});
		}
	*/	
}

function emailPage() {
    var qs = pageQueryString();
    var loc = getRootUrl() + "ELCA/Email-a-Page.aspx?SCItemID=" + SCItemID;
    
    if (qs == "")
        window.location = loc;
    else
        window.location = loc + "&" + qs;
}

/* MailTo's 2nd parameter: SUBJECT is optional */
function MailTo(Recipient) {
	var qs = pageQueryString();
	var loc = getRootUrl() + "ELCA/MailTo.aspx?SCItemID=" + SCItemID + "&To=" + Recipient;

	if (typeof (arguments[1]) != "undefined") {
		loc = loc + "&Subj=" + encodeURIComponent(arguments[1]);
	}

	if (qs == "")
		window.location = loc;
	else
		window.location = loc + "&" + qs;
}
/* ===== End Common Site Tools ===== */

/* ===== Start Navigation ===== */

var onID = "";
var hiID = "";
var globalNav;
var globalNavItems;

//function hideSubNavs() {

//    // Turn off all subnavs
//    $("sHome").style.display="none";
//    $("sWho").style.display="none";
//    $("sWhat").style.display="none";
//    $("sAction").style.display="none";
//    $("sGrowing").style.display="none";
//	}

//function showSubNav(id) {

//	hideSubNavs();

//    thisSubNav = id.substring(1);
//    hiID = 'g'+thisSubNav;
//    // Turn off all global nav states, except current on state. Ensures current top level item always stays "lit"
//    var globalNav = $('globalNavItems');
//    var globalNavItems = globalNav.childElements();
//    globalNavItems.each(function(item) {
//        thisID = item.identify();
//        if(thisID != onID) {
//            item.removeClassName('hi');
//            }
//        });

//    // Turn highlighted nav and subnav on
//    $(hiID).addClassName('hi');
//    thisSubNav = thisSubNav.toLowerCase();
//    $(id).style.display="block";
//    }


//function defaultNavState() {
//    // Reverts to page's default global nav and subnav state
//    globalNavItems.each(function(item) {
//        if(item.hasClassName('on')) {
//            onID = item.identify();
//            subNavID = "s"+ onID.substring(1);
//            showSubNav(subNavID);
//            }
//        });
//        
//	if(onID == "") {
//		// No top level navigation is "on" so mimic nav state of home page
//		showSubNav("sHome");
//		}
//    }

//function initGlobalNav() {
//	if($('globalAnnounce')) {  // Check to see if there IS a global announcement
//		cookieGA = getCookie('hideELCAGA'); // Check for hideELCAGA cookie. If true, don't display globalAnnounce div.
//		if(cookieGA != "1") {
//			$('globalAnnounce').style.display="block";
//			}
//		}
//		
//    globalNavItems = $('globalNavItems').childElements();
//    defaultNavState();
//    
//    
//    globalNavItems.each(function(item) {
//		
//		navText = item.down();
//        // mouseover event to trigger the call to showSubNav()
//        Event.observe(navText,'mouseover',function() {
//            thisID = item.identify();
//            thisSubNav = thisID.substring(1);
//            thisSubNav = thisSubNav.toLowerCase();
//            subNavID = "s"+ thisID.substring(1);
//            showSubNav(subNavID);
//            }.bind(item));
//    
//        // mouseout event to remove highlight state from top nav if item is not current on-state
//        
//        Event.observe(navText,'mouseout',function() {
//            thisID = item.identify();
//            if(onID != thisID && hiID != thisID) {
//                $(thisID).removeClassName('hi');
//                }
//            }.bind(item));
//        });

//    // Reset global and subnav to default page state when user mouses over other areas
//    resetZone = new Array();
//    resetZone[0] = "globalSiteHeader";
//    resetZone[1] = "globalContent";

//    resetZone.each(function(zone) {
//        if($(zone)) {
//            Event.observe($(zone),'mouseover',function() {
//                defaultNavState();
//                });
//            }
//        });
//    }

//Event.observe(window,'load',initGlobalNav);

/* ===== End Navigation ===== */



/* ===== Start Spotlight Tabs ===== */
var slTabs;
var slContent;
var totTabs;
var currTab = 0;
var nextTab = 0;
var slPE;
var ctrlTab = "pause";
var slCycle = 0;
var slDelay = 8; // default delay in seconds

// Initialize the Spotlight Tabs
function initSpotlight() {
    slTabs = $('slTabs').childElements();
    slContent = $('slContent').childElements();
    totTabs = slTabs.length;
    
    // Turn off cycling if only one tab.
    if(totTabs < 2) {
    	slCycle = 0;
    	}
    if(slCycle == 1) {
        // Add play/pause button to tabs if cycle is on
        $('slPlayPause').insert('<a href="javascript:ctrlSpotlight();"><img src="/Design/SiteImages/Structure/btn_play.gif" border="0" alt="Play/Pause" id="slToggle" /></a>');
        }
    
    ctrlSpotlight();
    }

// Handles user click on a tab
function clickSpotlight(tab) {
    if(slPE) {
        slPE.stop();
        ctrlTab = "pause";
        $('slToggle').setAttribute('src','/Design/SiteImages/Structure/btn_play.gif');
        }
    showSpotlight(tab);
    }

// Displays current tab
function showSpotlight(tab) {
    slTabs.each(function(t) {
        t.removeClassName('on');
        });
    
    slTabs[tab].addClassName('on');
    
    slContent.each(function(c) {
        c.removeClassName('on');
        });
    slContent[tab].addClassName('on');
    currTab = tab;
    nextTab = currTab+1;
    if(nextTab == totTabs) {
        nextTab = 0;
        }
    }

// Toggles play/pause
function ctrlSpotlight() {
    // Self-running set of tabs cycle through
    if(slCycle == 1) { 
        if (ctrlTab == "pause") { 
            ctrlTab = "play"; 
            $('slToggle').setAttribute('src','/Design/SiteImages/Structure/btn_pause.gif');
            } 
        else { 
            ctrlTab = "pause"; 
            $('slToggle').setAttribute('src','/Design/SiteImages/Structure/btn_play.gif');
            }
    
        if(ctrlTab == "pause") {
            slPE.stop();
            }
        else {
            showSpotlight(nextTab);
            slPE = new PeriodicalExecuter(function() {
                showSpotlight(nextTab);
                }, slDelay);
            }
        }
        
    // Just initialize first tab
    else {
        showSpotlight(nextTab);
        }
    }

/* ===== End Spotlight Tabs ===== */

/* ===== Start Image Slideshow ===== */

var ssNum;
var ssPrev;
var ssNext;
var ssTot;
var ssPhotos;
var ssPath = "/"; // Optional media library path, otherwise each photo should include full path

// Pre-load images
if(typeof ssPhotos != "undefined") {
	for(p=0;p<ssPhotos.length;p++) {
		newPhoto = "photo"+p;
		newPhoto = new Image();
		newPhoto.src = ssPath+ssPhotos[p];
		}
	}

function ssPrevcb() {
    // Callback function for previous button
    ssShow(ssPrev);
    }

function ssNextcb() {
    // Callback function for next button
    ssShow(ssNext);
    }

function ssCurr(ssNum) {
    ssNum = parseInt(ssNum);
    ssPrev = ssNum-1;
    ssNext = ssNum+1;

    if(ssPrev < 0) {
        ssPrev = ssTot-1;
        }
    if(ssNext > ssTot-1) {
        ssNext = 0;
        }

    $('ssLgImg').setAttribute('src',ssPath+ssPhotos[ssNum]);
    $('ssCaption').replace('<span id="ssCaption">'+ssCaptions[ssNum]+'</span>');
    new Effect.Appear('ssLgImg',{to: 1.0, from: 0.1, duration: 1.2});
    $('dispSSCurr').replace("<span id='dispSSCurr'> | Image "+(ssNum+1)+" of "+ ssTot +" | </span>");
    }

function ssShow(ssNum) {
	if(!ssNum) {
		ssNum = 0;
		}
    $('ssWrap').style.display="block";
    ssCurr(ssNum);
    }
    
function ssHide() {
    new Effect.Fade('ssWrap',{to:0.0, from: 1.0, duration: 1.2});
    }    

function ssPrevImg() {
	new Effect.Fade('ssLgImg',{to:0.1, from: 1.0, duration: 1.2, afterFinish: ssPrevcb});
    }    
    
function ssNextImg() {
	new Effect.Fade('ssLgImg',{to:0.1, from: 1.0, duration: 1.2, afterFinish: ssNextcb});
    }

/* ===== End Image Slideshow ===== */

/* ===== Start Re-position Header Graphics ===== */
// For 3 column and 2 column with left-hand navigation, re-position the header graphic image
//function moveContentHeader() {
//    var main3Col = $('globalWrap').down('#main3Col');
//    var main2ColNav = $('globalWrap').down('#main2ColNav');
//    // If a main3Col or main2ColNav layout
//    if(main3Col != undefined || main2ColNav != undefined) {
//		// Find the header image and re-position
//        if(typeof $$('div.contentHeader img') == "object") {
//        	hdrImage = $$('div.contentHeader img');
//        	hdrImage[0].style.padding="0 0 0 25px";
//        	}
//        }
//    }
//Event.observe(window,'load',moveContentHeader);
/* ===== End Re-position Header Graphics ===== */

/* ===== Start Highlight Boxes ===== */
/*

/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/

function createElement(element) {
    if (typeof document.createElementNS != 'undefined') {
        return document.createElementNS('http://www.w3.org/1999/xhtml', element);
    }
    if (typeof document.createElement != 'undefined') {
        return document.createElement(element);
    }
    return false;
}

function insertTop(obj) {
    // Create the two div elements needed for the top of the box
    d=createElement("div");
    d.className="bt"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
    obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
    // Create the two div elements needed for the bottom of the box
    d=createElement("div");
    d.className="bb"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
    obj.appendChild(d);
}

function wrapHLBox(d) {
    var thediv, outer, i1, i2;
    // Save the original outer div for later
        thediv = d;
    //     Create a new div, give it the original div's class attribute, and replace 'boxWrap' with 'cb'
        outer = createElement('div');
        outer.className = thediv.className;
        outer.className = thediv.className.replace('boxWrap', 'cb');
    // Change the original div's class name and replace it with the new div
        thediv.className = 'i3';
        thediv.parentNode.replaceChild(outer, thediv);
    // Create two new div elements and insert them into the outermost div
        i1 = createElement('div');
        i1.className = 'i1';
        outer.appendChild(i1);
        i2 = createElement('div');
        i2.className = 'i2';
        i1.appendChild(i2);
    // Insert the original div
        i2.appendChild(thediv);
    // Insert the top and bottom divs
        insertTop(outer);
        insertBottom(outer);
    }

function wrapWhiteBox(d) {
    var thediv, outer, i1, i2;
    // Save the original outer div for later
        thediv = d;
    //     Create a new div, give it the original div's class attribute, and replace 'boxWrap' with 'cb'
        outer = createElement('div');
        outer.className = thediv.className;
        outer.className = thediv.className.replace('boxWhite', 'wcb');
    // Change the original div's class name and replace it with the new div
        thediv.className = 'i3';
        thediv.parentNode.replaceChild(outer, thediv);
    // Create two new div elements and insert them into the outermost div
        i1 = createElement('div');
        i1.className = 'i1';
        outer.appendChild(i1);
        i2 = createElement('div');
        i2.className = 'i2';
        i1.appendChild(i2);
    // Insert the original div
        i2.appendChild(thediv);
    // Insert the top and bottom divs
        insertTop(outer);
        insertBottom(outer);
    }

function initBoxWrap() {
    var boxHL = $$('div.boxWrap');
    var boxWH = $$('div.boxWhite');
    
    // Loop through the found div elements    
    boxHL.each(function(d) {
        wrapHLBox(d);
        });
        
    boxWH.each(function(d) {
        wrapWhiteBox(d);
        });    
    }


Event.observe(window,'load',initBoxWrap);

/* ===== End Highlight Boxes ====== */

/* ===== Start Video Pop Up ====== */
var vpWin = "";

function popVideo(file,id,ttl) {
	vpWin = window.open('/Design/Javascript/pop_video.htm?file='+file+'&id='+id+'&ttl='+ttl,'videoPop','width=573,height=510,left=100,top=40,personal=no,toolbar=no,menubar=no,scrollbars=no,location=no,status=yes,resizable=no,directories=no');
	
	}

/* ===== End Video Pop Up ====== */

/* ===== Start E-Card Pop Up ====== */
var ecWin = "";

function writeCard(ttl,img,msg) {
	ecWin = window.open('/Design/Javascript/pop_ecard.htm?ttl='+ttl+'&img='+img+'&msg='+msg,'ecardPop','width=827,height=510,left=100,top=40,personal=no,toolbar=no,menubar=no,scrollbars=no,location=no,status=yes,resizable=yes,directories=no');
	
	}

/* ===== End E-Card Pop Up ====== */

/* ===== Start Get/Set Cookie Functions ===== */
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) {
				c_end=document.cookie.length; 
				}
			return unescape(document.cookie.substring(c_start,c_end));
			} 
		}
	return "";
	}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/");
	}

/* ===== End Get/Set Cookie Functions ===== */

/* ===== Start Hide Global Announce ====== */

function hideGlobalAnnounce() {
	setCookie('hideELCAGA','1',1);
	$('globalAnnounce').style.display = "none";
	}

/* ===== End Hide Global Announce ======  */
