var state;

var moveBGTimer    = null;
var moveDivTimer   = null;
var updateColTimer = null;
var unhideCtl      = true;
// flicker prevention must be in sync with this list
var colours        = [
						[0,0x36,0x63],
						[0,0,0],
						[48,0,0],
						[0,52,0],
						[128,64,0],
						[0,0x36,0x63]
					];



function saveState()
{
	document.cookie = 'toxABGFXstate=' + state.join( '|' );
}

function getState()
{

	var cookies = document.cookie.split( ';' );
	var pattern = /^\s*([^\s=]+)=(.+)$/;

	for ( var i = 0; i < cookies.length; i++ )
		if ( pattern.exec( cookies[i] ) )
			if ( RegExp.$1 == 'toxABGFXstate' )
			{

				state = RegExp.$2.split( '|' );

				state[0] = parseFloat( state[0] );
				state[1] = parseFloat( state[1] );
				state[2] = parseFloat( state[2] );

				if ( !isNaN( state[0] ) && !isNaN( state[1] ) && !isNaN( state[2] ) )
					return;

			}


	state = [
				Math.random() * 2 * Math.PI,
				Math.random() * 2 * Math.PI,
				0
			];

}

getState();



function moveBG( noMove )
{

	var bg = document.getElementsByTagName('BODY')[0];
	if ( !bg )
		return;


	var x = Math.round( 60 * Math.sin( state[0] ) );
	var y = Math.round( 120 * Math.cos( state[0] ) );

	if ( !noMove )
		state[0] = ( state[0] + 0.004 ) % ( 2 * Math.PI );

	bg.style.backgroundPosition = x + 'px ' + y + 'px';

}

function moveDiv( noMove )
{

	var div = document.getElementById( 'secwm');
	if ( !div )
		return;

	var x = Math.round( 140 * Math.sin( state[1] ) );
	var y = Math.round( 70 * Math.cos( state[1] ) );

	if ( !noMove )
		state[1] = ( state[1] - 0.004 ) % ( 2 * Math.PI );

	div.style.backgroundPosition = x + 'px ' + y + 'px';

}

function updateColour( step )
{

	var bg = document.getElementsByTagName('BODY')[0];
	if ( !bg )
		return;


	var index = step ? step : state[2];

	var cl = ( index % ( Math.PI / 2 ) );
	var cs = Math.floor( index / ( Math.PI / 2 ) ) % ( colours.length - 1 );
	var cf = 1 - ( Math.cos( cl ) * Math.cos( cl ) );

	if ( !step )
		state[2] += 0.012;

	var r = Math.floor( colours[cs][0] + cf * ( colours[cs+1][0] - colours[cs][0] ) );
	var g = Math.floor( colours[cs][1] + cf * ( colours[cs+1][1] - colours[cs][1] ) );
	var b = Math.floor( colours[cs][2] + cf * ( colours[cs+1][2] - colours[cs][2] ) );

	bg.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';

}

function updateHeights()
{

	unhideController();


	// get current height of document
	var h = window.innerHeight + window.pageYOffset;
	if ( !h && document.documentElement )
		h = document.documentElement.clientHeight + document.documentElement.scrollTop;
	if ( !h && document.body )
		h = document.body.clientHeight + document.body.scrollTop;

	if ( !h ) return;



	// calculate height of banner, header and footer
	var hbf_height;

	div = document.getElementById( 'header' );
	if ( !div ) return;

	hbf_height = div.offsetHeight;

	div = document.getElementById( 'banner' );
	if ( !div ) return;

	hbf_height += div.offsetHeight;

	div = document.getElementById( 'footer' );
	if ( !div ) return;

	hbf_height += div.offsetHeight;


	// calculate height of page
	div = document.getElementById( 'content' );
	if ( !div ) return;

	var minHeight, tallest = findTallestPanel();
	if ( tallest && tallest.offsetHeight > div.__initialHeight )
		minHeight = tallest.offsetHeight;
	else
		minHeight = div.__initialHeight;

	var th = hbf_height + minHeight;

	// special case: window is taller than content -> stretch content to fit
	if ( h > th )
		th = h;



	var div = document.getElementById( 'secwm');
	if ( !div ) return;

	div.style.height = th + 'px';

	div = document.getElementById( 'page' );
	if ( !div ) return

	div.style.height = th + 'px';



	div = document.getElementById( 'content' );
	if ( !div ) return;

	// set height of content
	h = th - hbf_height;

	if ( h < minHeight )
		h = minHeight;

//	div.style.height = h + 'px';

	div = document.getElementById( 'body' );
	if ( !div ) return;

	div.style.height = h + 'px';

}

