var isAlterHomePage = false;
var isOverview = false;
var isRedirecting = false;
var isSpecial = false;
var itsAnImagePage = false;
var alterLinkURLs = new Array();
var alter_col3_width = 180;
var alter_col3_padding_right = 10;

//IE 6.0.2900.2180.XPSP_SP2_gdr.070227-2254 does not work with fade

/* CSS INIT */
document.write('<style type="text/css"> ');
if ( !(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) ) {
	document.write('#alterego_postcard_middle { filter:alpha(opacity=0); opacity:0; } ');
}
document.write('#alterego_postcard_middle_col1 { overflow:hidden; } ' );
document.write('</style> ');

/* GET ELEMENT BY CLASS NAME SCRIPT */
function getElementsByClassName(oElm, strTagName, strClassName){
	// http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}

/* OPACITY CODE */
function setOpacity(object,value) {
	object.style.opacity = value/10;
	object.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/* FADE OUT AND GO SCRIPT */
function alterFadeAndGo(newURL,ff) {
	isRedirecting = true;
	if (ff>=0) {
		ftext = document.getElementById('alterego_postcard_middle');
		setOpacity(ftext,ff);
		setTimeout( "alterFadeAndGo('"+newURL+"',"+(ff-2)+")" ,10);
	} else {
		document.location=newURL;
	}
}

//FADE IN SCRIPT
function firstchangeopacity(ii) {
	setOpacity(document.getElementById('alterego_postcard_middle'),ii);
	ii=ii+2;
	if (ii <= 10) { setTimeout('firstchangeopacity('+ii+')',10); }
}

/*SCROLLING CODE */
var scrolling = 0;
function scrollContent(upordown,divHeight ) {
	if (scrolling == 1) {
		objDiv = document.getElementById("alterego_postcard_middle_col1");
		if ( (objDiv.scrollTop+divHeight < objDiv.scrollHeight && upordown == 1) || (objDiv.scrollTop > 0 && upordown == -1) ) {
			objDiv.scrollTop = objDiv.scrollTop + upordown;
			setTimeout('scrollContent('+(upordown).toString()+','+divHeight.toString()+')',5);
		} else {
			scrolling = 0;
		}
	}
}
function scrollContentUp() {
	scrolling = 1; 
	if (document.getElementById('alterego_navlink_image_container1')) scrollContent(-1,111);
	else scrollContent(-1,300);
}
function scrollContentDown() { 
	scrolling = 1; 
	if (document.getElementById('alterego_navlink_image_container1')) scrollContent(1,111);
	else scrollContent(1,300);
}
function scrollToTop() {
	scrolling = 0;
	objDiv = document.getElementById("alterego_postcard_middle_col1")
	objDiv.scrollTop = 0;
}
function scrollToBottom() {
	scrolling = 0;
	objDiv = document.getElementById("alterego_postcard_middle_col1")
	if (document.getElementById('alterego_navlink_image_container1')) objDiv.scrollTop = objDiv.scrollHeight+111;
	else objDiv.scrollTop = objDiv.scrollHeight+300;
}
function stopScroll() {	scrolling = 0; }

/* THIRD COLUMN, SCROLLING CODE */
function removeScrollBars() {
	if (BrowserDetect.browser !== 'Safari' && document.getElementById('alterego_postcard_middle_col3') ) {
		var w = alter_col3_width; // total width 
		var p = alter_col3_padding_right; // padding right 
		x = document.getElementById('alterego_postcard_middle_col3');
		xin = document.getElementById('alterego_postcard_middle_col3_inside');
		var s = x.clientWidth+0; // small width 
		var d = w-s+0; // scrollbar width (diff)
		// if there is a scrollbar 
		if (d>0) {
			needed = true;
			xin.style.width = (w-p*2-d).toString()+'px';
			// x.style.width = (w-p*2-d).toString()+'px'; x.style.paddingRight = (p+d).toString()+'px'; // does not work in FF 
			// x.className = 'alterego_optionalScrollOn'; 
			x.style.overflow = 'hidden';
		}
	}
}
function alterBarsOn() {
	if (BrowserDetect.browser !== 'Safari') {
		x = document.getElementById('alterego_postcard_middle_col3');
		x.style.overflowY = 'auto';
	}
}
function alterBarsOff() {
	if (BrowserDetect.browser !== 'Safari') {
		x = document.getElementById('alterego_postcard_middle_col3');
		x.style.overflowY = 'hidden';
	}
}


function showBarsIfNeeded() {
	/*show scroll bars if needed (the homepage has a height of 111 px)*/
	optionalScrollButtons = document.getElementById('alterego_postcard_middle_scrollbuttons');
	scrollingContent = document.getElementById("alterego_postcard_middle_col1");
	if ( scrollingContent.scrollHeight > 330 || (scrollingContent.scrollHeight > 151 && document.getElementById('alterego_navlink_image_container1') ) ) {		
		optionalScrollButtons.innerHTML =  '<img onmouseover="scrollContentDown()" onmouseout="stopScroll()" onclick="scrollToBottom()" alt="scroll down" title="click to scroll to the bottom" src="config_central/alter_images/down.gif" width="9" height="5"/> ';
		optionalScrollButtons.innerHTML += '<img onmouseover="scrollContentUp()" onmouseout="stopScroll()" onclick="scrollToTop()"      alt="scroll up"   title="click to scroll to the top"    src="config_central/alter_images/up.gif"   width="9" height="5"/> ';
	} else {
		optionalScrollButtons.innerHTML = '';
	}
}

/* RESET HOMEPAGE NAVIGATION */
function resetHomepageNavigation() {
	n = document.getElementById('alterego_postcard_navigation_inside');
	im1 = document.getElementById('alterego_navlink_image_container1');
	im2 = document.getElementById('alterego_navlink_image_container2');
	im3 = document.getElementById('alterego_navlink_image_container3');
	im4 = document.getElementById('alterego_navlink_image_container4');
	n.style.height = (210).toString()+'px';
	im1.style.height = im2.style.height = im3.style.height = im4.style.height = '180px';
	navLinks = n.getElementsByTagName('a');
//	onclick="alterNavClick(this,\''.$children[$i]->getPid().'\')"
	for (ii = 0; ii < navLinks.length; ii++) {
		//navLinks[ii].href='javascript:alterNavClick(this,\''+navLinks[ii].href+'\')';
		//labels[ii].onmouseover = function() { showBlurb(this) };
		alterLinkURLs[ii] = navLinks[ii].href;
		if (ii==0) navLinks[ii].onclick = function() { alterNavClick(this,alterLinkURLs[0]); } //why? because inside the function, ii is not seen! Alt, rel could be used?
		else if (ii==1) navLinks[ii].onclick = function() { alterNavClick(this,alterLinkURLs[1]); }
		else if (ii==2) navLinks[ii].onclick = function() { alterNavClick(this,alterLinkURLs[2]); }
		else if (ii==3) navLinks[ii].onclick = function() { alterNavClick(this,alterLinkURLs[3]); }
		if (ii < 4) navLinks[ii].href = '#';
	}
}

function replaceLinksWithScriptInContainer(linkContainer) {
	pageLinks = linkContainer.getElementsByTagName('a');
	for (ii = 0; ii < pageLinks.length; ii++) { 
		if ( 
			(pageLinks[ii].href.toString().indexOf('tell.php') < 0)
			&&
			(pageLinks[ii].href.toString().indexOf('javascript') < 0)
			&&
			(pageLinks[ii].href.toString().indexOf('#') < 0)
			&&
			(
			 	(pageLinks[ii].href.toString().indexOf('page.php') > 0)
				||
				(pageLinks[ii].href.toString() == 'http://www.alter-e.com/')
			)
		) { 
			//only change links that 1) do not contain script or tell.php, 2) go to page.php, and 3) are not bookmarks, 4) are page.php files or links home
			pageLinks[ii].href='javascript:alterFadeAndGo(\''+pageLinks[ii].href+'\',10)';
		}
	}
}

/* INIT CODE */
function doThisOnLoad() {
	
	/* if it's the homepage, then reset the navigation */
	if ( isAlterHomePage ) resetHomepageNavigation();
	
	/* fade in the text */
	if ( !(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) ) {
		if (!(isSpecial)) setTimeout('firstchangeopacity(0)',10);
	}
	
	//show scroll bars for main content if needed
	if (!(isSpecial)) showBarsIfNeeded();
	
	/* remove scrollbars on third column */
	if (!(isSpecial)) removeScrollBars();
	else document.getElementById('alterego_postcard_middle_col1').style.overflow = 'auto';
	
	/* This should only happen the FIRST time someone visits the site - use php sessions. */
	if (isAlterHomePage) { alterFancyFadeIntro(); }
	
	/* Hide Blurbs if Overview */
	if (isOverview) { addBlurbEvents(); }
	
	/* Format the image gallery if it is one */
	if (itsAnImagePage) { formatImageGallery(); }
	
	/* Replace links in the main column with fading links */
	if ( !(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) ) {
		if (!(isSpecial)) {
			if ( !(isAlterHomePage) ) {
				replaceLinksWithScriptInContainer( document.getElementById('alterego_logo') );
				replaceLinksWithScriptInContainer( document.getElementById('alterego_postcard_navigation_inside') );
			}
			replaceLinksWithScriptInContainer( document.getElementById('alterego_postcard_middle') );
			replaceLinksWithScriptInContainer( document.getElementById('alterego_footer') );
	//		replaceLinksWithScriptInContainer( document.getElementById('alterego_postcard_top') );
		}
	}
}

window.onload = function() { doThisOnLoad(); }
window.onpageshow = function (evt) {
	if (evt.persisted) window.onload();
}