﻿
// Danville.js
var offsetHeight = 79;
var mapId = null;
var headerPadding = 1;
var headerArea = null;
var ovMapArea = null;
var ovMapCornerButton = null;
var mapArea = null;
var mapMenuBar = null;
var statusBar = null;
var tabArea = null;
var tabContent = null;
var tocDiv = null;
var wvsNavigationArea = null;
var wvsLoadingImage = null;
var coordDisplayArea = null;
var reloadTimer = null;


function selectedItemChanged(sender, e)
{
	$get(hdnPanelResultsTableId).value = e.table;
	$get(hdnPanelResultsIDId).value = e.id;
	$get(btnGetPanelResultsId).click();
	e.cancel = true;
};

// function run at startup
function startUp() 
{   
    pushpinPanel = $find(pushpinPanelId);
    $find(taskResultsId).add_selectedItemChanged(selectedItemChanged);
    
$find(taskResultsId).set_vectorStyle(new MapsDirect.Vector.Style("solid","red", 4, 0.9, "#FFFF00", 0.3));

$find(zoomSliderId).mouseMoveSkipCount = 3;
//    if (document.documentElement) {
//        document.documentElement.style.overflow = "hidden";
//        document.documentElement.style.height = "100%"; 
//    } else {
//        document.body.style.overflow = "hidden";
//        document.body.style.height = "100%";
//    }         
    
    if(isDefined("editor"))initEditor();
    
    document.body.focus();
	
	map = $find(mapId); 
	
	//SET THIS TO TRUE TO PREVENT DYNAMIC TILE GENERATION DURING SCROLLWHEEL ZOOMING	
    map.useSimpleZoom=false;															    
	map.add_requestTiles(showLoading);
	map.add_requestTilesComplete(hideLoading);
	map.set_dynamicLayerOpacity(90);
	
	// set the default click tool to the identify tool
	identify.setDefaultTool();
	identify.setPixelTolerance(10);
		
     // set window resize event handler
	window.onresize = AdjustMapSizeHandler; 
	AdjustMapSize(); 
}

    

	

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustMapSize(true);",1000);
}

