// CivXplorer Code to support viewer layout and functionality
// Suzanne Fliege
// Red Horse Geographics, LLC

// user screen and window resolutions
var reloadTimer = 0;
var reloadInterval = 0;
// page form
var theCXForm = null;
// menu constants
var menuItemList = new Array();
var menuItemListCount = 0;
var menuSpace = "&nbsp;&nbsp;";
// layout
var borderWidth = 10;
var frameWidth = 5;
var headerHeight = 60;
var footerHeight = 30;
var menuWidth = 112;
var menuHeight = 20;
// sidebar
var sidebarResizeTimer = null;
var sidebarWidth = 200;
var sidebarHeight = 500;
var openSidebarHeight = 338;
var sidebarMode = 4;
var oldSidebarMode = 0;
var sidebarFloatCount = 4;
var sidebarFloatHeight = 20;
var moveSidebarCount = 0;
var moveSteps = 20;
// tools
var toolbarLeft = 0;
var iTop = 0;
var iLeft = 0;
var iWidth = 0;
var iHeight = 0;
var iRight = 0;
var iBottom = 0;
// command line startup
var startQueryString = "";

// *****************
//    page load
// *****************
// functions to run on CX start
function CXload() {
	// set page form variable
	theCXForm = document.getElementById('cxForm');
    // layout adjusted to browser size
    ResizePageControls(false);
	// load sidebars and start page
	loadCustomPages();
    // set background properties
    document.getElementById('cxBackground').onmouseover = "JavaScript:setMenus('nothing');hideHelp();";
    // dropdown menus
	createToolMenus();
    // activate starting sidebar
    showSidebar(startingSidebar);
	// reset map dropdown
	theCXForm.cxPrintMapType.selectedIndex = 0;
	// start loading javascript	
	var cxScripts = document.createElement('script');
	cxScripts.setAttribute("type","text/javascript");
	cxScripts.setAttribute("src","javascript/cxCommon.js");
	document.getElementsByTagName('head')[0].appendChild(cxScripts);
}

// update content in a layer
function updateContent(name, content) {
	document.getElementById(name).innerHTML = content;
}

// interval to check if previous map redraw is complete before resizing
function checkOkToResize() {
	if (!waitForMap) {
		clearInterval(reloadInterval);
		sendMapRequest();
	}
	return false;
}

