    
// Many functions/fixes depend on browser detection
var IE = document.all?true:false;

// The zooming and panning functions depend on this being true or false
var mouseButtonDown = false;



/***** Simple manager for setting and getting cookies
*
* Cookies are set to track things such as which tool was selected before a reload
*
* Usage: cookieMgr.set("foo", "bar"); myCookie = cookieMgr.get("foo");
*
*****/
var cookieMgr = 
{

    get:function(name)
    {  
        // Create a regular expression object with name param
        var regex = new RegExp(name + "=([^;]*);?");

        try
        {
            // Return a match if found
            return document.cookie.match(regex)[1];

	    }
	    catch (e)
	    {
	        // If no match is found return null
	        return null;
	    }

    },
    
    set:function(name, value)
    {  
        document.cookie = name + '=' + value;
    }
};




// The xyObj constructor is used so that multiple values (x and y)
// may be returned by the getMouseCoords function
function xyObj(x,y)
{
	this.x = x;
	this.y = y;
}


// getMouseCoords will trap the current mouse coords and return an xyObj
// constructor whose values may be references as xyObj.x and xyObj.y
function getMouseCoords(eventObj)
{
	// Declare an object to hold the coords using zero values
	var mouseCoords = new xyObj(0,0)
	
	// Test the browser to see if it is IE
	var IE = document.all?true:false;
	
	if (IE)
	{
		// Use these values so that the values return reflect the point of the cursor
		var originTopOffset = 3;
		var originLeftOffset = 2;
	
		// Get the client X and Y coords (minus offsets) and populate the xyObj's x and y values
		mouseCoords.x = event.clientX - originLeftOffset;
		mouseCoords.y = event.clientY - originTopOffset;
	}
	else // Netscape, Mozilla (etc.???)
	{
		// Get the event X and Y coords and populate the xyObj's x and y values
		mouseCoords.x = eventObj.pageX;
		mouseCoords.y = eventObj.pageY;
	}
	
	// return the coords to the calling function
	return(mouseCoords);
}





// XP function to attach (add) an event listener to an object
// Accepts target object id as string, event as string, function as function
// Sample syntax: attachEventListener("mapEventLayer","onmouseover", fooFunc);
function attachEventListener(sTargetObj, sEvent, fFunction)
{
	// Get the targetObj from the id string
	var targetObj = document.getElementById(sTargetObj);

	// Check browser before attaching
	if (IE)
	{
		// Attach the event listener
		targetObj.attachEvent(sEvent, fFunction);
	}
	else
	{
		// If not IE, we need to translate the event names
		sEvent = sEvent.replace("on", "");

		// Attach the event listener
		targetObj.addEventListener(sEvent, fFunction, false);
	}
}


// XP function to detach (remove) an event listener from an object
// Accepts target object id as string, event as string, function as function
// Sample syntax: detachEventListener("mapEventLayer","onmouseover", fooFunc);
function detachEventListener(sTargetObj, sEvent, fFunction)
{
	// Get the targetObj from the id string
	var targetObj = document.getElementById(sTargetObj);

	// Check browser before removing
	if (IE)
	{
		// Remove the event listener
		targetObj.detachEvent(sEvent, fFunction);
	}
	else
	{
		// If not IE, we need to remove "on" from the event names
		sEvent = sEvent.replace("on", "");

		// Remove the event listener
		targetObj.removeEventListener(sEvent, fFunction, false);
	}
}



/*****
*
* tool selection code
*
*****/

var selectedTool = null;
var eventCalled = null;


var mdFunction = null; // mousedown function
var mmFunction = null; // mousemove function
var muFunction = null; // mouseup function