// function for adjusting element sizes when brower is resized
function AdjustMapSize() {
	
	
	var screenBounds = getClientBounds();
    var mapArea = $get(mapId)	;
	var headerBounds = Sys.UI.DomElement.getBounds($get("header"));	 
	
	// move the tab area
    var tabArea = $get("tabArea"); 
    tabArea.style.top = headerBounds.height + "px";
    var tabBounds = Sys.UI.DomElement.getBounds($get("tabArea"));
    	
    // move the slitter
	var splitter = $get("splitter");
	splitter.style.left = (tabBounds.x + tabBounds.width) + "px";
    var splitterBounds = Sys.UI.DomElement.getBounds(splitter);	     
    
    // move the toolbar
    var toolbar = $get("toolBar");    
    toolbar.style.top = headerBounds.height + "px";
    var toolbarBounds = Sys.UI.DomElement.getBounds(toolbar);
    
    
    // status bar 
    var statusBar = $get("statusBar");
    var statusBounds = Sys.UI.DomElement.getBounds(statusBar);
    
    // calc dimensions needed for map
    var appFramePixels = 6;
	var mapWidth = screenBounds.width - tabBounds.x - tabBounds.width - splitterBounds.width;
	var mapHeight = screenBounds.height - headerBounds.y - headerBounds.height - toolbarBounds.height - statusBounds.height - appFramePixels; 	
	
	// adjust for css border around map
	mapWidth -= 4;
	
    if (mapWidth < 5) mapWidth = 200;
    if (mapHeight < 5) mapHeight = 200;

	// set map size	 	
	mapArea.style.position = "absolute";
	mapArea.style.left = (splitterBounds.x + splitterBounds.width) + "px";
	mapArea.style.top = (toolbarBounds.y + toolbarBounds.height) + "px";
	
	    

    //alert("map.resize("+mapWidth+", " + mapHeight + ", true); ");	
	    															
	map.resize(mapWidth, mapHeight, true);   
	
    var mapBounds = Sys.UI.DomElement.getBounds(mapArea);	
	
	var loading = $get("loadingImage");
	loading.style.left = (parseInt(mapArea.style.left) + 1) + "px";	
	loading.style.top = mapArea.style.top;
	
	
	
	// set the tab area
    tabArea.style.height = (toolbarBounds.height + mapBounds.height + statusBounds.height - 1) + "px";
    var tabSize = Sys.UI.DomElement.getBounds($get("tabTOC"));
    $get("tabContent").style.height = (parseInt(tabArea.style.height) - tabSize.height) + "px";
   
    var taskResults = $find(taskResultsId);
    taskResults.set_width(parseInt(tabArea.style.width));
    taskResults.set_height(mapHeight + statusBounds.height - 7);    
    
    var toc = $find(tocId);
    toc.set_width(taskResults.get_width());
    toc.set_height(taskResults.get_height());
    
    var progressSearch = $get("progreeSearchLoader");
    progressSearch.style.width = taskResults.get_width() + "px";
	progressSearch.style.height = taskResults.get_height() + "px";	
    $get("progreeSearchLoaderImage").style.top = ((taskResults.get_height() / 2) - 20) + "px";
    
    // splitter height    
    splitter.style.top = tabArea.style.top;
    splitter.style.height = tabArea.style.height;
    
    // set the toolbar size
    toolbar.style.left = mapArea.style.left;
	toolbar.style.width = mapWidth + "px";
		
    // move the statusbar
    statusBar.style.left = mapArea.style.left;
    statusBar.style.width = mapWidth + "px";
    statusBar.style.top = (mapBounds.y + mapBounds.height) + "px";
    		
	 var zoomSlider = $get(zoomSliderId);
	 var zoomZoomSliderBounds = Sys.UI.DomElement.getBounds(zoomSlider);	 
	 zoomSlider.style.position = "absolute";
	 zoomSlider.style.top = (mapBounds.y + 20) + "px";
	 zoomSlider.style.left = (screenBounds.width - zoomZoomSliderBounds.width - 20) + "px";
	 zoomSlider.style.zIndex = 100;
	 
	 var cacheButtons = $get(cachedButtonListId);
	 var cacheButtonsBounds = Sys.UI.DomElement.getBounds(cacheButtons);
	 cacheButtons.style.top = (mapBounds.y + 20) + "px";
	 cacheButtons.style.left = (screenBounds.width - cacheButtonsBounds.width - zoomZoomSliderBounds.width - 40) + "px";
	 
	 var ovmap = $get("ovmap");
	 var ovmapBounds = Sys.UI.DomElement.getBounds(ovmap); 	 
	 
	 
	 ovmap.style.top = parseInt(mapArea.style.top) + mapHeight - ovmapBounds.height + "px";
	 
	 //alert("set overview map top to " + ovmap.style.top + " based on: parseInt(mapArea.style.top)=" + parseInt(mapArea.style.top) + ", mapHeight=" + mapHeight + ", ovmapBounds.height=" + ovmapBounds.height);
	 
	 ovmap.style.left = parseInt(mapArea.style.left) + mapWidth - ovmapBounds.width + "px";
	 
	 var ovmapButton = $get("ovmapButton");
	 var ovmapButtonBounds = Sys.UI.DomElement.getBounds(ovmapButton);
	 ovmapButton.style.top = parseInt(mapArea.style.top) + mapHeight - ovmapButtonBounds.height + "px";
	 ovmapButton.style.left = parseInt(mapArea.style.left) + mapWidth - ovmapButtonBounds.width + "px";

    return false;
}

var splitterHalfWidth = 0;
var splitter;
function splitterMouseDown()
{
	splitter = $get("splitter");
	splitterHalfWidth  = parseInt(splitter.style.width) / 2;
	splitter.style.backgroundColor = "#333333";
	splitter.style.zIndex = 9999999;
	document.onmousemove = splitterMouseMove;
	document.onmouseup = splitterMouseUp;
	return false;
}

