//Written by Jayson Ward
//Updated: 5/5/2008, 8/20/2008

function $(id) { return document.getElementById(id); } //based on prototype library

// set global variables

var isSafari;
var isIE;
var isFox;
var leftColumnHeight;
var rightColumnHeight;
var numberOfImages;
var imageCaptions;
var imageCounter = 1;
var galleryChoice;
var galleries;
var eventType;
var imageAltAtt;

window.onload = init;

function init()
{
	getBrowser();
	setColumnSizes();
	pathDepth = getPageLocation();
	//setLogoBackground();
	
	if( $("agendaLink") )
	{
		setupAgendaLinks();	
	}
	
	if( $("photoGallery") )
	{
		setGalleryButtons();
		
		// set array of possible galleries "foldername|number of images|alt attribute info"
		galleries = [ "esd2008|92|Endangered Species Day 2008", "staff|12|RCHCA Staff" ]; 
		 		
	}
}

function setGalleryButtons()
{
	(isIE) ? $("nextButton").attachEvent("onmousedown", function(){ eventType = "nextImage"; photoGallery(); }) : $("nextButton").addEventListener("mousedown", function(){ eventType = "nextImage"; photoGallery(); }, false); 
	(isIE) ? $("lastButton").attachEvent("onmousedown", function(){ eventType = "lastImage"; photoGallery(); }) : $("lastButton").addEventListener("mousedown", function(){ eventType = "lastImage"; photoGallery(); }, false);
	
	// Note: using a nested function as an event handler can cause memory leaks in IE through v6 per OReily's Javascript Definitive Guide p. 414, not sure about IE7

	var choiceButtons = $("photoGalleryButtons").getElementsByTagName("li");
	
	for(a = 0; a < choiceButtons.length; a ++)
	{	
		var buttonID = choiceButtons[a].getAttribute("id");
		choiceButtons[a].style.cursor = "pointer";		
		(isIE) ? choiceButtons[a].attachEvent( "onmousedown", setGallery ) : choiceButtons[a].addEventListener( "mousedown", setGallery, false );
	}	
}

function setGallery(e)
{
	(isIE) ? galleryChoice = e.srcElement.getAttribute("id") : galleryChoice = this.getAttribute("id");
	
	var counter = 0;
	var found = false;

	while(found != true)
	{
		var galleryData = galleries[counter].split("|");
		
		if(galleryData[0].indexOf(galleryChoice) != -1)
		{
			found = true;			
		}
		else
		{
			counter++;	
		}		
	}	
	eventType = "newGallery";
	numberOfImages = parseInt( galleryData[1] );
	imageAltAtt = galleryData[2];
	$("totalPhotos").innerHTML = numberOfImages;
	$("photoCounter").style.display = "block";
	$("photoNavigation").style.display = "block";
	photoGallery();
}

function photoGallery()
{
	if( eventType == "nextImage" || eventType == "newGallery" )
	{	
		(eventType == "newGallery") ? imageCounter = 0 : imageCounter = imageCounter;
	
		if(imageCounter < numberOfImages )
		{
			getNextPhoto();
		}
		else
		{
			imageCounter = 0;
			getNextPhoto();
		}
	}
	else
	{
		if(imageCounter == 1 )
		{
			imageCounter = numberOfImages+1;
			getLastPhoto();
		}
		else
		{
			getLastPhoto();	
		}
	}
}

function getLastPhoto()
{
	imageCounter--;
	$("photoContainer").innerHTML = "<img src=\"images/photo_gallery/" + galleryChoice + "/image" +  imageCounter + ".jpg\" alt=\"" + imageAltAtt + "\" width=\"542\" height=\"407\" />";
	$("photoNumber").innerHTML = imageCounter;
}

function getNextPhoto()
{
	imageCounter++;
	$("photoContainer").innerHTML = "<img src=\"images/photo_gallery/" + galleryChoice + "/image" +  imageCounter + ".jpg\" alt=\"" + imageAltAtt + "\" width=\"542\" height=\"407\" />";
	$("photoNumber").innerHTML = imageCounter;
}

function setColumnSizes()
{
	if( getLeftColumnHeight() > getRightColumnHeight() )
	{
		$("rightColumn").style.height = getLeftColumnHeight() + "px";
	}
}

function getLeftColumnHeight()
{	
	return $("leftColumn").offsetHeight;	
}

function getRightColumnHeight()
{
	return $("rightColumn").offsetHeight;	
}

function setLogoBackground()
{
	var thisDate = new Date();

	var thisSecond = thisDate.getSeconds();
	
	if (thisSecond >= 0 && thisSecond <= 10)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner1.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
	else if (thisSecond >= 11 && thisSecond <= 20)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner2.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
	else if (thisSecond >= 21 && thisSecond <= 30)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner3.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
	else if (thisSecond >= 31 && thisSecond <= 40)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner4.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
	else if (thisSecond >= 41 && thisSecond <= 50)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner5.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
	else if (thisSecond >= 51 && thisSecond <= 60)
	{
		$('logoDiv').innerHTML = '<img src="' + pathDepth + 'images/rchca_logo_banner6.jpg" width="767" height="100" alt="RCHCA - Riverside County Habitat Conservation Agency" />'
	}
}