function unhideController()
{

	if ( unhideCtl )
	{

		var c = document.getElementById( 'fx-control' );
		if ( c )
			c.style.display = 'block';

		unhideCtl = false;

		// have some more initialization for updating height here
		var div = document.getElementById( 'content' );
		if ( !div ) return;

		div.__initialHeight = div.offsetHeight;

	}
}

function findTallestPanel()
{

	var maxNode   = null;
	var maxHeight = 0;
	var i, divs   = [
					'left-panel', 'right-panel'
					];

	for ( i = 0; i < divs.length; i++ )
	{

		node = document.getElementById( divs[i] );
		if ( node )
		{
			if ( node.offsetHeight > maxHeight )
			{
				maxHeight = node.offsetHeight;
				maxNode   = node;
			}
		}
	}


	return maxNode;

}

function updateBGFX( controller )
{

	if ( !controller )
		return;

	controller = controller.parentNode;
	if ( !controller )
		return;

	var buttons = controller.getElementsByTagName( 'A' );
	if ( !buttons )
		return;


	if ( moveBGTimer )
	{
		buttons[0].className = 'disabled';
		buttons[1].className = '';
	}
	else
	{
		buttons[0].className = '';
		buttons[1].className = 'disabled';
	}
}

function startBGFX( controller, single, start, disableFX )
{

	if ( start )
		window.setTimeout( 'updateColour(); moveBG( true ); moveDiv( true );', 40 );
	else
		state[3] = 1;


	if ( !moveBGTimer && false )
	{

		if ( single )
			window.setTimeout( 'updateBGFX( document.getElementById( "fx-control" ).lastChild );', 60 );
		else
			moveBGTimer = window.setInterval( 'moveBG();', 120 );

	}

	if ( !moveDivTimer && false )
	{

		if ( !single )
			moveDivTimer = window.setInterval( 'moveDiv();', 120 );

	}


	if ( disableFX )
	{
		unhideCtl = false;
		return false;
	}



	if ( !updateColTimer )
		updateColTimer = window.setInterval( 'updateColour();', 1000 );


	updateBGFX( controller );


	return false;

}

function stopBGFX( controller )
{

	if ( moveBGTimer )
	{
		window.clearInterval( moveBGTimer );
		moveBGTimer = null;
	}

	if ( moveDivTimer )
	{
		window.clearInterval( moveDivTimer );
		moveDivTimer = null;
	}

	// colour fade is unstoppable

	state[3] = 2;

	updateBGFX( controller );

	return false;

}

function initFX()
{

	var tag      = navigator.userAgent;

	var isSafari = ( tag.indexOf( 'Safari' ) >= 0 );
	var isOpera  = ( tag.indexOf( 'Opera' ) >= 0 );
	var isMSIE   = !isSafari && !isOpera && ( tag.indexOf( 'MSIE' ) >= 0 );
	var isFF     = !isMSIE && !isSafari && !isOpera;

	var version;

	if ( isSafari )
		version = parseInt( tag.substr( tag.indexOf( 'Version/' ) + 8, 1 ) );
	else if ( isMSIE )
		version = parseInt( tag.substr( tag.indexOf( 'MSIE ' ) + 5, 1 ) );
	else if ( isOpera )
		version = parseInt( tag.substr( tag.indexOf( 'Opera/' ) + 6, 1 ) );
	else if ( isFF )
		version = parseInt( tag.substr( tag.indexOf( 'Firefox/' ) + 8, 1 ) );
	else
		version = 1;



	if ( false && ( isOpera || isSafari ) )
		// disable all FX
		startBGFX( null, true, true, true );
	else
	{

		var single;

		if ( state[3] )
			single = ( state[3] == 2 );
		else
//			single = ( isFF && ( version <= 3 ) ) || ( isMSIE && ( version < 8 ) );
			single = ( isMSIE && ( version < 8 ) );


		startBGFX( null, single, true, false );

		window.setInterval( 'saveState()', 1000 );

	}

	window.setInterval( 'updateHeights()', 20 );

}


initFX();
