var HideMenuTimer, HideAttachmentTimer, DisplayTimer, DesactivateMenuTimer;

setMainMenu = function(navig) {
var navroot;
if(!(navroot=xGetElementById(navig))) return;
var lis=xGetElementsByTagName("LI",navroot);
for (i=0; i< lis.length; i++){
	switch(lis[i].id){//Second level
		case 'AboutUs':
		menudisplay = new menuDisplay('aboutus','framebox',true,true,true);
		var MenuWidth = 0;
		var Menu=xGetElementsByTagName("li",lis[i]);
		for (j=0;j<Menu.length;j++){
			if (xWidth(Menu[j]) > MenuWidth) MenuWidth = xWidth(Menu[j]);
			switch(Menu[j].id){		
					case 'ValuesMenu':
					menuSetup(Menu[j],'Our values','pointer');
					Menu[j].box = new menuBox("about_us/valueshome.html",menudisplay,'framebox');
					break;
					case 'StaffMenu':
					menuSetup(Menu[j],"The team",'pointer');
					Menu[j].box = new menuBox("about_us/staffhome.html",menudisplay,'framebox');
					break;
					case 'TransportMenu':
					menuSetup(Menu[j],'Transport','pointer');
					Menu[j].box = new menuBox("about_us/transporthome.html",menudisplay,'framebox');
					break;
			}
		}
		for (j=0;j<Menu.length;j++) xWidth(Menu[j],MenuWidth+3);
		lis[i].menu = new rollOverMenu(lis[i].lastChild);
		break;
		case 'DestinationMenu':
		menuSetup(lis[i],false,false);
		lis[i].attachment = new attachmentMenu('sa_map'); // slow down drop menu bar!!!
		subMenu(lis[i]);
		break;
		case 'ThemeMenu':
		menuSetup(lis[i],false,false);
		subMenu(lis[i]);
		break;
		case 'Incentive':
		lis[i].links = new notReady();//section not ready!
		break;
		case 'HomeMenu':
		menuSetup(lis[i],'Home','pointer');
		lis[i].preloadpics = preloadHomePics;
		menudisplay = new menuDisplay('',"home",true,false,false);
		lis[i].box = new menuBox('',menudisplay);
		lis[i].box.rotatePics = true;
		lis[i].box.screenImage = HomePic;
		lis[i].box.slideImages = HomeSlidePics;
		break;
		case 'Contact':
		menuSetup(lis[i],'Contact us','pointer');
		menudisplay = new menuDisplay('',"longbox",true,false,true);
		lis[i].box = new menuBox("contact/contacthome.html",menudisplay);
		break;
		}
}
return lis;
}

function menuBox(ref,display,inframe){
this.iframe = ref?trimPath(ref):0;//href of the page
this.display = display;
this.inframe = inframe?xGetElementById(inframe).firstChild:false;//iframe used to display the page
}

function menuDisplay(classbox,displaybox,no_mouseover,closefilmframe,hidescreen){
this.classbox = classbox;
this.displaybox = displaybox;
this.no_mouseover = no_mouseover;
this.closefilmframe = closefilmframe;
this.hidescreen = hidescreen;
}

function menuSetup(ele,sectiontitle,mouse){
ele.sectiontitle = sectiontitle;
if (ele.style) ele.style.cursor = (!mouse)?'default':mouse;
}

function subMenu(ele){
Album = new Array();
Menu = new Array();
Album = ele.id=="DestinationMenu"? Destination: Theme;
if (ele.id=="ThemeMenu") MenuBis = new Array();
var MenuWidth = 0;
for (j=0;j<Album.length;j++){
	Menu[j] = document.createElement("LI");
	ele.lastChild.appendChild(Menu[j]);
	Menu[j].map_popup = Album[j].map_popup;
	Menu[j].innerHTML = Album[j].tname;
 	Menu[j].links = Album[j].links;
	Menu[j].tours = Album[j].tours;
	Menu[j].tname = Album[j].tname;
	if (ele.id=="DestinationMenu") Menu[j].setSubMenu = setSubMenu;
	menuSetup(Menu[j],Album[j].tname,'pointer');
	if (xWidth(Menu[j]) > MenuWidth) MenuWidth = xWidth(Menu[j]);
	}
if (ele.id=="ThemeMenu") {
	j++;
	Menu[j] = document.createElement("LI");
	ele.lastChild.appendChild(Menu[j]);
	Menu[j].innerHTML = "Tailor made tours";
	menuSetup(Menu[j],"Tailor made tours",'pointer');
	menudisplay = new menuDisplay('',"longbox",true,false,true);
	Menu[j].box = new menuBox("contact/customhome.html",menudisplay);
	if (xWidth(Menu[j]) > MenuWidth) MenuWidth = xWidth(Menu[j]);
	}
for (j=0;j<Menu.length;j++) xWidth(Menu[j],MenuWidth+3);
ele.menu = new rollOverMenu(ele.lastChild);
}

