

//  Infinite C

function Hello() {}
Number.prototype.pad = function( length )
{
	var str = '' + this;
	while( str.length < length )
	{
		str = '0' + str;
	};
	return str;
};




//  Audio Controls

function aPlay( id )
{
	id = "aFile" + id;
	if( $( id ) ) document[ id ].Play();
};
function aStop( id )
{
	id = "aFile" + id;
	if( $( id ) ) document[ id ].Stop();
};
function aRewind( id )
{
	id = "aFile" + id;
	if( $( id ) ) document[ id ].Rewind();
};




//  Audio Loader

function aLoad( file )
{
	var alib = $( "audioLibrary" );

	var obj = document.createElement( "object" );
	obj.setAttribute( "class", "alib" );
	obj.setAttribute( "id", "aFile" + file );
	obj.setAttribute( "classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" );
	obj.setAttribute( "codebase", "http://www.apple.com/qtactivex/qtplugin.cab" );
	alib.appendChild( obj );

	var par = document.createElement( "param" );
	par.setAttribute( "name", "src" );
	par.setAttribute( "value", "med/" + file + ".mid" );
	obj.appendChild ( par );

	var emb = document.createElement( "embed" );
	emb.setAttribute( "class", "alib" );
	emb.setAttribute( "name", "aFile" + file );
	emb.setAttribute( "type", "audio/midi" );
	emb.setAttribute( "pluginspage", "www.apple.com/quicktime/download" );
	emb.setAttribute( "src", "med/" + file + ".mid");
	emb.setAttribute( "loop", "false" );
	emb.setAttribute( "autoplay", "false" );
	emb.setAttribute( "enablejavascript", "true" );
	emb.setAttribute( "cache", "true" );
	obj.appendChild ( emb );
};




//  Audio Loader Holding Tank

function aHold( i )
{
	document.title = "Loading "+ i +" of " + aList.length;
	aLoad( aList[ i ] );
};




//  Toggle Play and Pause

function buttonToggle()
{
	if( aFlag )
	{
		aFlag = false;
		$( "toggle" ).className = "offHold";
		NTP.sync();
	}
	else
	{
		aFlag = true;
		$( "toggle" ).className = "onHold";
		NTP.sync();
	};
};




//  Audio Clock

function aClock()
{
	var time  = new Date();
	var gmtMS = time.getTime() + ( time.getTimezoneOffset() * 60000 );
	if( NTP ) gmtMS = NTP.fixTime( gmtMS );
	var gmtTime = new Date( gmtMS );
	var aTime = gmtTime.getSeconds();

	if( aTime != aTimeOld )
	{
		if( !(aTime % 4) )
		{
			if( aThis )
			{
				aStop(   aList[ aThis ] );
				aRewind( aList[ aThis ] );
			};
			if( aFlag )
			{
				aThis = Math.floor( Math.random() * aList.length );
				aRewind( aList[ aThis ] );
				aPlay(   aList[ aThis ] );
				document.title = "Infinite C : "+ aList[ aThis ];
			}
			else
			{
				document.title = "Infinite C";
			};
			$( "clockC" ).src = "med/char.note.gif";
			if( $( "toggle" ).className == "onHold" )
			{
				$( "buttonImage" ).src = "med/char.pause.gif";
				$( "toggle" ).className =  "onNow" ;
			}
			else if( $( "toggle" ).className == "offHold" )
			{
				$( "buttonImage" ).src = "med/char.play.gif";
				$( "toggle" ).className =  "offNow";
			};
		}
		else
		{
			$( "clockC" ).src = "med/char.null.gif";
		};
		$( "clockA" ).src = "med/char." + parseInt( aTime / 10, 10 ) + ".gif";
		$( "clockB" ).src = "med/char." + (aTime % 10) + ".gif";
		aTimeOld = aTime;
	};
};




//  Global Variables

var aList = new Array();
var aThis = 0;
var aFlag = true;
var aTimeOld = 0;