function selectTool(toolName)
{
	//alert("selecting new tool");
	
	// If a(ny) tool was used previously, we need to remove the event listeners that were assigned to it
	// This will only happen if a) a tool is selected and b) it is a different tool (user did not activate the same tool button)
	if (selectedTool != null && selectedTool != toolName) 
	{
		//alert(mdFunction);
		
		detachEventListener("mapEventLayer", "onmousedown", mdFunction);		
		detachEventListener("mapEventLayer", "onmousemove", mmFunction);
		detachEventListener("mapEventLayer", "onmouseup", muFunction);
	}
	
	// Let selectedTool know the name of the newly (currently) selected tool
	selectedTool = toolName;

	// Find out which tool is selected and change function pointers for mapEventLayer mouse events
	switch(selectedTool) 
	{
		case "zoomInTool": // zoom in tool
			instantiateZoomInTool();

			mdFunction = setZoomRectOrigin; mmFunction = drawZoomRect; muFunction = getZoomInCoords;
			
			attachEventListener("mapEventLayer", "onmousedown", mdFunction);
			attachEventListener("mapEventLayer", "onmousemove", mmFunction);
			attachEventListener("mapEventLayer", "onmouseup", muFunction);
			
			break;
			
		case "zoomOutTool": // zoom out tool
			instantiateZoomOutTool();
			
			mdFunction = setZoomOutToolOrigin; muFunction = getZoomOutCoords;
			
			attachEventListener("mapEventLayer", "onmousedown", mdFunction);
			attachEventListener("mapEventLayer", "onmouseup", muFunction);
			
			break;
			
		case "panTool": // pan tool
			instantiatePanTool();
		//alert(mdFunction);	
			mdFunction = setPanOrigin; mmFunction = doPanMove; muFunction = getPanCoords;
			
			attachEventListener("mapEventLayer", "onmousedown", mdFunction);
			attachEventListener("mapEventLayer", "onmousemove", mmFunction);
			attachEventListener("mapEventLayer", "onmouseup", muFunction);
		//alert(mdFunction);	
			break;
			
		case "infoTool": // info tool
			instantiateInfoTool();
			
			mdFunction = setInfoToolOrigin; muFunction = getInfoCoords;
			
			attachEventListener("mapEventLayer", "onmousedown", mdFunction);
			attachEventListener("mapEventLayer", "onmouseup", muFunction);
			
			break;
	}

	//update the cookie
	cookieMgr.set("tool", selectedTool); 
}

// end of tool selection code





/*****
/
/ Zoom In Tool
/
*****/

// vars to track where the zoom rectangle's origin is
var originLeft = 0;
var originTop = 0;

// vars to correct for the visual alignment of the crosshair cursor with the zoom rect,
// taking into account XP differences
var leftOffset, rightOffset, topOffset, bottomOffset;




function instantiateZoomInTool()
{
	mapEventLayer.style.cursor = "crosshair";
}


function setZoomRectOrigin(eventObj)
{
	// Check to see that the left mouse button is held down
	if ( eventObj.button == 1 || eventObj.which == 1)
	{		
		// Get the zoom rectangle
		zoomOrSelectBoxRect = document.getElementById("zoomOrSelectBoxRect");

		// Set its width and height to 0 px
		zoomOrSelectBoxRect.style.width = "0px";
		zoomOrSelectBoxRect.style.height = "0px";
		
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);

		// Set the origin point for drawing 
		// (Note that if the mouse goes left or above the origin point,
		// the zoom rectangle's SE corner will end up at the origin instead
		originTop = objCoords.y;
		originLeft = objCoords.x;
		
		//alert(originLeft + ", " + originTop);
		
		zoomOrSelectBoxRect.style.top = objCoords.y + topOffset + "px";
		zoomOrSelectBoxRect.style.left = objCoords.x + leftOffset + "px";
		
		
		// Make the zoom box visible
		zoomOrSelectBoxRect.style.visibility = "visible";
	
		// Set the mousebutton's status to true
		mouseButtonDown = true;
	}
}