function initializeDestinationSubMenu(){
var submenu = xGetElementById('destination_submenu');
var lis = xGetElementsByTagName("LI",submenu);
lis[0].onmouseover = function(){this.className = "active"; lis[1].className = null;}
lis[1].onmouseover = function(){this.className = "active"; lis[0].className = null;}
}

function setSubMenu() {
	var submenu = xGetElementById('destination_submenu');
	this.appendChild(submenu);
	initializeDestinationSubMenu();
	xDisplay(submenu,'block');
	xLeft(submenu,xWidth(this)-1);
	//xWidth(this,xWidth(this)+xWidth(submenu));
}

function mouseOverMenu() {
	var ele = this;
	ele.className = (!ele.hover)?"hover":"active";
	if (ele.map_popup) xShow(ele.map_popup);
	if (ele.menu) dropMenu(ele);
	if (ele.attachment) displayAttachment(ele);
	//if (ele.setSubMenu) ele.setSubMenu();
}

function mouseOutMenu() {
	var ele=this;
	removeDisplay(ele);
	if (!ele.hover) ele.className = 'default';
}

function mouseDownMenu(){
var ele = this;
removeDisplay(ele);
xGetElementById('framebox').firstChild.src = '';//stop framebox iframe download
ele.rst = function(){
	resetBrowsers();
	this.rst=false;
	if (AD.previousdisplay && AD.display!=xGetElementById('HomeMenu')) xDisplay('backbutton','block'); else xDisplay('backbutton','none');
	if (!xGetElementById('homepics_iframe').loaded && this.preloadpics) this.preloadpics();//preloadpics only now when landing on secondary pages
	if (this.sectiontitle) setSelectionTitle(this.sectiontitle);
	if (this.box) displayBox(this.box); else if (this.links) loadSelection(this.links); else if (this.tours) activateTourList(this.tours);
	}
if (ele.box || ele.links || ele.tours) {
	if (this.preloadpics) if(!testLandingPage()) document.location = "index.html";//allow to test if a secondary page is not showing on the location bar																									//in which case index will be reloaded whne clicking on HomeMenu
	clearTimeout(AD.randomimagestimer);
	AD.previousdisplay = (AD.previousdisplay && AD.previousdisplay==ele)?xGetElementById('HomeMenu'):AD.display;//for backbutton
	AD.display=ele;
	setActiveButton(AD.refmainmenu,ele);
	desactivateMenu();
	xGetElementById('googlemap').firstChild.src='';
	if (ele.parentNode) ele.parentNode.parentNode.className = null;
	if (ele.parentNode) ele.parentNode.parentNode.hover = false;
	clearTimeout(AD.textotimer);
	flipText(ele,false);//fliptext will activate ele.rst at the end!
	}
}

function displayVideo(){
ele = new Object();
menudisplay = new menuDisplay("","framebox",true,true,true);
ele.box = new menuBox("social_medias/videos.html",menudisplay,"framebox");
ele.sectiontitle = 'Videos';
ele.activate = mouseDownMenu;
ele.activate();
}

function setGalleryMenu(){
var gallery = xGetElementById('gallery');
Menu = new Array();
for (j=0;j<GALLERY.length;j++){
	Menu[j] = document.createElement("LI");
	gallery.lastChild.appendChild(Menu[j]);
	Menu[j].innerHTML = GALLERY[j].tname;
	menuSetup(Menu[j],'Photos&nbsp;'+GALLERY[j].tname,'pointer');
	setEventListener(Menu[j]);
	menudisplay = new menuDisplay("","framebox",true,true,true);
	Menu[j].box = new menuBox(GALLERY[j].iframe,menudisplay,"framebox");
	Menu[j].onmousedown = mouseDownMenu;
	}
gallery.menu = new rollOverMenu(gallery.lastChild,true);//true for inverting direction of dropping
setEventListener(gallery);
gallery.onmousedown = function(){removeDisplay(this)}
}

