Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring (navigator.userAgent.indexOf("MSIE")+5))==6;
Prototype.Browser.IE7 = Prototype.Browser.IE && !Prototype.Browser.IE6;

var Navigation = Class.create({
	initialize: function(){
		//INITIALIZE CLASS PRIVATE VARIABLES
		this._videoOpen = false;
		this._reserveOpen = false;
		this._flashEnabled = false;
		this._controlEnabled = true;
		this._navEnabled = false;
		this._navReady = false;
		this._navPause = this.pauseHomeNav.bind(this);
		this._navMakeReady = this.readyHomeNav.bind(this);
		this._slideActive = 1;
		this._slideInterval = 15;//TIME IN SECONDS
		this._slideTimer = this.slideShow.bind(this);
		this._timeOut = 0;
		this._menuPositioned = false;
		this._eggVisible = false;
		this._eggData = null;
		//IF THIS IS THE HOME PAGE INITIALIZE THE NAVIGATION
		if($('homenav-hit_')){
			$w("images/bg-about-us.png images/bg-accommodations.png images/bg-dining.png images/bg-occasions.png images/bg-activities.png images/bg-specials.png").each(function(i){
				var someImage = new Image();
				someImage.src = i;
			})
			$('homenav-hit_').observe('mouseover', this.showHomeNav.bindAsEventListener(this));
			$('homenav-hit_').observe('mouseout', this.hideHomeNav.bindAsEventListener(this));
			$('homenav_').select('ul.main li a').invoke('observe', 'mouseover', this.showSubnav.bindAsEventListener(this));
			$('homenav_').select('ul.main li a').invoke('observe', 'mouseout', this.hideSubnav.bindAsEventListener(this));
			$('homenav_').select('ul.subnav').invoke('observe', 'mouseout', this.leaveSubnav.bindAsEventListener(this));
		}else{
			//ON WINDOW LOAD GET THE WIDTH OF THE CONTENT AREA
			Event.observe(window, 'load', this.getContentWidth.bind(this));
		}
		//IF THERE IS A SUBNAVIGATION MENU INITIALIZE MOUSE EVENTS
		if($('subnavigation_')){
			//$('subnavigation_').select('a').invoke('observe', 'mouseover', this.showTertiaryNav.bindAsEventListener(this));
			//$('subnavigation_').select('a').invoke('observe', 'mouseout', this.hideTertiaryNav.bindAsEventListener(this));
		}
		//IF THERE ARE TERTIARY LEVEL MENUS INITIALIZE MOUSE EVENTS
		//$$('div.navL3').invoke('observe', 'mouseout', this.leaveTertiaryNav.bindAsEventListener(this));
		//IF MORE THAN ONE BACKGROUND IMAGE INITIALIZE THE SLIDE SHOW CONTROLS
		if($('control_')){
			this._timeOut = this._slideTimer.delay(this._slideInterval);
			$('control_').observe('mouseover', this.showControlButtons.bindAsEventListener(this));
			$('control_').observe('mouseout', this.hideControlButtons.bindAsEventListener(this));
			$('control_').select('div.control').invoke('observe', 'click', this.changeBackground.bindAsEventListener(this));
		}
		//IF THERE ARE EASTER EGGS INITIALIZE MOUSE EVENTS
		var eggCount = $('easter-eggs_').select('img').size();
		if(eggCount){
			this._eggData = new Array(eggCount);
			Event.observe(window, 'load', this.populateEggData.bind(this));
			document.observe('mousemove', this.watchEggs.bindAsEventListener(this));
		}
		//IF THIS IS A PHOTO UPLOAD PAGE INTIALIZE THE IFRAME
		if($('photo-upload_')){
			$('photo-upload_').observe('click', this.openPhotoUpload);
		}
		//INITIALIZE CONTROLS FOR VIDEO PLAYER
		$('button-tour_').observe('click', this.toggleTour.bindAsEventListener(this));
		$('button-tour_close_').observe('click', this.toggleTour.bindAsEventListener(this));
		//var so = new SWFObject("/swf/jw_player.swf", "videoPlayer", "450", "300", "9", "#000");
		//so.addParam('quality', 'high');
		//so.addParam('scale', 'noscale');
		//so.addParam('allowFullScreen', 'true');
		//so.addVariable('file', '/swf/HA-virtual-tour-500.flv');
		//so.addVariable('image', '/images/video-placeholder.jpg');
		//if(so.write('video-container_')){
		//	this._flashEnabled = true;
		//}
		
		
		//INITIALIZE CONTROLS FOR RESERVATIONS TAB
		this._calendar = new Calendar();
		$('button-reserve_').observe('click', this.toggleReserve.bindAsEventListener(this));
		$('button-reserve_close_').observe('click', this.toggleReserve.bindAsEventListener(this));
		$$('input.date-select_').invoke('observe', 'change', this.validateInput.bindAsEventListener(this));
		$('form-reservation_').observe('submit', this.validateReservation.bindAsEventListener(this));
		//INITIALIZE CONTROLS FOR SLIDING CONTENT WINDOW
		if($('copy-button-container_')){
			$('copy-open-button_').observe('click', this.toggleContentArea.bindAsEventListener(this));
			$('copy-close-button_').observe('click', this.toggleContentArea.bindAsEventListener(this));
		}
		//ADJUST THE PAGE ELEMENTS ON WINDOW RESIZE
		Event.observe(window, 'load', this.adjustPageElements.bindAsEventListener(this));
		Event.observe(window, 'resize', this.adjustPageElements.bindAsEventListener(this));
	},

	//A UTILITY FUNCTION FOR EMULATING MOUSELEAVE
	validateMouseLeave: function(someEvent, someId){
		var someElement = $(someId);
		var someOffset = someElement.cumulativeOffset();
		var someWidth = someOffset.left + someElement.getWidth();
		var someHeight = someOffset.top + someElement.getHeight();
		var someX = someEvent.pointerX();
		var someY = someEvent.pointerY();
		if(someX > someOffset.left && someX < someWidth && someY > someOffset.top && someY < someHeight){
			return false;
		}else{
			return true;
		}
	},
	
	getContentWidth: function(){
		this._copyWidth = $('copy_').getWidth();
		this._subheadWidth = $('subhead_').getWidth();
	},
	
	adjustPageElements: function(someEvent){
		$('overlay').setStyle({ height: $('root_').getHeight() + 'px' });
		$('easter-eggs_').setStyle({ left: $('root_').positionedOffset().left + 'px' });
	},
	
	showHomeNav: function(someEvent){
		if((!this._navEnabled) && (!this._navReady)){
			this._navEnabled = true;
			new Effect.Parallel([
				new Effect.Scale('homenav-container_', (51/3)*100, { duration: 0.5, scaleX: false, scaleContent: false, scaleFromCenter: true, sync: true }),
				new Effect.Appear('homenav_', { duration: 0.5, sync: true })],
				{ queue: { position: 'end', scope: 'home' }, afterFinish: this._navPause});
		}
	},
	
	hideHomeNav: function(someEvent){
		if(this.validateMouseLeave(someEvent, 'homenav-hit_') && this._navEnabled && this._navReady){
			new Effect.Parallel([
				new Effect.Scale('homenav-container_', (3/51)*100, { duration: 0.5, scaleX: false, scaleContent: false, scaleFromCenter: true, sync: true }),
				new Effect.Fade('homenav_', { duration: 0.5, sync: true })],
				{ queue: { position: 'end', scope: 'home' }, afterFinish: function(){ myNavigation._navEnabled = false; myNavigation._navReady = false; } });
		}
	},
	
	pauseHomeNav: function(){
		this._navMakeReady.delay(10);
	},
	
	readyHomeNav: function(){
		this._navReady = true;
	},
	
	showSubnav: function(someEvent){
		var someElement = someEvent.findElement('a');
		someElement.addClassName('active');
		var someId = someElement.innerHTML.replace(/\s/, '-') + "-subnav_";
		if($(someId)){
			$(someId).show();
		}
	},
	
	hideSubnav: function(someEvent){
		var someElement = someEvent.findElement('a');
		var someId = someElement.innerHTML.replace(/\s/, '-') + "-subnav_";
		if($(someId)){
			if(this.validateMouseLeave(someEvent, someElement) && this.validateMouseLeave(someEvent, someId)){
				someElement.removeClassName('active');
				$(someId).hide();
			}
		}else{
			if(this.validateMouseLeave(someEvent, someElement)){
				someElement.removeClassName('active');
			}
		}
	},
	
	leaveSubnav: function(someEvent){
		var someElement = someEvent.findElement('ul');
		var someId = someElement.id.replace('-subnav_', '-nav_');
		if(this.validateMouseLeave(someEvent, someElement) && this.validateMouseLeave(someEvent, someId)){
			$(someId).removeClassName('active');
			someElement.hide();
		}
	},

	showTertiaryNav: function(someEvent){
		var someElement = someEvent.findElement('a');
		someElement.addClassName('active');
		var someId = someElement.innerHTML.stripTags().replace(/\s/g, '-') + '-nav_';
		if($(someId)){
			$(someId).show();
		}
	},
	
	hideTertiaryNav: function(someEvent){
		var someElement = someEvent.findElement('a');
		var someId = someElement.innerHTML.stripTags().replace(/\s/g, '-') + '-nav_';
		if($(someId)){
			if(this.validateMouseLeave(someEvent, someElement) && this.validateMouseLeave(someEvent, someId)){
				someElement.removeClassName('active');
				$(someId).hide();
			}
		}else{
			someElement.removeClassName('active');
		}
	},
	
	leaveTertiaryNav: function(someEvent){
		var someElement = someEvent.findElement('div');
		if(!someElement.hasClassName('navL3')){
			someElement = someElement.up('div.navL3');
		}
		var someLink = $('subnavigation_').down('a.active');
		if(this.validateMouseLeave(someEvent, someElement) && this.validateMouseLeave(someEvent, someLink)){
			someLink.removeClassName('active');
			someElement.hide();
		}
	},
	
	toggleTour: function(someEvent){
		someEvent.stop();
		var someDirection;
		if(this._videoOpen){
			this._videoOpen = false;
			someDirection = -443;
		}else{
			someDirection = 443;
			this._videoOpen = true;
		}
		new Effect.Parallel([
			new Effect.Move('video_', { x: 0, y: someDirection, mode: 'relative', sync: true }),
			new Effect.Move('button-tour_', { x: 0, y: someDirection, mode: 'relative', sync: true })
		]);
	 if (this._reserveOpen){

		new Effect.Parallel([		
			new Effect.Move('reservation_', { x: 0, y: -150, mode: 'relative', sync: true }),
			new Effect.Move('button-reserve_', { x: 0, y: -150, mode: 'relative', sync: true })	
		]);this._reserveOpen = false;
		}

	},
	
	toggleReserve: function(someEvent){
	if (Prototype.Browser.IE6){
		window.location= "https://www.phgsecure.com/IBE/bookingRedirect.ashx?propertyCode=CHICC&style=17"
		}else{
		someEvent.stop();
		var someDirection;
		if(this._reserveOpen){
			this._reserveOpen = false;
			someDirection = -150;
		}else{
			someDirection = 150;
			this._reserveOpen = true;
		}
		new Effect.Parallel([
			new Effect.Move('reservation_', { x: 0, y: someDirection, mode: 'relative', sync: true }),
			new Effect.Move('button-reserve_', { x: 0, y: someDirection, mode: 'relative', sync: true })
		]);
	 if (this._videoOpen){
		new Effect.Parallel([		
			new Effect.Move('video_', { x: 0, y: -443, mode: 'relative', sync: true }),
			new Effect.Move('button-tour_', { x: 0, y: -443, mode: 'relative', sync: true })	
		]);this._videoOpen = false;
		}


		}
	},
	
	validateInput: function(someEvent){
		var someInput = someEvent.findElement('input');
		var re = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
		if(!re.test(someInput)){
			this._calendar.close(someEvent);
			someInput.focus();
			alert("Please enter a valid date in the format MM/DD/YYYY");
		}
	},
	
	validateReservation: function(someEvent){
		someEvent.stop();
		var someDate = new Date();
		var someForm = document.forms.reservations;
		var re = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
		if(re.test($F('arrival-date_'))){
			var startDate = new Date(RegExp.input);
			someForm.arriveDate.value = RegExp.$2;
			someForm.arriveMonth.value = RegExp.$1;
			someForm.arriveYear.value = RegExp.$3;
		}else{
			$('arrival-date_').focus();
			//alert("Please enter a valid date in the format MM/DD/YYYY");
			//return false;
		}
		if(re.test($F('departure-date_'))){
			var endDate = new Date(RegExp.input);
			someForm.departDate.value = RegExp.$2;
			someForm.departMonth.value = RegExp.$1;
			someForm.departYear.value = RegExp.$3;
		}else{
			$('departure-date_').focus();
			//alert("Please enter a valid date in the format MM/DD/YYYY");
			//return false;
		}
		someDate.setTime(Math.abs(endDate.getTime() - startDate.getTime()));
		var diff = someDate.getTime();
		someForm.numberOfNights.value = Math.floor(diff/(1000 * 60 * 60 * 24));
		someForm.submit();
	},
	
	showControlButtons: function(someEvent){
		if(this._controlEnabled){
			this._controlEnabled = false;
			var someArray = new Array();
			$('control_').select('div.control').each(function(someElement){
				someArray.push(new Effect.Move(someElement, { x: 0, y: -21, mode: 'relative', sync: true }));
			});
			new Effect.Parallel(someArray, { queue: { position: 'end', scope: 'control' }, duration: 0.25 });
		}
	},
	
	hideControlButtons: function(someEvent){
		if(this.validateMouseLeave(someEvent, 'control_') && (this._controlEnabled == false)){
			var someArray = new Array();
			$('control_').select('div.control').each(function(someElement){
				someArray.push(new Effect.Move(someElement, { x: 0, y: 21, mode: 'relative', sync: true }));
			});
			new Effect.Parallel(someArray, { queue: { position: 'end', scope: 'control' }, duration: 0.25, afterFinish: function(){ myNavigation._controlEnabled = true } });
		}
	},
	
	changeBackground: function(someEvent){
		var someElement = someEvent.findElement();
		if(!someElement.hasClassName('selected')){
			var someInt = parseInt(someElement.innerHTML);
			var someId = "img_" + someInt;
			var someSelected = $('control_').select('.selected').first();
			var someSelectedId = "img_" + parseInt(someSelected.innerHTML);
			someSelected.removeClassName('selected');
			someElement.addClassName('selected');
			window.clearTimeout(this._timeOut);
			new Effect.Parallel([
				new Effect.Fade(someSelectedId, { sync: true }),
				new Effect.Appear(someId, { sync: true })
			]);
			this._slideActive = someInt;
			this._timeOut = this._slideTimer.delay(this._slideInterval);
		}
	},
	
	slideShow: function(){
		var slideCount = $('control_').select('div.control').size();
		var someSelected = $('control_').select('.selected').first();
		var someInt = parseInt(someSelected.innerHTML);
		var selectedButton, nextInt;
		if(someInt == slideCount){
			selectedButton = $('control_').select('div.control').first();
			nextInt = 1;
		}else{
			selectedButton = someSelected.next('div.control');
			nextInt = someInt + 1;
		}
		someSelected.removeClassName('selected');
		selectedButton.addClassName('selected');
		new Effect.Parallel([
			new Effect.Fade("img_" + someInt, { sync: true }),
			new Effect.Appear("img_" + nextInt, { sync: true })
		]);
		this._slideActive = nextInt;
		this._timeOut = this._slideTimer.delay(this._slideInterval);
	},
	
	populateEggData: function(){
		$('easter-eggs_').select('img').each(function(someImage){
			var someInt = parseInt(someImage.className.replace('egg-', ''));
			var someIndex = someInt - 1;
			var imgLeft = parseInt(someImage.getStyle('left').replace('px', ''));
			var imgTop = parseInt(someImage.getStyle('top').replace('px', ''));
			var imgRight = imgLeft + parseInt(someImage.getStyle('width').replace('px', ''));
			var imgBottom = imgTop + parseInt(someImage.getStyle('height').replace('px', ''));
			this._eggData[someIndex] = { x1: imgLeft, y1: imgTop, x2: imgRight, y2: imgBottom };
		}, this);
	},

	watchEggs: function(someEvent){
		var someIndex = this._slideActive - 1;
		if(this._eggData[someIndex] != undefined){
			if(this._videoOpen == false && this._reserveOpen == false){
			var targetClass = ".egg-" + (someIndex + 1);
			var target = $('easter-eggs_').select(targetClass).last();
			var rootX = $('root_').positionedOffset().left;
			var someX = someEvent.pointerX();
			var someY = someEvent.pointerY();
			if(this._eggVisible){
				if(this._eggEnabled){
					if((someX < (this._eggData[someIndex].x1 + rootX)) || (someY < this._eggData[someIndex].y1) || (someX > (this._eggData[someIndex].x2 + rootX)) || (someY > this._eggData[someIndex].y2)){
						this._eggEnabled = false;
						new Effect.Fade(target, { duration: 0.5, afterFinish: this.hideEggs.bind(this) });
					}
				}
			}else{
				if((someX > (this._eggData[someIndex].x1 + rootX)) && (someY > this._eggData[someIndex].y1) && (someX < (this._eggData[someIndex].x2 + rootX)) && (someY < this._eggData[someIndex].y2)){
					this._eggEnabled = false;
					this._eggVisible = true;
					window.clearTimeout(this._timeOut);
					$('overlay').show();
					$('easter-eggs_').show();
					new Effect.Appear(target, { duration: 0.5, afterFinish: this.showEggs.bind(this) });
				}
			}
			}
		}
	},
	
	showEggs: function(){
		this._eggEnabled = true;
	},
	
	hideEggs: function(){
		$('easter-eggs_').hide();
		$('overlay').hide();
		this._eggVisible = false;
		this._timeOut = this._slideTimer.delay(this._slideInterval);
		this._eggEnabled = true;
	},
	
	toggleContentArea: function(someEvent){
		someEvent.stop();
		var someElement = someEvent.findElement('a');
		var someTarget = $('copy_').down('div.container_');
		someElement.hide();
		if(someElement.identify() == "copy-close-button_"){
			$('copy-open-button_').show();
			new Effect.BlindUp(someTarget, { scaleContent: false, queue: { position: 'front', scope: 'content' } });
			if(this._copyWidth != this._subheadWidth){
				new Effect.Scale('copy_', (((this._subheadWidth - 50)/(this._copyWidth - 50)) * 100),{ scaleY: false, scaleContent: false, scaleMode: { originalWidth: (this._copyWidth - 50) }, queue: { position: 'end', scope: 'content' } });
			}
		}else{
			$('copy-close-button_').show();
			if(this._copyWidth != this._subheadWidth){
				new Effect.Scale('copy_', (((this._copyWidth - 50)/(this._subheadWidth - 50)) * 100),{ scaleY: false, scaleContent: false, scaleMode: { originalWidth: (this._subheadWidth - 50) }, queue: { position: 'front', scope: 'content' } });
			}
			new Effect.BlindDown(someTarget, { scaleContent: false, queue: { position: 'end', scope: 'content' } });
		}
	},
	
	openPhotoUpload: function(someEvent){
		someEvent.stop();
		window.open("/scrollgallery/index.php", "uploadWindow", "status=no,toolbar=no,location=no,menubar=no,directories=no,resizeable=no,scrollbars=no,width=770,height=421");
	}
});

function initNavigation(){ myNavigation = new Navigation(); if(typeof galleryphotos != "undefined") loadlightbox(); }
document.observe("dom:loaded", function(){
	initNavigation();
});