function splitterMouseMove(e)
{
	if(!e)	e = window.event;
	var x = getMouseX(e);
	if(x > 200 && x < 600)
		splitter.style.left = (x - splitterHalfWidth) + "px";
	return false;
}

function splitterMouseUp(e)
{
	if(!e)	e = window.event;
	var x = getMouseX(e);
	splitter.style.backgroundColor = "";
	splitter.style.zIndex = null;
	document.onmousemove = null;
	document.onmouseup = null;
	
	
	// move the tab area
    var tabArea = $get("tabArea"); 
    var width = parseInt(splitter.style.left);
    tabArea.style.width = splitter.style.left;
    $find(taskResultsId).set_width(width);
    $find(tocId).set_width(width);
    
    AdjustMapSize();
    
	return false;
}


function showLoading() {
    //alert('loading');
    $get("loadingImage").style.visibility = "visible";
}


function hideLoading() {
   $get("loadingImage").style.visibility = "hidden";
} 

function Mod(X, Y) {
	return X - Math.floor(X / Y) * Y;
}

function HideOverviewMap(){
	var ovmap = $get("ovmap");
	var ovmapButton = $get("ovmapButton");
	if(ovmap.style.display == 'none'){
		ovmap.style.display = ''; 			
		ovmapButton.src = ovmapButton.src.replace('up', 'down');
	}else{
		ovmap.style.display = 'none';
		ovmapButton.src = ovmapButton.src.replace('down', 'up');
	}
	return false;
};

function toggleHelpPanel(link, panelId){
	var panel = $get(panelId);
	if(panel.style.display == "none"){
		link.innerHTML = "hide help";
		panel.style.display = "block";
	}else{
		link.innerHTML = "show help";
		panel.style.display = "none";
	}
	return false;
}

// function to set extent to given bookmark
function ResetBookmark(ddl) {
	if(ddl.options[ddl.selectedIndex].value != ''){
		ddl.SelectedIndex = 0;
	}
}

function SwitchTab(tabName){
    SetTab('Search', (tabName == 'search' ? true : false));
	SetTab('TOC', (tabName == 'toc' ? true : false));
	SetTab('Results', (tabName == 'results' ? true : false));
}

function SetTab(tabName, isVisible){
	var className = isVisible ? 'tabOn' : 'tabOff';
	$get('tab' + tabName).className = className;
	$get('tabLink' + tabName).className = className;
	$get('tabContent' + tabName).style.display = isVisible ? 'block' : 'none';
}

function changeMap(mapName){
	var map = $find(mapId);
	var services = "";
	for(var key in map.get_resources())
    {			
		var resource = map.get_resources()[key];
        if(Object.getTypeName(resource) == "MapsDirect.WebControls.CachedResource") {
	        services += "&" + key + "=" + resource.get_visible();
	    }
    }
	window.location = "default.aspx?service=" + mapName +"&" + map.get_extent().toString() + services;
};

 function toogleTabArea(){
	var tabArea = $get("tabArea");
	var splitter = $get("splitter");
	var splitterButton = $get("splitterButton");
	
	if(tabArea.style.display == "block"){
		tabArea.style.display = "none";
		splitterButton.style.display = "block";
		splitterExpandedLeft = splitter.style.left;
		splitterExpandedWidth = splitter.style.width;
		splitterExpandedCursor = splitter.style.cursor;
		splitterExpandedOnmousedown = splitter.onmousedown;
		splitter.style.left = "0px";
		splitter.style.width = "23px";
		splitter.style.cursor = "default";
		splitter.onmousedown = null;
	}else{
		splitterButton.style.display = "none";
		splitter.style.width = "5px";
		splitter.style.width = splitterExpandedWidth;
		splitter.style.left = splitterExpandedLeft;
		splitter.style.cursor = splitterExpandedCursor;
		splitter.onmousedown = splitterExpandedOnmousedown;
		tabArea.style.display = "block";
		
	}
	AdjustMapSize();
  }
 
function isDefined(variable){ 
	return (typeof(window[variable]) != "undefined");
}