/****************************************************************************************
 * File:		init.js
 * Author:		Drew Eggers, Watauga County 2005
 * Revised:		Oct. 11 2005
 * Functions:	jsInit(thetool) - Call SetTool(), set start map position on page,
 *								  and hide 'loading page...' animated gif
 *				SetTool(thetool) - Call GetTool() with what tool should be AFTER submit()
 *				GetTool(thetool) - Set tool image shadow and set state of tool for
 *								   next page reload, submit tool if not a 'dragging' event
 *				GetCursor() - Sets the appropriate cursor type to be used when the mouse 
 *                            is over the map
 *****************************************************************************************/

/* Globar vars for tab control */
var panels = new Array('Layers', 'Legend', 'Search', 'BooneLayers');
var selectedTab;

/************************************************************************ 
 * Function:	jsInit
 * Date:		2005
 * Params:		thetool - current tool being used
 * Returns:		none.	
 * Description: Call SetTool(), set start map position on page, 
 *				and hide 'loading page...' animated gif
 *				(Called after page load)         
 ************************************************************************/
function jsInit(thetool)
{
	SetTool(thetool);				// Set default tool for the next page load
	document.all.waiting.style.visibility="hidden";  // hide the loading gif
	showPanel(document.getElementById('tab1'), 'Layers');
	initMap();
}

/************************************************************************
 * Function:	SetTool
 * Date:		2005
 * Params:		thetool - current tool being used
 * Returns:		none.
 * Description: Call GetTool() with what tool is at current page's load
 *				(only called at page load)
 ************************************************************************/
function SetTool(thetool)
{
	switch(thetool)
    {
		case "":				// If there is no tool, zoomin is the tool
			GetTool("zoomin")   // to be used next
            break
        case "zoomfull":		// If just zoomed to full extent, zoomin
            GetTool("zoomin")	// should be the selected tool
            break
        case "zoomlast":		// If just zoomed to previous extent, 
			GetTool("zoomin")   // zoomin should be the selected tool
			break
		case "print":			// after clicking the print button,
			GetTool("zoomin")	// zoomin should be the selected tool
			break
		case "search":
		    GetTool("zoomin")
		    break
    }
}

/************************************************************************
 * Function:	GetTool
 * Date:		2005
 * Params:		thetool - tool to be used at next page load
 * Returns:		none.
 * Description: Set tool image shadow and set state of tool for next 
 *			    page reload, submit form if not a 'dragging event'.
 *				Note: this function is NOT ALWAYS called from SetTool();
 *				It is also called from index.asp when a tool is selected 
 ************************************************************************/
function GetTool(thetool)
{
    document.images.zoominbtn.src = "images/zoomin_1.gif";
    document.images.zoominbtn.style.border = "outset";
    document.images.zoomoutbtn.src = "images/zoomout_1.gif";
    document.images.zoomoutbtn.style.border = "outset";
    document.images.panbtn.src = "images/pan_1.gif";
    document.images.panbtn.style.border = "outset";
    document.images.selectbtn.src = "images/select_1.gif";
    document.images.selectbtn.style.border = "outset";
    document.images.identifyallbtn.src = "images/identifyall_1.gif";
    document.images.identifyallbtn.style.border = "outset";
//	    document.images.measurebtn.src = "images/measure_1.gif";
//	    document.images.measurebtn.style.border = "outset";
    document.images.zoomfullbtn.src = "images/fullextent_1.gif"
    document.images.zoomfullbtn.style.border = "outset";
    document.images.zoomlastbtn.src = "images/zoomlast_1.gif"
    document.images.zoomlastbtn.style.border = "outset";
    document.images.printbtn.src = "images/print_1.gif";
    document.images.printbtn.style.border = "outset";
    document.cookie = "tool=" + thetool;

    switch (thetool)
    {
        case "zoomfull":
            document.images.zoomfullbtn.src = "images/fullextent_2.gif";
            document.images.zoomfullbtn.style.border = "inset";
            toolform.submit();
            break;
        case "zoomlast":
            document.images.zoomlastbtn.src = "images/zoomlast_2.gif";
            document.images.zoomlastbtn.style.border = "inset";
            toolform.submit();
            break;
        case "print":
            document.images.printbtn.src = "images/print_2.gif";
            document.images.printbtn.style.border = "inset";
            toolform.submit();
            break;
        case "zoomin":
            document.images.zoominbtn.src = "images/zoomin_2.gif";
            document.images.zoominbtn.style.border = "inset";
            break;
        case "zoomout":
            document.images.zoomoutbtn.src = "images/zoomout_2.gif";
            document.images.zoomoutbtn.style.border = "inset";
            break;
        case "pan":
            document.images.panbtn.src = "images/pan_2.gif";
            document.images.panbtn.style.border = "inset";
            break;
        case "select":
            document.images.selectbtn.src = "images/select_2.gif";
            document.images.selectbtn.style.border = "inset";
            break;
        case "identifyall":
            document.images.identifyallbtn.src = "images/identifyall_2.gif";
            document.images.identifyallbtn.style.border = "inset";
            break;
        case "measure":
            document.images.measurebtn.src = "images/measure_2.gif";
            document.images.measurebtn.style.border = "inset";
            break;
    }

}

