// Copyright (C) 2003-2007 AvantLogic Corporation: http://www.mapsalive.com

var maClient = new Object();

maClient.markerId = 0;
maClient.tid = 1;
maClient.guid = (new Date()).getTime()+""+Math.floor((Math.random()*8999)+1000);
maClient.ie = ((navigator.appVersion.indexOf("MSIE") != -1) && (navigator.userAgent.search(/Opera/) == -1)) ? 1 : 0;
maClient.macintosh = navigator.userAgent.toLowerCase().indexOf("macintosh") != -1;
maClient.path = "";
maClient.preview = false;
maClient.page = false;
maClient.okToShowPopup = false;

maClient.swfPath = function()
{
	return maTour.editMode ? "../Tour/" + maTour.version + "/" : maClient.path;
}

function maGetQueryStringArg(arg)
{
	var q = document.location.search.toLowerCase();
	if (q && q.length > 1)
	{
		var i = q.indexOf(arg.toLowerCase() + "=");
		if (i > -1)
		{
			var j = (q.indexOf("&", i) > -1) ? q.indexOf("&", i) : q.length;
			return q.substring(q.indexOf("=", i) + 1, j);
		}
	}
	return "";
}

function maAddLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
		window.addEventListener('load', fn, false);
	else if (typeof document.addEventListener != 'undefined')
		document.addEventListener('load', fn, false);
	else if (typeof window.attachEvent != 'undefined')
		window.attachEvent('onload', fn);
}

function maAttachEventListener(target, eventType, functionRef)
{
	if (typeof target.addEventListener != "undefined")
		target.addEventListener(eventType, functionRef, false);
	else
		target.attachEvent("on" + eventType, functionRef);
	return true; 
}

maClient.Flash = function()
{
}

maClient.Flash.prototype.init = function(swf, id, mfn, pid, popups)
{
	this.id = id;
	
	this.params = new Object();
	this.addParam('bgcolor', '');
	this.addParam('quality', 'high');
	this.addParam("swliveconnect", "true");
	this.addParam("wmode", "transparent");
	
	maClient.isMacFirefox = maClient.macintosh && maClient.uaIs('firefox');
	
	this.flashVars = new Object();
	this.addFlashVar("fv_blink", maClient.isMacFirefox && popups ? 0 : 1);
	this.addFlashVar("fv_clipId", id);
	this.addFlashVar("fv_guid", maClient.guid);
	this.addFlashVar("fv_ie", maClient.ie);
	this.addFlashVar("fv_pageId", pid);
	this.addFlashVar("fv_swf", maClient.path + swf); 
	this.addFlashVar("fv_themeId", maClient.tid);
	this.addFlashVar("fv_stageW", maTour.stageW);
	this.addFlashVar("fv_stageH", maTour.stageH);
}

maClient.Flash.prototype.addParam = function(name, value)
{
	this.params[name] = value;
}

maClient.Flash.prototype.addFlashVar = function(name, value)
{
	this.flashVars[name] = value;
}

maClient.flashPresent = function()
{
    return maClient.getPlayerVersion().major >= 7;
}

maClient.showFlashMsg = function()
{
	var e = document.createElement("div");
	e.innerHTML = "<b>This page needs Flash 7 or higher</b><hr/>" +
		"Click the button below to install the latest version of Flash Player.<br/>" +
		"After installation, close the Adobe window and refresh this page.";
	var s = e.style;
	s.position = "absolute";
	s.zIndex = 1000;
	s.left = "30px";
	s.top = "30px";
	s.backgroundColor = "#f3f3f3";
	s.border = "1px solid #777777";
	s.color = "#000055";
	s.fontSize = "11px";
	s.padding = "8px";
	s.textAlign = "center";
	s.width = "230px";
	var map = document.getElementById('maMap');
	if (!map)
		return;
	map.appendChild(e);
	var d = document.createElement("div");
	d.style.padding = "12px 0px 4px 0px";
	d.style.textAlign = "center";
	e.appendChild(d);
	var a = document.createElement("a");
	a.href = "http://macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
	a.target = "_blank";
	d.appendChild(a);
	var i = document.createElement("img");
	i.src = maTour.editMode ? "../Runtime/get_flash_player.gif" : maClient.path + "0020_" + maTour.version + ".gif";
	i.style.border = 0;
	a.appendChild(i);
}

maClient.Flash.prototype.getParamTags = function()
{
	var paramTags = "";
	for (var key in this.params)
		paramTags += '<param name="' + key + '" value="' + this.params[key] + '" />';
	return paramTags;
}

maClient.Flash.prototype.getFlashVars = function()
{
	var pair = new Array();
	for (var key in this.flashVars)
		pair.push(key + "=" + this.flashVars[key]);
	return pair.join("&");
}

maClient.Flash.prototype.getHTML = function()
{
	var fsid = 'fs' + this.id;
	var movidId = this.id == "mapViewer" ? "0001_" : "0000_";
	var movie = maClient.swfPath() + movidId + maTour.version + ".swf";
    var html = "";
    
    if (maClient.flashPresent())
    {
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) // netscape plugin architecture
		{
			html += '<embed type="application/x-shockwave-flash" src="' + movie + '"';
			html += ' width="'+ maTour.stageW + '" height="' + maTour.stageH + '"';
			html += ' id="' + fsid + '" name="' + fsid + '"';
			for (var key in this.params)
				html += ' '+ key + '="' + this.params[key] + '"';
			html += ' flashvars="'+ this.getFlashVars() +'"';
			html += '></embed>';
		} 
		else // IE
		{
	 		html += '<script type="text/vbscript" language="VBScript">';
	 		html += 'Sub ' + fsid + '_FSCommand(ByVal command, ByVal args)';
	 		html += 'call ' + this.id + '.fs(command, args)';
	 		html += 'end sub';
	 		html += '</script>';
			html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'; 
			html += ' width="' + maTour.stageW + '" height="' + maTour.stageH +'"';
			html += ' id="' + fsid +'">';
			html += '<param name="movie" value="' + movie + '" />';
			html += this.getParamTags();
			html += '<param name="flashvars" value="'+ this.getFlashVars() +'" />';
			html += '</object>';
		}
    }
    else
    {
	 	var url = maTour.editMode ? "ImageRenderer.ashx?type=map" : this.getAttribute('mapFileName');
		var s = document.getElementById('maMap').style;
		s.backgroundImage = "url(" + url + ")";
		s.backgroundRepeat = "no-repeat";
		s.width = maTour.stageW + "px";
		s.height = maTour.stageH + "px";
	}
    return html;
}

