// JavaScript Document
//<!--

//vars

initLoadTimeout = 8000;//time in milliseconds to wait for a movie to start loading before timing out
loadProgressPauseTimeout = 4000;//time in milliseconds to gibe movie to progress in it's loading before timout-ing
beginLoadTime = new Date().getTime();

//what level of message to actually output
debug = 4;//0 = description, 1 = notification, 2 = warning, 3 = error, >3 = none!

function init()
{
	//needs to deal with back button being pressed - loadBegun event not fired, 
	//as movies inserted and started before js running..?
	curMovie = -1;
	curAnim = 0;
	
	for( var i = 0; i < allFlashElements.length; ++i )
	{
		allFlashElements[i].loadBegun = false;
	}
	
	loadNextMovie();
}

function loadNextMovie()
{
	//now, the fun begins..
	//allFlashElements <- array, each element is an object, with two properties, name, and vars. vars is an array, each element is an object, for a different flash version
	if( curMovie+1 < allFlashElements.length )
	{
		curMovie++;
		
		insertFlashMovie();
	}
}

function insertFlashMovie()
{
	var id = allFlashElements[curMovie].name;
	var vars = allFlashElements[curMovie].vars;
	
	trace( 0, 'insert movie: '+id );
	
	if( getSwf( id ) )//check if this has already been done (firefox back button issue!)
	{
		try
		{
			var prog = getSwf( id ).getProgress();
			
			if( prog.bytesLoaded == prog.bytesTotal )
			{
				loadBegun( id );
			}
		}
		
		catch( e )
		{
			trace( 1, 'cannot get progress' );//doesn't seem to be an issue when this happens..
		}
	}
	else
	{
		trace( 0, 'detected flash version: '+GetSwfVer() );
		//first, find which flash movie to use (if any available)
		for( var i = 0; i < vars.length; ++i )
		{
			if( DetectFlashVer( vars[i].majorVersion, ( vars[i].minorVersion ? vars[i].minorVersion : 0 ), ( vars[i].revision ? vars[i].revision : 0 ) ) )
			{
				var f = vars[i];
				
				break;
			}
		}
		
		if( f )
		{
			//
			f.id = id;
			f.name = id;
			f.quality = f.quality ? f.quality : 'high' ;
			f.align = f.align ? f.align : 'middle' ;
			f.play = f.play ? f.play : 'true' ;
			f.loop = f.loop ? f.loop : 'false' ;
			f.allowScriptAccess = f.allowScriptAccess ? f.allowScriptAccess : 'always' ;
			f.type = f.type ? f.type : 'application/x-shockwave-flash' ;
			f.pluginspage = f.pluginspage ? f.pluginspage : 'http://www.macromedia.com/go/getflashplayer' ;
			
			var fv = f.flashVars;
			f.flashVars = 'name='+id;
			
			if( navigator.userAgent.indexOf( 'Opera' ) != -1 )
			{
				f.flashVars += '&autostart=true';
			}
			
			if( fv )
			{
				for( var s in fv )
				{
					f.flashVars += '&'+s+'='+fv[s];
				}
			}
			
			//build string to execute - clumsy, but the only reliable cross browser solution
			var code = 'AC_FL_RunContent( "menu", "false" ';
			
			for( var s in f )
			{
				code+= ', "'+s+'", "'+f[s]+'"';
			}
			
			code += ');';
			
			if( ( !( navigator.userAgent.indexOf( 'Opera' ) != -1 ) ) && allFlashElements[curMovie].delay >= 0 )
			{
				loadId = setTimeout( abortLoad, initLoadTimeout );
			}
			else
			{
				allFlashElements[curMovie].loadBegun = true;
				loadId = monitorId = 1;
				lastProgUpdate = new Date().getTime();
				loadComplete( id );
				curAnim++;
			}
			
			var container = getElement( 'fc_'+id );
			container.innerHTML = eval( code );
		}
		else
		{
			//make alternate content visible
			switchToAlternate( id );
			
			//curMovie++;
			curAnim++;
			
			loadNextMovie();
		}
	}
}

function loadBegun( id )
{
	if( id == allFlashElements[curMovie].name && !allFlashElements[curMovie].loadBegun )
	{
		trace( 0, 'loadBegun: '+id );
		clearTimeout( loadId );
		
		allFlashElements[curMovie].loadBegun = true;
		
		//now, start monitoring progress
		monitorId = setInterval( monitorLoading, 100 );
		
		prog = 0;
		lastProgUpdate = new Date().getTime();
	}
}