// resize page controls when map/sidebar/browser area resizes
function ResizePageControls(doRedraw) {
	// window size
    GetPageDimensions();
	// background
	document.getElementById('cxBackground').style.height = winHeight + "px";
	document.getElementById('cxBackground').style.width = winWidth + "px";
	// header & toolmenu background
	document.getElementById('cxHeader').style.width = winWidth + "px";
	document.getElementById('cxMenuBackground').style.width = winWidth + "px";
	// sidebar elements
	document.getElementById('cxSidebarTop').style.top = (headerHeight+menuHeight) + "px";
	document.getElementById('cxSidebarTop').style.left = borderWidth + "px";
	document.getElementById('cxSidebarBottom').style.top = (headerHeight+menuHeight+32+sidebarHeight) + "px";
	document.getElementById('cxSidebarBottom').style.left = borderWidth + "px";
	document.getElementById('cxSidebarLeft').style.height = sidebarHeight + "px";
	document.getElementById('cxSidebarLeft').style.left = borderWidth + "px";
	document.getElementById('cxSidebarRight').style.height = sidebarHeight + "px";
	document.getElementById('cxSidebarRight').style.left = (borderWidth+frameWidth+sidebarWidth) + "px";
	document.getElementById('cxSidebarBackground').style.top = (headerHeight+menuHeight+32) + "px";
	document.getElementById('cxSidebarBackground').style.left = borderWidth+frameWidth + "px";
	document.getElementById('cxSidebarBackground').style.height = sidebarHeight + "px";
    // move sidebar headers below the open one
    for (var i=sidebarMode;i<sidebarFloatCount;i++){
        divName = "cxSidebar" + (i+1) + "Header";
        document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*i+openSidebarHeight) + "px";
    }
    // adjust height of open sidebar
    var divName = "cxSidebar" + sidebarMode;
    document.getElementById(divName).style.height = openSidebarHeight + "px";
	// dropdown menus
	document.getElementById('cxMapMenus').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('menuFind').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('menuSelect').style.left = (sidebarWidth+borderWidth*2+menuWidth+frameWidth*3) + "px";
	document.getElementById('menuAdvanced').style.left = (sidebarWidth+borderWidth*2+menuWidth*2+frameWidth*3) + "px";
	document.getElementById('menuSettings').style.left = (sidebarWidth+borderWidth*2+menuWidth*3+frameWidth*3) + "px";
	// map & footer border elements
	document.getElementById('cxMapBorderUL').style.top = (headerHeight+menuHeight) + "px";
	document.getElementById('cxMapBorderUL').style.left = (sidebarWidth+borderWidth*2+frameWidth*2) + "px";
	document.getElementById('cxMapBorderTop').style.top = (headerHeight+menuHeight) + "px";
	document.getElementById('cxMapBorderTop').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('cxMapBorderTop').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderUR').style.top = (headerHeight+menuHeight) + "px";
	document.getElementById('cxMapBorderUR').style.left = (sidebarWidth+borderWidth*2+frameWidth*3+mapWidth) + "px";
	document.getElementById('cxMapBorderLeft').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('cxMapBorderLeft').style.left = (sidebarWidth+borderWidth*2+frameWidth*2) + "px";
	document.getElementById('cxMapBorderLeft').style.height = (mapHeight+footerHeight+frameWidth) + "px";
	document.getElementById('cxMapBorderRight').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('cxMapBorderRight').style.left = (sidebarWidth+borderWidth*2+frameWidth*3+mapWidth) + "px";
	document.getElementById('cxMapBorderRight').style.height = (mapHeight+footerHeight+frameWidth) + "px";
	document.getElementById('cxMapBorderMiddle').style.top = (headerHeight+menuHeight+frameWidth+mapHeight) + "px";
	document.getElementById('cxMapBorderMiddle').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('cxMapBorderMiddle').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderLL').style.top = (headerHeight+menuHeight+frameWidth*2+mapHeight+footerHeight) + "px";
	document.getElementById('cxMapBorderLL').style.left = (sidebarWidth+borderWidth*2+frameWidth*2) + "px";
	document.getElementById('cxMapBorderBottom').style.top = (headerHeight+menuHeight+frameWidth*2+mapHeight+footerHeight) + "px";
	document.getElementById('cxMapBorderBottom').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('cxMapBorderBottom').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderLR').style.top = (headerHeight+menuHeight+frameWidth*2+mapHeight+footerHeight) + "px";
	document.getElementById('cxMapBorderLR').style.left = (sidebarWidth+borderWidth*2+frameWidth*3+mapWidth) + "px";
	// map
	document.getElementById('theMap').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('theMap').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('theMap').style.width = mapWidth + "px";
	document.getElementById('theMap').style.height = mapHeight + "px";
	// zoom
	document.getElementById('theZoomBox').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('theZoomBox').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('theZoomBox').style.width = mapWidth + "px";
	document.getElementById('theZoomBox').style.height = mapHeight + "px";
	// polyline
	document.getElementById('thePolyline').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('thePolyline').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('thePolyline').style.width = mapWidth + "px";
	document.getElementById('thePolyline').style.height = mapHeight + "px";
	// rubberband
	document.getElementById('theRubberband').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('theRubberband').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('theRubberband').style.width = mapWidth + "px";
	document.getElementById('theRubberband').style.height = mapHeight + "px";
	// mouse capture
	document.getElementById('mouseCapture').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('mouseCapture').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('mouseCapture').style.width = mapWidth + "px";	
	document.getElementById('mouseCapture').style.height = mapHeight + "px";	
	document.getElementById('mouseCaptureImage').style.width = mapWidth + "px";	
	document.getElementById('mouseCaptureImage').style.height = mapHeight + "px";	
	// footer
	document.getElementById('cxFooter').style.top = (headerHeight+menuHeight+frameWidth*2+mapHeight) + "px";
	document.getElementById('cxFooter').style.left = (sidebarWidth+borderWidth*2+frameWidth*3) + "px";
	document.getElementById('cxFooter').style.width = mapWidth + "px";
	document.getElementById('cxFooter').style.height = footerHeight + "px";
	// map tools
	toolbarLeft = sidebarWidth+borderWidth*2+frameWidth*3;
	document.getElementById('cxMapToolsVertical').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsVertical').style.top = (headerHeight+menuHeight+mapHeight+frameWidth-210) + "px";
	document.getElementById('cxMapToolsVertical').style.visibility = "visible";
	document.getElementById('cxMapToolsDial').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsDial').style.top = (headerHeight+menuHeight+mapHeight+frameWidth-54) + "px";
	document.getElementById('cxMapToolsDial').style.visibility = "visible";
	document.getElementById('cxMapToolsHorizontal').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsHorizontal').style.top = (headerHeight+menuHeight+mapHeight+frameWidth-34) + "px";
	document.getElementById('cxMapToolsHorizontal').style.visibility = "visible";
	// locator
	updateOVMap();
	// tool help dialog
	document.getElementById('cxToolHelpDialog').style.top = (headerHeight+menuHeight+frameWidth) + "px";	
	document.getElementById('cxToolHelpDialog').style.left = (sidebarWidth+frameWidth*3+borderWidth*2) + "px";
	document.getElementById('cxToolHelpDialog').style.width = (mapWidth-frameWidth*2) + "px";
	// help
	document.getElementById('cxHelp').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('cxHelp').style.left = (sidebarWidth+frameWidth*3+borderWidth*2) + "px";
	document.getElementById('cxHelp').style.height = mapHeight + "px";
	document.getElementById('cxHelp').style.width = mapWidth + "px";
	// reporting
	document.getElementById('cxPopupMask').style.top = (headerHeight+menuHeight+frameWidth) + "px";
	document.getElementById('cxPopupMask').style.left = (sidebarWidth+frameWidth*3+borderWidth*2) + "px";
	document.getElementById('cxPopupMask').style.height = (mapHeight+footerHeight+frameWidth) + "px";
	document.getElementById('cxPopupMask').style.width = mapWidth + "px";
	document.getElementById('cxPopup').style.top = (headerHeight+menuHeight+frameWidth+25) + "px";
	document.getElementById('cxPopup').style.left = (sidebarWidth+frameWidth*3+borderWidth*2+5) + "px";
	document.getElementById('cxPopup').style.height = (mapHeight+footerHeight+frameWidth-30) + "px";
	document.getElementById('cxPopup').style.width = (mapWidth-10) + "px";
    // entry page (disclaimer)
	document.getElementById('cxEntryPage').style.height = (winHeight-headerHeight) + "px";
	document.getElementById('cxEntryPage').style.width = (winWidth) + "px";
	// spinner
	document.getElementById('cxProtectorSpinner').style.top = (headerHeight) + "px";
	document.getElementById('cxProtectorSpinner').style.left = (sidebarWidth+frameWidth*2+borderWidth*2) + "px";
	document.getElementById('cxProtectorSpinner').style.height = (menuHeight+frameWidth*3+mapHeight+footerHeight) + "px";
	document.getElementById('cxProtectorSpinner').style.width = (mapWidth+frameWidth*2) + "px";
	// timeout before map redraw
	if (doRedraw) {
		window.clearTimeout(reloadTimer);
		reloadTimer = window.setTimeout("if (waitForMap) { reloadInterval=setInterval('checkOkToResize();',500) } else { sendMapRequest(); }",2000);
	}
}

