$(document).ready(function() {

	// External links
	$('a').each(function() {
		if($(this).attr('rel') == 'external') {
			$(this).click(function() {
				window.open($(this).attr('href'));
				return false;
			});
		}
	});

	// Init navigation menu
	nav.init();

	// Init explore menu
	explore.init();
	
});

// Navigation menu
var nav = {
	delay: 200,
	timer: null,
	init: function() {
		$('#nav a.main').mouseover(function() {
			nav.keepOpen();
			$('#nav li ul').hide();
			$(this).parent().find('ul').show();									
		}).mouseout(function() {
			nav.closeAll();
		});
		$('#nav li ul li').mouseover(function() {
			nav.keepOpen();							
		}).mouseout(function() {
			nav.closeAll();
		});		
	},
	keepOpen: function() {
		clearTimeout(nav.timer);
	},
	closeAll: function() {
		clearTimeout(nav.timer);
		nav.timer = setTimeout(function() {
			$('#nav li ul').hide();					   
		}, nav.delay);
	}
};


// Explore menu
var explore = {
	init: function() {
		$('#explore').change(function() {
			var url = $(this).val();
			if(url.length) {
				document.location = url;	
			}
		});
	}
}



// Old
function hl(e){
	if(e.className!='sectionListOn'){
		if(e.className) e.className='';
		else e.className='sectionListOver';
	}
}
function hl2(e){
	if(e.className) e.className='';
	else e.className='sectionRelatedOver';
}


var om=0;
var od=0;
dFlag=1;
tFlag=1;

function openMenu(m){
	if(om && om!=m) closeAllMenus();
	if(!om){
		newTo=setTimeout("delayOpen("+m+")",100);
		dFlag=0;
	}
}

function delayOpen(m){
	document.getElementById('nav'+m).className="navTopLevelOn";
	document.getElementById('menu'+m).style.visibility='visible';
	om=m;
}

function closeAllMenus(){
	for (i=1; i<11; i++){
		document.getElementById('nav'+i).className="navTopLevel";
		document.getElementById('menu'+i).style.visibility='hidden';
		om=0;
	}
}

function killMenu(){
	if(!dFlag){
		clearTimeout(newTo);
		dFlag=1;
	}
	if(om){
		killDiv = setTimeout("closeAllMenus()",250);
		tFlag = 0;
	}
	if(od){
		killDiv = setTimeout("closeDrop()",100);
		tFlag = 0;
	}
}

function keepAlive(){
	if (!tFlag){
		clearTimeout(killDiv);
		tFlag=1;
	}
}

function openDrop(d){
	if(om) closeAllMenus();
	document.getElementById(d).style.visibility='visible';

	if(document.all && !opera) document.getElementById(d).childNodes[0].doScroll("scrollbarPageUp");
	od=d;
}

function closeDrop(){
	document.getElementById(od).style.visibility='hidden';
	od=0;
}

function killDrop(){
	if(od){
		killDiv = setTimeout("closeDrop()",100);
		tFlag = 0;
	}
	if(om){
		killDiv = setTimeout("closeAllMenus()",250);
		tFlag = 0;
	}
}

function changeSelection(i){
	document.getElementById('menuType').innerHTML=i;
	document.getElementById('menuSelected').innerHTML=i;
	closeDrop();
}

function setSearchArea(area){
	document.search_form.area.value = area;
}