function loadComplete( id )
{
	if( allFlashElements[curMovie].delay >= 0 )
	{
		trace( 0, 'load complete: '+id );
		clearInterval( monitorId );//stop monitoring load
		
		var now = new Date().getTime();
		var startTime = beginLoadTime + allFlashElements[curMovie].delay;
		
		if( startTime <= now )
		{
			trace( 0, 'play movie now' );
			beginMovie();
		}
		else if( curAnim < allFlashElements.length )
		{
			trace( 0, 'set timeout' );
			setTimeout( beginMovie, startTime - now );
		}
	}
	
	loadNextMovie();
}

function beginMovie()
{
	trace( 0, 'beginMovie?: '+curAnim );
	
	if( !( navigator.userAgent.indexOf( 'Opera' ) != -1 ) && allFlashElements[curAnim].delay >= 0 )
	{
		if( curAnim < allFlashElements.length )
		{
			trace( 0, 'beginMovie: '+allFlashElements[curAnim].name );
			
			var now = new Date().getTime();
			
			try
			{
				var swf = getSwf( allFlashElements[curAnim].name );
				
				swf.begin();
			}
			
			catch( error )
			{
				trace( 3, 'begin error: '+error.message+', '+allFlashElements[curAnim].name+', '+swf.name );
				
				//something swrong - switch to alternate content
				switchToAlternate( allFlashElements[curAnim].name );
			}
			
			curAnim++;
			
			beginLoadTime = now;
		}
	}
	else
	{
		curAnim++;
		
		beginLoadTime = new Date().getTime();
	}
	
}

function monitorLoading()
{
	var id = allFlashElements[curMovie].name;
	
	var now = new Date().getTime();
	
	try
	{
		var swf = getSwf( id );
		
		var curProg = swf.getProgress();
		
		if( curProg > prog )
		{
			prog = curProg;
			lastProgUpdate = now;
		}
		else if( lastProgUpdate + loadProgressPauseTimeout < now )
		{
			//give up!
			clearInterval( monitorId );
			
			abortLoad( 'stalled' );
		}
	}
	
	catch( error )
	{
		//um..?
		trace( 3, 'monitor error: '+error.message+', '+curMovie );
		
		//unable to monitor loading..
		
	}
}

function abortLoad( reason )
{
	reason = reason ? reason : 'timeout' ;
	
	trace( 2, 'load aborted: '+curMovie+', '+reason );
	
	//movie has not begun to load - abort!
	var id = allFlashElements[curMovie].name;
	
	switchToAlternate( id );//hide the flash movie, show the alternate content
	
	//clear interval
	clearInterval( monitorId );
	
	//start the next movie!
	loadNextMovie();
}

function switchToAlternate( id )
{
	try
	{
		trace( 0, 'switched to alternate: '+id );
		
		var flash = getElement( 'fc_'+id );
				
		flash.style.display = 'none';
		//flash.style.visibility = 'hidden';
		
		
		var alternate = getElement( 'afc_'+id );
				
		alternate.style.display = 'block';
	}
	
	catch( e )
	{
		trace( 3, 'unable to switch: '+e.message );
	}
}

function showHide( t, d )
{
	if( !d )
	{
		d = 'hide';
	}
	
	//var element = 
	if( document.getElementById )
	{
		var element = document.getElementById( t );
	}
	else if( document.all )
	{
		element = document.all[t];
	}
	else
	{
		return;
	}
	
	element.style.display = element.style.display == 'none' ? '' : 'none' ;
	
}

