var pi = Math.PI;
var boxes = new Array("demos", "video", "contact", "about");
var mailCheckPrefixes = new Array("Security check: First", "Next", "Finally");
var dataArray = new Array(106,117,115,116,105,110,98,118,111,99,97,108,64,103,109,97,105,108,46,99,111,109);
var fade_delay = 20;
var fade_steps = 5;
var fade_step_amt = 1 / fade_steps;
var active_button = "";
var top_shrunk = false;
var tallestBox = "";

// setup mail check string
var mailCheckString = "";
var index = 0;
for (i=0; i<3; i++) {
	index = 12;
	while (index > 11)
		index = Math.round(Math.random() * 12);
	mailCheckString += "justinBvocal".charAt(index);
}

var default_video_content = '<p style="text-align:center;margin-top:150px">Click a video name to open it.</p>';

// setup movie data
var movies = new Array();
var movieDesc = new Array();
movies[0] = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/HrWJS2GyVjE&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/HrWJS2GyVjE&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
movieDesc[0] = '<img src="images/boomBloxChars.gif" style="float:right;width:80px;height:80px" />In this spot, I provided voices for all the game characters<br />you see to the right (speaking, crowing, bleating, etc.).';

movies[1] = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/2SjWbQdbgfs&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/2SjWbQdbgfs&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
movieDesc[1] = 'I am the voice of Pterry the pterodactyl from <a href="http://www.webosaurs.com/" target="_blank">Webosaurs</a>';

movies[2] = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/cM7KKnMB95s&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/cM7KKnMB95s&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
movieDesc[2] = 'I am the voice of Pterry the pterodactyl from <a href="http://www.webosaurs.com/" target="_blank">Webosaurs</a>';

movies[3] = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/b5E0110VbKU&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/b5E0110VbKU&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
movieDesc[3] = 'I am the voice of Pterry the pterodactyl from <a href="http://www.webosaurs.com/" target="_blank">Webosaurs</a>';

movies[4] = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/Qo3CZJd5e7I&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Qo3CZJd5e7I&hl=en&fs=1&color1=0xe1600f&color2=0xfebd01&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
movieDesc[4] = 'I am the voice of Pterry the pterodactyl from <a href="http://www.webosaurs.com/" target="_blank">Webosaurs</a>';

movies[5] = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/SsRuvB3daos&hl=en_US&fs=1&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SsRuvB3daos&hl=en_US&fs=1&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
movieDesc[5] = 'Demo movie from the <a href="http://www.brandmebig.com/" target="_blank">Brand Me Big</a> web site.';


// setup demo data
var demos = new Array();
demos[0] = '<script language="JavaScript" src="http://www.justinbvocal.com/js/audio-player.js"></script><object type="application/x-shockwave-flash" data="http://www.justinbvocal.com/swf/player.swf" id="demo_commercial" height="24" width="290"><param name="movie" value="http://www.justinbvocal.com/swf/player.swf"><param name="FlashVars" value="playerID=1&amp;soundFile=http://www.justinbvocal.com/audio/demos/JustinBarrett_Commercial.mp3"><param name="quality" value="high"><param name="menu" value="false"><param name="wmode" value="transparent"></object>';


function initBoxes() {
	var boxHeight = 0;
	for (b in boxes) {
		box = document.getElementById(boxes[b]);
		height = box.offsetHeight;
		if (height > boxHeight) {
			tallestBox = boxes[b];
			boxHeight = height;
		}
		box.style.top = -1 * height;
	}
}

function testDiv() {
	return document.getElementById("test");
}

function show (box) {
	// set button visibility
	oldButton = active_button;
	active_button = "button_" + box;
	setTimeout(getOpacityString(active_button, 1.0), 0);
	if (oldButton != "") {
		buttonFadeOut(oldButton);
	} else {
		// initial morph
		growShrink();
	}
	
	// show the audio players if opening the "Demos" section
//	if (box == "demos" && oldButton != "demos") {
//		unhideAudio();
//	}
	
	// show the appropriate content box
	for (b in boxes) {
		if (boxes[b] == box) {
			moveIt(boxes[b], 1);
		} else {
			moveIt(boxes[b], -1);
		}
	}
		if (oldButton == "button_video")
			setInner("moviespace", default_video_content);
//		if (oldButton == "button_demos")
//			hideAudio();
}

function setInner (itemId, content) {
	document.getElementById(itemId).innerHTML = content;
}

function addInner (itemId, content) {
	document.getElementById(itemId).innerHTML += content;
}