function drawZoomRect(eventObj)
{
	// Check to see that the mousebutton is still being held down. If so, draw the zoom rectangle
	if ( mouseButtonDown == true)
	{
		// Hide btnPalette
	/////	var btnPalette = document.getElementById("btnPalette");
		
		//var mapFrameRight =  
		
		//if(mapFrame.style.width > btnPalette.style.left)
		//{
	/////	    btnPalette.style.visibility = "hidden";
        //}
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);

		// Test to see if the mouse cursor is to the right of the origin point. If it is,
		// draw the zoom rectangle from the origin point to the current mouse position
		if ( objCoords.x > originLeft )
		{
			zoomOrSelectBoxRect.style.left = originLeft + leftOffset + "px";
			
			var zoomOrSelectBoxRectWidth = objCoords.x - originLeft + rightOffset;
			
			if(zoomOrSelectBoxRectWidth > 0)
			{
			    zoomOrSelectBoxRect.style.width = zoomOrSelectBoxRectWidth + "px";
			}
			else
			{
			    zoomOrSelectBoxRect.style.width = 1 + "px";
			}
		}
		else // Move the zoom rectangle and draw from the mouse position to the origin point
		{
			zoomOrSelectBoxRect.style.left = objCoords.x + leftOffset + "px";
			
			var zoomOrSelectBoxRectWidth = originLeft - objCoords.x + rightOffset;
			
			if(zoomOrSelectBoxRectWidth > 0)
			{
			    zoomOrSelectBoxRect.style.width = zoomOrSelectBoxRectWidth + "px";
			}
			else
			{
			    zoomOrSelectBoxRect.style.width = 1 + "px";
			}
		}
		
		// Test to see if the mouse cursor is below the origin point. If it is,
		// draw the zoom rectangle from the origin point to the current mouse position
		if ( objCoords.y > originTop )
		{
			zoomOrSelectBoxRect.style.top = originTop + topOffset + "px";
			
			var zoomOrSelectBoxRectHeight = objCoords.y - originTop + bottomOffset;
			
			if(zoomOrSelectBoxRectHeight > 0)
			{
			    zoomOrSelectBoxRect.style.height = zoomOrSelectBoxRectHeight + "px";
			}
			else
			{
			    zoomOrSelectBoxRect.style.height = 1 + "px";
			}
		}		
		else // Move the zoom rectangle and draw from the mouse position to the origin point
		{
			zoomOrSelectBoxRect.style.top = objCoords.y + topOffset + "px";
			
			var zoomOrSelectBoxRectHeight = originTop - objCoords.y + bottomOffset;
			
			if(zoomOrSelectBoxRectHeight > 0)
			{
			    zoomOrSelectBoxRect.style.height = zoomOrSelectBoxRectHeight + "px";
			}
			else
			{
			    zoomOrSelectBoxRect.style.height = 1 + "px";
			}
		}
	}	
}

// Not sure if this is needed, but probably because it prevents multiple zoom calculations
// while the page is refreshing
var redrawInProgress = false; ///DEBUG

function getZoomInCoords(eventObj)
{
    // NOTE: When this is AJAXed, you'll need to make the btnPalette visible again

	// Set this to false - no more drawing will occur
	mouseButtonDown = false;
	
	// Get the zoom rectangle object
	zoomOrSelectBoxRect = document.getElementById("zoomOrSelectBoxRect");
	
	// Hide it
	zoomOrSelectBoxRect.style.visibility = "hidden";

	// Check that a redraw is not already in progress. If not, go ahead and process, then submit
	if (redrawInProgress == false)
	{
		// Var to pass to the routeTool function so it knows which action to take
		eventCalled = "getZoomInCoords";
	
		// Find the left coordinate of the zoom rectangle
		var zoomOrSelectBoxRectLeft = zoomOrSelectBoxRect.style.left.replace("px", "");
		// Find the width of the rectangle
		var zoomOrSelectBoxRectWidth = zoomOrSelectBoxRect.style.width.replace("px", "");

		// Find the top coordinate of the zoom rectangle
		var zoomOrSelectBoxRectTop = zoomOrSelectBoxRect.style.top.replace("px", "");
		// Find the height of the rectangle
		var zoomOrSelectBoxRectHeight = zoomOrSelectBoxRect.style.height.replace("px", "");
		
		// Get the mapImageHolder height
		var mapImageHeight = mapImageHolder.style.height.replace("px", "");
		
		// Calculate the image's xmin, xmax, ymin and ymax coords in pixels
		var imageXmin = parseInt(zoomOrSelectBoxRectLeft) - parseInt(mapImageLeft); 
		var imageXmax = imageXmin + parseInt(zoomOrSelectBoxRectWidth); 
	
		var imageYmin = ( parseInt(mapImageTop) + parseInt(mapImageHeight) ) - ( parseInt(zoomOrSelectBoxRectTop) + parseInt(zoomOrSelectBoxRectHeight) );
		var imageYmax = ( parseInt(mapImageTop) + parseInt(mapImageHeight) ) - parseInt(zoomOrSelectBoxRectTop);
		
		// if the zoom rect is smaller than 10 x 10 pixels, just zoom in by a factor of 2, centered on the origin point
		if ( (imageXmax - imageXmin) < 10 && (imageYmax - imageYmin) < 10 )
		{
			zoomFactor = 2;
			
			// Get the current mouse coords. objCoords will be populated with an xyObj constructor
			var objCoords = getMouseCoords(eventObj);
		
			var imageCX = parseInt(objCoords.x) - parseInt(mapImageLeft);
			var imageCY = mapImageHeight - (parseInt(objCoords.y) - parseInt(mapImageTop));
			
			imageCX = parseInt(objCoords.x) - parseInt(mapFrameLeft);
	        imageCY = mapImageHeight - parseInt(objCoords.y) + parseInt(mapFrameTop);
			
			zoomMap(zoomFactor, imageCX, imageCY);
		}
		else
		{
			zoomOrSelectMapByRect(imageXmin, imageYmin, imageXmax, imageYmax);
		}
	}
		
	redrawInProgress = true;
}


