// preload images for mouseovers
if (document.images)
{
	footerOn2 = new Image();
	footerOn2.src = "images/footergrntop2_on.gif";
	footerOn4 = new Image();
	footerOn4.src = "images/footergrntop4_on.gif";
	footerOn6 = new Image();
	footerOn6.src = "images/footergrntop6_on.gif";
	footerOnBottom = new Image();
	footerOnBottom.src = "images/footergrnbottom2_on.gif";
}

function setUpSplat()
{
	initialAngle = Math.random() * Math.PI * 2; angle = initialAngle;

	leftMargin = 0;
	topMargin = 0;
	isMoving = false;
	isMouseOver = false;

	frameDelay = 100;
	minimumSpeed = 0;
	initialSpeed = 20;
	acceleration = -2;

	windowX = windowWidth();
	windowY = windowHeight();

	if (canDHTML)
	{
		window.onresize = checkWindowSize;
        if (isNetscape)
		{
                button = document.splatDiv;
                button.xpos = parseInt(button.left);
                button.ypos = parseInt(button.top);
        }
        if (isIE)
		{
                button = splatDiv.style;
                button.xpos = parseInt(button.posLeft);
                button.ypos = parseInt(button.posTop);
        }
	setMovementParameters();
	}
}

function checkWindowSize()
{
	setMovementParameters();

	// got this from www.terrarium.ml.html who credits www.webmonkey.ocm
	// I assume that NS 4 sometimes fails to update window.innerheight and .innerwidth on resize?
	// if this occurs, reload the page

	if ((windowX == windowWidth()) && (windowY == windowHeight()))
		this.location = "bio.html";
}

function setMovementParameters()
{
	if (button.clip)
	{
		rightMargin = leftMargin + bounceWidth() - button.clip.right;
		bottomMargin = topMargin + bounceHeight() - button.clip.bottom;
	}
	else
		if ((button.posWidth>=0) && (button.posHeight>=0))
		{
			rightMargin = leftMargin + bounceWidth() - button.posWidth;
			bottomMargin = topMargin + bounceHeight() - button.posHeight;
		}
}

function bounceWidth()
{
	return (documentWidth() > windowWidth()) ? documentWidth() : windowWidth();
}

function bounceHeight()
{
	return (documentHeight() > windowHeight()) ? documentHeight() : windowHeight();
}

function documentWidth()
{
	if (isNetscape)
		return document.width;
	else
		if (isIE)
			return document.body.scrollWidth;
		else
			return null;
}

function documentHeight()
{
	if (isNetscape)
		return document.height;
	else
		if (isIE)
			return document.body.scrollHeight;
		else
			return null;
}

function windowWidth()
{
	if (isNetscape)
		return window.innerWidth;
	else
		if (isIE)
			return document.body.clientWidth;
		else
			return null;
}

function windowHeight()
{
	if (isNetscape)
		return window.innerHeight;
	else
		if (isIE)
			return document.body.clientHeight;
		else
			return null;
}

function splatMouseOver()
{
	if (isInitialized)
	{
        isMouseOver = true;
        runAway();
	}
}     

function runAway()
{
	if (canDHTML && !isMoving)
	{
		isMoving = true;
		angle = Math.random() * Math.PI * 2;
		move(initialSpeed,angle);
	}
}

function move(speed,angle)
{
	if (Math.abs(speed) <= minimumSpeed)
	{
		isMoving = false;
		shouldRunAgain();
	}
	else
	{
		button.xpos += speed * Math.cos(angle);
        button.ypos += speed * Math.sin(angle);

        if (button.xpos <= leftMargin)
        {
			angle += Math.PI;
			button.xpos = leftMargin;
        }

        if (button.xpos >= rightMargin)
        {
			angle += Math.PI;
			button.xpos = rightMargin;
        }

        if (button.ypos <= topMargin)
        {
			angle += Math.PI;
			button.ypos = topMargin;
		}

        if (button.ypos >= bottomMargin)
        {
			angle += Math.PI;
			button.ypos = bottomMargin;
        }

        button.left = button.xpos;
        button.top = button.ypos;
        speed += acceleration;
        setTimeout("move(" + speed + "," + angle + ")",frameDelay);
	}
}

function shouldRunAgain()
{
	if(isMouseOver)
		runAway();
}

function splatMouseOut()
{
	isMouseOver = false;
}

function splatClicked(image)
{
	if (isInitialized)
        alert('coming soon');
}

//
// image handling functions
//

function imgState(Img)
{
	return Img.src.substring(Img.src.lastIndexOf("_"),Img.src.length - 4);
}

function toggle(Img)
{
	Img.src = toggleURL(Img);
}

function turnOff(Img)
{
	Img.src = imagePath(Img) + imageName(Img) + "_off" + imageType(Img);
}

function turnOn(Img)
{
	Img.src = imagePath(Img) + imageName(Img) + "_on" + imageType(Img);
}

function gray(Img)
{
	Img.src = grayURL(Img);
}

function grayURL(Img)
{
	return imagePath(Img) + imageName(Img) + "_gry" + imageType(Img);
}

function toggleURL(Img)
{
	return imagePath(Img) + imageName(Img) + ((imgState(Img) == "_off") ? "_on" : "_off") + imageType(Img);
}

function imageName(Img)
{
	return Img.src.substring(Img.src.lastIndexOf("/")+1,Img.src.lastIndexOf("_"));
}

function imagePath(Img)
{
	return Img.src.substring(0,Img.src.lastIndexOf("/")+1);
}