maClient.Flash.prototype.write = function(elementId)
{
	if (elementId)
		document.getElementById(elementId).innerHTML = this.getHTML();
	else
		document.write(this.getHTML());
}

maClient.Flash.prototype.editMarker = function(id)
{
	maEditMarker(id);
}

maClient.Flash.prototype.editSlide = function(id)
{
	maEditSlide(id);
}

maClient.goToPage = function(url)
{
	if (maClient.preview)
	{
		var aid = maGetQueryStringArg("aid");
		var pn = url.substr(4, url.indexOf('.') - 4);
		window.location = 'TourPreview.aspx?aid=' + aid + '&pn=' + pn;
	}
	else
	{
		if (maClient.page)
			window.location = url;
		else
			alert("You cannot use menus with frameless tours.\nYou must provide your own page navigation.");
	}
}

maClient.Flash.prototype.goToPage = function(url)
{
	if (url == null)
		return;
	var pathname = location.pathname;
	var pathsegs = pathname.split('/');
	var pathend = pathsegs[pathsegs.length- 1];
	if (pathend == url)
		return;
	maClient.goToPage(url);
}

maClient.Flash.prototype.hideMarkers = function(idList)
{
	var id = idList.split(",");
	for (var i = 0; i < id.length; i++)
		maShowMarkerThumbAsHidden(id[i]);
}

maClient.Flash.prototype.linkToUrl = function(url)
{
	if (url == null)
		return;
	var popup = url.substring(0,1) == "1";
	url = url.substring(1);
	if (popup)
	{
		var wnd = window.open(url, "_blank");
		if (wnd == null)
		{
			if (confirm("A popup blocker prevented the following page from being opened in a new window:\n\n" + url + "\n\nPress OK to open the page in the current window."))
				location = url;
		}
	}
	else
		location = url;
}

maClient.Flash.prototype.markerCoordsChanged = function(coords)
{
	maGetElementByPageId("MarkerCoords").value = coords;
	maChangeDetected();
}

maClient.Flash.prototype.zoomStateChanged = function(state)
{
	maGetElementByPageId("ZoomState").value = state;
	maChangeDetected();
}

maClient.Flash.prototype.markerSelected = function (id)
{
	maShowSelectedMarkerThumb(id);
	maShowMarkerInSlideList(id);
}

maClient.Flash.prototype.execJavascript = function (script)
{
	try
	{
		maClient.hideTooltip();
		eval(script);
	}
	catch(error)
	{
		alert('MapsAlive detected an error in the following\n' +
			'JavaScript that was provided for this marker:\n\n' +
			script + '\n\n' + error.message +
			'\n\nPlease report this problem to the author\n' +
			'of this tour so that they can correct it.');
	}
}

maClient.Flash.prototype.markerDeselected = function (id)
{
	maDeselectSelectedMarkerThumb();
}

maClient.Flash.prototype.viewChanged = function (id,x,y,w,h,mouseX,mouseY)
{
   	okToShowPopup = true;
   	maTour.marker.id = parseInt(id);
   	maTour.marker.x = x;
   	maTour.marker.y = y;
   	maTour.marker.w = w;
   	maTour.marker.h = h;
    maClient.showSlide(mouseX, mouseY);
}

maClient.Flash.prototype.showTooltip = function (text,x,y)
{
	maClient.showTooltip('mapViewer', parseInt(x), parseInt(y), text);
}

maClient.Flash.prototype.hideTooltip = function ()
{
	maClient.hideTooltip();
}

maClient.Flash.prototype.markerMouseOut = function ()
{
    if (maTour.usesPopup && !maClient.fixedPopupPersist())
		maClient.hidePopup();
}

