// ************* MAIN MENU ITEMS *************
var oNavItems = {
	// RESORTS MENU
	resorts: [
		{text: 'Overview',url: 'http://www.voyages.com.au/resorts/'},
		{text: 'Ayers Rock Resort',url: 'http://www.ayersrockresort.com.au/'},
		{text: 'Brampton Island',url: 'http://www.voyages.com.au/corporate/announcement-10/'},
		{text: 'Longitude 131&deg;',url: 'http://www.longitude131.com.au/'}
	],
	// EXPERIENCES MENU
	experiences: [
		{text: 'Backpacking',url: 'http://www.ayersrockresort.com.au/backpacking/'},
		{text: 'Camping & Caravanning',url: 'http://www.voyages.com.au/experiences/camping/'},
		{text: 'Family Holiday',url: 'http://www.ayersrockresort.com.au/family-holiday/'},
		{text: 'Luxury in the Outback',url: 'http://www.longitude131.com.au/'},
		{text: 'Red Centre Itineraries',url: 'http://www.ayersrockresort.com.au/itineraries/'},
		{text: 'Sounds of Silence',url: 'http://www.ayersrockresort.com.au/sounds-of-silence/'},
		{text: 'Desert Awakenings',url: 'http://www.ayersrockresort.com.au/desert-awakenings/'}
	],
	// WEDDINGS MENU
	weddings: [
		{text: 'Overview',url: 'http://www.voyages.com.au/weddings/'},
		{text: 'Wedding Enquiries',url: 'http://www.voyages.com.au/weddings/wedding-enquiries/'},
		{text: 'Honeymoon Packages',url: 'http://www.voyages.com.au/weddings/honeymoon-packages/'}
	],
	// CONFERENCES AND INCENTIVES MENU
	conferences: [
		{text: 'Overview',url: 'http://www.voyages.com.au/conferences/'},
		{text: 'Special Offers',url: 'http://www.voyages.com.au/conferences/special-offer/'},
		{text: 'Request a Quote',url: 'http://www.voyages.com.au/conferences/quote-request/'}
	],

	// CORPORATE MENU
	about: [
		{text: 'Career Opportunities',url: 'http://www.voyages.com.au/corporate/careers/'},
		{text: 'Company Information',url: 'http://www.voyages.com.au/corporate/'},
		{text: 'News &amp; Media',url: 'http://www.voyages.com.au/corporate/news/'},
		{text: 'Corporate Responsibility',url: 'http://www.voyages.com.au/corporate/our-responsibilities/'},
		{text: 'Contact Us',url: 'http://www.voyages.com.au/corporate/contact/'},
		{text: 'Make a Booking',url: 'http://www.voyages.com.au/bookings/'}
	]

};
// ************* THE FOLLOWING CODE DISPLAYS THE DROPDOWN MENU *************
// Constants
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;

// Variables
var oCurrentMenuItem;
var oCurrentSubMenu;
var oBuiltSubMenus = {};
var isArrow = false;

document.onmouseover = function(e){

	Event = (e == null) ? event : e;
	Event.sourceElement = (Event.target == null) ? Event.srcElement : Event.target;

	// Capture mouseover events on the main navigation buttons

	oSubMenu = null;


	if(Event.sourceElement.parentNode.parentNode != null){
		blnIsButtonTextNode = (Event.sourceElement.id.split('_')[0] == 'nav' && Event.sourceElement.nodeType == TEXT_NODE);
	}else{
		blnIsButtonTextNode = false;
	}

	if(Event.sourceElement.id.split('_')[0] == 'nav' || blnIsButtonTextNode){

		oMenuItem = (blnIsButtonTextNode) ? Event.sourceElement.parentNode : Event.sourceElement;
		
		if (Event.sourceElement.id.split('_')[2] == 'arrow') {
			isArrow = true
		}else{
			isArrow = false
		}
		
		
		strNavID = oMenuItem.id.split('_')[1];

		if(oNavItems[strNavID] != null){
		
			if(oBuiltSubMenus[strNavID] != null){
				oSubMenu = document.getElementById('sub_' + strNavID);
			}else{
				oNewSubMenu = document.createElement('div');
				oNewSubMenu.id = 'sub_' + strNavID;
				oNewSubMenu.className = 'submenu';

				for(i = 0; i < oNavItems[strNavID].length; i++){
					oNewSubMenuItem = document.createElement('a');
					oNewSubMenuItem.innerHTML = oNavItems[strNavID][i].text;
					oNewSubMenuItem.href = oNavItems[strNavID][i].url;
					oNewSubMenu.appendChild(oNewSubMenuItem);
				}

				oMainNav = document.getElementById('mainNav');
				
				oMainNav.insertBefore(oNewSubMenu, oMenuItem.nextSibling);

				oBuiltSubMenus[strNavID] = true;
				oSubMenu = oNewSubMenu;
			}
		}

		if(oSubMenu != null && oSubMenu.nodeType == ELEMENT_NODE){

			if(oCurrentMenuItem == oMenuItem){
				return false;
			}

			if(oCurrentMenuItem != null && oCurrentMenuItem != oMenuItem){
				
				if (oCurrentMenuItem.id.split('_')[2] == 'arrow') {
					oCurrentMenuItem.parentNode.className = '';
				}else{
					oCurrentMenuItem.className = '';
				}
				
			}

			if(oCurrentSubMenu != null && oCurrentSubMenu != oSubMenu){
				oCurrentSubMenu.style.visibility = 'hidden';
			}

			iSubMenuWidth = parseInt(oMenuItem.offsetWidth);
			if(iSubMenuWidth < 170){
					switch (strNavID)
						{
							case "experiences":
								iSubMenuWidth = 170;
								break;
							case "spas":
								iSubMenuWidth = 170;
								break;
							case "weddings":
								iSubMenuWidth = 150;
								break;
							case "conferences":
								iSubMenuWidth = 150;
								break;
							case "about":
								//iSubMenuWidth = 133;
								iSubMenuWidth = 150;
								break;
							default:
								iSubMenuWidth = 194;
						}
			}

			oSubMenu.style.width = iSubMenuWidth + 'px';
			
			if (isArrow == true) {
				
				oSubMenu.style.left = oMenuItem.parentNode.offsetLeft + 'px';
				
				if(oMenuItem.parentNode.className != 'current'){
					oMenuItem.parentNode.className = 'active';
				}else{
					oMenuItem.parentNode.style.borderTopColor = '#7BD3EC';
				}
				
			}else{
				
				oSubMenu.style.left = oMenuItem.offsetLeft + 'px';
				
				
				
				if(oMenuItem.className != 'current'){
					oMenuItem.className = 'active';
				}else{
					oMenuItem.style.borderTopColor = '#7BD3EC';
				}
			} //if (isArrow == true)
			
			

			/*
				Changing the display property aswell as the visibility 
				prevents NS6+ from flashing the submenu before it is 
				positioned
			*/
			oSubMenu.style.display = 'none';
			oSubMenu.style.visibility = 'visible';
			oSubMenu.style.display = 'block';
			oCurrentMenuItem = oMenuItem;
			oCurrentSubMenu = oSubMenu;
		}
	}

}

