/*function init () {
	initializeMover();
	flyout();
}*/

var amenitiesUserMax = 0;

function getPosition(obj) { //get something's position on pageload
		  objectTop = 0;
		  if (obj.offsetParent) {
			while (obj.offsetParent) {
				objectTop += obj.offsetTop
				obj = obj.offsetParent;
			}
		  }
		  else if (obj.y)
			objectTop += obj.y;
		  return (objectTop);
}

function initializeMover() { // this gets the positions of these things on pageload
	obstacle = document.getElementById('leftRailAds');
	if(document.getElementById('newHtlChgOrbEnabled')){
		newHtlChgOrbEnabled= document.getElementById('newHtlChgOrbEnabled').value;
	}
	mover = document.getElementById('floating');
	mover.style.position = "relative";
	moverPosY = getPosition( mover );
	stopper = false;
		if (document.getElementById('orbotStopper') != null) {stopper = document.getElementById('orbotStopper')};
		if (document.getElementById('footer') != null) {stopper = document.getElementById('footer')};
	
		if (document.getElementById('expandSearch') != null) {
	expandSearchLink = document.getElementById('expandSearch');
	expandSearchLink.style.visibility = "visible";
	}
	
	if (document.getElementById('selectAmenities') != null && amenitiesUserMax > 0) {
		limitChecks(amenChecks, amenitiesUserMax);
	}
	
	submitButtons = false;
	submitHover = false;
	if (document.getElementById('floating').getElementsByTagName("button")[0] != null) {
		var submitButtons = document.getElementById('floating').getElementsByTagName("button")[0];
		submitButtons.onmouseover = function () {submitHover = true;}
		submitButtons.onmouseout = function () {submitHover = false;}
	}

	if (stopper) { //if there's a footer that needs to stop the scrolling
	stopperPosY = getPosition (stopper);
	}

	if (obstacle != null) { //if there is an obstacle to jump...
	obstaclePosY = getPosition( obstacle );
	obstacleHeight = obstacle.offsetHeight;
		if (obstacleHeight <= 0) {obstacle = "";}
	moverHeight = (obstaclePosY - moverPosY);
			anchorlink = document.getElementById('changeSearchLink');
			if (anchorlink != null) {
			anchorlink.style.visibility = "hidden";
			}
	}
	
	else { //set this to false so the mover doesn't have to jump anything
	obstacle = "";
	obstaclePosY = 0;
	obstacleHeight = 0;
	moverHeight = mover.offsetHeight;
	}
	if(document.getElementById('newHtlChgOrbEnabled') && newHtlChgOrbEnabled == 'true'){
		popout = document.getElementById('optionsHook');
	}
	else
	{
	popout = document.getElementById('options'); //this can be removed if popout behavior not desired
	}
	glideMenu();
}

function glideMenu() {
	var timerscroll=null;
	var glide=null;
	var offset = 0; //these offsets are here to deal with odd rounding issues in ff and IE
	var timerspeed = 25; // Controls the speed of the animation
	var decrease = 0.09; // controls the deceleration
	mover.y = getPosition (mover) - moverPosY; //convert position into top property by subtracting the height of what's above
	closeOnScroll = false;

    scrollDist = 0; //find out how far the page has scrolled
			if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				scrollDist = document.body.scrollTop; //DOM compliant
			} 
			else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				scrollDist = document.documentElement.scrollTop; //IE6 standards compliant mode
			}
 	viewportHeight = 0; // available height
			if (self.innerHeight) { // all except Explorer
				viewportHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				viewportHeight = document.documentElement.clientHeight;
			}
			else if (document.body) { // other Explorers
				viewportHeight = document.body.clientHeight;
			}
	
	pageHeight = 0; //total body height
			if (document.body.scrollHeight) {
				pageHeight = document.body.scrollHeight;
			}
	
	if (!obstacle || (moverHeight < pageHeight - (obstaclePosY + obstacleHeight))) { //don't move orbot if there's no room between ads/footer
		
	if (stopper && (scrollDist > (stopperPosY - moverHeight )) ) { //if user scrolls to bottom of page (avoid footer overlap)
		mover.style.top = (stopperPosY - moverHeight - moverPosY ) + "px";
		mover.style.position = "relative";
	}
	
    else if ( (!obstacle && scrollDist >= moverPosY) || obstacle && (scrollDist >= (obstaclePosY + obstacleHeight))) { //if user scrolls below bottom of ad, or there isn't one
	
		if (viewportHeight >= moverHeight) { // the orbot can be fully visible w/o scrolling, keep it at window top
			endTop = (scrollDist - moverPosY); //keep orbot below ad
				closeOnScroll = true;
			
			if (is_ie6down) {
				glide = "true";
				offset = 5;
			}
			
			else {
				mover.style.position = "fixed";
				mover.style.top = "0";
			}
	 	}
		else { //the orbot is taller than the window height
			endTop = (obstaclePosY - moverPosY + obstacleHeight); //orbot goes below ad
		}
	}
	 
    else if (obstacle && scrollDist >= obstaclePosY) { //if user scrolls below top of ad
			
			endTop = (obstaclePosY - moverPosY + obstacleHeight); //orbot goes below ad
			offset = 5;
			mover.style.position = "relative";
			mover.style.top = endTop + "px";
	} 
	
    else { // user has not scrolled below top of ad
	endTop = 0; // orbot goes above ad
	offset = -5;
	mover.style.position = "relative";
	mover.style.top = endTop + "px";
	}
    
	if (mover.y < endTop) {
		clearTimeout(timerscroll);
	}
	
	else if (mover.y >= endTop) {
		clearTimeout(timerscroll);
	}
	
	if (glide) {
		mover.y = getPosition (mover) - moverPosY; //convert position into top property by subtracting the height of what's above
		distance = endTop - mover.y + offset; // where the orbot's top should be - what it is now
		if ( (distance > (viewportHeight - moverHeight)) || distance < (-viewportHeight) ) {//don't let the orbot get too far behind where it should be, by taking a shortcut when large distances are being traveled
			steps = distance;
		}
		else {
			steps = distance * decrease; // a small portion of distance
		}
		mover.y = Math.round(mover.y + steps); //new orbot top = current orbot top + a step toward what the top should be 
		mover.style.position = "relative";
		mover.style.top = mover.y + "px"; // set orbot top to new orbot position increment
	}

	timerscroll= setTimeout("glideMenu()",timerspeed); //repeat this function ~ every timerspeed millisecond
}
}