// zoomMap differs from zoomOrSelectMapByRect in that it is intended to work with just point clicks, not an envelope.
function zoomMap(zoomFactor,X,Y)
{
	// Calculate the amount of width and height to add (likely a negative value for a zoom in)
	var widthExtension = (1/zoomFactor)/2 * mapWidth; // amount to add on either side of origin point
	var heightExtension = (1/zoomFactor)/2 * mapHeight; // amount to add above and below origin point
	
	//Y = mapImageHeight - Y // reverse Y to fit map coord system
		
	// calculate the real world coordinates of the origin point
	mapCX = (X/mapImageWidth*mapWidth) + mapMinX;
	mapCY = (Y/mapImageHeight*mapHeight) + mapMinY;
	
	mapMinX = mapCX - widthExtension;
	mapMaxX = mapCX + widthExtension;
	
	mapMinY = mapCY - heightExtension;
	mapMaxY = mapCY + heightExtension;	

	//alert(mapMinX + ", " + mapMinY + " & " + mapMaxX + ", " + mapMaxY); ///DEBUG

	routeToolCall();	
}


function zoomOrSelectMapByRect(imageXmin, imageYmin, imageXmax, imageYmax)
{
	mapMaxX = (imageXmax/mapImageWidth*mapWidth) + mapMinX;
	mapMinX = (imageXmin/mapImageWidth*mapWidth) + mapMinX;	
		
	mapMaxY = (imageYmax/mapImageHeight*mapHeight) + mapMinY;
	mapMinY = (imageYmin/mapImageHeight*mapHeight) + mapMinY;

	//alert(mapMinX + ", " + mapMinY + " & " + mapMaxX + ", " + mapMaxY); ///DEBUG

	routeToolCall();	
}





/*****
*
*	zoom out tool code block
*
*****/

// The zoom out tool simply zooms out by a factor of 2 for now. Eventually, we will make this adjustable.

function instantiateZoomOutTool()
{
	mapEventLayer.style.cursor = "crosshair";
}


var zoomoutX = 0;
var zoomoutY = 0;

function setZoomOutToolOrigin(eventObj)
{
	// Get the current mouse coords. objCoords will be populated with an xyObj constructor
	var objCoords = getMouseCoords(eventObj);

	zoomoutX = parseInt(objCoords.x) - parseInt(mapFrameLeft);
	zoomoutY = mapImageHeight - parseInt(objCoords.y) + parseInt(mapFrameTop);

}
 

function getZoomOutCoords()
{
	eventCalled = "getZoomOutCoords";
	
	zoomFactor = .5;
	zoomMap(zoomFactor, zoomoutX, zoomoutY);
}


// end of zoom out tool code block





// Pan functions

function instantiatePanTool()
{
	mapEventLayer.style.cursor = "move";
}

// vars to track where the mouse was clicked relative to the coordinates of the map image
// used to offset the difference so that the image moves with the mouse no matter where on the image
// the user clicks
var imageOffsetX, imageOffsetY;

function setPanOrigin(eventObj)
{
	// Check to see that the left mouse button is down
	if ( eventObj.button == 1 || eventObj.which == 1)
	{
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);
		
		imageOffsetX = objCoords.x;
		imageOffsetY = objCoords.y;
	
		mouseButtonDown = true;
	}
}


