//-----From http://brainerror.net/scripts/javascript/blendtrans/

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

//------end from brainerror.com-----------

var scrollBarHeight = 338;
var contentPaneHeight = 272;
var diffX, diffY, theElement, eventVar;
var contentHeight;
var contentMargin = 20;
var scrollFaceHeight = 58;

contentMargin = contentMargin/2;
var current = "";
var t;


subNav00 = new subNav('subNav00');
subNav01 = new subNav('subNav01');
subNav02 = new subNav('subNav02');
subNav03 = new subNav('subNav03');
subNav04 = new subNav('subNav04');
subNav05 = new subNav('subNav05');

function scrollDrag(event)
{
	eventVar = event;
	theElement = document.getElementById('scrollFace');//event.currentTarget;
	var posX = parseInt(theElement.style.left);
	var posY = parseInt(theElement.style.top);
	
	diffX =eventVar.clientX - posX;
	diffY =eventVar.clientY - posY;
	
	document.onmousemove = mover;
	document.onmouseup = dropper;
	
	try{event.stopPropagation();}
	catch(e){}
	try {event.preventDefault();}
	catch(e){}
	
}

function mover(event)
{
	try{event.clientY}
	catch(e){event = eventVar}
	
	if (event.clientY-diffY <= 0)  //top of scroll
	{
		theElement.style.top = "0px";
		document.getElementById('content').style.top = "0px";
	}
	else if(event.clientY-diffY >= scrollBarHeight-58) //bottom of scroll
	{
		theElement.style.top = scrollBarHeight - 58 + "px";
		document.getElementById('content').style.top = -(contentHeight-contentPaneHeight)+"px";
	}
	else  //somewhere in mid of scroll
	{
		theElement.style.top = (event.clientY - diffY) + "px";
		document.getElementById('content').style.top = -(contentHeight-contentPaneHeight) * ((event.clientY - diffY) / (scrollBarHeight-scrollFaceHeight)) +"px";
	}
	//document.getElementById('debug').innerHTML = "TOP: "+document.getElementById('content').style.top+" --- diffX: " +diffX+ ", DiffY: "+diffY;
	try{eventVar.stopPropagation();}
	catch(e){}
}

function dropper(event) 
{	
	document.onmousemove = null;
	document.onmouseup = null;
	try{eventVar.stopPropagation();}
	catch(e){}
}

function getHeight()
{
	contentHeight = document.getElementById('content').offsetHeight;
	contentHeight += contentMargin*2;
	if (contentHeight > contentPaneHeight)
	{
		document.getElementById('scrollBar').style.visibility = "visible";
	}
	else
	{
		document.getElementById('scrollBar').style.visibility = "hidden";
	}
}

function subNav(id) 
{
	this.id = id;
	this.timer;
	this.open = false;
	
	this.animate = function() {
		if (!this.open)
		{	
			if (current != "") //if there is a previously opened tab, stops animation on that tab and makes itself the new 'current'
			{
				clearTimeout(current.timer);
				current.fadeOut();
			}
			document.getElementById(this.id).style.display = "inline";
			this.open = true;
			current = this;
			changeOpac(100, this.id);
			this.showArrow(0);
		}
	}
	this.reset = function() {
		changeOpac(0, this.id);
		document.getElementById(this.id).style.visibility = "visible";
		document.getElementById(this.id).style.display = "none";
		document.getElementById(this.id+"marker").style.width = "4px";
		document.getElementById(this.id+"marker").style.height = "4px";
		this.open = false;
		for (j = 1; j < document.getElementById(this.id).getElementsByTagName("div").length; j++) //  because first element is the marker
		{
			changeOpac(0, document.getElementById(this.id).getElementsByTagName("div")[j].id);
		}
	}
	this.fadeOut = function() {
		opacity(this.id, 100, 0, 400);
		setTimeout(this.id+".reset()", 410);
	}
	this.showLinks = function(x) { //must start with 1 as the first child of subNav is <div id=subNav##Marker>
		try{
			opacity(document.getElementById(this.id).getElementsByTagName("div")[x].id, 0, 100, 400);
			this.timer = setTimeout(this.id+".showLinks("+(x+1)+")", 100);
		}
		catch(e) {
			clearTimeout(this.timer);
		}
	}
	this.showArrow = function(i) {
		if (i < 23)
		{
			document.getElementById(this.id+"marker").style.height = i+"px";
			this.timer = setTimeout(this.id+".showArrow("+(i+1)+")", 10);
		}
		else if (i >= 23 && i < 43)
		{
			document.getElementById(this.id+"marker").style.width = i-23+4+"px";
			this.timer = setTimeout(this.id+".showArrow("+(i+1)+")", 10);
		}
		else
		{
			document.getElementById(this.id+"marker").style.height = "30px";
			clearTimeout(this.timer);
			this.showLinks(1);
		}
	}
}

function loadPage(section)
{	
	for (i=0; i<6; i++)
		eval("subNav0"+i+".reset()");
	
	getHeight();
	
	current = section;
	changeOpac(100, section.id);
	document.getElementById(section.id).style.display = "inline";
	document.getElementById(section.id+"marker").style.width = "23px";
	document.getElementById(section.id+"marker").style.height = "30px";
	for (j = 1; j < document.getElementById(section.id).getElementsByTagName("div").length; j++) //  because first element is the marker
	{
		changeOpac(100, document.getElementById(section.id).getElementsByTagName("div")[j].id);
	}
	section.open = true;
}

function showVid(id)
{
	document.getElementById('videoFrame').innerHTML = document.getElementById(id).innerHTML;
}