// close popup window
function closeEntryPageDisplay() {
	updateContent("cxEntryPage","");
    document.getElementById('cxEntryPage').style.display = "none";
    return false; 
}

// **************************************
// returns browser size information
// **************************************
function GetPageDimensions() {
	// window size
	if (!window.innerWidth) { //IE
		if (!(document.documentElement.clientWidth == 0)) { //strict mode
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		} else { //quirks mode
			winWidth = document.body.clientWidth;
			winHeight = document.body.clientHeight;
		}
	} else { //w3c
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	}
	// component sizes
	sidebarHeight = winHeight - headerHeight - menuHeight - borderWidth - 32 - 5;
	openSidebarHeight = sidebarHeight - (sidebarFloatCount*sidebarFloatHeight);
	mapHeight = winHeight - headerHeight - menuHeight - borderWidth - frameWidth*3 - footerHeight;
	mapWidth = winWidth - (sidebarWidth+frameWidth*2) - borderWidth*3 - frameWidth*2;
	// map interaction variables
	iWidth = mapWidth;
	iHeight = mapHeight;
	iTop = (headerHeight+menuHeight+frameWidth);
	iLeft = (sidebarWidth+borderWidth*2+frameWidth*3);
	iRight = iLeft + iWidth;
	iBottom = iTop + iHeight;
	return false;
}

// **************************************
//   page protection controls
// **************************************
// turn protectors on
function ProtectorOn() {
    document.getElementById('cxFooter').style.visibility = 'hidden';
    document.getElementById('cxProtector').style.visibility = 'visible';
    document.getElementById('cxProtectorSpinner').style.visibility = 'visible';
    return false;
}
// turn protectors off
function ProtectorOff() {
    document.getElementById('cxProtector').style.visibility = 'hidden';
    document.getElementById('cxProtectorSpinner').style.visibility = 'hidden';
    document.getElementById('cxFooter').style.visibility = 'visible';
    return false;
}

