var currentContent = 'home';
//init('header', setNavHoverEffects);

function setNavHoverEffects() {
	setNavHover('nav_clients');
	setNavHover('nav_about');
	setNavHover('nav_services');
	setNavHover('nav_contact');
	setNavHover('nav_home');
}

function setNavHover(id) {
	var foo = document.createElement('a');
	var e = $(id);
	setOpacity(foo, 0);
	foo.href = e.href;
	foo.onclick = function() { e.onclick(); }
	foo.style.backgroundColor = '#000000';
	foo.style.display = 'block';
	foo.style.height = e.offsetHeight + 'px';
	foo.style.marginLeft = getPosX(e) - getPosX($('header')) + 'px';
	foo.style.marginRight = getPosY(e) - getPosY($('header')) + 'px';
	foo.style.position = 'absolute';
	foo.style.width = e.offsetWidth + 'px';
	foo.style.zIndex = 5;
	e.parentNode.insertBefore(foo, e.nextSibling);
	foo.onmouseover = function() { setOpacity(foo, 10); }
	foo.onmouseout = function() { setOpacity(foo, 0); }
}

function setOpacity(element, opacity) {
	element.style.opacity = opacity / 100;
	element.style.filter = 'alpha(opacity=' + opacity + ')';
}

function togglePage(nav_id) {
	var id = nav_id.substring(4);
	
	if(id == currentContent) {
		return false;
	}

	var foo = $(id + '_header').cloneNode(true);
	var h = $('header');
	var bg = 'images/header_' + id + '.jpg';
	$('header').style.backgroundImage = "url('" + bg + "')";
	var element = $(id);
	element.style.position = 'absolute';
	element.style.left = '-1000px';
	element.style.height = 'auto';
	element.style.width = $('container').offsetWidth + 'px';
	var height = element.offsetHeight;
	element.style.height = '0px';
	element.style.position = '';
	element.style.left = '';
	element.parentNode.appendChild($(currentContent));
	resize(element, null, height, 1, 15);
	resize($(currentContent), null, 0, 1, 15);
	currentContent = id;
}

function resize(element, newWidth, newHeight, milliseconds, phases) {
	milliseconds = (milliseconds == null) ? 1000 : milliseconds;
	phases = (phases == null) ? milliseconds/20 : phases;
	var startWidth = element.offsetWidth;
	var startHeight = element.offsetHeight;
	var widthDif = newWidth - startWidth;
	var heightDif = newHeight - startHeight;
	var mp = milliseconds / phases;
	element.style.overflow = 'hidden';
	
	function doResize(currentPhase) {
		if(currentPhase > phases) {
			return false;
		}
		
		setTimeout(function() { doResize(currentPhase + 1) }, mp);

		if(newWidth != null) {
			element.style.width =  startWidth + (widthDif * currentPhase / phases) + 'px';
		}
		
		if(newHeight != null) {
			element.style.height =  startHeight + (heightDif * currentPhase / phases) + 'px';
		}
	}
	
	doResize(1);
}

function slide(element, distanceX, distanceY, milliseconds, callbackFunction) {
	milliseconds = (milliseconds == null) ? 1000 : milliseconds;
	millisecondsLeft = milliseconds;
	var startX = getPosX(element);
	var startY = getPosY(element);
	
	function do_slide() {	
		if(millisecondsLeft == 0) {
			callbackFunction();
			return false;
		}
		
		millisecondsLeft -= 20;
		setTimeout(do_slide, 20);
		var currentPhase = (milliseconds - millisecondsLeft) / 20;
		var totalPhases = milliseconds / 20;
		var moveX = distanceX * currentPhase / totalPhases;
		var moveY = distanceY * currentPhase / totalPhases;
		var nextX = startX + moveX;
		var nextY = startY + moveY;
		element.style.left =  nextX + 'px';
		element.style.top = nextY + 'px';
	}
	
	do_slide();
}