	var subMenusArray = [
		'villasByRegion',
		'villasByTheme',
		'aboutItaly',
		'howToBook'
		];

	var preLoadShadow = new Asset.image('/images/shadowsquare.png');
	
	window.addEvent('domready',function(){
		
		
		$('columnSideShadow').addEvent('mouseover', closeSubMenus);
		
		subMenusArray.each(function(value, index){

			$(value + 'Main').addEvents({
				'mouseover': function(event){
					closeSubMenus();
					showSubMenu(value, event);
				},
				'click': function(event){
					closeSubMenus();
					showSubMenu(value, event);
				}
			});

			$(value + 'List').addEvents({
				'mouseout': function(event){
					if(positionIsOutsideList(event, $(value + 'List'))){
						closeSingleMenu(value);
					}
				},
				'click': function(){
					closeSingleMenu(value);
				}
			});

		});
		
		
		//all items in the column on the left
		var otherMenuItems = $$('a.menu');
		otherMenuItems.each(function(value, index){
			var currentId = otherMenuItems[index].get('id');
			if($defined(currentId) && currentId.indexOf('Main') > -1){
				//if we are over a link for opening a sub menu, do nothing, each
				//individual link has its own call to close the sub menus
			}else{
				//if we are over any of the menu items other than the links for opening sub menus, 
				//then close any sub menus
				otherMenuItems[index].addEvent('mouseover', closeSubMenus);
			}//end if
		});
		
		//Affiliate Future hide phones
		setAffiliateFutureCookieFromRequest();
		
		
		var adnetworkCookieValue = getCookieValue('adnetwork');
		if (adnetworkCookieValue == 'af'){
//		if (adnetworkCookieValue.length > 0){
			$('timeawayPhones').setStyle('display','none');
			$('topBoxWithPhones').setStyle('height','30px');
			$('topBoxWithPhonesShadow').setStyle('height','30px');
			
			//on the contactus page
//			if($defined($('contactUsBookingCentre'))){
//				$('contactUsBookingCentre').setStyle('visibility','hidden');
//				$('contactUsTelInLondon').setStyle('visibility','hidden');
//			}

			//on the contactus page
			if($defined($('rightSide'))){
				$('rightSide').setStyle('display','none');
			}
			if($defined($('contactUsBookingProcedure'))){
				$('contactUsBookingProcedure').setStyle('display','block');
			}
			

			
			//top white phone in the top logo
			if($defined($('topWhitePhone'))){
				$('topWhitePhone').setStyle('visibility','hidden');				
			}
			
		}//end if

		
		
	});
	
	function setAffiliateFutureCookieFromRequest(){
		var searchString = window.location.search.substring(1);
		var Parameters = new Object();
		var nameValuePairs = searchString.split(/&/);
		var nameValuePair;
		var name;
		var value;
		 
		for (var i = 0; i < nameValuePairs.length; i++) {
		    nameValuePair = nameValuePairs[i].split(/=/);
		 
		  
		    name = nameValuePair[0];    
		    value = nameValuePair[1];
		  
		    name = name.toLowerCase();  
		       
		    if (name == "adnetwork"){
		      if (typeof(value)!="undefined"){
		           value = value.toLowerCase();
		 
		            if (value =="af"){
		  
		  //*********set cookie******************//
		   pathname = '/';
		   myDomain = '.timeaway.co.uk';
		   //pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		   // set expiry date to 30 days from now.
		   var largeExpDate = new Date ();
		   largeExpDate.setTime(largeExpDate.getTime() + (30 * 24 * 3600 * 1000));
		   SetCookie(name,value,largeExpDate,pathname,myDomain);
		  //*********set cookie******************//  
		 
		            }
		      }       
		    }
		}		 

	}
	
	function SetCookie (name, value) {
	        var argv = SetCookie.arguments;
	        var argc = SetCookie.arguments.length;
	        var expires = (argc > 2) ? argv[2] : null;       
	        var path = (argc > 3) ? argv[3] : null;
	        var domain = (argc > 4) ? argv[4] : null;
	        var secure = (argc > 5) ? argv[5] : false;
	        document.cookie = name + "=" + escape (value) +
	                ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	                ((path == null) ? "" : ("; path=" + path)) +
	                ((domain == null) ? "" : ("; domain=" + domain)) +
	                ((secure == true) ? "; secure" : "");
	}

	
	function getCookieValue(cookieName){
		var cookieNameValueString = document.cookie;
		if(!$defined(cookieNameValueString)){
			return '';
		}
		var allCookies = cookieNameValueString.split(';');
		for(var i = 0; i < allCookies.length; i =  i + 1){
			var currentNameValue = allCookies[i];
			if(currentNameValue != null && currentNameValue.indexOf('=') > 0){
				var nameValueArray = currentNameValue.split('=');
				if(nameValueArray[0].trim() == cookieName)
				return nameValueArray[1];
			}
		}//end for
	}
	
	function positionIsOutsideList(event, listObj){
		var eventXPos = event.page.x;
		var eventYPos = event.page.y;
		
		var listObjXPos = listObj.getPosition().x;
		var listObjYPos = listObj.getPosition().y;
		var listObjClientWidth = listObj.clientWidth;
		var listObjClientHeight = listObj.clientHeight;
		//we don't want the menu to be removed while the mouse is going over the 
		//shadow between the left column and the list so we take off the shadowWidth as well
		var shadowWidth = 5;
		//if it's in the list menu, we return false		
		if(eventXPos > listObjXPos - shadowWidth && eventXPos < (listObjXPos + listObjClientWidth)){
			if(eventYPos > listObjYPos && eventYPos < (listObjYPos + listObjClientHeight)){
				return false;
			}//end if
		}//end if
		
		return true;
		
	}//end function
	
	function showSubMenu(menuType, event){

		if(targetFromBug(event)){
			return;
		}
		
		var shadowHeight = 3;
		var shadowWidth = 5;
		$(menuType + 'List').setStyle('top', $(menuType + 'Main').getPosition().y);
//		$(menuType + 'List').setStyle('left', $(menuType + 'Main').getPosition().x + $(menuType + 'Main').clientWidth + shadowWidth);
		$(menuType + 'List').setStyle('left', $(menuType + 'Main').getPosition().x + $(menuType + 'Main').clientWidth);
		$(menuType + 'Arrow').set('src', '/images/openshut/rightArrowWhite.gif');
		addMenuShadow(menuType + 'List');
	}
	
	function addMenuShadow(menuId){
		var menuObj = $(menuId);
		var innerMenuObj = $('inner' + menuId);
		//don't need z-index because the menu already has a high z-index
		var shadow = new Element('img',{
			src: preLoadShadow.src,
			id: 'shadowImg',
			styles: {
				position: 'absolute',
				width: innerMenuObj.clientWidth + 'px',
				height: innerMenuObj.clientHeight + 'px',
				top: (menuObj.getPosition().y - 3) + 'px',
				left: (menuObj.getPosition().x + 6) + 'px'
			}
		});
		shadow.inject($$('body')[0]);
	}

	function closeSubMenus(){
		subMenusArray.each(function(value, index){
			if($defined($(value + 'List'))){
				closeSingleMenu(value);
			}
		});
	}
	function closeSingleMenu(name){
		$(name + 'List').setStyle('top', '-600px');
		$(name + 'Arrow').set('src', '/images/openshut/rightArrowRed.gif');
		if($defined($('shadowImg'))){
			$('shadowImg').destroy();	
		}
	}
	
	
	/**
	 * There's a bug in Firefox on mac where a relatedTarget (select object from the search parms for example did this)
	 * kept opening the About Italy menu.
	 * So we check whether either the relatedTarget id, if there is one, 
	 * matches one of the submenus or one of the other links in the SideColumn, which all must have ids with 'SideLink' in them!
	*/
	function targetFromBug(event){
		var appNamer = navigator.appName;
		if(appNamer.toLowerCase().indexOf('netscape') > -1){
			var version = navigator.appVersion;
			if (version.toLowerCase().indexOf('mac') > -1){
			
				var relatedTargetId;
				if($defined($(event.relatedTarget))){
					relatedTargetId = event.relatedTarget.id;
				}else{
					return false;
				}
		
				var targetOk = false;
				for(var i = 0; i < subMenusArray.length; i = i + 1){
					if($defined(relatedTargetId) && (relatedTargetId.indexOf(subMenusArray[i]) > -1 || relatedTargetId.indexOf("SideLink") > -1)){
						targetOk = true;
						break;
					}//end if
				}//end for
		
				if(!targetOk){
					return true;
				}
			}//end if
		}//end if
		return false;
	}//end function()
	
	