// **************************************
//   popup page controls
// **************************************
// open popup window with specified URL
function openPopupDisplay(theURL) {
    // hide protector overlay
    ProtectorOff();
    clickFunction("nothing");
    if (theURL.toUpperCase().indexOf("ERROR") == -1) {
        var theString = '<iframe id="InlinePrintFrame" name="InlinePrintFrame" width="100%" height="100%" scrolling="auto" frameborder="no" src="'+theURL+'" border="0"></iframe>';
        updateContent('cxPopup',theString);
        document.getElementById('cxPopup').style.overflow = "hidden";
        document.getElementById('cxPopupMask').style.display = "inline";  
        document.getElementById('cxPopup').style.display = "inline";
    } else {
        alert(theURL);
    }
}

// close popup window
function closePopupDisplay() {
    document.getElementById('cxPopupMask').style.display = "none";  
    document.getElementById('cxPopup').style.display = "none";
    return false; 
}

// **************************************
//   help
// **************************************
// turn help popup on/off
function toggleHelp() {
	// reset menus and tools
	resetAllMenus();
	resetDropdownItems();
	hideAllDropdowns();
	hideHelp();
	if (showUserHelp) {
		showUserHelp = false;
		theCXForm.cxShowHelpNotes.checked = false;
	} else {
		showUserHelp = true;
		theCXForm.cxShowHelpNotes.checked = true;
	}
}

// **************************************
//   OV map
// **************************************
// toggle ov map visibility
function toggleOVMap() {
    moveOVMapCount = 0;
	// reset menus and tools
	resetAllMenus();
	resetDropdownItems();
	hideAllDropdowns();
	hideHelp();
    if (showOverviewMap) {
		theCXForm.cxShowOVmap.checked = false;
	    showOverviewMap = false;
    } else {
		theCXForm.cxShowOVmap.checked = true;
	    showOverviewMap = true;
    }
	updateOVMap();
}

// resize ov map based on extent height/width ratio
function resizeOVMap() {
	// overview image size
	ovHeight = parseInt(limitHeight/limitWidth*ovWidth);
	i2Width = ovWidth;
	i2Height = ovHeight;
	updateOVMap();
	return false;	
}

// ov map resizing/visibility
function updateOVMap() {
	if (showOverviewMap) {
	    document.getElementById('cxOVMapBorder').style.height = (ovHeight+frameWidth) + "px";
	    document.getElementById('cxOVMapBorder').style.width = (ovWidth+frameWidth) + "px";
	    document.getElementById('cxOVMapBorder').style.top = (headerHeight+menuHeight+mapHeight-ovHeight) + "px";	
	    document.getElementById('cxOVMapBorder').style.left = (winWidth-borderWidth-ovWidth-frameWidth*2) + "px";
	    document.getElementById('theOVMap').style.height = ovHeight + "px";
	    document.getElementById('theOVMap').style.width = ovWidth + "px";
	    document.getElementById('theOVMap').style.top = (headerHeight+menuHeight+frameWidth+mapHeight-ovHeight) + "px";	
	    document.getElementById('theOVMap').style.left = (winWidth-borderWidth-ovWidth-frameWidth) + "px";
	    document.getElementById('theOVMapZoomBox').style.height = ovHeight + "px";
	    document.getElementById('theOVMapZoomBox').style.width = ovWidth + "px";
	    document.getElementById('theOVMapZoomBox').style.top = (headerHeight+menuHeight+frameWidth+mapHeight-ovHeight) + "px";	
	    document.getElementById('theOVMapZoomBox').style.left = (winWidth-borderWidth-ovWidth-frameWidth) + "px";
		document.getElementById('cxOVMapBorder').style.visibility = "visible";
		document.getElementById('theOVMap').style.visibility = "visible";
	    document.getElementById('theOVMapZoomBox').style.visibility = "visible";
	    document.getElementById('theOVMapArrow').style.top = (headerHeight+menuHeight+mapHeight-ovHeight) + "px";	
	    document.getElementById('theOVMapArrow').style.left = (winWidth-borderWidth-ovWidth-frameWidth*2) + "px";
	    document.getElementById('theOVMapArrow').className = "CXovMapClose";
	} else {
		document.getElementById('cxOVMapBorder').style.visibility = "hidden";
		document.getElementById('theOVMap').style.visibility = "hidden";
	    document.getElementById('theOVMapZoomBox').style.visibility = "hidden";
        document.getElementById('theOVMapArrow').style.top = (headerHeight+menuHeight+mapHeight+frameWidth-16) + "px";	
        document.getElementById('theOVMapArrow').style.left = (winWidth-borderWidth-frameWidth-16) + "px";
	    document.getElementById('theOVMapArrow').className = "CXovMapOpen";
    }
	return false;
}

