// JavaScript Document

addLoadListener(show);
addLoadListener(rollover);

function show() {
	var oBureau = document.getElementById("bureau");	
	attachEventListener(oBureau, "mouseover", showMenu, false);	
	
	var oPortfolio = document.getElementById("portfolio");	
	attachEventListener(oPortfolio, "mouseover", showMenu, false);	

	var oContact = document.getElementById("contact");	
	attachEventListener(oContact, "mouseover", showMenu, false);
	attachEventListener(oContact, "mouseout", setTimerContact, false);
}

function showMenu() {
	if(this.id=='bureau') {
		hidePortfolio();
		hideContact();
		var oBureauNav = document.getElementById("bureauNav");	
		oBureauNav.style.visibility = 'visible';
		
		attachEventListener(oBureauNav, "mouseover", clearTimerBureau, false);	
		attachEventListener(oBureauNav, "mouseout", setTimerBureau, false);
		
		var aLinks = oBureauNav.getElementsByTagName("a");	
		for(var i=0; i<aLinks.length; i++) {
			attachEventListener(aLinks[i], "mouseover", clearTimerBureau, false);
		}
	}	
	else if(this.id=='portfolio') {
		hideBureau();
		hideContact();
		this.style.top = '0';
		var oPortfolioNav = document.getElementById("portfolioNav");	
		oPortfolioNav.style.visibility = 'visible';
		
		attachEventListener(oPortfolioNav, "mouseover", clearTimerPortfolio, false);	
		attachEventListener(oPortfolioNav, "mouseout", setTimerPortfolio, false);
		
		var aLinks = oPortfolioNav.getElementsByTagName("a");	
		for(var i=0; i<aLinks.length; i++) {
			attachEventListener(aLinks[i], "mouseover", clearTimerPortfolio, false);
		}	
	}	
	else if(this.id=='contact') {
		hideBureau();
		hidePortfolio();
	}
	
	ext = this.src.substring(this.src.lastIndexOf('.'), this.src.length);
	if(this.src.indexOf('Over' + ext) == -1) {
		this.src = this.src.replace(ext, 'Over'+ext);
	}
}


function hideBureau() {
	clearTimerBureau();

	var oBureau = document.getElementById("bureau");	
	ext = oBureau.src.substring(oBureau.src.lastIndexOf('.'), oBureau.src.length);
	if(oBureau.src.indexOf('Over' + ext) != -1) {
		oBureau.src = oBureau.src.replace('Over'+ext, ext);
	}		
		
	var oBureauNav = document.getElementById("bureauNav");	
	oBureauNav.style.visibility = 'hidden';
		
	try {
	//detachEventListener(oBureauNav, "mouseout", clearTimerBureau, false);
	attachEventListener(oBureau, "mouseover", showMenu, false);	
	}
	catch(e) {	}
}

function hidePortfolio() {
	clearTimerPortfolio();

	var oPortfolio = document.getElementById("portfolio");	
	ext = oPortfolio.src.substring(oPortfolio.src.lastIndexOf('.'), oPortfolio.src.length);
	if(oPortfolio.src.indexOf('Over' + ext) != -1) {
		oPortfolio.src = oPortfolio.src.replace('Over'+ext, ext);
	}
	oPortfolio.style.top = '57px';
		
	var oPortfolioNav = document.getElementById("portfolioNav");	
	oPortfolioNav.style.visibility = 'hidden';
		
	try {
		detachEventListener(oPortfolioNav, "mouseout", setTimerPortfolio, false);
		//attachEventListener(oPortfolio, "mouseover", showMenu, false);	
	}
	catch(e) {	}
}

function hideContact() {
	clearTimerContact();
	var oContact = document.getElementById("contact");	
	oContact.src = 'media/home/contact.gif';
}


// -------- timeout functies ---------
function setTimerBureau() { timerBureau = setTimeout("hideBureau()", 1000); }
function clearTimerBureau() {
	if(typeof timerBureau != 'undefined') { clearTimeout(timerBureau); }
}

function setTimerPortfolio() { timerPortfolio = setTimeout("hidePortfolio()", 1000); }
function clearTimerPortfolio() {
	if(typeof timerPortfolio != 'undefined') { clearTimeout(timerPortfolio); }
}

function setTimerContact() { timerContact = setTimeout("hideContact()", 1000); }
function clearTimerContact() {
	if(typeof timerContact != 'undefined') { clearTimeout(timerContact); }
}



function rollover() {
	var aRollovers = document.getElementsByTagName("img");	
	for(var i=0; i<aRollovers.length; i++) {		
		if(aRollovers[i].className=='rollover' || aRollovers[i].className.indexOf('rollover')!=-1) {
			if(aRollovers[i].src.indexOf('Over.') == -1) {
				attachEventListener(aRollovers[i], "mouseover", mymouseover, false);
				attachEventListener(aRollovers[i], "mouseout", mymouseout, false);
			}
		}		
	}
	
	var aRollovers = document.getElementsByTagName("input");	
	for(var i=0; i<aRollovers.length; i++) {
		if(aRollovers[i].className=='rollover' || aRollovers[i].className.indexOf('rollover')!=-1) {
			if(aRollovers[i].src.indexOf('Over.') == -1) {
				attachEventListener(aRollovers[i], "mouseover", mymouseover, false);
				attachEventListener(aRollovers[i], "mouseout", mymouseout, false);
			}
		}		
	}
}

function mymouseover() {
	ext = this.src.substring(this.src.lastIndexOf('.'), this.src.length);
	if(this.src.indexOf('Over' + ext) == -1) {
		this.src = this.src.replace(ext, 'Over'+ext);
	}
}

function mymouseout() {
	ext = this.src.substring(this.src.lastIndexOf('.'), this.src.length);
	if(this.src.indexOf('Over' + ext) != -1) {
		this.src = this.src.replace('Over'+ext, ext);
	}
}
