/*
COPYRIGHT 1995-2005 ESRI

TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the 
Copyright Laws of the United States.

For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373

email: contracts@esri.com
*/

var ieBuffer = 0;
var ieContentStyle = "left:0px;";
if(window.navigator.appName.toLowerCase().indexOf("microsoft")>=0) {
  ieBuffer = 3;
  ieContentStyle = "left: -" + ieBuffer + "px;";
}

var formId = "mapForm";
var taskDetailsHeader;

var toolbarId = "toolbar";
var layoutSliderId = "layoutSlider";
var headerId = "header";
var contentId = "content";
var pageId = "page";
var panelId = "panel";
var statusId = "statusDiv";

var mapMargin = 10;
var mapBorder = 1;
var resizeCounter = 1;
var scaleBar, northArrowImg;
var toc1, ov1;

function initLayout() {
  toc1 = EsriControls.tocs["toc1"];
  ov1 = EsriControls.overviews["ov1"];

  resizeMap();

  EsriControls.addPostBackTagHandler("mapviewer-elements", mapViewerElementsHandler);
  if (ov1)
    ov1.isContinuousPan = true;

  if (toc1) {
    toc1.bounds.width = 300;
    toc1.bounds.height = 400;
  }

  closeTaskWindows();
  initNavAndSlider();
  initNorthArrow();
  window.onresize = handleResize;
  var slider = document.getElementById(layoutSliderId);
  slider.onclick = togglePanel;
}

function togglePanel() {
  EsriUtils.toggleElement(document.getElementById(panelId));
  resizeMap();
}

function handleResize(e) {
  resizeCounter++;
  resizeLayout();
  setTimeout('throttleResize(' + resizeCounter + ')', 250);
}

function resizeMap() {
  var dimensions = resizeLayout();
  var map = EsriControls.maps['map'];

  if (routePanel)
    dimensions[1] = dimensions[1] - EsriUtils.getElementPageBounds(routePanel).height;
  else
    dimensions[1] = dimensions[1] - routeTitleHt;
  if (dimensions[0] != map.bounds.width || dimensions[1] != map.bounds.height)
    map.resize(dimensions[0], dimensions[1]);
}

function resizeLayout() {
  var hdr = document.getElementById(headerId);
  var layout = document.getElementById(pageId);
  var panel = document.getElementById(panelId);
  var content = document.getElementById(contentId);
  var slider = document.getElementById(layoutSliderId);
  
  var layoutBounds = EsriUtils.getElementPageBounds(layout);
  var panelBounds = EsriUtils.getElementPageBounds(panel);
  var sliderBounds = EsriUtils.getElementPageBounds(slider);
  var hdrBounds = EsriUtils.getElementPageBounds(hdr);
  var bounds = EsriUtils.getPageBounds();
  
  if(panel.style.display == "none")  //ie doesn't get 0 width when panel is collapsed
    panelBounds.width = 0;

  var bottomHeight = bounds.height - hdrBounds.height;
  var contentWidth = bounds.width - (sliderBounds.width + sliderBounds.left) - ieBuffer;
  var newContentH = ((bottomHeight > 0) ? bottomHeight : 0);
  var newContentW = ((contentWidth > 0) ? contentWidth : 0);
  var contentClass = EsriUtils.getStyleByClassName("#content");
  var newContentStyle = "position:relative; width:" + newContentW + "px; height: " + newContentH + "px; top:0px; " + ieContentStyle;
  var newLayoutStyle = "height: " + bounds.height + "px;";
  var newPanelStyle = "height: " + (bounds.height - hdrBounds.height) + "px;";
  EsriUtils.setElementStyle(layout, newLayoutStyle);
  EsriUtils.setElementStyle(panel, newPanelStyle);
  EsriUtils.setElementStyle(slider, newPanelStyle);
  EsriUtils.setElementStyle(content, newContentStyle);
  return [newContentW, newContentH];
}

function throttleResize(val) {
  if(val != resizeCounter)
    return true;
  resizeMap();
  return true;
}

function toggleWindow(id) {
  if(taskWindowManager && taskWindowManager.windows[id])
    taskWindowManager.windows[id].toggleVisibility();
}

function closeTaskWindows() {
  var props = taskWindowManager.getProperties();
  for (var i=0;i<taskWindowManager.windowIds.length;i++) {
    var id = taskWindowManager.windowIds[i];
    if (props.indexOf(id) == -1) {
      var win = taskWindowManager.windows[id];
      win.center();
      win.hide();
    }
  }
}

function initNavAndSlider() {
  var map = EsriControls.maps["map"];
  var navi = new EsriMapNavigator("navi", null, map.id);
  navi.image_map_move_shape = navi.image_map_move_shape = null;
  navi.init(document.getElementById("nav-container"));
  for (var i=0; i < map.mapSourceNames.length; i++) {
    if (map.mapSources[map.mapSourceNames[i]].type == "tile") {
      var slider = new EsriMapSlider("slider", null, map.id, (map.mapSources[map.mapSourceNames[i]].numLevels) ? (map.mapSources[map.mapSourceNames[i]].numLevels - 1) : 10, (map.mapSources[map.mapSourceNames[i]].level) ? map.mapSources[map.mapSourceNames[i]].level : 0);
      slider.init(document.getElementById("slider-container"));
      break;
    }
  }
}

function initNorthArrow() {
  var northarrowDiv = document.getElementById("northarrow");
  if (northarrowDiv) {
    northArrowImg = northarrowDiv.appendChild(EsriUtils.createImage(document.getElementById(formId + ":northarrowHid").value, "100px", "100px"));
    northArrowImg.id = "northArrowImg";
  }
}

function mapViewerElementsHandler(xml, eventSources) {
  var northarrowTags = xml.getElementsByTagName("north-arrow-url");
  if (northArrowImg && northarrowTags.length > 0 && northarrowTags.item(0).hasChildNodes()) {
    var src = northarrowTags.item(0).firstChild.nodeValue;
    if (EsriUtils.isIE) {
      var img = EsriUtils.createImage(src, "100px", "100px");
      img.id = northArrowImg.id;
      northArrowImg.parentNode.replaceChild(img, northArrowImg);
      northArrowImg = img;
    }
    else
      northArrowImg.src = src;
  }
}