function doPanMove(eventObj)
{
	// Check to see that the left mousebutton is still down
	if (mouseButtonDown == true)
	{
	    // Hide btnPalette
		/////var btnPalette = document.getElementById("btnPalette");
		
		/////btnPalette.style.visibility = "hidden";
	
	
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);

        var bgX;
        var bgY;

	    // IE sets the background relative to the top and left coords of the mapImageHolder div. Others do not.
	    if (IE)
	    {
		    bgX = objCoords.x - imageOffsetX;
		    bgY = objCoords.y - imageOffsetY;
    		
	    }
	    else // fix background for Netscape
	    {
		    bgX = parseInt(objCoords.x) + parseInt(mapImageLeft);
		    bgY = parseInt(objCoords.y) + parseInt(mapImageTop);
    		
		    bgX = bgX - imageOffsetX;
		    bgY = bgY - imageOffsetY;
	    }

	    // Set the mapImageHolder's position to the new coordinates
    		
	    mapImageHolder.style.top = bgY + "px";
    	
	    mapImageHolder.style.left = bgX + "px";
    }
}

function getPanCoords(eventObj)
{
	// Get the current mouse coords. objCoords will be populated with an xyObj constructor
	var objCoords = getMouseCoords(eventObj);

	var mapImageLeft = mapImageHolder.style.left.replace("px", ""); 
	var mapImageTop = mapImageHolder.style.top.replace("px", "");

	// Calculate the map coordinates (in map units)
	mapMinX = mapMinX - (mapImageLeft/mapImageWidth * mapWidth);
	mapMaxX = mapMinX + mapWidth;
		
	mapMaxY = mapMaxY + (mapImageTop/mapImageHeight *  mapHeight);
	mapMinY = mapMaxY - mapHeight;
	
	//alert(mapMinX + ", " + mapMinY + " & " + mapMaxX + ", " + mapMaxY); ///DEBUG

	// Need to pass so that routeToolCall will know what action is required
	eventCalled = "panMap";

	routeToolCall();
	
	// Reset this to false so no more panning is done
	mouseButtonDown = false;	
}




// Info tool functions

function instantiateInfoTool()
{
	mapEventLayer.style.cursor = "pointer";
}

// vars to track where the mouse was clicked relative to the coordinates of the map image
// used to offset the difference so that the image moves with the mouse no matter where on the image
// the user clicks

var infoX = 0;
var infoY = 0;

function setInfoToolOrigin(eventObj)
{
	// Check to see that the left mouse button is down
	if ( eventObj.button == 1 || eventObj.which == 1)
	{
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);


		infoX = parseInt(objCoords.x) - parseInt(mapFrameLeft);
	    infoY = mapImageHeight - parseInt(objCoords.y) + parseInt(mapFrameTop);
	}
}

var mapCX;
var mapCY;

function getInfoCoords()
{

	// calculate the real world coordinates of the origin point
	mapCX = (infoX/mapImageWidth*mapWidth) + mapMinX;
	mapCY = (infoY/mapImageHeight*mapHeight) + mapMinY;
	
	eventCalled = "info";
	
	routeToolCall();	
}




function routeToolCall()
{


	switch(eventCalled) // Find out which tool is selected and call appropriate action
	{
		case 'getZoomInCoords': // zoom in
			submitZoom();
			break;
		case 'getZoomOutCoords': // zoom out
			submitZoom();
			break;
		case 'panMap': // pan map
			submitZoom();
			break;						
		case 'info': // get info
			submitInfo();
			break;
	}

}




function submitZoom()
{	
	// alert(initMinX + '|' + initMinY + '|' + initMaxX + '|' + initMaxY + '\n' 
    //    + mapMinX + '|' + mapMinY + '|' + mapMaxX + '|' + mapMaxY);
	
	document.coordForm.toolParams.value = mapMinX + '|' + mapMinY + '|' + mapMaxX + '|' + mapMaxY;
	
	// alert(document.coordForm.toolParams.value); ///DEBUG
	
	document.coordForm.submit();
}



















var req;



// our function to load the image
function loadimage(imagepath)
{


    // our XMLHttpRequest to grab the image
    req = getreq();
    req.onreadystatechange = imagexists;
    req.open("get", imagepath, true);
    req.send(null);     
}

function imagexists()
{
    // if the XMLHttpRequest is finished loading
    if(req.readyState == 4)
    {
        // and the file actually exists
        if(req.status == 200)
        {
          alert("found image");
        }
        else
        {
            alert("image not found");
        }
    }
}

