var isIE = false;
var isIE8 = false;
var isNotIE = false;
var isFox = false;
var isChrome = false;
var isSafari = false;

getBrowser();

// set functions for run on load and on resize
(isNotIE) ? window.addEventListener( "load", init, false ) : window.attachEvent( "onload", init );

function init()
{
	if(document.getElementById('popupWindows'))
	{
		setupPopups()
	}
}
function setupPopups()
{
	if(document.getElementById('popupWindows'))
	{
		if(window.innerHeight)//Fox
		{
			if(document.getElementById('cacMap')){ document.getElementById('cacMap').addEventListener('mousedown', showMap, false);	}
			if(document.getElementById('desertMap')){ document.getElementById('desertMap').addEventListener('mousedown', showMap, false); }
		}
		else //IE
		{
			if(document.getElementById('cacMap')){ document.getElementById('cacMap').attachEvent('onmousedown', showMap); }
			if(document.getElementById('desertMap')){ document.getElementById('desertMap').attachEvent('onmousedown', showMap); }
		}
	}
	else
	{
		return;
	}
}
function showMap(e) 
{
	var e = e || mouse.event;
	var mapToShow;

	var eventTop = document.getElementById('container-page').offsetTop;
	var eventLeft = document.getElementById('container-page').offsetLeft;
	
	( isNotIE ) ? mapToShow = this.getAttribute('id') : mapToShow = e.srcElement.getAttribute('id');
	
	var mapWindow = document.createElement('div');
	mapWindow.style.left = ( eventLeft + 60 ) + "px";
	mapWindow.style.top = "110px";
	mapWindow.setAttribute('id', 'mapWindows');
	( isIE == true && isIE8 == false ) ? mapWindow.className = 'popupWindowDiv' : mapWindow.setAttribute('class', 'popupWindowDiv');
	
	var mapWindowShadow = document.createElement('div');
	mapWindowShadow.style.left = ( eventLeft + 70 ) + "px";
	mapWindowShadow.style.top = "120px";
	mapWindowShadow.setAttribute('id', 'mapWindowShadow');
	( isIE == true && isIE8 == false ) ? mapWindowShadow.className = 'popupWindowDivShadow' : mapWindowShadow.setAttribute('class', 'popupWindowDivShadow');
	( isIE || isIE8 ) ? mapWindowShadow.style.filter = 'alpha(opacity=50)' : mapWindowShadow.style.opacity = '.5';
	
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function()
	{	
		if(xhr.readyState==4)
		{
			if(xhr.status==200)
			{
				mapWindow.innerHTML = '<img src="http://www.rctlma.org/images/' + mapToShow + '.gif" alt="' + mapToShow + '" width="552" height="388" style="float:left;margin:0px;padding:0px;"/>' + xhr.responseText;

			}
			else
			{
				mapWindow.innerHTML = "Error Loading Map. We apologize for any inconvenience.<br />" +
				"<p style=\"clear:both;margin-right:11px;\"><a href=\"#\" onmousedown=\"javascript: document.body.removeChild(document.getElementById('mapWindowShadow'))\" onclick=\"javascript: document.body.removeChild(document.getElementById('mapWindows'))\">Close Window</a></p>";
			}			
		}	
		else //While the external text is loading display a loading image
		{
			mapWindow.innerHTML = '<img src="http://www.rctlma.org/images/loading.gif">';
		}
	}
	
	var fileToGet = "http://www.rctlma.org/common/" + mapToShow + ".html";

	xhr.open("GET", fileToGet, true);
	xhr.send(null);
		
	document.body.appendChild(mapWindow);
	document.body.appendChild(mapWindowShadow);
}

function createXMLHttpRequest()
{
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;	
}

function getBrowser()
{
	// which version of IE do we have
	if( (navigator.userAgent).indexOf("MSIE") != -1 )
	{ 
		if( (navigator.userAgent).indexOf("MSIE 8.0") != -1 )
		{
			// IE 8 uses new javascript interpretation, specifically getAttribute('class') rather than getAttribute('className')
			isIE8 = true;
		}
		else
		{
			// old versions of IE
			isIE = true;
		}
	}
	//everything else
	else if( (navigator.userAgent).indexOf("Firefox") != -1 ) {	isNotIE = true; isFox = true; }
	else if( (navigator.userAgent).lastIndexOf("Chrome") != -1 ) { isNotIE = true; isChrome = true;	}
	else if( (navigator.userAgent).lastIndexOf("Safari") != -1 ) { isNotIE = true; isSafari = true;	}
}
