// IE doesn't have Array.indexOf !
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

// expand/collapse box
function toggleDisplay(boxId, expandedId, startWidth, endWidth) {
	var expandedEl = document.getElementById(boxId+"_"+expandedId);
	var widthAnim, heightAnim;
	var boxEl = document.getElementById(boxId);
	var expandedClassName = " "+expandedId+"expanded";
	var className = boxEl.className.replace(expandedClassName, "");
	if(className == boxEl.className) { // box is not expanded
		boxEl.className += expandedClassName;
	
		expandedEl.style.height = expandedEl.clientHeight+20+"px";
		expandedEl.style.width = "100%";
		expandedEl.style.display = "none";
		expandedEl.style.position = "relative";
		expandedEl.style.left = "0";
	
		widthAnim = new YAHOO.util.Anim(boxEl, {width:{to:endWidth}});
		widthAnim.animate();
		
		heightAnim = new YAHOO.widget.Effects.BlindDown(expandedEl, {delay: true});
		heightAnim.animate();		
	} else { // box is already expanded, so collapse it
		widthAnim = new YAHOO.util.Anim(boxEl, {width:{to:startWidth}});
		widthAnim.animate();
		
		heightAnim = new YAHOO.widget.Effects.BlindUp(expandedEl, {delay: true});
		heightAnim.onEffectComplete.subscribe(function() {
			expandedEl.style.width = endWidth+"px";
			expandedEl.style.display = "block";
			expandedEl.style.position = "absolute";
			expandedEl.style.left = "-10000px";			 
		});
		heightAnim.animate();
		
		boxEl.className = className;	
	}
}

// navigation script
function showPage(id) {
	if(["home","products","health","sponsors","contact","links"].indexOf(id)<0) return;
	var callback = { 
		success: function(o) {
			var html = o.responseText;
			
			document.getElementById("content").innerHTML = html;
			
			// eval is evil
			var scriptsRe = /<script type="text\/javascript">((\n|\r|.)*?)<\/script>/ig;
			var scripts = html.match(scriptsRe);
			for(var i=0, len=scripts.length; i<len; i++) {
				var script = scripts[i];
				script = script.replace('<script type="text/javascript">', "");
				script = script.replace('</script>', "");
				eval(script);
			}
			
			// remove activeNav class from active navigation item
			var activeId = "home";
			var activeNavs = YAHOO.util.Dom.getElementsByClassName("activeNav", "li");
			for(var i=0, len=activeNavs.length; i<len; i++) {
				activeNavs[i].className = "";
				activeId = activeNavs[i].id.substring(3); // remove "nav"
				break; // there's only one
			}
			
			// set activeNav class on active navigation item
			YAHOO.util.Dom.addClass("nav"+id, "activeNav");
			
			// animate the bike (only x changes, y is fixed)
			var positions = {
				home: 115,
				products: 210,
				health: 330,
				sponsors: 450,
				contact: 570,
				links: 690
			};
			var attributes = {
				left: {to: positions[id]}
			};
			var anim = new YAHOO.util.Motion("animBike", attributes);
			anim.animate();
		},
		failure: function(o) {
		},
		scope: this
	}
	var transaction = YAHOO.util.Connect.asyncRequest("GET", window.location.pathname+id+".php", callback,  null); 
}