//this stuff hides/reveals optional fields

function flyout() {
	if(document.getElementById('newHtlChgOrbEnabled')){
		newHtlChgOrbEnabled= document.getElementById('newHtlChgOrbEnabled').value;
	}
	addOptLink=mover;
	if(document.getElementById('newHtlChgOrbEnabled') && newHtlChgOrbEnabled == 'true')
	{
		addOptLink=expandSearchLink;		
	}
	if(document.getElementById('chgBotBox') && document.getElementById('newHtlChgOrbEnabled')){
		document.getElementById('chgBotBox').style.border = "0px";
	}
	addOptLink.onclick = function () {
	if(!submitHover) {
		expandSearchLink.style.display = "none";

		if (moverHeight > viewportHeight) { //prevent orbot from locking if it's too tall
			mover.style.position = "relative";
			mover.style.top = endTop + "px";
		}
		
		orbot = document.getElementById('chgBotBox');
		if (orbot != null) {
			orbotHeight = orbot.offsetHeight;
			}
		
		
		
		//styling stuff
		popoutHeight = popout.offsetHeight;
		shortBorder = false;
		if (document.getElementById('flyoutOptionsHook') != null) {shortBorder = document.getElementById('flyoutOptionsHook');}
		
		if (popoutHeight <= (orbotHeight -3)) {
			
			if (shortBorder) {
				shortBorder.style.height = (orbotHeight - popoutHeight) + "px";
				shortBorder.style.top = popoutHeight + "px";
			}
		}
		else {
			if (shortBorder) {
				shortBorder.style.height = (popoutHeight - orbotHeight) + "px";
				shortBorder.style.top = (orbotHeight -3) + "px";
			}
		}
		flyoutHasBeenOpened = true;
		reopenFlyout();
	}


	//close flyout when user clicks outside the flyout
		flyoutCloseable = false;
		
		mover.onmouseover = function () {
			flyoutCloseable = false;
		}
		
		mover.onmouseout = function () {
			flyoutCloseable = true;
		}
		
		document.onclick = function () {
			if (flyoutCloseable) {closeFlyout();}
		}
		
		if(self.frames['resultsFrame']) {
			self.frames['resultsFrame'].document.onclick = function () {
				if (flyoutCloseable && flyoutHasBeenOpened) {closeFlyout();}	
			}
		}
		
	//close flyout when user scrolls
		window.onscroll = function () {
			if (flyoutHasBeenOpened) {closeFlyout();}
		}
	}

}
//these use left values to move the optional fields off the screen and out of view, and then into view.  this is used instead of visibility: hidden to address IE 5,6 not loading the background images on the optional fields until their second expansion.
function reopenFlyout() {
popout = document.getElementById('options');
orbot = document.getElementById('chgBotBox');
	if(!(document.getElementById('newHtlChgOrbEnabled')) || newHtlChgOrbEnabled != 'true'){
		popout.style.left = "160px"; //orbot and leftcol width -- change this if that changes
		orbot.style.backgroundPosition = "-100px 0";
	}
	if(document.getElementById('newHtlChgOrbEnabled') && newHtlChgOrbEnabled == 'true'){		
		additionalSearch = new Array('htlchgDates','htlchgLocationTypeBox','htlchgLocations','searchLocationType','chgBotBox','htlchgGuestInfo','htlchgbotForm','addSearchContentBox','addSearchAction');
		additionalSearchAddress = new Array('htlchgDates','htlchgLocations','htlchgGuestInfo','addSearchAction');
		htlNameChainModule=document.getElementById('hotelNameChain');
		topHideLink=document.getElementById('topHideOptions');
		hideadvancedOptions=document.getElementById('hideOptions');
		advancedOptions=document.getElementById('optionsHook');
		searchByComponent=document.getElementById('searchByTitle');
		htlChgBox=document.getElementById('htlchg');
		popout = document.getElementById('optionsHook');
		document.getElementById('chgBotBox').style.border = "0px";
		
		for (var i=0;i<additionalSearch.length;i++)
		{				
			if(!document.getElementById(additionalSearch[i]).className.match("Expanded")){
				document.getElementById(additionalSearch[i]).className=document.getElementById(additionalSearch[i]).className+'Expanded';
			}
		}
		if(document.getElementById('htlchgAddress').checked){
			for (var j=0;j<additionalSearchAddress.length;j++)
			{		

				if(!document.getElementById(additionalSearchAddress[j]).className.match("Adr"))
					document.getElementById(additionalSearchAddress[j]).className=document.getElementById(additionalSearchAddress[j]).className+'Adr';
			}

		}
		if(document.getElementById('htlchgSpecificHotelAddOpts').value!= "Hotel name contains..."){
			document.getElementById('htlchgSpecificHotel').value=document.getElementById('htlchgSpecificHotelAddOpts').value;
		}

		if(document.getElementById('htlchgHotelChainAddOpts').value!= "all hotel companies"){
			document.getElementById('htlchgHotelChain').value=document.getElementById('htlchgHotelChainAddOpts').value;
		}

		document.getElementById('collapsedOrbotSubmitted').value="false";

		htlNameChainModule.style.display = "none";		
		topHideLink.style.display = "block";		
		hideadvancedOptions.style.display = "block";		
		advancedOptions.style.display = "block";		
		searchByComponent.style.display = "block";		
		htlChgBox.style.width = "660px";		
	}	
	//this addresses the IE bug of not letting dhtml overlap select boxes
	neighborhoodSelector = false;
		if (document.getElementById('hoodChooser') != null) {neighborhoodSelector = document.getElementById('hoodChooser');}
		if (document.frames && document.frames['resultsFrame'] && document.frames['resultsFrame'].window != null) {
			if (document.frames['resultsFrame'].window.document.getElementById('hoodChooser') != null) {
				neighborhoodSelector = document.frames['resultsFrame'].window.document.getElementById('hoodChooser');
			}
		}
     expandSearchLink.style.display = "none";
	if (neighborhoodSelector) {neighborhoodSelector.style.visibility = "hidden";}
}
	

