﻿// GeoDecisions GeoPRIME ArcGIS Server Map Component version 0.1
// uses geoDecisions.config.mapConfig to create the map, then runs the other tools
// Adam Conner 2/15/09

//the geoDecisions objects holds *all global variables (it should, but doesn't yet)
var geoDecisions = {};

// creating the map, all layers are defined in the mapConfig file\

var layerLength = 0;
var baseLength; 
var baseLayers = new Array();
var measureShow, searchShow, idShow, layersShow;
var mapConfig;

//initializing the page and all the JS 
//this function called once the page is loaded
function configAll()
{
	dojo.xhrGet( {
        // The following URL must match that used to test the server.
        url: "config.js", 
        handleAs: "json",
        load: function(responseObject, ioArgs) { 
          // Now you can just use the object
          //console.dir(responseObject);  // Dump it to the console          
          geoDecisions.config = responseObject;
		  mapInit();
        }});
}

function mapInit() 
{
		
	baseLength = geoDecisions.config.mapConfig.baseLayers - 1;
	if (kU() == "true") 
	{
		document.title = geoDecisions.config.mapConfig.pageTitle;
		//adjusting the esri Configurations
		esri.config.defaults.map.slider = {
			left: "15px",
			top: "45px",
			height: "200px;"
		};
		esri.config.defaults.io.proxyUrl = geoDecisions.config.mapConfig.proxyUrl;
		esri.config.defaults.io.alwaysUseProxy = false;
		
		geoDecisions.map = new esri.Map("MapDIV");
		//map layer definitions
		for (var i in geoDecisions.config.mapConfig.layers) {
			if (geoDecisions.config.mapConfig.layers[i].type == "tiles") {
				var layer = new esri.layers.ArcGISTiledMapServiceLayer(geoDecisions.config.mapConfig.layers[i].url, {
					id: geoDecisions.config.mapConfig.layers[i].id,
					opacity: geoDecisions.config.mapConfig.layers[i].opacity
				});
				geoDecisions.map.addLayer(layer);
				if (geoDecisions.config.mapConfig.layers[i].visibility == false) {
					layer.hide();
				}
				if (i <= baseLength) {
					baseLayers.push(geoDecisions.config.mapConfig.layers[i].id);
				}
			}
			else {
				var layer = new esri.layers.ArcGISDynamicMapServiceLayer(geoDecisions.config.mapConfig.layers[i].url, {
					id: geoDecisions.config.mapConfig.layers[i].id,
					opacity: geoDecisions.config.mapConfig.layers[i].opacity
				});
				geoDecisions.map.addLayer(layer);
				if (geoDecisions.config.mapConfig.layers[i].visibility == false) {
					layer.hide();
				}
				if (i <= baseLength) {
					baseLayers.push(geoDecisions.config.mapConfig.layers[i].id);
				}
			}
		}
		
		//adding the drawing toolbar (used for draw tools and measure tool)
		geoDecisions.tb = new esri.toolbars.Draw(geoDecisions.map);
		//adding the toolsets if they have been included in the page
		if (dojo.byId("idButton")) {
			idDialogbuilder();
		}
		if (dojo.byId("searchButton")) {
			searchDialogBuilder();
		}
		if (dojo.byId("addressSearch")) {
			addressSearchBuilder();
		}
		if (dojo.byId("measureButton")) {
			measureDialogBuilder();
		}
		if (dojo.byId("overviewButton")) {
			geoDecisions.overview.build();
		}
		if (dojo.byId("coordinatesButton")) {
			geoDecisions.coordinates.build();
		}
		//listening for layers to be loaded for building the legend
		if (dojo.byId("layersButton")) {
			dojo.connect(geoDecisions.map, "onLayerAdd", layerHandle);
		}
		else {
			splash.hide();
		}
	}
	else
	{
		alert("The Key associated with this site is not valid, please contact GeoDecisions and let them know a key is needed for " + window.location.host);
	}
}

//creating the 'splash' object and associating its methods
var splash = {
	hide : hideSplash,
	show : showSplash
}

//the function run by splash.hide();
function hideSplash()
{
    if (dojo.byId("load"))
    {
    	dojo.byId("load").style.display = "none";
    }
}

//the function run by splash.show();
function showSplash()
{
    if (dojo.byId("load"))
    {
    	dojo.byId("load").style.display = "block";
    }
}

function kU()
{
	if (window.location.host == key.domain) 
	{
		return "true";
	}
	else 
	{
		return "true";
		//return "false";
	}
}

function layersDialog()
{
    if (layersShow)
    {
        geoDecisions.hideListeners("layersShow();");
    }
    else 
    {
        dijit.byId('toast').setContent('The layers tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

function idDialog()
{
    if (idShow)
    {
        geoDecisions.hideListeners("idShow();");		
    }
    else
    {
        dijit.byId('toast').setContent('The identify tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

function searchDialog()
{
    if (searchShow)
    {
        geoDecisions.hideListeners("searchShow();");
    }
    else
    {
        dijit.byId('toast').setContent('The search tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

function measureDialog()
{
    if (measureShow)
    {
		geoDecisions.hideListeners("measureShow();");
        
    }
    else
    {
        dijit.byId('toast').setContent('The measure tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

function overviewDialog()
{
    if (geoDecisions.overview)
    {
        geoDecisions.overview.show();
    }
    else
    {
        dijit.byId('toast').setContent('The overview tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

function coordinatesDialog()
{
    if (geoDecisions.coordinates)
    {
		geoDecisions.hideListeners("geoDecisions.coordinates.show();");
    }
    else
    {
        dijit.byId('toast').setContent('The coordinates tool has not been correctly included in this application', 'warning', '500');
        dijit.byId('toast').show();
    }
}

geoDecisions.hideListeners = function(inputFunc)
{
	if (searchShow) {
		dijit.byId("resultsDialog").hide();
	}
	if (idShow) {
		dijit.byId("idDialog").hide();
	}
	if (measureShow) {
		dijit.byId("measureDialog").hide();
	}
	if (geoDecisions.coordinates) {
        geoDecisions.coordinates.hide();
    }
	if (searchShow){
		dijit.byId("searchDialog").hide();
	}
	if (layersShow){		
		dijit.byId("layersDialog").hide();
	}	
	if (inputFunc) {
		var t = setTimeout(inputFunc, 100);
	}
	else {
		setTimeout("dijit.byId('addressText').focus()",300);
	}
}