document.onmouseout = function(e){

	Event = (e == null) ? event : e;
	Event.sourceElement = (Event.target == null) ? Event.srcElement : Event.target;
	Event.relatedElement = (Event.relatedTarget == null) ? Event.toElement : Event.relatedTarget;

	// mouseout event fired by the top-level menu item
	if(
		(Event.sourceElement != null && Event.sourceElement.id != null && Event.sourceElement.id.split('_')[0] == 'nav')
		&& Event.sourceElement.nodeType == ELEMENT_NODE
		&& Event.sourceElement.className != 'submenu'
		&& Event.sourceElement.parentNode.className != 'submenu'
		&& Event.relatedElement != null	
		&& Event.relatedElement.className != 'submenu'
		&& Event.relatedElement.parentNode.className != 'submenu'
	){


		if(oCurrentSubMenu != null){
			oCurrentSubMenu.style.visibility = 'hidden';
			oCurrentSubMenu = null;
		}

		if (Event.sourceElement.id.split('_')[2] == 'arrow') {
			if(oCurrentMenuItem != null && oCurrentMenuItem.parentNode.className != 'current'){
				oCurrentMenuItem.parentNode.className = '';
			}else if(oCurrentMenuItem.parentNode.className == 'current'){
				oMenuItem.style.parentNode.borderTopColor = '#3ABDE4';
			}
		}else{			
			if(oCurrentMenuItem != null && oCurrentMenuItem.className != 'current'){
			oCurrentMenuItem.className = '';
			}else if(oCurrentMenuItem.className == 'current'){
				oMenuItem.style.borderTopColor = '#3ABDE4';
			}	
			
		}
		
	
		oCurrentMenuItem = null;
	}

	// mouseout event fired by the sub-menu
	if(
		Event.sourceElement != null
		&& Event.relatedElement != null
		&& (Event.sourceElement.parentNode.className == 'submenu' || Event.sourceElement.className == 'submenu')
		&& Event.relatedElement.nodeType != TEXT_NODE
		&& Event.relatedElement.className != 'submenu'
		&& Event.relatedElement.parentNode.className != 'submenu'
		&& Event.sourceElement.parentNode.id != 'mainNav'
		&& Event.relatedElement.parentNode.id != 'mainNav'
		&& Event.relatedElement.id.split('_')[2] != 'array'
	){

		if(oCurrentSubMenu != null){
			oCurrentSubMenu.style.visibility = 'hidden';
			oCurrentSubMenu = null;
		}

		if (Event.sourceElement.id.split('_')[2] == 'arrow') {
			if(oCurrentMenuItem != null && oCurrentMenuItem.className != 'current'){
				oCurrentMenuItem.className = '';
			}else if(oCurrentMenuItem.className == 'current'){
				oMenuItem.style.borderTopColor = '#3ABDE4';
			}
		} else {
			if(oCurrentMenuItem != null && oCurrentMenuItem.parentNode.className != 'current'){
				oCurrentMenuItem.parentNode.className = '';
			}else if(oCurrentMenuItem.parentNode.className == 'current'){
				oMenuItem.parentNode.style.borderTopColor = '#3ABDE4';
			}
			
		}
		oCurrentMenuItem = null;
	}

}