function closeFlyout() {
	if(!(document.getElementById('newHtlChgOrbEnabled')) || newHtlChgOrbEnabled != 'true'){
		popout.style.left = "-1600px";
		orbot.style.backgroundPosition = "100% 0";
	}
	if(document.getElementById('newHtlChgOrbEnabled') && newHtlChgOrbEnabled == 'true'){			
		for (i=0;i<additionalSearch.length;i++)
		{			
			document.getElementById(additionalSearch[i]).className=document.getElementById(additionalSearch[i]).className.replace(/Expanded/,'');			
		}
		if(document.getElementById('htlchgAddress').checked){		
			for (j=0;j<additionalSearchAddress.length;j++)
			{			
					document.getElementById(additionalSearchAddress[j]).className=document.getElementById(additionalSearchAddress[j]).className.replace(/Adr/,'');
			}
		}
		if(document.getElementById('htlchgSpecificHotel').value != "Hotel name contains..."){
			document.getElementById('htlchgSpecificHotelAddOpts').value=document.getElementById('htlchgSpecificHotel').value;
		}


		if(document.getElementById('htlchgHotelChain').value!= "all hotel companies"){
			document.getElementById('htlchgHotelChainAddOpts').value=document.getElementById('htlchgHotelChain').value;
		}
		
		document.getElementById('collapsedOrbotSubmitted').value="true";
		
		topHideLink.style.display = "none";
		advancedOptions.style.display = "none";
		searchByComponent.style.display = "none";
		htlNameChainModule.style.display = "block";
		hideadvancedOptions.style.display = "none";
		htlChgBox.style.width = "140px";
		orbot.style.backgroundPosition = "100% 0";
		orbot.style.width = "160px";		
	}	
	expandSearchLink.style.display = "block";
	if (neighborhoodSelector) {neighborhoodSelector.style.visibility = "visible";}
}

function cityOrAddressSearch() {	
	additionalSearchAddress = new Array('htlchgDates','htlchgLocations','htlchgGuestInfo','addSearchAction');
	if(document.getElementById('htlchgAddress').checked ){
		for (i=0;i<additionalSearchAddress.length;i++)
		{		
			if(document.getElementById(additionalSearchAddress[i]).className.match("Expanded") && !document.getElementById(additionalSearchAddress[i]).className.match("Adr"))
				document.getElementById(additionalSearchAddress[i]).className=document.getElementById(additionalSearchAddress[i]).className+'Adr';
		}

	}
	else if(document.getElementById('htlchgCityPOI').checked){
		for (j=0;j<additionalSearchAddress.length;j++)
			{			
					document.getElementById(additionalSearchAddress[j]).className=document.getElementById(additionalSearchAddress[j]).className.replace(/Adr/,'');
			}
	}	
}