// **************************************
//   sidebar controls
// **************************************
function resetSidebarTitles() {
    var titleString = "";
    for (var i=0;i<=(sidebarFloatCount+1);i++) {
        titleString = "cxSidebar" + i + "Title";
	    if (document.getElementById(titleString) != null) document.getElementById(titleString).className = "CXsideBarTextNormal";
	}
	return false;
}

function showSidebar(theSidebar) {
    if (sidebarMode != theSidebar) {
        resetSidebarTitles();
	    // sidebars
	    oldSidebarMode = parseInt(sidebarMode);
	    sidebarMode = parseInt(theSidebar);
        // change sidebar visibility for move
        document.getElementById(("cxSidebar" + oldSidebarMode)).style.visibility = "hidden";
	    // move intervals
	    moveSidebarCount = 0;
        clearInterval(sidebarResizeTimer);
        sidebarResizeTimer = setInterval("moveSidebars()",10);
    }
	return false;
}

function moveSidebars() {
    moveSidebarCount += 1;
    if (moveSidebarCount < moveSteps) {
        // new move interval
	    var moveInt = parseInt(openSidebarHeight/moveSteps);
        var moveSidebarHeight = moveInt*moveSidebarCount;
        var divName = "";
	    // move the sidebars
	    if (sidebarMode > oldSidebarMode) { // moving the sidebars up
	        // move headers
    	    for (var i=oldSidebarMode;i<sidebarMode;i++){
                divName = "cxSidebar" + (i+1) + "Header";
                document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*i+openSidebarHeight-moveSidebarHeight) + "px";
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.height = (openSidebarHeight-moveSidebarHeight) + "px";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*sidebarMode+openSidebarHeight-moveSidebarHeight) + "px";
            document.getElementById(divName).style.height = moveSidebarHeight + "px";
	    } else { // moving the sidebars down
	        // move headers
    	    for (var i=oldSidebarMode;i>sidebarMode;i--){
    	        if (i != 0) {
                    divName = "cxSidebar" + i + "Header";
                    document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*(i-1)+moveSidebarHeight) + "px";
                }
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*oldSidebarMode+moveSidebarHeight) + "px";
            document.getElementById(divName).style.height = (openSidebarHeight-moveSidebarHeight) + "px";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = moveSidebarHeight + "px";
	    }
    } else {
	    // move the sidebars
	    if (sidebarMode > oldSidebarMode) { // moving the sidebars up
	        // move headers
    	    for (var i=oldSidebarMode;i<sidebarMode;i++){
                divName = "cxSidebar" + (i+1) + "Header";
                document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*i) + "px";
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.height = "0px";
            //document.getElementById(divName).style.visibility = "hidden";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = openSidebarHeight + "px";
            document.getElementById(divName).style.visibility = "visible";
	    } else { // moving the sidebars down
	        // move headers
    	    for (var i=oldSidebarMode;i>sidebarMode;i--){
    	        if (i != 0) {
                    divName = "cxSidebar" + i + "Header";
                    document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*(i-1)+openSidebarHeight) + "px";
                }
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*oldSidebarMode+openSidebarHeight) + "px";
            document.getElementById(divName).style.height = "0px";
            //document.getElementById(divName).style.visibility = "hidden";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = openSidebarHeight + "px";
            document.getElementById(divName).style.visibility = "visible";
	    }
        document.getElementById(("cxSidebar" + sidebarMode + "Title")).className = "CXsideBarTextSelected";
        clearInterval(sidebarResizeTimer);
    }
}