function imageType(Img)
{
	return Img.src.substring(Img.src.lastIndexOf("."),Img.src.length);
}

//
// audio functions
//

function AUDIOCLIP(label,filename,length, size)
{
	this.label = label;
	this.filename = filename;
	this.length = length;
	this.size = size;
}

function audioPlayerCommand(command, trackInfoForm, tracksArray)
{
	currentTrack = parseInt(trackInfoForm.currentTrack.value);
	
	if (command == 'rew')
	{
		if (currentTrack > 0)
		{
			currentTrack--;
			setTrackInfo(trackInfoForm, tracksArray, currentTrack)
		}
		return;
	}
	
	if (command == 'ff')
	{
		if (currentTrack < tracksArray.length - 1)
		{
			currentTrack++;
			setTrackInfo(trackInfoForm, tracksArray, currentTrack)
		}
		return;
	}
	
	format = '.rpm';
	audioFilename = 'audio/' + eval('tracksArray[currentTrack]' + format + '.filename') + format;

	//alert('68K?' + (navigator.appVersion.indexOf("68K") != -1));
	if (navigator.appVersion.indexOf("68K") != -1)
	{
		// 68K Macs can't do Live Connect
		alert("can't play " + audioFilename + " with javascript on 68K mac");
		openSoundInNewWindow(audioFilename);
		return;
	}

	if (format == '.wav' || format=='.au')
		nativeCommand(command, audioFilename);
	
	if (format == '.ram' || format == '.rpm')
		realAudioCommand(command, audioFilename);
}

function nativeCommand(command, audioFilename)
{
	if (!realAudioOCX && !document.activeMovieOCX)
	{
		// non-ocx browser (netscape)
		// wanted to test !activeMovieOCX for netscape, but it caused errors??!!
		if (document.embeds["wavPlayer"] != null && format == '.wav')
			audioPlayer = document.embeds["wavPlayer"];
		else
		{
			//alert("can't play " + audioFilename + " with javascript");
			newWindowCommand(command,audioFilename);
			return;
		}
	}
	else
		if (activeMovieOCX)
			audioPlayer = activeMovieOCX;
		else
			if (document.activeMovieOCX)
				audioPlayer = document.activeMovieOCX;

	if (command == 'play')
	{
		if (!realAudioOCX && !document.activeMovieOCX)
		{
			// wanted to test !activeMovieOCX for netscape, but it caused errors??!!
			if (navigator.plugins && document.all)
				audioPlayer.run(false,audioFilename);
			else
			{
				audioPlayer.play(false,audioFilename);
			}
		}
		else
			if (activeMovieOCX)
				activeMovieOCX.FileName = audioFilename;
	}
		
	if (command == 'pause')
		audioPlayer.pause();
	
	if (command == 'stop')
		audioPlayer.stop();
}

function realAudioCommand(command, audioFilename)
{
	//audioFilename = 'http://www.dinformation.com/' + audioFilename;
	
	if (!realAudioOCX)
		if (!document.realAudioOCX)
			if (document.embeds["raPlugin"] == null)
			{
				//alert("can't play " + audioFilename + " with javascript");
				newWindowCommand(command,audioFilename);
				return;
			}
			else
				audioPlayer = document.embeds["raPlugin"];
		else
			audioPlayer = document.realAudioOCX;
	else
		audioPlayer = realAudioOCX;
			
	if (command == 'play')
	{
 		if (!realAudioOCX && !document.realAudioOCX)
			audioPlayer.SetSource(audioFilename);
  		else
           	audioPlayer.Source = audioFilename;
           	
		audioPlayer.DoPlayPause();
	}

	if (command == 'pause')
	{
		audioPlayer.DoPlayPause();
	}
		
	if (command == 'stop')
	{
		audioPlayer.DoStop()
	}
}

function newWindowCommand(command,audioFilename)
{
	if (command=='play')
	{
		soundWindow = open(audioFilename,'soundWindow','width=1,height=1,statusbar=no,locationbar=no,scrollbars=no,toolbar=no');
		this.focus();
	}
	if (command=='stop')
		if (!soundWindow.closed)
			soundWindow.close();
}

function setTrackInfo(trackInfoForm, tracksArray, currentTrack)
{
	if (!isInitialized)
		return;
	trackInfoForm.currentTrack.value = parseInt(currentTrack);
	trackInfoForm.trackInfo.value = (parseInt(currentTrack) + 1) + ') ' + eval('tracksArray[currentTrack].rpm.label');
}

//
// Form Validation functions
//
function checkFieldLength(field)
{
	if (field.value.length == 0)
	{
		field.value = prompt('Please enter your ' + field.name, '');
		if (field.value=='null')
		{
			field.value = '';
			return false;
		}
		else
			checkFieldLength(field);
	}
	return true;
}

function checkEmail(field)
{
	var address = field.value;
	var atSignPosition = address.indexOf('@');
	var dotPosition = address.lastIndexOf('.');
	if 	(atSignPosition == -1 ||
		dotPosition == -1 ||
		atSignPosition != address.lastIndexOf('@') ||
		dotPosition <= atSignPosition ||
		dotPosition == (atSignPosition + 1) ||
		dotPosition == address.length
		)
	{
		field.value = prompt('"' + address + '" is not a valid email address, please re-enter yours.', '');
		if (field.value=='null')
		{
			field.value = '';
			return false;
		}
		else
			checkEmail(field);
	}
	return true;
}
