/*
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 taskBox = null;
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, map1;

function initLayout() {
  map1 = EsriControls.maps["map1"];
  toc1 = EsriControls.tocs["toc1"];
  ov1 = EsriControls.overviews["ov1"];

  loadTaskCenter();
  resizeMap();

  EsriControls.addPostBackTagHandler("task-result", taskResultHandler);
  EsriControls.addPostBackTagHandler("mapviewer-elements", mapViewerElementsHandler);
  EsriControls.tocs["resultsToc"].addUpdateListener("resultsDivShowHide", resultsDivShowHide);
  if (ov1 && map1.numLevels > 0)
    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 loadTaskCenter() {
  var cont = document.getElementById("taskCenterPanel");
  taskBox = new TaskBox(cont, formId + ":taskWindows");
  taskBox.windowForm = document.getElementById(formId);
  if (ov1)
    taskBox.overviewId = ov1.id;

  if(document.getElementById("taskPanelContent").innerHTML.trim().length > 0)
    taskBox.addPanel(document.getElementById("taskPanelContent"), Res.getString("Tasks"), false);
  taskBox.addPanel(document.getElementById("resultsPanelContent"), Res.getString("Results"), false);
  taskBox.addPanel(document.getElementById("resultDetailsPanelContent"), Res.getString("Result Details"), true, formId);
  if(toc1)
    taskBox.addPanel(document.getElementById("tocPanelContent"), Res.getString("Map Contents"), true, formId, toc1.bounds);
  taskBox.addPanel(document.getElementById("navPanelContent"), Res.getString("Navigation"), true, formId);
  if(ov1)
    taskBox.addPanel(document.getElementById("overviewPanelContent"), Res.getString("Map Overview"), true, formId, ov1.bounds);

  taskBox.hidePanel("resultsPanelContent");
  taskBox.hidePanel("resultDetailsPanelContent");
  if(toc1) taskBox.hidePanel("tocPanelContent");
  taskBox.hidePanel("navPanelContent");
}

function togglePanel() {
  EsriUtils.toggleElement(document.getElementById(panelId));
  resizeMap();
}

function handleResize(e) {
  resizeCounter++;
  resizeLayout();
  setTimeout('throttleResize(' + resizeCounter + ')', 250);
}

function resizeMap() {
  var dimensions = resizeLayout();
  if (dimensions[0] != map1.bounds.width || dimensions[1] != map1.bounds.height)
    map1.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 setStatus(s) {
  document.getElementById(statusId).innerHTML = s + "<br/>" + document.getElementById(statusId).innerHTML;
}

function resultsDivShowHide(toc) {
  if (toc.nodes.length == 0)
    taskBox.hidePanel("resultsPanelContent");
  else
    taskBox.showPanel("resultsPanelContent");
}

function taskResultHandler(xml, eventSources) {
  var table = document.getElementById("task-result-table");
  while (table.hasChildNodes())
    table.removeChild(table.firstChild);

  var tbody = table.appendChild(document.createElement("tbody"));
  var actions = xml.getElementsByTagName("action");
  if (actions.length > 0) {
    var actionsTd = tbody.appendChild(document.createElement("tr")).appendChild(document.createElement("td"));
    actionsTd.colSpan = "2";

    var span = actionsTd.appendChild(document.createElement("span"));
    span.appendChild(document.createTextNode(Res.getString("Actions") + ":  "));
    span.className = "med";
    EsriUtils.setElementStyle(span, "color:#000; font-weight:bold;");

    for (var i=0;i<actions.length;i++) {
      var a = actionsTd.appendChild(document.createElement("a"));
      a.href = "#";
      a.className = "med";
      EsriUtils.setElementStyle(a, "color:#000;");
      var action = actions.item(i).firstChild.nodeValue;
      a.setAttribute("onclick", "return requestTaskDetailsAction('" + action + "');");
      a.appendChild(document.createTextNode(action));
      actionsTd.innerHTML += "&nbsp;&nbsp;";
    }
  }

  if (! taskDetailsHeader) {
    taskDetailsHeader = document.createElement("tr");
    var nameTd = taskDetailsHeader.appendChild(document.createElement("td"));
    nameTd.className = "med";
    EsriUtils.setElementStyle(nameTd, "color:#000000; font-weight:bold;");
    nameTd.appendChild(document.createTextNode("Name"));
    var valueTd = taskDetailsHeader.appendChild(document.createElement("td"));
    valueTd.className = "med";
    EsriUtils.setElementStyle(valueTd, "color:#000000; font-weight:bold;");
    valueTd.appendChild(document.createTextNode("Value"));
  }
  tbody.appendChild(taskDetailsHeader);

  var details = xml.getElementsByTagName("detail");
  for (var i=0;i<details.length;i++) {
    var detail = details.item(i);
    var tr = tbody.appendChild(document.createElement("tr"));
    var key = tr.appendChild(document.createElement("td"));
    key.appendChild(document.createTextNode(detail.getAttribute("key")));
    key.className = "med";
    EsriUtils.setElementStyle(key, "color:#000000;");

    var value = tr.appendChild(document.createElement("td"));
    value.appendChild(document.createTextNode(detail.getAttribute("value")));
    value.className = "med";
    EsriUtils.setElementStyle(value, "color:#000000;");
  }

  taskBox.showPanel("resultDetailsPanelContent");
}

function requestTaskDetailsAction(action) {
  map1.showLoading();
  var params = "mapViewerAjax=mapViewerAjax&formId=" + formId + "&" + formId + ":an=" + action + "&" + formId + ":pa=" + formId + ":pa&" + EsriUtils.buildRequestParams(formId);
  var xh = EsriUtils.sendAjaxRequest(EsriUtils.getServerUrl(formId), params, false, function() { EsriControls.processPostBack(xh); });
  return false;
}

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 mapNavigator = new EsriMapNavigator("mapNavigator", null, map1.id);
  mapNavigator.image_map_move_shape = mapNavigator.image_map_move_shape = null;
  mapNavigator.init(document.getElementById("nav-container"));

  if (map1.numLevels > 0)
    var mapSlider = new EsriMapSlider("mapSlider", document.getElementById("slider-container"), map1.id, map1.numLevels - 1, map1.level);
}

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;
  }
}