function getreq()
{
    if(window.XMLHttpRequest)
        return new XMLHttpRequest();
    else if(window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
}


















function submitFullExtent()
{
	/*var initialMapImageURL = cookieMgr.get("initialMapImageURL");
alert("on calling submit: " + initialMapImageURL);
    var mapImage = document.getElementById("mapImage");




loadimage(initialMapImageURL);



 
    try
    {   
        if(initialMapImageURL != null)
        {
            
            var testImage = new Image();
            testImage.src = initialMapImageURL;
            //alert("tcomp: " + testImage.complete);
            if(testImage.complete == false)
            {
                throw testImage.complete;
            }
            else         
            {
                // NOTE: Also put in test to see if the src file still exists!!!
                mapImage.src = initialMapImageURL;



             
                mapMinX = initMinX;
                mapMinY = initMinY;
                mapMaxX = initMaxX;
                mapMaxY = initMaxY;
             
                mapWidth = mapMaxX - mapMinX; ///DEBUG
                mapHeight = mapMaxY - mapMinY; ///DEBUG
             
             
               // alert(initMinX + '|' + initMinY + '|' + initMaxX + '|' + initMaxY + '\n' 
               // + mapMinX + '|' + mapMinY + '|' + mapMaxX + '|' + mapMaxY);
           }
        }
    }
    catch(e)
    {
        alert("catch: " + e);
       
        cookieMgr.set("initialMapImageURL", null); // This will force it to renew the image
        */ 
        document.coordForm.toolParams.value = initMinX + '|' + initMinY + '|' + initMaxX + '|' + initMaxY;
	
	    // alert(document.coordForm.toolParams.value); ///DEBUG
    	
	    document.coordForm.submit();
    //}
}



function submitInfo()
{
	document.coordForm.toolParams.value = mapMinX + '|' + mapMinY + '|' + mapMaxX + '|' + mapMaxY;
	//document.coordForm.infoCoords.value = mapCX + '|' + mapCY;
	//document.coordForm.toolName.value = eventCalled;
	
	//alert("coords: " + mapCX + '|' + mapCY + "\n\n(This will lead to page with image results).");
	
	// alert(document.coordForm.toolParams.value); ///DEBUG
	
	//document.coordForm.submit();
	
	var coordsStr = mapCX + '|' + mapCY;
	
	document.infoForm.coordParams.value = coordsStr;
	//alert(document.infoForm.coordParams.value);
	document.infoForm.submit();
}




// These vars represent the div objects on the screen that make up the map window
var mapImageHolder = null;
var zoomOrSelectBoxRect = null;
var mapEventLayer = null;
var mapImage = null;
var mapFrame = null;




var mapWidth = null; ///DEBUG
var mapHeight = null; ///DEBUG

var mapImageWidth = null;
var mapImageHeight = null;
var mapImageLeft = null;
var mapImageTop = null;

var mapFrame = null;
var mapFrameTop = null;
var mapFrameLeft = null;

function mapInit()
{
	mapWidth = mapMaxX - mapMinX; ///DEBUG
	mapHeight = mapMaxY - mapMinY; ///DEBUG

	mapImageHolder = document.getElementById("mapImageHolder");
	
	mapFrame = document.getElementById("mapFrame");


    mapFrameLeft = mapFrame.style.left.replace("px", "");
    
   mapFrameTop = mapFrame.style.top.replace("px", "");

    leftOffset = -mapFrameLeft -1;
    topOffset = -mapFrameTop -1;
    bottomOffset = -1;
    rightOffset = -1;

	
	mapImage = document.getElementById("mapImage");
	
	mapImage.src = mapImageUrl;
	
	mapEventLayer = document.getElementById("mapEventLayer");


	mapImageWidth = mapImageHolder.style.width.replace("px", "");
	mapImageHeight = mapImageHolder.style.height.replace("px", "");
	mapImageLeft = mapImageHolder.style.left.replace("px", "");
	mapImageTop = mapImageHolder.style.top.replace("px", "");




	selectedTool = cookieMgr.get("tool");
    /////alert(selectedTool);
	if (selectedTool != null) // set tool cookie
	{
		selectTool(selectedTool);
	}
	else // use Get Data as the default
	{
        /////alert('info');
	   
	    selectTool('infoTool'); 
	    
	    changeBtnsStatus('btnImages'); 
	}


	var dialogBoxSeen = cookieMgr.get("dialogBoxSeen");
    /////alert(dialogBoxSeen);
    
	if (dialogBoxSeen != "true") // set tool cookie
	{
        var dialogBox = document.getElementById('noticeDiv');

        dialogBox.style.visibility = 'visible';
	}




/* 
    var initialMapImageURL = cookieMgr.get("initialMapImageURL");
//alert("on calling init: " + initialMapImageURL);    
    var mapImage = document.getElementById("mapImage");
    
    if(initialMapImageURL == "null")
    {
        //alert("setting");
        
        cookieMgr.set("initialMapImageURL", mapImage.src);
    }
   
    var btnPalette = document.getElementById("btnPalette");
    
    // Get palette position(s)
    var btnPalettePos = cookieMgr.get("btnPalette");

    if(btnPalettePos != null)
    {
        var btnPaletteCoords = btnPalettePos.split(",");
        
        var btnPaletteX = btnPaletteCoords[0];
       
        var btnPaletteY = btnPaletteCoords[1];
  //alert(btnPaletteX + ", " + btnPaletteY);       
        
        
        btnPalette.style.left = btnPaletteX;
        
       btnPalette.style.top = btnPaletteY;
       
       
    }
    
    btnPalette.style.visibility = "visible";
*/
}







/***** Code to manage moving a palette
*
*
*
*****/

var palette, paletteOffsetX, paletteOffsetY, paletteLeft, paletteTop;

var pmdFunction = null; // mousedown function
var pmmFunction = null; // mousemove function
var pmuFunction = null; // mouseup function

function initPaletteMove(paletteID)
{
	
	
	palette = document.getElementById(paletteID);
	
	pmdFunction = getPaletteOrigin; pmmFunction = doPaletteMove; pmuFunction = stopPaletteMove;
	
	attachEventListener(paletteID, "onmousedown", pmdFunction);
	attachEventListener("paletteEventLayer", "onmousemove", pmmFunction);
	attachEventListener("paletteEventLayer", "onmouseup", pmuFunction);
	
	 var paletteEventLayer = document.getElementById("paletteEventLayer");
        
        paletteEventLayer.style.visibility = "visible";
 }
 

 
function getPaletteOrigin(eventObj)
{
	
	
	// Check to see that the left mouse button is down
	if ( eventObj.button == 1 || eventObj.which == 1)
	{
       
//alert("down");
		paletteLeft = palette.style.left.replace("px", "");
		
		paletteTop = palette.style.top.replace("px", "");
		
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);
		
		paletteOffsetX = paletteLeft - objCoords.x ;
		paletteOffsetY = paletteTop - objCoords.y;
		
		mouseButtonDown = true;
	}
}


