// Project: EAC web-site v2
// File: general.js
//
// Created: 2008-01-26
// Copyright: Bob Barnes
// Author: Bob Barnes
//
// Description: general javascript for layout and effects





// Having loaded the page, if Javascript is available this is called
//		to remove the stuff which is there only for non-javascript browsers
//		and which is to the detriment of the experience on other browsers

function MenuSetup()
{
	var menu = document.getElementById( "topmenu" );
	
	var arrA = menu.getElementsByTagName( "a" );
	
	for ( var i=arrA.length-1; i >= 0; i-- )
	{
		var A = arrA[i];
		var Container = A.parentNode;

		// Copy the href from the anchor to the TD
		Container.setAttribute( 'href', A.href );
		
		// Remove the anchor by replacing it with the text it contains
		Container.innerHTML = A.innerHTML;
	}
}



// An expander has been clicked

function expander_do(TheElement,BlockID)
{
	if ( TheElement.nodeName == "IMG" )
	{
		TheExpander = TheElement;
	}
	else
	{
		TheIMGs = TheElement.getElementsByTagName('img');
		TheExpander = TheIMGs[0];
	}

	var TheBlock = document.getElementById('expander_' + BlockID);
	if ( TheBlock.style.display == 'block' )
	{
		TheBlock.style.display = 'none';
		TheExpander.src = "/assets/expander_closed.png";
		TheExpander.alt = "click this to expand";
	}
	else
	{
		TheBlock.style.display = 'block';
		TheExpander.src = "/assets/expander_open.png";
		TheExpander.alt = "click this to collapse";
	}

}



function getElementsByClassName(oElm, strTagName, strClassName)
{
	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)
}


// Called when the page loads to close all the expanders, this means that if javascript is not
// available, they will be open by default

function expander_closeall()
{
	var arrElements = getElementsByClassName(document.body, "*", "expander_target");
	for( var i=0; i < arrElements.length; i++ )
	{
		var Element = arrElements[i];
		Element.style.display = 'none';
	}

	var arrElements = getElementsByClassName(document.body, "*", "expander");
	for( var i=0; i < arrElements.length; i++ )
	{
		var Element = arrElements[i];
		if ( Element.nodeName == "IMG" )
		{
			Element.src = '/assets/expander_closed.png';
		}
		Element.style.cursor = 'pointer';
	}
}



function menu_go(theTD)
{
//	var theAs = theTD.getElementsByTagName('a');
//	document.location = theAs[0].href;

	document.location = theTD.getAttribute( 'href' );
}


var Faders = new Array();

function FadeToTimeout( thisId, FromColour, ToColour, Step )
{

	FromR = parseInt( FromColour.slice( 1, 3 ), 16 );
	FromG = parseInt( FromColour.slice( 3, 5 ), 16 );
	FromB = parseInt( FromColour.slice( 5 ), 16 );

	ToR = parseInt( ToColour.slice( 1, 3 ), 16 );
	ToG = parseInt( ToColour.slice( 3, 5 ), 16 );
	ToB = parseInt( ToColour.slice( 5 ), 16 );
	
	MidR = Math.round( ( ToR - FromR ) * Step / 7 ) + FromR;
	MidG = Math.round( ( ToG - FromG ) * Step / 7 ) + FromG;
	MidB = Math.round( ( ToB - FromB ) * Step / 7 ) + FromB;
	
	MidRGB = "#" + MidR.toString(16) + MidG.toString(16) + MidB.toString(16);
	
	thisObject = document.getElementById( thisId );
	thisObject.style.backgroundColor = MidRGB;

	if ( Step < 7 )
	{
		Step = Step + 1;
		var FunCall = "FadeToTimeout( '" + thisId + "', '" + FromColour + "', '" + ToColour + "', " + Step + " )"
		var t = setTimeout( FunCall, 50 );
	}
	else
	{
		var FaderId = thisId.slice( thisId.lastIndexOf( "_" ) + 1 );
		Faders[FaderId] = "deleted";
	}
}


function FadeTo( thisId, FromColour, ToColour )
{
	var FaderId = thisId.slice( thisId.lastIndexOf( "_" ) + 1 );

	if ( Faders[FaderId] && ( Faders[FaderId] != "deleted" ) )
	{
//alert("Cleared " + Faders[thisId] );
		clearTimeout( parseInt(Faders[FaderId]) );
	}
	var FunCall = "FadeToTimeout( '" + thisId + "', '" + FromColour + "', '" + ToColour + "', 1 )"
	Faders[FaderId] = setTimeout( FunCall, 50 );
}



function menu_over(thisTD)
{
	//		thisTD.style.backgroundColor = '#E0E2E4';
	FadeTo( thisTD.id, '#FFFCFB', '#E0E2E4' );
	thisTD.style.cursor = 'pointer';

	window.status = thisTD.getAttribute( 'href' );
}


function menu_out(thisTD)
{
	//		thisTD.style.backgroundColor = '';
	FadeTo( thisTD.id, '#E0E2E4', '#FFFCFB' );
	thisTD.style.cursor = '';

	window.status = '';
}