/************************************************************************
 * Function:	GetCursor
 * Date:		2005
 * Params:		none.
 * Returns:		none.
 * Description: Sets the appropriate cursor type to be used when the  
 *              mouse is over the map 
 *				Called from onmouseover element of MapTable on page
 *				index.asp
 ************************************************************************/
function GetCursor()
{
	var cursor = "crosshair";				// Default cursor is crosshair
	
	if (get_cookie("tool") == "pan")		// If current tool is pan
	{
		cursor = "all-scroll";				// make cursor the four arrows
	}
	else if (get_cookie("tool") == "select")
	{
		cursor = "hand";
	}
	else if (get_cookie("tool") == "identifyall")
	{
		cursor = "hand";
	}
	
	return cursor;
}

/************************************************************************
 * Function:	get_cookie
 * Date:		2005
 * Params:		cookie_name : the name of the cookie to get the value 
 *							  from.
 * Returns:		Value of the cookie, or null if non-existant
 * Description: finds the value of a cookie given the cookie's name
 *				taken from (permission on page):
 *		 http://www.elated.com/tutorials/programming/javascript/cookies/
 ************************************************************************/
function get_cookie (cookie_name)
{
	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

	if (results)
		return (unescape(results[1]));
	else
		return null;
}

/************************************************************************ 
 * Function:	getLayer
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: returns the html element that is the map image
 *              function taken (and edited) from ESRI's HTML viewer code      
 ************************************************************************/
function getLayer(name) 
{
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is netscape 4 type
	ie4 = (document.all) ? true:false		// ie4 is true if browser is IE 4 type
	
	if (ns4)								// If Netscape 4 type, return layer object
		return(document.layers[name]);
	else if (ie4)							// If IE 4 type, return the 'style' of html object
	{
	    layer = eval('document.all.' + name + '.style');
	    return(layer);
	} 
	else									// If > IE4 or > Netscape 4, return style of html element
	{
		var theObj = document.getElementById(name);
		return theObj.style
	}
}

function stoptools()
{
	document.cookie = "tool=";
}

//this function assigns the javascript functions to the map's event handlers
function initMap()
{
   map.onmousedown = map_mouse_down;
   map.onmousemove = Map_onMouseMove;
   map.onmouseup = Map_onMouseUp;
}

function Map_onMouseDown()
{
   rubberbander = new RubberRectangle(box, "blue", "dotted", "2px");
   rubberbander.processEvent(event);
}

function Map_onMouseMove()
{
   if (rubberbander)
   {
      rubberbander.processEvent(event);
   }
}

function Map_onMouseUp()
{
   if (rubberbander)
   {
      rubberbander.processEvent(event);
   }
}

function SetActiveLayer(theLayer, visible)
{
	var thecookie = "activelayer=" + theLayer;
	
	if (visible)
	{
		document.cookie = thecookie;
	}
	else
	{
		alert("Layer must be visible to become the Active Layer");
	}
}

function Resized()
{
	//alert("Resize function not yet implemented!  Map will likely not fit will to screen!");
}

function showPanel(tab, name)
{
    if (selectedTab) 
    {
        selectedTab.style.backgroundColor = '';
        selectedTab.style.paddingTop = '';
        selectedTab.style.paddingBottom = '';
    }
    selectedTab = tab;
    selectedTab.style.backgroundColor = 'white';
    selectedTab.style.paddingTop = '6px';
    for(i = 0; i < panels.length; i++)
    {
        document.getElementById(panels[i]).style.display = (name == panels[i]) ? 'block':'none';
    }
    return false;
}

/**********************************************************************************************
 * Name:    SearchParcels
 * Author:  Drew Eggers, Watauga County
 * Date:    August 29, 2006
 * Purpose: Store form variables from index.asp in cookies to be used for the parcel search.  
 *          Also, make the current tool=search so that program 'knows' to do the search when the
 *          page reloads
 **********************************************************************************************/
function SearchParcels()
{
    document.cookie = "tool=search"; 
    document.cookie = "searchpin=" + document.getElementById("searchpin").value;
    // Following elements may contain spaces which get removed in a cookie, so
    // they must be escaped to preserve the spaces
    document.cookie = "searchaddress=" + escape(document.getElementById("searchaddress").value);
    document.cookie = "searchname=" + escape(document.getElementById("searchname").value);
}
