//|20070329|Ryan|Center property popup in browser screen tm 11416
//|20070330|Ryan|Make property popup display when clicking away from it tm 11416
//|20070507|allan|Center property popup on location name, or higher, if needed, to maximize the visible portion of the popup.

var bubble_click_event = true;

function GetBrowserHeight()
{	
	if (window.innerHeight) // all except Explorer
		h = window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		h = document.documentElement.clientHeight;
	else if (document.body) // other Explorers
		h = document.body.clientHeight;
		
	return h;
}

function GetScrollTop()
{
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	var dsoctop=document.all? iebody.scrollTop : pageYOffset
		
	return dsoctop;
}

/* displays a residence club/private residence location info
params:
info - the html element containing the location info
toggle - the html element that was clicked on
*/
function ShowLocation(info, toggle) {
	var close_top, browser_height=GetBrowserHeight(), scroll_top=GetScrollTop();

	HideLocations();
	$("location_close").style.display = "block";
	info.style.display = "block";
	//center popup on location name
	close_top = (GetPosition(toggle).y  - ((info.offsetHeight + $("location_close").offsetHeight)/2));
	//lift bottom of the popup to the bottom of the window to avoid needing to scroll to see bottom of popup
	if ((close_top + info.offsetHeight + $("location_close").offsetHeight) > (browser_height + scroll_top))

		close_top = browser_height + scroll_top - info.offsetHeight - $("location_close").offsetHeight - 10;
	//max functions to make sure top of popup is visible, and below the nav header so popup header is not confused with nav header
	$("location_close").style.top = Math.max(close_top,scroll_top + Math.max(0,$("nav_header").offsetHeight - scroll_top)) + "px";
	info.style.top = (GetPosition($("location_close")).y + $("location_close").offsetHeight) + "px";
	$("location_pointer").style.display = "block";
	$("location_pointer").style.top = Math.floor(GetPosition(toggle).y - (toggle.offsetHeight / 2)) + "px";
	$("location_bottom").style.display = "block";
	$("location_bottom").style.top = GetPosition(info).y + info.offsetHeight + "px";
}

/* hide all the location info elements */
function HideLocations () {
	var locations = $S(".location_info");
	locations.each(
		function (location) {
			if (location.style.display == "block") {
				location.style.display = "none";
				$("location_pointer").style.display = "none";
				$("location_bottom").style.display = "none";
				$("location_close").style.display = "none";
				return false;
			}
		}
	);
	return false;
}

/* show/hide the location info element
params:
i - index of the location info elements collection to toggle
toggle - the html element that was clicked on
*/
function ToggleLocation(i, toggle) {
	var info = $S(".location_info");
	if (info[i]) {
		if (info[i].style.display != "block") {
			ShowLocation(info[i], toggle);
		} else {
			HideLocations();
		}
	}
	return false;
}


/* initialize the locations functionality */
function InitLocations() {
	var locations = $("locations");
	if (locations) {
		var toggles = $S(".location_toggle"); // toggles collection
		var info = $S(".location_info"); // info collection
		var el;
		var txt;
		
		// create close button element;
		txt = document.createTextNode("Close");
		el = document.createElement("div");
		el.id = "location_close";
		el.appendChild(txt);
		el.onclick = HideLocations;
		el.onmouseover = function () {
			this.style.textDecoration = "underline";
		}
		el.onmouseout = function () {
			this.style.textDecoration = "none";
		}
		locations.appendChild(el);

		// create pointer;
		txt = document.createTextNode(" ");
		el = document.createElement("div");
		el.id = "location_pointer";
		el.appendChild(txt);
		locations.appendChild(el);

		// create bottom;
		txt = document.createTextNode(" ");
		el = document.createElement("div");
		el.id = "location_bottom";
		el.appendChild(txt);
		locations.appendChild(el);

		// add onclick event handler to each of the toggles
    toggles.each(
      function (toggle, index) {
				if (info[index]) {
					toggle.onclick = function() {
						this.blur();
						ToggleLocation(index, this);
						bubble_click_event = false;
						return false;
					};
				}
      }
    );
    
    info.each
    (
    	function (toggle, index)
    	{
    		toggle.onclick = function()
    		{
    			bubble_click_event = false;
    			return true;
    		}
    	}
    );

	}

	document.body.onclick = function () 
													{ 
														if( bubble_click_event ) 
														{
															HideLocations();
														} 
														bubble_click_event = true;
													};
}

Event.observe(window, 'load', InitLocations);
