var menuLinks = $$('.menu li a')
menuLinks.each(function(el) {
	el.addEvent('click', function() { highlightParent(this) });
})

var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"

$('countrydivcontainer').style.display = 'none';

countries.onajaxpageload = function() {
	

	var links = document.getElementById('countrydivcontainer').getElementsByTagName('a');
	for(i = 0; i < links.length; i++) {
		if(links[i].rel == 'ajax') {
			
			links[i].onclick = function() {

				loadLink(this.href);
				return false;
			}
			
		}		
	}
	
	Slimbox.scanPage()
		
}
countries.init()

if(location.href.indexOf('#') != -1) {
	var autoLoad = location.href.substr(location.href.indexOf('#')+1);
	loadLink(autoLoad)
}

$('countrydivcontainer').style.display = 'block';

function loadLink(linkhref) {

	var found = false;
	for(j = 0; j < menuLinks.length; j++) {
		if(menuLinks[j].href == linkhref) {
			menuLinks[j].className = 'selected';
			highlightParent(menuLinks[j]);
			found = true;
		}			
	}
	
	if(found)
		for(j = 0; j < menuLinks.length; j++)
			if(menuLinks[j].href != linkhref)
				menuLinks[j].className = '';

	countries.loadajaxpage(linkhref);
}

document.getElementById('listaLink').onclick = function() {
	return toggleMenu();	
}

function highlightParent(link) {
	$$('h2.menuH').each(function(el) { el.removeClass('active') })
	$(link).getParent().getParent().getPrevious().addClass('active');
}


var menu = $$('h2.menuH');
for(i = 0; i < menu.length; i++) {

	menu[i].getNext().style.height = '140px'
		
	menu[i].getFirst().onclick = function() {
			
		return toggleMenu()
	}

}

function toggleMenu() {
	var menu = $$('h2.menuH');	
	for(i = 0; i < menu.length; i++) {
		
		var myEffect = new Fx.Morph(menu[i].getNext(), {duration: 'long'});
	
		if(menu[i].getNext().style.height != '0px') {
		
			myEffect.start({
				'height': [140, 0]
			});
			
		} else {
		
			myEffect.start({
				'height': [0, 140]
			});
			
		}
				
	}

	return false;
}