function moveIt (boxName, dir) {
	box = document.getElementById(boxName);
	oldPos = box.offsetTop;
	newPos = 0;
	if (dir == -1)
		newPos = -1 * box.offsetHeight;
	diff = newPos - oldPos;
	if (Math.abs(diff) > 1) {
		var x = 1;
		var execString = "";
		for (i=0; i<=pi; i+=pi/25) {
			val = Math.round(Math.round(Math.cos(i) * (box.offsetHeight / 2)) + (box.offsetHeight / 2));
			if (dir == -1)
				val = -1 * (box.offsetHeight - val);
			else
				val = val * -1;
			execString = "setStylePX('" + boxName + "', 'top', " + val + ");";
			setTimeout(execString, 20 * x)
			x ++;
		}
		execString = "setStylePX('" + boxName + "', 'top', " + newPos + ");";
		setTimeout(execString, 20 * x)
	}
}

function growShrink() {
	// expand base
	box = document.getElementById(tallestBox);
	newHeight = box.offsetHeight + 5;
	oldHeight = document.getElementById("contentOuter").offsetHeight;
	var x = 1;
	var execString = "";
	for (i=0; i<=pi; i+=pi/25) {
		val = newHeight - Math.round(Math.round(Math.cos(i) * (newHeight / 2)) + (newHeight / 2));
		if (val < 0)
			val = 0;
		execString = "setStylePX('contentOuter', 'height', " + val + ");";
		setTimeout(execString, 20 * x);
		x ++;
	}
	
	// shrink top
	box = document.getElementById("logoDiv");
	var diff = 0 - box.offsetHeight;
	var x = 1;
	var execString = "";
	for (i=0; i<=pi; i+=pi/25) {
		val = 0 - Math.round(Math.round(Math.cos(i) * (diff / 2)) + (diff / 2));
		if (val < 0) 
			val = 0;
		execString = "setStylePX('logoDiv', 'height', " + val + ");";
		opacity = x * .5 * (1/25);
		execString += getOpacityString('miniLogo', opacity);
		execString += "setStylePX('bar_left', 'paddingRight', " + (x * 2.5) + ");";
		execString += "setStylePX('bar_right', 'paddingLeft', " + (x * 2.5) + ");";
		//testDiv().innerHTML += execString + "<br /><br />";
		setTimeout(execString, 20 * x);
		x ++;
	}
}

function getOpacityString(buttonId, opacity) {
	outString = "setOpacity('" + buttonId + "', " + opacity + ");";
	return outString;
}

function setStylePX(itemId, styleType, pxValue) {
	eval("document.getElementById('" + itemId + "').style." + styleType + " = '" + pxValue + "px';");
}

function buttonFadeIn(buttonId) {
	if (buttonId != active_button) {
		for (i=1; i<=fade_steps; i++) {
			opacity = i * fade_step_amt;
			setTimeout(getOpacityString(buttonId, opacity), fade_delay * i);
		}
	}
}

function buttonFadeOut(buttonId) {
	if (buttonId != active_button) {
		for (i=fade_steps; i>=0; i--) {
			opacity = i * fade_step_amt;
			setTimeout(getOpacityString(buttonId, opacity), fade_delay * (fade_steps - i));
		}
	}
}

function showMovie(num) {
	document.getElementById("moviespace").innerHTML = movies[num] + "<p>" + movieDesc[num] + "</p>";
}

function setOpacity(id, opacityVal) {
	var item = document.getElementById(id).style;
	item.opacity = opacityVal;
	item.MozOpacity = opacityVal;
	item.KhtmlOpacity = opacityVal;
	item.filter = "alpha(opacity=" + opacityVal * 100 + ")";
}

function hideAudio() {
	for (d=0; d<demos.length; d++) {
		document.getElementById("demo_" + d).innerHTML = " ";
	}
}

function unhideAudio() {
	for (d=0; d<demos.length; d++) {
		document.getElementById("demo_" + d).innerHTML = demos[d];
	}
}

/*function mailCheck(clicked, index) {
	var outString = "";
	var ch = "";
	var clickString = "justinBvocal";
	var valid = true;
	if (clicked != "start") {
		if (clicked != mailCheckString.charAt(index)) {
			outString = "Sorry, wrong click. Feel free to reload the page and try again.";
			valid = false;
		}
	}
	if (index == 2 && valid == true) {
		outString = "<a href=\"mai" + "lto:";
		for (i=0; i<dataArray.length; i++)
			outString += String.fromCharCode(dataArray[i]);
		outString += "\">Mail me!</a>";
	} else {
		index += 1;
		for (i=0; i<12; i++) {
			ch = clickString.charAt(i);
			outString += ch.link("javascript:mailCheck('" + ch + "', " + index + ");") + " ";
		}
		outString += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;-- ";
		outString += mailCheckPrefixes[index] + " click the \"" + mailCheckString.charAt(index) + "\"";
	}
	document.getElementById("mailConfirm").innerHTML = outString;
}*/