// reset sidebars
function resetSidebars() {
	resetSidebarTitles();
	// sidebars
	oldSidebarMode = parseInt(sidebarMode);
	sidebarMode = 0;
	// change sidebar visibility for move
	document.getElementById(("cxSidebar" + oldSidebarMode)).style.visibility = "hidden";
	// move headers
	for (var i=1;i<sidebarFloatCount;i++){
		divName = "cxSidebar" + (i+1) + "Header";
		document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*i) + "px";
	}
	// move & resize sidebars
	divName = "cxSidebar" + oldSidebarMode;
	document.getElementById(divName).style.height = "0px";
	//document.getElementById(divName).style.visibility = "hidden";
	divName = "cxSidebar" + sidebarMode;
	document.getElementById(divName).style.top = (headerHeight+menuHeight+32+sidebarFloatHeight*sidebarMode) + "px";
	document.getElementById(divName).style.height = openSidebarHeight + "px";
	document.getElementById(divName).style.visibility = "visible";
	document.getElementById(("cxSidebar" + sidebarMode + "Title")).className = "CXsideBarTextSelected";
	return false;
}

// *****************************
//    tools/menus/dropdowns
// *****************************
// create tool menus
function createToolMenus() {
    var theString = '';
	// find/search dropdown
	theString = '';
	theString += createMenuItem("menuItem_FindInParcels","writeParcelQueryForm();",menuSpace+txtSearchParcels);
	if (useFindIntersection) theString += createMenuItem("menuItem_FindByIntersection","writeIntersectionQueryForm();",menuSpace+"Find By Street Intersection");
	if (findbylandmarkAvailable) theString += addZoomAreaMenuItems();
	if (latlongptAvailable) { theString += addLatLongPtMenuItems(); }
	updateContent("menuFind",theString);
	// select dropdown
	theString = '';
	theString += createMenuItem("menuItem_Identify","setMenus('nothing');clickFunction('identify');",menuSpace+"Identify");
	theString += createMenuItem("menuItem_SelectOnMap","setMenus('nothing');clickFunction('select');",menuSpace+"Select On Map");
	theString += createMenuItem("menuItem_SelectByLocation","setMenus('nothing');writeBufferForm(false);",menuSpace+"Select By Location (Buffer)");
	theString += createMenuItem("menuItem_SelectByAttribute","setMenus('nothing');writeQueryByAttributeForm();",menuSpace+"Select By Attribute (Query)");
	theString += createMenuItem("menuItem_ClearSelections","setMenus('nothing');clickFunction('clearsel');",menuSpace+"Clear All Selections");
	updateContent("menuSelect",theString);
    // advanced dropdown
	theString = '';
	//if (VEavailable) { theString += createMenuItem("menuItem_VEBirdsEye","openVEBirdsEye();",menuSpace+"Open VE Bird's Eye"); }
	theString += createMenuItem("menuItem_MapGraphicDraw","clickFunction(\'mapgraphic\');",menuSpace+"Add Map Annotation");
	theString += createMenuItem("menuItem_MapGraphicClear","clearMapGraphics();",menuSpace+"Clear Map Annotations");
	if (theString == "") theString += createMenuItem("menuItem_NoAdvanced","setMenus('nothing');",menuSpace+"No Tools Available!");
	updateContent("menuAdvanced",theString);
	// settings dropdown
	theString = '';
	if (showOverviewMap) {
	    theString += createMenuItem("menuItem_OverviewMap","toggleOVMap();","<input type=\"checkbox\" id=\"cxShowOVmap\" checked class=\"CXmeasurement\"/>"+menuSpace+"Reference Map");
	} else {
    	theString += createMenuItem("menuItem_OverviewMap","toggleOVMap();","<input type=\"checkbox\" id=\"cxShowOVmap\" class=\"CXmeasurement\"/>"+menuSpace+"Reference Map");
	}
	if (showUserHelp) {
	    theString += createMenuItem("menuItem_Help","toggleHelp();","<input type=\"checkbox\" id=\"cxShowHelpNotes\" checked class=\"CXmeasurement\"/>"+menuSpace+"Help Notes");
	} else {
    	theString += createMenuItem("menuItem_Help","toggleHelp();","<input type=\"checkbox\" id=\"cxShowHelpNotes\" class=\"CXmeasurement\"/>"+menuSpace+"Help Notes");
	}
	theString += createMenuItem("menuItem_ChangeUnits","writeSetUnitsForm();",menuSpace+"Change Scale Units");
	if (faqURL != "") theString += createMenuItem("menuItem_FAQ","openPopupDisplay('" + faqURL + "');",menuSpace+"Show FAQ");
	if (MetaDataURL != "") theString += createMenuItem("menuItem_Metadata","openPopupDisplay('" + MetaDataURL + "');",menuSpace+"Show Metadata");
	theString += createMenuItem("menuItem_Reset","clickFunction('reset');",menuSpace+"Reset Map");
	updateContent("menuSettings",theString);
    return false;
}