function getVisible( t )
{
	if( document.getElementById )
	{ // DOM3 = IE5, NS6
		if( document.getElementById( t ).style.display == 'none' )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		if (document.layers)
		{ // Netscape 4
			if( document[t].display == 'none' )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{ // IE 4
			if( document.all[t].style.display == 'none' )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function querystring()
{
	//get current querystring into array
	var qs = window.location.search.substr( 1 );//remove '?'
	
	var qsa = qs.split( '&' );
	
	var get = [];
	
	for( i = 0; i < qsa.length; i++ )
	{
		
		var val = qsa[i].split( '=' );
		get[(val[0])] = val[1];
	}
	
	//alter/add/remove values
	
	//alter/remove
	for( i = 0; i < arguments.length; i+=2 )
	{
		for( j in get )
		{
			if( arguments[i] == j )
			{
				if( arguments[i+1] == null )//if user hasn't supplied a new value for this, then delete it
				{
					delete get[j];
				}
				else
				{
					get[j] = arguments[i+1];
				}
				
				arguments[i] = null;//unset this from teh arguments object, for the next step
			}
		}
	}
	
	for( i = 0; i < arguments.length; i+=2 )
	{
		if( arguments[i] )
		{
			if( typeof arguments[i+1] == 'object' )
			{
				get[(arguments[i]+'[]')] = arguments[i+1].join( '&'+arguments[i]+'[]=' );
			}
			else
			{
				get[(arguments[i])] = escape( arguments[i+1] );
			}
		}
	}
	
	//make new querystring
	var qsa = [];
	
	for( i in get )
	{
		if( get[i] != null )
		{
			qsa.push( i+'='+get[i] );
		}
	}
	
	return '?'+qsa.join( '&' );
}

function externalWrite( str )
{
	document.write( str );
}

//dropdown menu stuff
function getElement( id, doc )
{
	doc = doc ? doc : document ;
	var x = null;
	
	if( doc.getElementById )
	{
		x = doc.getElementById( id );
	}
	else if( !( x = doc[id] ) && doc.all )
	{
		x = doc.all[id];
	}
	
	return x;
}

//debugging stuff
function list( obj )
{
	var str = '';
	
	for( var i in obj )
	{
		str += i+': '+obj[i]+"\n";
	}
	
	return str;
}

//takes an array of vals as the first argument, and a reg-exp pattern string as the second
//and returns an array of vals in the first argument that match the search string
function filter( vals, str )
{
	var regex = new RegExp( str, 'i' );
	
	var res = [];
	
	for( var i = 0; i < vals.length; ++i )
	{
		if( regex.test( vals[i] ) )
		{
			res.push( vals[i] );
		}
	}
	
	return res;
}

function getSwf( movieName )//seems to have issues with dynamically attached swf's in ie
{
	try
	{
		if( navigator.appName.indexOf( "Microsoft" ) != -1 )
		{
			return window[movieName]
		}
		else
		{
			return document[movieName]
		}
	}
	
	catch( error )
	{
		trace( 3, 'get swf: '+error.message, 'red' );
		
		return null;
	}
}

function trace( debugLevel, msg, col )
{
	if( debugLevel >= debug )
	{
		if( !window.outputWindow )
		{
			outputWindow = window.open( '', 'outputWindow', 'width=450,height=500,scrollbars=yes' );
			outputWindow.document.write( '<html><head><title>Output console</title><script language="javascript" type="text/javascript">function output( str ){ document.getElementById( \'output\' ).innerHTML += str; window.scrollTo( 0, window.innerHeight ); }</script></head><body onload="this.focus();" onunload="delete window.opener.outputWindow;"><h3>Output Console</h3><hr /><div id="output"></div></body></html>' );
			outputWindow.document.close()
			outputFunc = outputWindow.output;
		}
		
		if( window.focus )
		{
			outputWindow.focus();
		}
		
		try
		{
			outputFunc( '<div style="color:'+( col ? col : '#000000' )+'">'+msg+'</div>'+"\r\n" );
		}
		
		catch( error )
		{
			alert( msg );
		}
	}
}

function setOnload( func )
{
	var init = function ( e )
	{
		// quit if this function has already been called
		if (arguments.callee.done)
		{
			return;
		}
		// flag this function so we don't do the same thing twice
		arguments.callee.done = true;
		
		// kill the timer
		if (_timer) clearInterval(_timer);
		
		// call your onload function
		arguments.callee.func.apply( window, arguments.callee.args );
	};
	
	var addArgs = [];
	
	for( var i = 1; i < arguments.length; ++i )
	{
		addArgs.push( arguments[i] );
	}
	
	init.func = func;
	init.args = addArgs;
	
	/* for Mozilla/Opera9 */
	if( window.addEventListener )
	{
		window.addEventListener( "DOMContentLoaded", init, false );
		window.addEventListener( 'pageshow', init, false );
		window.addEventListener( 'pagehide', function(){ init.done = false; }, false );
	}
	else if(  document.addEventListener )
	{
		document.addEventListener( "DOMContentLoaded", init, false );
	}
	
	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
	  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	  var script = document.getElementById("__ie_onload");
	  script.onreadystatechange = function() {
		if (this.readyState == "complete") {
		  init(); // call the onload handler
		}
	  };
	/*@end @*/
	
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent))// sniff
	{ 
		var timerFunc = function()
		{
			if (/loaded|complete/.test(document.readyState))
			{
				argument.callee.init(); // call the onload handler
			}
		}
		
		timerFunc.init = init;
		
		var _timer = setInterval( timerFunc, 10 );
	}

	/* for other browsers */
	window.onload = init;
}

function externalWrite( str )
{
	document.write( str );
}


//-->