function doPaletteMove(eventObj)
{
	// Check to see that the left mousebutton is still down
	if (mouseButtonDown == true)
	{
		// Get the current mouse coords. objCoords will be populated with an xyObj constructor
		var objCoords = getMouseCoords(eventObj);

		var bgX = objCoords.x + paletteOffsetX;
		var bgY = objCoords.y + paletteOffsetY;
    		

	    // Set the btnPalette's position to the new coordinates
    		
	    palette.style.top = bgY + "px";
    	
	    palette.style.left = bgX + "px";
	    

    }
}

function stopPaletteMove(eventObj)
{
	var paletteEventLayer = document.getElementById("paletteEventLayer");
	
	paletteEventLayer.style.visibility = "hidden";
	
	detachEventListener(palette.id, "onmousedown", pmdFunction);		
	detachEventListener("paletteEventLayer", "onmousemove", pmmFunction);
	detachEventListener("paletteEventLayer", "onmouseup", pmuFunction);

	// Set positional cookie for the palette

	    var palettePos = palette.style.left+ "," + palette.style.top;
	//alert(palettePos);    
	    cookieMgr.set(palette.id, palettePos);

//alert(cookieMgr.get("btnPalette"));
	
	
	
	// Reset this to false so no more moving is done
	mouseButtonDown = false;
	
}




// prevent IE text selection while dragging!!!
function preventSelection(evt) {
    
    document.body.ondrag = function () { return false; };
    document.body.onselectstart = function () { return false; };
}

// If IE is the browser, call preventSelection so that text and other UI
// doesn't get hilited during panning or zooming.
if(IE)
{
    window.attachEvent("onload", function(){ preventSelection(event);} );
}