function createMenuItem(theID,theMouseClick,theContent) {
	var theString = '<DIV id="' + theID + '" class="CXmenuItem" oncontextmenu="JavaScript:return false;" onclick="JavaScript: ' + theMouseClick + '" onmouseover="JavaScript:highlightItem(\'' + theID + '\');if (showUserHelp) showHelp(\'' + theID + '\');" >' + theContent + '</DIV>'
	// add to list of active menu items for color control
	menuItemList[menuItemListCount] = theID;
	menuItemListCount += 1;
	return theString;
}

// set menu & dropdown visibility on mouseovers
function setMenus(theTool) {
	// reset everything
	resetAllMenus();
	resetDropdownItems();
	hideAllDropdowns();
	hideHelp();
	// set menu visible (if not nothing)
	if (theTool != "nothing") document.getElementById(theTool).className = "CXmenuTextHighlight";
	showDropdown(theTool);
	return false;
}
// turn highlight off all menus
function resetAllMenus() {
	if (document.getElementById('menutabFind') != null) document.getElementById('menutabFind').className = "CXmenuTextNormal";
	if (document.getElementById('menutabSelect') != null) document.getElementById('menutabSelect').className = "CXmenuTextNormal";	
	if (document.getElementById('menutabAdvanced') != null) document.getElementById('menutabAdvanced').className = "CXmenuTextNormal";
	if (document.getElementById('menutabSettings') != null) document.getElementById('menutabSettings').className = "CXmenuTextNormal";
    return false;
}

// hide all dropdowns
function hideAllDropdowns() {
	if (document.getElementById('menuFind') != null) document.getElementById('menuFind').style.visibility = "hidden";
	if (document.getElementById('menuSelect') != null) document.getElementById('menuSelect').style.visibility = "hidden";	
	if (document.getElementById('menuAdvanced') != null) document.getElementById('menuAdvanced').style.visibility = "hidden";
	if (document.getElementById('menuSettings') != null) document.getElementById('menuSettings').style.visibility = "hidden";
	return false;
}

// show needed dropdown
function showDropdown(theTool) {
	if (theTool == "menutabFind" && document.getElementById('menuFind') != null) document.getElementById('menuFind').style.visibility = "visible";
	if (theTool == "menutabSelect" && document.getElementById('menuSelect') != null) document.getElementById('menuSelect').style.visibility = "visible";
	if (theTool == "menutabAdvanced" && document.getElementById('menuAdvanced') != null) document.getElementById('menuAdvanced').style.visibility = "visible";
	if (theTool == "menutabSettings" && document.getElementById('menuSettings') != null) document.getElementById('menuSettings').style.visibility = "visible";
	return false;
}

// reset menu dropdown
function resetDropdownItems() {
	for (var i=0;i<menuItemListCount;i++) {
		var tempMenuItem = document.getElementById(menuItemList[i]);
		tempMenuItem.className = "CXmenuItem";
	}
	return false;
}

// highlight menu item
function highlightItem(menuItemName) {
	resetDropdownItems();
	document.getElementById(menuItemName).className = "CXmenuItemMouseOver";
	return false;
}

// highlight maptool
function highlightMapTool(maptoolName) {
	// highlight toolbars
	highlightMapToolbars();
	// highlight tool
	resetMapTools();
	document.getElementById(maptoolName).className = "CXtoolBarButtonHighlight";
	if (showUserHelp) showHelp(maptoolName);
	return false;
}

