var elHeadingTitle;
var elSectionFirst;
var elSectionCurrent;
var elSectionSelected;
var urlAnchor;
var arHeadings;
var arThumbnails;
var elExpand;
var elSquash;
var blActive = false;
var HEIGHTDIV = 325;
var MILLISECONDS = 1;



//add interactive events to the page

fnAttachPopups();
fnThumbnailDuplication();

//remove the navigation strip
var navList = document.getElementById("nav");
navList.parentNode.removeChild(navList);

//check to see if there is an anchorname in the URL
if (location.href.split("#",2)[1]) {
	urlAnchor = location.href.split("#",2)[1];
}

//make title heading clickable
elHeadingTitle = document.getElementsByTagName("h1")[0]
elHeadingTitle.style.cursor="pointer";
elHeadingTitle.onclick = fnToggleContent;

//prepare top DIV with correct styling for animation
elSectionFirst = document.getElementsByTagName("div")[0];
elSectionCurrent = elSectionFirst;
elSectionCurrent.style.height = HEIGHTDIV+"px";
elSectionCurrent.style.padding = "0";

arHeadings = document.getElementsByTagName("h2");

//make sub-arHeadings clickable
for (i=0, j=arHeadings.length; i<j; i++) {
	arHeadings[i].onmouseover = fnHoverHeading;
	arHeadings[i].onmouseout = fnLeaveHeading;
	arHeadings[i].onclick = fnToggleContent;

	var elDiv = identifySection(arHeadings[i]);
	elDiv.style.height = HEIGHTDIV+"px";
	elDiv.style.padding = "0";

	//only turn off section if it is not an anchor name specified in the URL
	if (urlAnchor != arHeadings[i].getAttribute("id")) {
		identifySection(arHeadings[i]).style.display = "none";
	} else {
		elSectionFirst.style.display="none";
		elSectionCurrent = identifySection(document.getElementById(urlAnchor));
	}
}

//fix first and last heading borders to space them out from the header and footers
arHeadings[0].style.borderTop="solid 10px white";
arHeadings[j-1].style.borderBottomWidth="10px";

//stop the last section having too much spacing after the header
elTopParagraph = identifySection(arHeadings[j-1]).firstChild;
while(elTopParagraph.nodeType != 1) {elTopParagraph = elTopParagraph.nextSibling;}
elTopParagraph.style.paddingTop = "5px";





//if a new section is clicked, open it and close the old one
function fnToggleContent() {
	if (blActive == true) return false;

	elSectionSelected= identifySection(this);

	if (elSectionSelected != elSectionCurrent) {
		if (this != elHeadingTitle) {
			this.className = "headingSelected";
		}

		elExpand = elSectionSelected;
		elSquash = elSectionCurrent;

		elExpand.style.display = "";
		elExpand.style.height = "0px";
		expandDiv(0);
		squashDiv(0);

		identifySelectedHeading().className = "";
	}
	elSectionCurrent = elSectionSelected;
}





//takes the selected element and finds the next adjacent element and sends it back
function identifySection(selected) {
	while(selected.nextSibling.nodeType != 1) {selected = selected.nextSibling;}
	selected = selected.nextSibling;
	return selected;
}

//uses the currently selected section and finds the previous adjacent element and sends it back
function identifySelectedHeading() {
	var selected = elSectionCurrent.previousSibling;
	while(selected.nodeType != 1) {selected = selected.previousSibling;}
	return selected;
}





//takes a block and expands it
function expandDiv(intExpand) {
	//convert from radians to degrees and get sine
	var flExpand = Math.sin(intExpand*(Math.PI/180));
	elExpand.style.height = Math.floor(HEIGHTDIV*flExpand)+"px";
//	elExpand.style.opacity = flExpand;
	intExpand = intExpand + 1;

	if (intExpand <= 90) {
		t=setTimeout("expandDiv("+intExpand+")",MILLISECONDS);
	}
}





//takes a block and squashes it
function squashDiv(intSquash) {
	blActive = true;
	var flSquash = 1-(Math.sin(intSquash*(Math.PI/180)));
	elSquash.style.height = Math.floor(HEIGHTDIV*flSquash) + "px";
//	elSquash.style.opacity = flSquash;
	intSquash = intSquash + 1;

	if (intSquash <= 90) {
		t=setTimeout("squashDiv("+intSquash+")",MILLISECONDS);
	}
	else {
		elSquash.style.height = "0px";
		elSquash.style.display = "none";
		blActive = false;

	}
}




//swap styles on mouseovers, only if not currently animating and not the active section
function fnHoverHeading() {
	if (blActive == true) return false;
	if (this != identifySelectedHeading()) {this.className = "headingHover";}
}

function fnLeaveHeading() {
	if (this != identifySelectedHeading()) {this.className = "";}
}





//cycle through page links and check class
function fnAttachPopups() {
	if(!document.getElementsByTagName) return false;
	var arLinks = document.getElementsByTagName("a");
	for (i=0, j=arLinks.length; i<j; i++) {

		//if popup class then trigger a standard new window
		if ((arLinks[i].getAttribute("class") == "popup") || (arLinks[i].getAttribute("className") == "popup")) {
			arLinks[i].onclick = function() {
				fnPopup(this.getAttribute("href"),"toolbar=1,scrollbars=1,status=1,resizable=1,menubar=1,location=1");
				return false;
			}
		}
		//if popupFlash class then trigger a resized new window
		if ((arLinks[i].getAttribute("class") == "popupClean") || (arLinks[i].getAttribute("className") == "popupClean")) {
			arLinks[i].onclick = function() {
				fnPopup(this.getAttribute("href"),"width=670,height=510,toolbar=0,scrollbars=0,status=0,resizable=0,menubar=0,location=0");
				return false;
			}
		}
	}
}





//open a new popup window
function fnPopup(winURL,options) {
	window.open(winURL,"",options);
}





//grab the image URL from the LI IMGs and use it as a background for the LI
function fnThumbnailDuplication() {
	arThumbnails = document.getElementsByTagName("li");
	for (i=0, j=arThumbnails.length; i<j; i++) {
		var arImages = arThumbnails[i].getElementsByTagName("img");
		if(arImages.length >= 1) {
			arThumbnails[i].style.backgroundImage = "url("+arImages[0].getAttribute("src")+")";
		}
	}
}
