var aniInc; //the animation distance incrimenter
var aniOpenFinal = 546; //the final open width
var aniCloseFinal = 24; //the final close width
var aniTiming = 30; //the animation timing in ms
var aniCloseSection; //the section that is closing
var aniOpenSection; //the section that is opening
var aniCount = 0; //the count of the animation
var aniRunning = false; //the flag to signify if animation is running
var aniInterval; //the animation interval that is currently running

var prevSection; //the previously selected section
var openContent; //the currently selected section's content
var sidebar; //the side of the site with top 5, lists, tidbits

var maxSize;
var sections; //the different section divs

var currentFrameWidth; //the current width of the browser frame
var currentFrameWidth; //the current height of the browser frame

var isFirefox = false;
var isIE = false;
var isOpera = false;
var isSafari = false;

function init(openedSection)
{
	isFirefox = navigator.userAgent.indexOf("Firefox") != -1;
	isIE = navigator.userAgent.indexOf("Microsoft") != -1;
	isOpera = navigator.userAgent.indexOf("Opera") != -1;
	isSafari = navigator.userAgent.indexOf("Safari") != -1;
	
	sidebar = document.getElementById("sidebar");
	sections = new Array(document.getElementById("thoughtsTab"),
						 document.getElementById("photosTab"),
						 document.getElementById("projectsTab"),
						 document.getElementById("everythingelseTab"));	
		
	// open the url specified section and remove the click
	prevSection = document.getElementById(openedSection);
	prevSection.style.width = aniOpenFinal + "px";
	prevSection.onclick = null;
	
	// set the cursor for the tab to normal
	var prevSectionTab = document.getElementById(openedSection + "Tab");
	prevSectionTab.style.cursor = "default";
	
	// open the content for the section
	openContent = document.getElementById(openedSection + "Content");
	
	// disply teh section and set up the rest of the page
	openContent.style.display = "block";
	setOpacity(openContent, 10);
	
	// show the main page body
	var bodyContentEl = document.getElementById("bodyContent");
	bodyContentEl.style.display = "block";
	
	if(openContent.offsetHeight > sidebar.offsetHeight)
	{
		maxSize = openContent.offsetHeight;
	}
	else
	{
		maxSize = sidebar.offsetHeight;
	}
	
	setSectionSize(maxSize);
	setPageDims();
	
	if(openedSection == "thoughts")
	{
		buildNav();
	}
}


// gets teh dimentions of the page and sets them in vars for use later
function setPageDims()
{
	if (self.innerWidth)
	{
		currentFrameWidth = self.innerWidth;
		currentFrameWidth = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		currentFrameWidth = document.documentElement.clientWidth;
		currentFrameWidth = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		currentFrameWidth = document.body.clientWidth;
		currentFrameWidth = document.body.clientHeight;
	}
}

// sets the given object's opacity to the given value
function setOpacity(obj, value) 
{
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

// fades te current openContent object in to 100% opaque
function fadeIn(newValue, timing)
{
	setOpacity(openContent, newValue);
	
	if(newValue < 10)
	{
		setTimeout('fadeIn(' + (newValue + 1) + ', ' + timing + ')', timing);
	}
}

// sets all defined sections to the same height given
function setSectionSize(newSize)
{
	for(var i = 0; i < sections.length; i++)
	{
		sections[i].style.height = newSize + "px";		
	}
}

// opens the selected div while closing the previous one
function switchDiv(newDiv)
{	
	// only do this if an animation is not currently running
	if(!aniRunning)
	{
		newDiv.onclick = null;
		var newTab = document.getElementById(newDiv.id + "Tab");
		newTab.style.cursor = "default";
		
		aniInc = 1;
		aniCloseSection = prevSection;
		aniOpenSection = newDiv;
		
		aniInterval = setInterval('aniWidthDiv()', aniTiming);
		prevSection.onclick = function() {switchDiv(this);};
		var prevTab = document.getElementById(prevSection.id + "Tab");
		prevTab.style.cursor = "pointer";
	
		prevSection = newDiv;
	}
}

// open the new/close the old div
function aniWidthDiv()
{
	aniRunning = true;
	aniCount++;
	adjust = 0;
	closeContent = document.getElementById(aniCloseSection.id + "Content");
	openContent = document.getElementById(aniOpenSection.id + "Content");
	
	currOpenWidth = aniOpenSection.offsetWidth;
	currCloseWidth = aniCloseSection.offsetWidth;
	
	closeContent.style.display = "none";
	setOpacity(closeContent, 0);
	
	// open
	if((aniOpenFinal - currOpenWidth) < aniInc)
	{
		aniOpenSection.style.width = aniOpenFinal + "px";
	}
	else
	{
		aniOpenSection.style.width = (currOpenWidth + aniInc) + "px";
	}

	// close
	if((currCloseWidth - aniCloseFinal) < aniInc)
	{
		aniCloseSection.style.width = aniCloseFinal + "px";
	}
	else
	{
		if(aniCount > 1 && !isFirefox)
		{
			adjust = 1;
		}
		aniCloseSection.style.width = ((currCloseWidth - aniInc) + adjust) + "px";
	}
	
	aniInc *= 1.35;
	
	
	if((aniOpenSection.offsetWidth >= aniOpenFinal && aniCloseSection.offsetWidth <= aniCloseFinal) || aniCount > 25)
	{
		clearInterval(aniInterval);
		aniInc = 1;
		aniCount = 0;
		aniRunning = false;
		
		//Load up content, this will also display the section
		YAHOO.util.Connect.asyncRequest('GET', aniOpenSection.id + "Content.php", callback, null); 
	}
}

// Yahoo User Interface callback construction
// Passing an example of array of arguments to both
// the success and failure callback handlers.
var args = ['foo','bar'];

var responseSuccess = function(o){
	//alert("AJAX Success");
	openContent.innerHTML = o.responseText;
	openContent.style.display = "block";
	fadeIn(0, 1);
		
	if(openContent.offsetHeight > sidebar.offsetHeight)
	{
		maxSize = openContent.offsetHeight;
	}
	else
	{
		maxSize = sidebar.offsetHeight;
	}
	
	setSectionSize(maxSize);
	
	if(aniOpenSection.id == "thoughts")
	{
		buildNav();
	}

/* Please see the Success Case section for more
 * details on the response object's properties.
 * o.tId
 * o.status
 * o.statusText
 * o.getResponseHeader[ ]
 * o.getAllResponseHeaders
 * o.responseText
 * o.responseXML
 * o.argument
 */
};

var responseFailure = function(o){
	alert("AJAX Failure - " + o.status + " - " + o.statusText);
// Access the response object's properties in the
// same manner as listed in responseSuccess( ).
// Please see the Failure Case section and
// Communication Error sub-section for more details on the
// response object's properties.
}

var callback =
{
  success:responseSuccess,
  failure:responseFailure,
  argument:args
};