function setupAgendaLinks()
{
	var numberOfLinks = document.getElementsByTagName('a');
	
	for(i=0; i<numberOfLinks.length; i++)
	{
		if( numberOfLinks[i].getAttribute('id') == "agendaLink" )
		{			
			(isIE) ? numberOfLinks[i].attachEvent('onmousedown', showAgenda) : numberOfLinks[i].addEventListener('mousedown', showAgenda, false);			
		}
	}		
}

function showAgenda(e)
{
	var e = e || mouse.event;
	
	var eventTop = "";//e.clientY;
	var eventLeft = "";//e.clientX;
	
	(isIE) ? eventTop = e.clientY : eventTop = e.clientY;
	(isIE) ? eventLeft = e.clientX : eventLeft = e.clientX;
	
	var agendaToLoad = "";
	
	(isIE) ? agendaToLoad = e.srcElement.getAttribute('name') : agendaToLoad = e.target.getAttribute('name');

	var popupWindow = document.createElement("div");
	popupWindow.setAttribute("id", "popupWindow");
	popupWindow.style.left = ( eventLeft - 250 ) + "px";
	popupWindow.style.top = ( eventTop - 50 ) + "px";
	(isIE) ? popupWindow.className = "popupWindow" : popupWindow.setAttribute("class", "popupWindow");
	
	var popupWindowShadow = document.createElement("div");
	popupWindowShadow.setAttribute("id", "popupWindowShadow");
	popupWindowShadow.style.left = ( eventLeft - 155 ) + "px";
	popupWindowShadow.style.top = ( eventTop - 43 ) + "px";
	(isIE) ? popupWindowShadow.style.filter = 'alpha(opacity=50)' : popupWindowShadow.style.opacity = '.5';
	(isIE) ? popupWindowShadow.className = "popupWindowShadow" : popupWindowShadow.setAttribute("class", "popupWindowShadow");
	
	var windowBlocker = document.createElement("div");
	windowBlocker.setAttribute("id", "windowBlocker");
	windowBlocker.style.width = $("rightColumn").offsetWidth + "px";
	windowBlocker.style.height = $("rightColumn").offsetHeight + "px";
	(isIE) ? windowBlocker.style.top = ( $("rightColumn").offsetTop + 15 ) + "px" : windowBlocker.style.top = $("rightColumn").offsetTop + "px";
	(isIE) ? windowBlocker.style.left = ( $("mainDiv").offsetLeft + 195 ) + "px" : windowBlocker.style.left = $("rightColumn").offsetLeft + "px";
	(isIE) ? windowBlocker.style.filter = 'alpha(opacity=30)' : windowBlocker.style.opacity = '.3';
	(isIE) ? windowBlocker.className = "windowBlocker" : windowBlocker.setAttribute("class", "windowBlocker");

	var xhr = createXMLHttpRequest();
	
		xhr.onreadystatechange = function()	
		{		
			if(xhr.readyState==4)	
			{	
				if(xhr.status==200)	
				{	
					popupWindow.innerHTML = xhr.responseText;	
				}	
				else	
				{	
					popupWindow.innerHTML = "Error Loading. We apologize for any inconvenience.<br />" +
					"<p style=\"clear:both;margin-right:11px;\"><a href=\"#\" onmousedown=\"javascript: removeAllPopups\">Close Window</a></p>";	
				}			
			}		
			else //While the external text is loading display a loading image	
			{	
				popupWindow.innerHTML = 'Loading';	
			}	
		}	
		var fileToGet = 'docs/agendas/' + agendaToLoad + '/' + agendaToLoad + '.html';	
		xhr.open("GET", fileToGet, true);	
		xhr.send(null);	
	
	document.body.appendChild(windowBlocker);
	document.body.appendChild(popupWindow);
}

function removeAllPopups()
{
	document.body.removeChild( $("popupWindow") );
	document.body.removeChild( $("windowBlocker") );
}

function createXMLHttpRequest()
{

	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}

	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}

	try { return new XMLHttpRequest(); } catch(e) {}

	alert("XMLHttpRequest not supported");

	return null;
}

function getPageLocation()
{
	var windowLocation = window.location.toString();
	var pageLocation = window.location.pathname.toString();
	var pathName = pageLocation.split("/");
	
	if(windowLocation.indexOf("netdev") != -1)
	{
	 	var fileDepth = pathName.length - 3; // FOR DEVELOPMENT VERSION
	}
	else
	{
		var fileDepth = pathName.length - 2; // FOR LIVE VERSION
	}

	var pathDepthToAdd = '';
	
	for(i = 0; i<fileDepth; i++)
	{
		pathDepthToAdd += "../";
	}
	
	return pathDepthToAdd;
}

function getBrowser()
{
	if(window.event)
	{
		if(navigator.userAgent.lastIndexOf("Apple") != -1 )
		{
			isSafari = true;
		}
		else
		{
			isIE = true;
		}	
	}
	else
	{
		isFox = true;	
	}
}