maClient.Flash.prototype.fs = function (command, argstring)
{
	var args = argstring.split('\2');
	switch(command)
	{
		case "editMarker":
			this.editMarker(args[0]);
			break;
		case "editSlide":
			this.editSlide(args[0]);
			break;
		case "execJavascript":
			this.execJavascript(args[0]);
			break;
		case "goToPage":
			this.goToPage(args[0]);
			break;
		case "hideMarkers":
			this.hideMarkers(args[0]);
			break;
		case "hideTooltip":
			this.hideTooltip();
			break;
		case "linkToUrl":
			this.linkToUrl(args[0]);
			break;
		case "markerCoordsChanged":
			this.markerCoordsChanged(args[0]);
			break;
		case "markerDeselected":
			this.markerDeselected(args[0]);
			break;
		case "markerMouseOut":
			this.markerMouseOut();
			break;
		case "markerSelected":
			this.markerSelected(args[0]);
			break;
		case "showTooltip":
			this.showTooltip(args[0],args[1],args[2]);
			break;
		case "zoomStateChanged":
			this.zoomStateChanged(args[0]);
			break;
		case "viewChanged":
			this.viewChanged(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
			break;
	}
}

maClient.Flash.prototype.invokeGateway = function(method,args,position)
{
	var gatewayTagId = this.id + "Gateway";
	var newGatewayTag = document.createElement("div");
	newGatewayTag.innerHTML =
		"<embed " + 
		"id='" + gatewayTagId + "' " +
		"style='position:absolute' " +
		"src='" + maClient.swfPath() + "0002_" + maTour.version + ".swf' " +
		"FlashVars='" +
		"movie=" + this.id + maClient.guid +
		"&method=" + method +
		"&args=" + args.join(',') + "' " +
		"width='1' height='1' wmode='transparent' type='application/x-shockwave-flash'>" +
		"</embed>";
	
	var parentTagId = this.id == "mapViewer" ? "maLayout" : "Gateway" + position;
	var previousGatewayTag = document.getElementById(gatewayTagId);
	var parent = document.getElementById(parentTagId);
	if (previousGatewayTag)
		previousGatewayTag.parentNode.removeChild(previousGatewayTag);
	parent.insertBefore(newGatewayTag.firstChild, parent.firstChild);
}

maClient.getPlayerVersion = function()
{
	var playerVersion = new maClient.PlayerVersion([0,0,0]);
	if (navigator.plugins && navigator.mimeTypes.length)
	{
		var plugin = navigator.plugins["Shockwave Flash"];
		if (plugin && plugin.description)
		{
			playerVersion = new maClient.PlayerVersion(plugin.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}
	else
	{
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try
		{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}
		catch(e)
		{
			try
			{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				playerVersion = new maClient.PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always"; // throws if player version < 6.0.47
			}
			catch(e)
			{
				if (playerVersion.major == 6)
				{
					return playerVersion;
				}
			}
			try
			{
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			}
			catch(e)
			{}
		}
		if (axo != null)
		{
			playerVersion = new maClient.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return playerVersion;
}

maClient.PlayerVersion = function(arrVersion)
{
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}

maClient.selectMarkerAndShowView = function(id)
{
    if (id == -1)
		return;
    if (typeof mapViewer == "undefined")
    {
		maTour.marker.id = parseInt(id);
   		maTour.marker.x = 0;
   		maTour.marker.y = 0;
   		maTour.marker.w = 0;
	   	maTour.marker.h = 0;
		maClient.showSlide(0, 0); // Page has slide, but no map.
	}
    else
    {
	    mapViewer.selectMarkerAndShowView(id);
	}
}

maClient.setText = function(e, index)
{
    var utx = unescape(maTour.tp[maTour.tpi['t' + index + '_' + maClient.markerId + '_' + maClient.tid]]);
    if (e != null)
    {
		try
		{
 		    if (index == 1 && utx.substring(0,1) == '_')
 		    {
 				utx = utx.substring(1);
 			}
 			if (index == 2 && utx.length == 0)
			{
				e.style.display = 'none';
			}	
 			else
 			{
 				e.style.display = 'block';
 				e.innerHTML = utx;
 			}
		}
		catch(error)
		{
			e.innerHTML='The browser reported an error: ' + error.message;
		}
	}
}

maClient.showPopup = function(show)
{
	if (show)
	{
		maClient.loadingPanel.style.visibility = "hidden";
		if (!okToShowPopup)
			return;
	}
	
	if (show)
		maClient.startRefresh();
	else
		maClient.stopRefresh();

	var visibility = show ? "visible" : "hidden";
	maClient.popupPanel.style.visibility = visibility;
	if (maTour.popup.showArrow)
		maClient.arrowPanel.style.visibility = visibility;
	if (maClient.slideImage)
		maClient.slideImage.style.visibility = visibility;
	if (maClient.closePanel)
		maClient.closePanel.style.visibility = visibility;
}

maClient.startRefresh = function()
{
	maClient.refreshCycle = maClient.macintosh ? 60 : 5;
	maClient.stopRefresh();
	maClient.refreshIntervalId = setInterval(maClient.refreshPopup, 50);
}

maClient.stopRefresh = function()
{
	if (typeof maClient.refreshIntervalId != "undefined")
		clearInterval(maClient.refreshIntervalId);
}

maClient.refreshPopup = function()
{
	maClient.refreshCycle--;
	maClient.popupPanel.style.backgroundColor = maClient.refreshCycle % 2 == 0 ? maTour.popup.backgroundColor : maTour.popup.backgroundColorAlt;
	if (maClient.isMacFirefox && maTour.popup.showArrow)
		maClient.arrowPanel.style.color = maClient.refreshCycle % 2 == 0 ? maTour.popup.backgroundColor : maTour.popup.backgroundColorAlt;
	maClient.setSlideText();
	if (maClient.refreshCycle == 0)
	{
		maClient.stopRefresh();
		if (maClient.macintosh)
			maClient.refreshIntervalId = setInterval(maClient.refreshPopup, 500);
	}
}

maClient.fixedPopupPersist = function()
{
	return maTour.popup.fixed && maTour.popup.persist;
}

maClient.okToShowSlide = function()
{
	if (maTour.usesPopup && !maTour.popup.initialized && !maClient.fixedPopupPersist() && maClient.eid.length == 0)
	{
		// Ignore initial call from movie so that popup does not appear when page loads.
		maTour.popup.initialized = true;
		return false;
	}
	return true;
}

maClient.populateSlideSelector = function()
{
	// If this page has a dropdown list, keep the selected item in the list in sync with the selected view.
	var dropdownList = document.getElementById('maSlideDropdown');
	if (dropdownList)
	{
	    for (var option = 0; option < dropdownList.options.length; option++)
	    {
	        if (dropdownList.options[option].value == maClient.markerId)
	        {
	            dropdownList.options[option].selected = true;
	            dropdownList.selectedIndex = dropdownList[option].index;
	            break;
	        }
	    } 
	}
}

maClient.setLeftMenuItem = function()
{
	// If this page is showing slide names in the left menu, deselect the currently selected slide name 
	// and select the slide name corresponding to the the selected view.
	var slideNamesInMenu = document.getElementById('maSlideNamesInMenu');
	if (slideNamesInMenu)
	{
	    var newSelectedViewId = document.getElementById("ma" + maClient.markerId);
	    if (newSelectedViewId)
	    {
	        var oldSelectedViewId = document.getElementById("ma" + maTour.selectedViewId);
	        if (oldSelectedViewId) 
	            oldSelectedViewId.className = '';
	        newSelectedViewId.className = "currentSlide";
	        maTour.selectedViewId = maClient.markerId; // save the new selected slide for next time
	    }
	}
}

maClient.setPopupSize = function()
{
	var bordersW = parseInt(maTour.layoutMarginLeft) + parseInt(maTour.layoutMarginRight);
	var bordersH = parseInt(maTour.layoutMarginTop) + parseInt(maTour.layoutMarginBottom);
	var contentHeight;
	var popupWidth;
	var textAreaHeight;
	var textHeight;
	var textWidth;

	switch (maTour.layout)
	{
		case "HII":
			maClient.setActualPopupWidth(parseInt(maTour.image.width) + bordersW);
			maClient.setActualPopupHeight(parseInt(maTour.image.height) + bordersH);
			return;
		
		case "HTT":
		case "HIITT":
		case "HTTII":
			var nonTextHeight;
			var isHTT = maTour.layout == "HTT";
			popupWidth = isHTT ? maTour.popup.maxW : parseInt(maTour.image.width) + bordersW;
			maClient.setActualPopupWidth(popupWidth);
			textWidth = maTour.popup.actualW - bordersW;
			textHeight = maClient.getTextHeight(textWidth);
			nonTextHeight = isHTT ? 0 : parseInt(maTour.image.height) + parseInt(maTour.layoutSpacingH);
			contentHeight =	nonTextHeight + textHeight + bordersH;
			textAreaHeight = maTour.popup.maxH - (nonTextHeight + bordersH);
			break;
		
		case "VIITT":
		case "VTTII":
			popupWidth = maTour.popup.maxW - maTour.image.deltaW;
			maClient.setActualPopupWidth(popupWidth);
			textWidth = maTour.popup.actualW - maTour.image.width - parseInt(maTour.layoutSpacingV) - bordersW;
			textHeight = maClient.getTextHeight(textWidth);
			contentHeight = Math.max(maTour.image.height, textHeight) + bordersH;
			textAreaHeight = maTour.textAreaH;
			break;
	}
		
	var extraHeight = maTour.popup.maxH - contentHeight;
	if (extraHeight > 0)
	{
		maClient.setActualPopupHeight(maTour.popup.maxH - extraHeight);
		maClient.textArea.style.height = textHeight + "px";
	}
	else
	{
		maClient.setActualPopupHeight(maTour.popup.maxH);
		maClient.textArea.style.height = textAreaHeight + "px";
	}
	maClient.textArea.style.overflow = maTour.popup.persist ? "auto" : "hidden";
}

maClient.getTextHeight = function(textWidth)
{
	// Get the text height for this width by forcing the offset height to be calculated.
	maClient.textArea.style.width = textWidth + "px";
	maClient.textArea.style.height = "auto";
	return maClient.textArea.offsetHeight;
}

maClient.setActualPopupWidth = function(w)
{
	maTour.popup.actualW = Math.max(w, maTour.popup.minW);
	maClient.popupPanel.style.width = maTour.popup.actualW + "px";
}

maClient.setActualPopupHeight = function(h)
{
	maTour.popup.actualH = Math.max(h, maTour.popup.minH);;
	maClient.popupPanel.style.height = maTour.popup.actualH + "px";
}

maClient.adjustTextAreaSize = function()
{
	switch (maTour.layout)
	{
		case "HMMTI":
		case "HTIMM":
		case "HITMM":
		case "HMMIT":
		case "VIITT":
		case "VTTII":
			maClient.textArea.style.width = maTour.textAreaW + maTour.image.deltaW + "px";
			if (maClient.textArea.style.position == "absolute")
				maClient.textArea.style.left = maClient.textAreaLeft - maTour.image.deltaW + "px";
			if (maTour.layout == "HMMTI" || maTour.layout == "HTIMM" || maTour.layout == "VTTII")
			{
				var pb = document.getElementById("maAbsBlock");
				pb.style.width = maTour.image.width + "px";
				pb.style.left = maTour.layoutMarginLeft + maTour.textAreaW + maTour.image.deltaW + maTour.layoutSpacingV + "px";
			}
			maClient.textArea.style.height = maTour.textAreaH + "px";
			break;
			
		case "VMMIT":
		case "VMMTI":
		case "VITMM":
		case "VTIMM":
		case "HIITT":
		case "HTTII":
			maClient.textArea.style.width = maTour.textAreaW + "px";
			// Force the text area offsetHeight to be recalculated for the width.
			maClient.textArea.style.height = "auto";
			if (maClient.textArea.offsetHeight >= maTour.textAreaH + maTour.image.deltaH)
			{
				maClient.textArea.style.height = maTour.textAreaH + maTour.image.deltaH + "px";
			}
			break;
			
		default:
			maClient.textArea.style.width = maTour.textAreaW + "px";
			maClient.textArea.style.height = maTour.textAreaH + "px";
			break;
	}
	maClient.textArea.style.overflow = "auto";
}

maClient.showImageLoadingIndicator = function(markerChanged, mouseX, mouseY)
{
	if (!markerChanged || maTour.layout == "HTT")
		return;
	
	var loaded = false;
	for (var i in maTour.images)
	{
		if (maTour.images[i] == maClient.markerId)
		{
			loaded = true;
			break;
		}
	}
	
	if (!loaded)
	{
		var l = maTour.mapLocationRelative;
		var s = maClient.loadingPanel.style;
		s.left = (l.x + parseInt(mouseX) - 20) + 'px';
		s.top = (l.y + parseInt(mouseY) - 20) + 'px';  
		s.visibility = "visible";
	}
}

maClient.setSlideImage = function(markerChanged, mouseX, mouseY)
{
	if (maTour.usesPopup)
	{
		maClient.showImageLoadingIndicator(markerChanged, mouseX, mouseY);
		maClient.showPopup(false);

		if (maTour.layout == "HTT")
		{
			maClient.showPopup(true);
		}
		else
		{
			if (markerChanged)
			{
				maClient.slideImage.onload = maImageLoaded;
				maClient.slideImage.src = maTour.image.src;
			}
			else
			{
				maClient.showPopup(true);
			}
		}
	}
	else
	{
		if (markerChanged && maClient.slideImage)
		{
			maClient.slideImage.src = maTour.image.src;
		}
	}
}

maClient.setPopupLocation = function()
{
	if (maTour.popup.fixed)
		return;
	
	var l = maClient.popupLocation();
	var s = maClient.popupPanel.style;
	s.left = l.x + 'px';
	s.top = l.y + 'px';

	if (maClient.closePanel)
	{
		s = maClient.closePanel.style;
		s.top = (l.y + maTour.popup.borderWidth + 2) + 'px';
		s.left = (l.x + maTour.popup.actualW - 12 + (maTour.popup.borderWidth - 2)) + 'px';
	}
}

maClient.setSlideText = function()
{
	if (maTour.showSlideTitle)
		maClient.setText(maClient.slideTitle, 1);
	maClient.setText(maClient.slideText, 2);
}

maClient.getSlideImageData = function()
{
	var data = maTour.ip[maTour.ipi['i_' + maClient.markerId + '_' + maClient.tid]].split(',');
	maTour.image.src = maClient.path + data[0];
	maTour.image.width = data[1];
	maTour.image.height = data[2];
	maTour.image.deltaW = maTour.imageAreaW - maTour.image.width;
	maTour.image.deltaH = maTour.imageAreaH - maTour.image.height;
	if (maClient.slideImage)
	{
		maClient.slideImage.style.width = maTour.image.width + "px";
		maClient.slideImage.style.height = maTour.image.height + "px";
	}
}

maClient.showSlide = function(mouseX, mouseY)
{
	if (!maClient.okToShowSlide())
		return;
    
    var markerChanged = maTour.marker.id != maClient.markerId; 
		
	if (markerChanged)
	{
		maClient.markerId = maTour.marker.id;
		maClient.populateSlideSelector();
		maClient.setLeftMenuItem();
		maClient.setSlideText();
		maClient.getSlideImageData();
	}
		
	if (maTour.usesPopup)
	{
		maClient.setPopupSize();
		maClient.setPopupLocation();
	}
	else if (maClient.textArea)
	{
		maClient.adjustTextAreaSize();
	}
	
	maClient.setSlideImage(markerChanged, mouseX, mouseY);
}

maClient.setArrowPanel = function(panel, type, size, x, y)
{
	if (!maTour.popup.showArrow)
		return;
	
	var id;
	switch (type)
	{
		case "left1":	id = 30; break;
		case "left2":	id = 31; break;
		case "right1":	id = 32; break;
		case "right2":	id = 33; break;
		case "up1":		id = 34; break;
		case "up2":		id = 35; break;
		case "down1":	id = 36; break;
		case "down2":	id = 37; break;
	}
	panel.style.width = size + "px";
	panel.style.height = size + "px";
	panel.setAttribute("src", maClient.path + "00" + id + "_" + maTour.version + ".gif");
	var s = panel.style;
	s.left = x + 'px';
	s.top = y + 'px';
	s.display = "block";
}

maClient.positionPopupLeftOrRight = function(side, popupOffsetX, arrowSize, scroll)
{
	var arrow = side == "right" ? "left" : "right";
	var arrow1 = arrow + "1";
	var arrow2 = arrow + "2";
	var arrowType = arrow1;           
    var border = parseInt(maTour.popup.borderWidth);
	var middle = Math.floor(maTour.popup.actualH / 2);
	var arrowOffset = middle + border;
	var borderTweak = border > 0 ? 1 : 0;

	if (arrowSize == 0)
	{
		arrowOffset = 0;
	}
	else if (middle < arrowSize)
	{
		arrowOffset = border - borderTweak;
	} 
	else if (arrowOffset > maTour.marker.rectTop.h || maTour.popup.actualH > maTour.marker.rectRight.h)
	{
		arrowOffset -= arrowOffset - maTour.marker.rectTop.h + 1;
		if (arrowOffset < border - 1)
			arrowOffset = border - (border > 0 ? 1 : 0);
	}
	else if (arrowOffset > maTour.marker.rectBottom.h)
	{
		arrowType = arrow2;         
		arrowOffset += arrowOffset - maTour.marker.rectBottom.h + 1;
		if (arrowOffset > maTour.popup.actualH)
			arrowOffset = maTour.popup.actualH + border + (border > 0 ? 1 : 0);
	}

    var popupX;
    var popupY;
    var arrowX;
    var arrowY;
   
    if (side == "right")
    {
		popupX = maTour.marker.absX + popupOffsetX;
		arrowX = popupX - arrowSize + border;
	}
	else
	{
		popupX = maTour.marker.absX - (maTour.popup.actualW + (border * 2) + popupOffsetX);
		arrowX = popupX + maTour.popup.actualW + border;
	}
    
    popupY = maTour.marker.absY - arrowOffset;
	arrowY = maTour.marker.absY - (arrowType == arrow1 ? 0 : arrowSize);
    
	maClient.setArrowPanel(maClient.arrowPanel, arrowType, arrowSize, arrowX, arrowY);
    
   	var pt = new Object;
   	pt.x = popupX;
   	pt.y = popupY;
	return pt;
}

maClient.positionPopupUpOrDown = function(side, popupOffsetY, arrowSize, scroll)
{
	var arrow = side == "bottom" ? "up" : "down";
	var arrow1 = arrow + "1";
	var arrow2 = arrow + "2";
	var arrowType = arrow1;           
    var border = parseInt(maTour.popup.borderWidth);
    var middle = Math.floor(maTour.popup.actualW / 2);
	var arrowOffset = middle + border;
	var borderTweak = border > 0 ? 1 : 0;

	if (middle < arrowSize)
	{
		arrowOffset = border - borderTweak;
	} 
	else if (arrowOffset > maTour.marker.rectLeft.w || maTour.popup.actualW > maTour.marker.rectBottom.w)
	{
		arrowOffset -= arrowOffset - maTour.marker.rectLeft.w + 1;
		if (arrowOffset < border - 1)
			arrowOffset = border - borderTweak;
	}
	else if (arrowOffset > maTour.marker.rectRight.w)
	{
		arrowType = arrow2;         
		arrowOffset += arrowOffset - maTour.marker.rectRight.w + 1;
		if (arrowOffset > maTour.popup.actualW)
			arrowOffset = maTour.popup.actualW + border + borderTweak;
	}

    var popupX;
    var popupY;
    var arrowX;
    var arrowY;
   
    if (side == "bottom")
    {
		popupY = maTour.marker.absY + popupOffsetY;
		arrowY = popupY - arrowSize + border;
	}
	else
	{
		popupY = maTour.marker.absY - (maTour.popup.actualH + (border * 2) + popupOffsetY);
		arrowY = popupY + maTour.popup.actualH + border;
	}
    
    popupX = maTour.marker.absX - arrowOffset;
	arrowX = maTour.marker.absX - (arrowType == arrow1 ? 0 : arrowSize);
	maClient.setArrowPanel(maClient.arrowPanel, arrowType, arrowSize, arrowX, arrowY);
    
   	var pt = new Object;
   	pt.x = popupX;
   	pt.y = popupY;
	return pt;
}

maClient.getScrollingPosition = function()
{
	var position = [0, 0];

	if (typeof window.pageYOffset != 'undefined')
	{
		position = [window.pageXOffset,	window.pageYOffset];
	}

	else if (typeof document.documentElement.scrollTop != 'undefined' && (document.documentElement.scrollTop > 0 || document.documentElement.scrollLeft > 0))
	{
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	}

	else if (typeof document.body.scrollTop != 'undefined')
	{
		position = [document.body.scrollLeft, document.body.scrollTop];
	}

	return position;
}

maClient.getViewportSize = function()
{
	var size = [0, 0];

	if (typeof window.innerWidth != 'undefined')
	{
		size = [window.innerWidth,	window.innerHeight];
	}
	else if (typeof document.documentElement != 'undefined'	&& typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
	}
	else
	{
		size = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
	}

	return size;
}

maClient.getRect = function(side, size, scroll)
{
	var rect = new Object;
	var absX = parseInt(maTour.marker.absX);
	var absY = parseInt(maTour.marker.absY);
	var scrollW = scroll[0];
	var scrollH = scroll[1];
	var w = size[0];
	var h = size[1];
	
	switch (side)
	{
		case "top":
			rect.x = scrollW;
			rect.y = scrollH;
			rect.w = w;
			rect.h = absY - scrollH;
			break;

		case "bottom":
			rect.x = scrollW;
			rect.y = scrollH + absY;
			rect.w = w;
			rect.h = h - (absY - scrollH);
			break;
			
		case "right":
			rect.x = scrollW + absX;
			rect.y = scrollH;
			rect.w = w - (absX - scrollW);
			rect.h = h;
			break;
	
		case "left":
			rect.x = scrollW;
			rect.y = scrollH;
			rect.w = absX - scrollW;
			rect.h = h;
			break;
	}
	
	var e = document.getElementById("maSlideDropdown");
	if (e)
	{
		var dropdownH = 24;
		var pt = maClient.tagLocation(null, "maSlideDropdown");
		var ptX = pt.x + scrollW;
		var ptY = pt.y + scrollH + dropdownH;
		var rectX = rect.x + scrollW;
		var rectY = rect.y + scrollH;
		if (ptX >= rectX && ptX <= rectX + rect.w && ptY >= rectY && ptY <= rectY + rect.h)
		{
			rect.h -= (ptY - rect.y);
			rect.y = ptY + dropdownH;
		}
	}
	
	return rect;
}

maClient.clippedArea = function(dw, dh)
{
	return dw > 0 || dh > 0 ? Math.abs(dw * dh) : 0;
}

maClient.popupLocation = function()
{
    var hasMarker = parseInt(maTour.marker.x) > 0 && parseInt(maTour.marker.y) > 0;
	var	markerX = hasMarker ? maTour.marker.x : 64;
	var	markerY = hasMarker ? maTour.marker.y : 64;
	var arrowSize = hasMarker ? 36 : 0;
	var arrowOffset = hasMarker && maTour.popup.showArrow ? arrowSize - maTour.popup.borderWidth : 4;
	var popupOffsetX = Math.round(maTour.marker.w / 2) + arrowOffset;
	var popupOffsetY = Math.round(maTour.marker.h / 2) + arrowOffset;
	
	// Get the absolute location of the marker's center. We have to calculate it every time in case the user
	// changed the window size and caused the map to move relative to the upper left corner of the browser window.
	maTour.mapLocationAbsolute = maClient.tagLocation(null, "maMap");
	maTour.marker.absX = Number(maTour.mapLocationAbsolute.x) + Number(markerX);
	maTour.marker.absY = Number(maTour.mapLocationAbsolute.y) + Number(markerY);
	
	var size = maClient.getViewportSize();
	var scroll = maClient.getScrollingPosition();
	
	maTour.marker.rectTop = maClient.getRect("top", size, scroll);
	maTour.marker.rectBottom = maClient.getRect("bottom", size, scroll);
	maTour.marker.rectRight = maClient.getRect("right", size, scroll);
	maTour.marker.rectLeft = maClient.getRect("left", size, scroll);
	
	var borders = maTour.popup.borderWidth * 2;
	var popupW = maTour.popup.actualW + borders;
	var popupH = maTour.popup.actualH + borders;
	var clippedArea = new Object();
	var bestSide;
	
	clippedArea.right = maClient.clippedArea(popupW - (maTour.marker.rectRight.w - popupOffsetX), popupH - maTour.marker.rectRight.h);
	clippedArea.least = clippedArea.right;
	bestSide = "right";
	
	if (clippedArea.least > 0)
	{
		clippedArea.bottom = maClient.clippedArea(popupH - (maTour.marker.rectBottom.h - popupOffsetY), popupW - maTour.marker.rectBottom.w);
		if (clippedArea.bottom < clippedArea.least)
		{
			clippedArea.least = clippedArea.bottom;
			bestSide = "bottom";
		}
	}
	
	if (clippedArea.least > 0)
	{
		clippedArea.left = maClient.clippedArea(popupW - (maTour.marker.rectLeft.w - popupOffsetX), popupH - maTour.marker.rectLeft.h);
		if (clippedArea.left == 0)
		{
			clippedArea.least = clippedArea.left;
			bestSide = "left";
		}
	}
	
	if (clippedArea.least > 0)
	{
		clippedArea.top = maClient.clippedArea(popupH - (maTour.marker.rectTop.h - popupOffsetY), popupW - maTour.marker.rectTop.w);
		if (clippedArea.top == 0)
		{
			clippedArea.least = clippedArea.top;
			bestSide = "top";
		}
	}
		
	var pt;
	switch (bestSide)
	{
		case "right" :
			pt = maClient.positionPopupLeftOrRight(bestSide, popupOffsetX, arrowSize, scroll)
			break;
		
		case "bottom" :
			pt = maClient.positionPopupUpOrDown(bestSide, popupOffsetY, arrowSize, scroll)
			break;
		
		case "left" :
			pt = maClient.positionPopupLeftOrRight(bestSide, popupOffsetX, arrowSize, scroll)
			break;
		
		case "top" :
			pt = maClient.positionPopupUpOrDown(bestSide, popupOffsetY, arrowSize, scroll)
			break;
	}
	return pt;
}

maClient.uaIs = function(ua) 
{
    return navigator.userAgent.toLowerCase().indexOf(ua) != -1;
}

maClient.tagLocation = function(outerTagId, innerTagId)
{
	var e = document.getElementById(innerTagId);
	var left = 0;
	var top = 0;
	while (e && (outerTagId == null || e.id != outerTagId))
	{
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}
	var pt = new Object();
	pt.x = left;
	pt.y = top;
	return pt;
}

maAddLoadListener(maEndLoad);
maAttachEventListener(window, "resize", maClosePopup);

function maEndLoad()
{
	if (maTour.editMode)
		return;

	if (document.getElementById('maMap') && !maClient.flashPresent())
		maClient.showFlashMsg();
	
	maClient.maTour = document.getElementById('maTour');
	
	if (maTour.usesPopup)
	{
		maClient.createPopupPanel();
		maTour.mapLocationRelative = maClient.tagLocation("maTour", "maMap");
	}
	else
	{
		maClient.slideImage = document.getElementById("maSlideImage");
		maClient.slideTitle = document.getElementById("maSlideTitle");
		maClient.slideText = document.getElementById("maSlideText");
		maClient.textArea = document.getElementById("maTextArea");
		
		if (maClient.textArea && maClient.textArea.style.position == "absolute")
		{
			var leftPx = maClient.textArea.style.left;
			maClient.textAreaLeft = parseInt(leftPx.substr(0, leftPx.length - 2));
		}
		
		if (maTour.infoPage && maClient.textArea)
		{
			maClient.textArea.style.width = maTour.textAreaW + "px";
			maClient.textArea.style.height = maTour.textAreaH + "px";
			maClient.textArea.style.overflow = "auto";
		}
		else if (maTour.layout == "HIITT" || maTour.layout == "HTTII" || maTour.layout == "VIITT" || maTour.layout == "VTTII" || maTour.layout == "HTT")
		{
			maClient.selectMarkerAndShowView(maTour.selectedViewId);
		}
	}
	
	if (maClient.slideImage)
	{
		maClient.slideImage.src = maClient.path + maTour.firstImageSrc;
		
		if (!maTour.usesPopup || maClient.fixedPopupPersist())
			maClient.slideImage.style.visibility = "visible";
	}
	
	maTour.images = new Array();
}

function maClosePopup()
{
	if (maClient.popupPanel)
		maClient.hidePopup()
}

maClient.createPopupPanel = function()
{
	var e = document.createElement("img");
	e.setAttribute("src", maClient.path + "0023_" + maTour.version + ".gif");
	e.style.visibility = "hidden";
	e.style.width = "20px";
	e.style.height = "20px";
	e.style.zIndex = 3001;
	e.style.position = "absolute";
	maClient.maTour.appendChild(e);
	maClient.loadingPanel = e;
	
	if (maTour.popup.fixed)
	{
		maTour.popup.showArrow = false;
	}
	
	if (false && maTour.popup.persist && !maTour.popup.fixed)
	{
		e = document.createElement("img");
		e.onclick = maClosePopup;
		e.setAttribute("src", maClient.path + "0024_" + maTour.version + ".gif");
		e.style.visibility = "hidden";
		e.style.width = "12px";
		e.style.height = "12px";
		e.style.zIndex = 3001;
		e.style.position = "absolute";
		e.style.cursor = "pointer";
		document.body.appendChild(e);
		maClient.closePanel = e;
	}
	
	if (maTour.popup.showArrow)
	{
		e = document.createElement("img");
		e.style.visibility = "hidden";
		e.style.zIndex = 3001;
		e.style.position = "absolute";
		document.body.appendChild(e);
		maClient.arrowPanel = e;
	}
	
	var hex = '0123456789abcdefe';
	var c = maTour.popup.backgroundColor.substr(6,1);
	var i = hex.indexOf(c.toLowerCase());
	maTour.popup.backgroundColorAlt = maTour.popup.backgroundColor.substr(0,6) + hex.substr(i + 1,1);
	
	e = document.createElement("div");
	e.setAttribute("id", "maPopup");
	e.style.textAlign = "left";
	e.style.visibility = "hidden";
	e.style.zIndex = 3000;
	e.style.position = "absolute";
	e.style.backgroundColor = maTour.popup.backgroundColor;
	e.style.borderTop = maTour.popup.borderTop;
	e.style.borderRight = maTour.popup.borderRight;
	e.style.borderBottom = maTour.popup.borderBottom;
	e.style.borderLeft = maTour.popup.borderLeft;
	
	maClient.popupPanelWidth = maTour.popup.maxW;
	
	if (maTour.popup.fixed)
	{
		e.style.top = maTour.popup.top + "px";
		e.style.left = maTour.popup.left + "px";
		maClient.maTour.appendChild(e);
	}
	else
	{
		e.style.width = maTour.popup.minW + "px";
		e.style.height = maTour.popup.minH + "px";
		e.style.top = "0px";
		e.style.left = "0px";
		document.body.appendChild(e);
	}
	maClient.popupPanel = e;
	
	var mT = maTour.layoutMarginTop + "px ";
	var mR = maTour.layoutMarginRight + "px ";
	var mB = maTour.layoutMarginBottom + "px ";
	var mL = maTour.layoutMarginLeft + "px ";
	var mH = maTour.layoutSpacingH + "px ";
	var mV = maTour.layoutSpacingV + "px ";
	var mNone = "0px ";
	
	if (maTour.layout != "HTT" && maTour.layout != "HMM")
	{
		maClient.slideImage = document.createElement("img");
		maClient.slideImage.setAttribute("id", "maSlideImage");
		maClient.slideImage.style.margin = mT + mR + mNone + mL;
	}
	
	if (maTour.layout != "HII" && maTour.layout != "HMM")
	{
		maClient.textArea = document.createElement("div");

		if (maTour.showSlideTitle)
		{
			maClient.slideTitle = document.createElement("div");
			maClient.slideTitle.setAttribute("id", "maSlideTitle");
			maClient.textArea.appendChild(maClient.slideTitle);
		}
		
		maClient.slideText = document.createElement("div");
		maClient.slideText.setAttribute("id", "maSlideText");
		maClient.slideText.style.marginTop = (maTour.showSlideTitle ? 4 : 0) + "px";
		maClient.textArea.appendChild(maClient.slideText);
	}
	
	switch (maTour.layout)
	{
		case "HIITT":
			maClient.slideImage.style.margin =  mT + mR + mH + mL;;
			maClient.textArea.style.margin = "0px " + mR + mB + mL;
			maClient.popupPanel.appendChild(maClient.slideImage);
			maClient.popupPanel.appendChild(maClient.textArea);
			maClient.popupPanel.style.width = maClient.popupPanelWidth + "px";
			break;
		
		case "HTTII":
			maClient.slideImage.style.margin = mH + mR + mB + mL;
			maClient.textArea.style.margin = mT + mR + "0px " + mL;
			maClient.popupPanel.appendChild(maClient.textArea);
			maClient.popupPanel.appendChild(maClient.slideImage);
			maClient.popupPanel.style.width = maClient.popupPanelWidth + "px";
			break;
		
		case "VIITT":
		case "VTTII":
			var table = document.createElement("table");
			var tbody = document.createElement("tbody");
			table.setAttribute("cellPadding", "0");
			table.setAttribute("cellSpacing", "0");
						
			var row = document.createElement("tr");
			var cell1 = document.createElement("td");
			var cell2 = document.createElement("td");
			cell1.style.verticalAlign = "top";
			cell2.style.verticalAlign = "top";
			row.appendChild(cell1);
			row.appendChild(cell2);
			
			tbody.appendChild(row);
			table.appendChild(tbody);
			maClient.popupPanel.appendChild(table);
			
			if (maTour.layout == "VIITT")
			{
				maClient.textArea.style.margin = mT + mR + mB + mNone;
				cell2.style.textAlign = "left";
				maClient.slideImage.style.margin = mT + mV + mB + mL;
				cell1.appendChild(maClient.slideImage);
				cell2.appendChild(maClient.textArea);
			}
			else
			{
				maClient.textArea.style.margin = mT + mNone + mB + mL;
				cell1.style.textAlign = "left";
				maClient.slideImage.style.margin = mT + mR + mB + mV;
				cell1.appendChild(maClient.textArea);
				cell2.appendChild(maClient.slideImage);
			}
			break;
		
		case "HII":
			maClient.slideImage.style.margin = mT + mR + mB + mL;
			maClient.popupPanel.appendChild(maClient.slideImage);
			break;
		
		case "HTT":
			maClient.textArea.style.margin = mT + mR + mB + mL;
			maClient.popupPanel.appendChild(maClient.textArea);
			maClient.popupPanel.style.width = maClient.popupPanelWidth + "px";
			break;
	}
}

function maImageLoaded()
{
	maClient.showPopup(true)
	maTour.images.push(maClient.markerId);
}

maClient.MapViewer = function(swf, id, mfn, pid, popups)
{
	this.init(swf, id, mfn, pid, popups);
	this.addFlashVar("fv_mapPath", maClient.path);
	maClient.eid = maGetQueryStringArg("marker");
	if (maClient.eid.length != 0)
		this.addFlashVar("fv_markerId", maClient.eid);
}

maClient.MapEditor = function(swf, id)
{
	this.init(swf, id, "", 0, false);
}

maClient.MapViewer.prototype = maClient.Flash.prototype;

maClient.MapViewer.prototype.selectMarkerAndShowView = function (markerid)
{
	if (maTour.usesPopup)
		maClient.hidePopup();
	this.invokeGateway("selectMarkerAndShowView",[markerid]);
}

maClient.MapViewer.prototype.saveState = function (save)
{
	this.invokeGateway("saveState",[save],1);
}

maClient.MapEditor.prototype = maClient.Flash.prototype;

maClient.MapEditor.prototype.selectMarker = function (markerid,gateway)
{
	this.invokeGateway("selectMarker",[markerid],gateway);
}

maClient.hidePopup = function()
{
	if (!maTour.usesPopup)
		return;
	
	if (maClient.fixedPopupPersist())
		return;
	
	okToShowPopup = false;
	maClient.showPopup(false);
}

maClient.showTooltip = function(tag, x, y, text)
{
    if (text.length == 0)
		return;
    var e = document.getElementById('maTooltip');
    if (!e)
    {
		e = document.createElement("div");
		e.setAttribute("id", "maTooltip");
		maClient.maTour.appendChild(e);
	}
    var s = e.style;
    if (!s) return;
    var l = maClient.tagLocation("maTour", "fs" + tag);
    l.x += 10;
    s.left = l.x + x + 'px';
    s.top = l.y + y + 'px';
    e.innerHTML = text;
    s.visibility = "visible";
}

maClient.hideTooltip = function()
{
	var e = document.getElementById('maTooltip');
	var s = e ? e.style : null;
	if (!s) return;
	s.visibility= "hidden";
}