var BoxHeight				= 254;
var Expanded 				= Array();
var InProgress				= Array();
var PreviousViewPortHeight 	= null;

//http://weblogs.asp.net/skillet/archive/2006/03/23/440940.aspx
function dumpObj(obj, name)
{
	if (typeof obj == "object")
	{
		var output = name + ": ";
		for (var item in obj)
		{
			output += "<" + item + ">" + obj[item] + "\n";
		}

		return output;
	} else {
		return obj;
	}
}

function getCurrentExpandingHeight( onLoadAmt )
{
	if( onLoadAmt == null )
	{
		onLoadAmt = 0;
	}
	else if( onLoadAmt == true )
	{
		onLoadAmt = 20;
	} // otherwise onLoadAmt is an integer.
	return YAHOO.util.Dom.getViewportHeight() - BoxHeight - onLoadAmt;
}

function forceContractBox( boxId )
{
	if( Expanded[ boxId ] == true )
	{
		expandBox( boxId, previousViewPortHeight );
	}
}

var expandAndContractBox = function(e, boxId)
{
	if( Expanded[ boxId ] == null )
	{
		Expanded[ boxId ] = false;
	}

	expandBox( boxId, null );
}

var addInProgressElement = function()
{
	var element = this.getEl();
	element = element['id'] + 'Inside';
	InProgress[ element ] = true;
}

var removeInProgressElement = function()
{
	var element = this.getEl();
	element = element['id'];
	InProgress[ element ] = null;
}

function expandBox( boxId, forceAmount )
{
	if( forceAmount == null )
	{
		forceAmount = getCurrentExpandingHeight( true );
	}
	//alert( forceAmount );

	var expandImage = document.getElementById( boxId + 'Image' );
	var MoveBy 		= -1 * forceAmount;
	var ExpandBy 	= forceAmount;

	if( Expanded[ boxId ] == true )
	{
		MoveBy 		*= -1;
		ExpandBy	*= -1;
		expandImage.src = '../img/arrow_up.gif';
	} else {
		expandImage.src = '../img/arrow_down.gif';
	}

	var myAnim = new YAHOO.util.Motion(boxId, {
		height: { by: ExpandBy },
		points: { by: [0, MoveBy] }
	}, 0.6);
	myAnim.onStart.subscribe(addInProgressElement); 
	myAnim.animate();
	var insideBoxId = boxId + 'Inside';
	var myAnimInside = new YAHOO.util.Motion(insideBoxId, {
		height: { by: ExpandBy },
		points: { by: [0, 0] }
	}, 0.6);
	myAnimInside.onComplete.subscribe(removeInProgressElement);
	myAnimInside.animate();
	Expanded[ boxId ] = !Expanded[ boxId ];

	if( Expanded[ boxId ] == true )
	{
		previousViewPortHeight = getCurrentExpandingHeight( true );
	}
	//alert( 'done expanding, changed to  (' + Expanded[ boxId ] );
}

var resizedAlignBottom = function()
{
	for(var i in Expanded)
	{
		if( Expanded[ i ] == true && InProgress[ i + 'Inside' ] == null )
		{
			forceContractBox(i);
		}
	}
	alignBottom();
}

var alignBottom = function()
{
	YAHOO.util.Dom.setStyle('global', 'top', getCurrentExpandingHeight( null ) + 'px'); 
}

var init = function()
{
	alignBottom();

	/*var links = ['bioboxHeader', 'photoBoxLink', 'flickrBoxLink', 'projectsBoxLink', 'blogBoxLink', 'internetBoxLink', 'movieBoxLink', 'carUptimeBoxLink' ];
	YAHOO.util.Event.addListener( links, 'click', expandAndContractBox2 ); */
	YAHOO.util.Event.addListener(document.getElementById('bioBoxHeader'), 'click', expandAndContractBox, 'bioBox');
	YAHOO.util.Event.addListener(document.getElementById('photoBoxLink'), 'click', expandAndContractBox, 'photoBox');
	YAHOO.util.Event.addListener(document.getElementById('flickrBoxLink'), 'click', expandAndContractBox, 'flickrBox');
	YAHOO.util.Event.addListener(document.getElementById('projectsBoxLink'), 'click', expandAndContractBox, 'projectsBox');
	//YAHOO.util.Event.addListener(document.getElementById('blogBoxLink'), 'click', expandAndContractBox, 'blogBox');
	//YAHOO.util.Event.addListener(document.getElementById('internetBoxLink'), 'click', expandAndContractBox, 'internetBox');
	//YAHOO.util.Event.addListener(document.getElementById('movieBoxLink'), 'click', expandAndContractBox, 'movieBox');
	//YAHOO.util.Event.addListener(document.getElementById('carUptimeBoxLink'), 'click', expandAndContractBox, 'carUptimeBox');
}

YAHOO.util.Event.addListener(window, 'resize', resizedAlignBottom);
YAHOO.util.Event.addListener(window, 'load', init);