function removeDisplay(ele,e){
if (ele.map_popup) xHide(ele.map_popup);
//if (e && !(ele=mouseOutTarget(e))) return;
//if (e && ele.parentmenu) ele=ele.parentmenu;
if (ele.attachment) {
	clearTimeout(ele.attachment.timerdisplay);
	clearTimeout(ele.attachment.timerhide);
	ele.attachment.hideA();
	}
if (ele.menu){
	clearTimeout(ele.menu.timerroll);
	clearTimeout(ele.menu.timerdrop);
	ele.menu.rollup();
	}
}

function activateMainMenu() {
for (i=0; i< AD.homemenu.length; i++){
	AD.homemenu[i].onmousedown=mouseDownMenu;
	AD.homemenu[i].onmouseover=mouseOverMenu;
	AD.homemenu[i].onmouseout=mouseOutMenu;
	}
}

function desactivateMenu(){
DesactivateMenuTimer = setTimeout(function(){for (i=0; i< AD.homemenu.length; i++) AD.homemenu[i].onmousedown=null;},200);
for (i=0; i< AD.homemenu.length; i++){
	AD.homemenu[i].onmouseout=null;
	AD.homemenu[i].onmouseover=null;
	}

var slideroot=xGetElementById('slidenav');
var tr = xGetElementsByClassName('picSlide',slideroot,'img');
for (i=0; i<tr.length; i++){
	tr[i].onmouseover=null;
	tr[i].onmousedown=null;
	tr[i].style.cursor="default";
	}
}

function dropMenu(ele) {
clearTimeout(ele.menu.timerroll);
clearTimeout(ele.menu.timerdrop);
ele.menu.drop();
}

function displayAttachment(ele) {
//clearTimeout(ele.attachment.timerdisplay);
//clearTimeout(ele.attachment.timerhide);
ele.attachment.displayA();
}

function drop(){
var H,T=0;
if (this.counter<10){
	this.counter+=1;
	if(this.counter>10) this.counter = 10;
	H = Math.round(this.h*(Math.log(this.counter)/Math.LN10));
	if (this.invert){T=this.h-H;H=this.h;};
	xClip(this.menu,T,500,H,0);
	xShow(this.menu);
	var menu = this;
	this.timerdrop = setTimeout(function(){menu.drop()},30);
	} else clearTimeout(this.timerdrop);
}

function rollup(){
var H,T=0;
if (this.counter>1){
	this.counter+=-0.83;//must be slower than drop to prevent flickering!
	if(this.counter<1) this.counter = 1;
	H = Math.round(this.h*(Math.log(this.counter)/Math.LN10));
	if (this.invert){T=this.h-H;H=this.h;};
	xClip(this.menu,T,500,H,0);
	var menu = this;
	this.timerroll = setTimeout(function(){menu.rollup()},30);
	} else {clearTimeout(this.timerroll); xHide(this.menu)}//opera needs the xHide to avoid mouseover problem!
}

function displayA(){xDisplay(this.attachment,'block');}

function hideA(){xDisplay(this.attachment,'none');}

function rollOverMenu(menu,invert){
this.menu = menu;
this.h = xHeight(menu);
this.w = xWidth(menu);
this.counter = 1;
this.drop = drop;
this.rollup = rollup;
this.invert = invert;
}

function attachmentMenu(attachment){
this.attachment = attachment;
this.displayA = displayA;
this.hideA = hideA;
this.opacity = 0;
}

function setSelectionTitle(title){
xGetElementById('SelectionTitle').innerHTML = title;
}

/*function displayA(){
if (this.opacity<1){
	this.opacity+=0.25;
	xOpacity(this.attachment, this.opacity);
	if (this.opacity >= 1 && IE) xGetElementById(this.attachment).style.removeAttribute('filter');
	xDisplay(this.attachment,"block");
	var menu = this;
	this.timerdisplay = setTimeout(function(){menu.displayA()},20);
	} else clearTimeout(this.timerdisplay);
}

function hideA(){
if (this.opacity>0){
	this.opacity+=-0.2;
	xOpacity(this.attachment, this.opacity);
	var menu = this;
	this.timerhide = setTimeout(function(){menu.hideA()},20);
	} else {xDisplay(this.attachment,"none");clearTimeout(this.timerhide);}
}*/
