(function() {
// Check for needed functionality
/* preload images */
var containerImg, controlImg; // prevImg, nextImg;
containerImg = new Image();
containerImg.src = '/images/expertise/control-container.jpg';
controlImg = new Image();
controlImg.src = '/images/expertise/control.gif';

YAHOO.util.Event.on(window,'load',init);
// the current element shown on the control
var currentElement = 0;
var initialLeft = 0;
var halfwayLeft = 0;
var halfwayIndex = 0;
function init (e) {
// Build DHTML Logo Area
/*
	<div id="control-container">
		<div class="previous"><a href="#">&lt;</a></div>
		<div id="">
			<a href="#"><img src="" alt=""></a>
			<a href="#"><img src="" alt=""></a>
			<a href="#"><img src="" alt=""></a>
			<a href="#"><img src="" alt=""></a>
		</div>
		<div class="next"><a href="#">&gt;</a></div>
	</div>
*/
	// declare variables
	var container, next, nextLink, prev;
	var prevLink, control, controlList;
	// build HTML
	container = document.createElement('div');
	container.id = "control-container";
	control = document.createElement('div');
	control.className = "control";
	controlList = document.createElement('ol');
	control.appendChild(controlList);
	container.appendChild(control);
	next = document.createElement('div');
	next.className = "next button";
	nextLink = document.createElement('a');
	nextLink.href = "#";
	nextLink.appendChild(document.createTextNode('>'));
	next.appendChild(nextLink);
	container.appendChild(next);
	prev = document.createElement('div');
	prev.className = "previous button";
	prevLink = document.createElement('a');
	prevLink.href = "#";
	prevLink.appendChild(document.createTextNode('<'));
	prev.appendChild(prevLink);
	container.appendChild(prev);
	var whitepapers = document.getElementById('whitepapers');
	whitepapers.parentNode.parentNode.appendChild(container);
//	whitepapers.style.paddingTop = parseInt(YAHOO.util.Dom.getStyle(container,'height')) / 2 + 'px';
	// Get all of the available sections
	var controlListWidth = 0;
	var wpNodes = whitepapers.childNodes;
	var wpNodesLength = wpNodes.length;
	for (var i = 0; i < wpNodesLength; i++) {
		var node = wpNodes[i];
		if (node.nodeType == 1) {
			// get the relevant elements in the section
			var h3 = node.getElementsByTagName('h3')[0];
			var logo = h3.getElementsByTagName('img')[0];
			controlListWidth += (logo.width*1 + 50);
			// build the control list structure
			var li = document.createElement('li');
			var a = document.createElement('a');
			a.href = "#";
			a.title = "View the whitepaper for "+logo.alt;
			a.appendChild(logo);
			a.relatedElement = node;
			a.elNum = controlList.childNodes.length;
			h3.appendChild(document.createTextNode(logo.alt));
			li.appendChild(a);
			controlList.appendChild(li);
			// add the appropriate listeners
			YAHOO.util.Event.on(a,"click",swapFocus);
			logo = h3 = null;
		}
		node = null;
	}
	wpNodes = null;
	// set the first element as active
	YAHOO.util.Dom.addClass(whitepapers,'has-js');
	swapFocus.call(controlList.firstChild.firstChild);
	// remember states for smooth scrolling
	initialLeft = parseInt(YAHOO.util.Dom.getStyle(controlList,'left'));
	halfwayLeft = controlListWidth;
	halfwayIndex = controlList.childNodes.length;
	// create two copies of list items, in order to make scrolling smooth
	for (var i = 0; i < halfwayIndex; i++) {
		var oldLi = controlList.childNodes[i];
		var newLi = oldLi.cloneNode(true);
		newLi.firstChild.relatedElement = oldLi.firstChild.relatedElement;
		YAHOO.util.Event.on(newLi.firstChild,"click",swapFocus);
		controlList.appendChild(newLi);
		oldLi = newLi = null;
	}
	// elongate list to match all contents
	YAHOO.util.Dom.setStyle(controlList, 'width', (2 * controlListWidth + 'px'));
	// used for "fun way" to swap children
//	YAHOO.util.Dom.setStyle(whitepapers,'height',whitepapers.firstChild.offsetHeight+'px');
//	YAHOO.util.Dom.setStyle(whitepapers,'overflow','hidden');
	// activate the first element in the list
	YAHOO.util.Event.on(nextLink,"click",nextSection);
	YAHOO.util.Event.on(prevLink,"click",prevSection);
	document.getElementById("case_studies_h").style.visibility = "visible";
}
function swapFocus (e) {
	var whitepapers = document.getElementById('whitepapers');
	var newChild = this.relatedElement; // .cloneNode(true);
	var oldChild = YAHOO.util.Dom.getElementsByClassName('active','div',whitepapers)[0];
	if (oldChild == newChild) { }
	else if (oldChild) {
		// lame way
		YAHOO.util.Dom.removeClass(oldChild,'active');
		YAHOO.util.Dom.addClass(newChild,'active');
		// fun way
/*		YAHOO.util.Dom.addClass(oldChild);
		YAHOO.util.Dom.setStyle(newChild,'opacity','0');
		YAHOO.util.Dom.setStyle(newChild,'position','absolute');
		YAHOO.util.Dom.setStyle(newChild,'top',-1*whitepapers.offsetTop+'px');
		YAHOO.util.Dom.addClass(newChild,'active');
		var newHeight = newChild.offsetHeight;
		var anim = new YAHOO.util.Anim(oldChild,{opacity: {to: 0}},0.3);
		anim.onComplete.subscribe(shrinkToFit,[whitepapers,newHeight,oldChild]);
		anim.animate();
*/	}
	else {
		YAHOO.util.Dom.addClass(newChild,'active');
//		YAHOO.util.Dom.setStyle(whitepapers,'height',newChild.offsetHeight+'px');
	}
	if (e) { YAHOO.util.Event.stopEvent(e) }
}
// used for "fun way" to swap children
function shrinkToFit(evt,args,self) {
	var wp = self[0];
	var newHeight = self[1];
	var oldChild = self[2];
	YAHOO.util.Dom.removeClass(oldChild,'active');
	var anim = new YAHOO.util.Anim(wp,{height: {to: newHeight}},0.003*newHeight);
	anim.onComplete.subscribe(showContents);
	anim.animate();
}
// also used for "fun way"
function showContents (evt,args,self) {
	alert('showing contents');
	var newChild = YAHOO.util.Dom.getElementsByClassName('active','div','whitepapers')[0];
	YAHOO.util.Dom.setStyle(newChild,'position','static');
	YAHOO.util.Dom.setStyle(newChild,'top','auto');
	var anim = new YAHOO.util.Anim(newChild,{opacity: {to: 1}},0.3).animate();
	window.location.replace('#whitepapers');
	// whew! all done!
}
function nextSection (e) {
	var list = YAHOO.util.Dom.get('control-container').getElementsByTagName('ol')[0];
	// if we're moving past the halfway point, jump back to the initial point.
	if (currentElement == halfwayIndex) {
		YAHOO.util.Dom.setStyle(list,'left',initialLeft+'px');
		currentElement = 0;
	}
	// figure out the width of the next element, and animate there 
	var curLeft = parseInt(YAHOO.util.Dom.getStyle(list,'left'));
	var moveDistance = list.childNodes[currentElement].firstChild.firstChild.width + 50
	var targLeft = curLeft - moveDistance;
	var anim = new YAHOO.util.Anim(
		list,
		{left:{ to:(curLeft - moveDistance)}},
		(0.003 * moveDistance),
		YAHOO.util.Easing.easeOut
	);
	anim.onComplete.subscribe(animateSwapFocus,list.childNodes[++currentElement].firstChild);
	anim.animate();
	YAHOO.util.Event.stopEvent(e);
}
function prevSection (e) {
	var list = YAHOO.util.Dom.get('control-container').getElementsByTagName('ol')[0];
	// if we're moving past the origin point, jump back to the halfway point.
	if (currentElement == 0) {
		YAHOO.util.Dom.setStyle(list,'left',(initialLeft - halfwayLeft)+'px');
		currentElement = halfwayIndex;		
	}
	var curLeft = parseInt(YAHOO.util.Dom.getStyle(list,'left'));
	var moveDistance = list.childNodes[--currentElement].firstChild.firstChild.width + 50
	var anim = new YAHOO.util.Anim(
		list,
		{left:{to:(curLeft + moveDistance)}},
		0.003 * moveDistance,
		YAHOO.util.Easing.easeOut
	);
	anim.onComplete.subscribe(animateSwapFocus,list.childNodes[currentElement].firstChild);
	anim.animate();
	YAHOO.util.Event.stopEvent(e);
}
function animateSwapFocus (evt,args,self) { swapFocus.call(self) }
}())