// reset maptool highlights
function resetMapTools() {
    if (toolModeText == "zoomin") { document.getElementById("cxOnMap-ZoomIn").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-ZoomIn").className='CXtoolBarButtonNormal'; }
    if (toolModeText == "zoomout") { document.getElementById("cxOnMap-ZoomOut").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-ZoomOut").className='CXtoolBarButtonNormal'; }
    document.getElementById("cxOnMap-ZoomAll").className='CXtoolBarButtonNormal';
    if (toolModeText == "pan") { document.getElementById("cxOnMap-Recenter").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-Recenter").className='CXtoolBarButtonNormal'; }
    document.getElementById("cxOnMap-ZoomPrevious").className='CXtoolBarButtonNormal';
    if (toolModeText == "identify") { document.getElementById("cxOnMap-Identify").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-Identify").className='CXtoolBarButtonNormal'; }
    if (toolModeText == "select") { document.getElementById("cxOnMap-Select").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-Select").className='CXtoolBarButtonNormal'; }
    if (toolModeText == "measure") { document.getElementById("cxOnMap-Measure").className='CXtoolBarButtonSelected'; } else { document.getElementById("cxOnMap-Measure").className='CXtoolBarButtonNormal'; }
	return false;
}

// highlight maptoolbar set
function highlightMapToolbars() {
	document.getElementById('cxMapToolsVertical').className = "CXtoolBarHighlight";
	document.getElementById('cxMapToolsHorizontal').className = "CXtoolBarHighlight";
	return false;
}

// reset entire maptoolbar set
function resetMapToolbars() {
	resetMapTools();
	document.getElementById('cxMapToolsVertical').className = "CXtoolBar";
	document.getElementById('cxMapToolsHorizontal').className = "CXtoolBar";
	return false;
}

// *************************************
//    printmap support
// *************************************
// set dialog visibility based on printmap type
function SetPrintMapDialog(theType) {
	document.getElementById('cxPrintMapPDF').style.display = "none";
	document.getElementById('cxPrintMapImage').style.display = "none";
	switch(theType) {
	case "nothing":
		window.status="";
		break
	case "pdf":
		document.getElementById('cxPrintMapPDF').style.display = "inline";
		break
	case "image":
	    document.getElementById('cxPrintMapImage').style.display = "inline";
		break
	default:
		//alert("Function Not Available");
	}
}

// *************************************
//    commandline startup support
// *************************************
// search parameters from URL
function getCXCommandLineParams(cmdString) {
	// Parse out from URL querystring parameters to pass to the viewer.
	// Syntax:
	// ActiveLayerID=layerID			ID of layer to be active as named in AXL
	// Query=queryExpression			query expression to be send on load - expression must be escaped in URL
	// LYRVIS=layerID					ID of layer (or group) to turn on at startup
	// OVRLAY=layerID					ID of overlay layer (or group) to turn on at startup
	var cmdString2 = unescape(cmdString.toUpperCase());
	var endpos = 0;
	var startpos = 0;
	// query layer
	var pos = cmdString2.indexOf("ACTIVELAYERID=");
	if (pos != -1) {
		startpos = pos + 14;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var actlyr = cmdString.substring(startpos,endpos);
		setActiveLayer(actlyr,false,false);
	}	
	// query string
	pos = cmdString2.indexOf("QUERY=");
	startQueryString = "";
	if ((pos != -1) && (ActiveLayerIndex != -1)) {
		startpos = pos + 6;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var escQuery = cmdString.substring(startpos,endpos);
		// expected format of query is query=fieldname%3D'fieldvalue'%20or%20fieldname%3D'fieldvalue'.....  (%3D="=",%22=""",%20=" ", use ' is ok...no code)
		escQuery = replacePlus(escQuery);
		startQueryString = unescape(escQuery);
	}	
	// set layer visibility
	pos = cmdString2.indexOf("LYRVIS=");
	if (pos!=-1) {
		startpos = pos + 7;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var tempString = unescape(cmdString.substring(startpos,endpos));
		var tempArray = tempString.split(":");
		for (var k=0;k<tempArray.length;k++) {
			for (var i=0;i<tocOptionalList.length;i++) {
				if (tempArray[k] == tocOptionalList[i][1]) {
					tocOptionalList[i][3] = "true";
					if (isNaN(tocOptionalList[i][2])) {  // is a group
						TurnGroupOptionalOn(tempArray[k]);
					} else { // is a single layer
						LayerVisible[tocOptionalList[i][2]] = "true";
					}
				}
			}
		}
	}
	// set map type (overlays)
	pos = cmdString2.indexOf("OVRLAY=");
	if (pos!=-1) {
		startpos = pos + 7;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var tempString = unescape(cmdString.substring(startpos,endpos));
		for (var i=0;i<tocOverlayList.length;i++) {
			if (tempString == tocOverlayList[i][1]) {
				if (isNaN(tocOverlayList[i][3])) {  // is a group
					TurnGroupOverlayOn(tempString);
				} else { // is a single overlay layer
					LayerVisible[tocOverlayList[i][3]] = "true";
					tocOverlayCurrent = tempString;
					tocOverlayCurrentType = LayerTypeUser[tocOverlayList[i][3]];
				}
			}
		}
	}
	// refresh layer TOC
	createTOC();
	// write starting query string or just draw map
	if (startQueryString != "") {
		var theString = writeQueryXML(startQueryString);
		zoomToSelected = true;
		webParams = "";
		sendToServer(imsQueryURL,theString,8);
	} else {
		sendMapRequest();
	}
}

