/****************************************************************************
 * @file          main.js
 *
 * @project       YAMI (Yet Another Mapping Interface)
 * @contributors  Rose Jackson, William Garrick, Eric Hanson, Morgan Harvey,
 *                Cristopher Holm, David Percy       
 * 
 * Copyright (c) 2006, Academic & Research Computing, Portland State 
 *                University, Portland Oregon.
 * 
 ***************************************************************************/

if (document.all && !document.getElementById) {
  document.getElementById = function(id) {
    return document.all[id];
  }
}

if (window.ActiveXObject)
  var isIE=true;
else
  var isIE=false;

// declare global objects
var queue;
var map;
var panels;
var rubber;
var currentMapState;
var currentLayerState;
var infoTabs;

var mapdiv,loadingdiv,headerdiv,updatediv,panelsdiv;
var toolspaneldiv,groupspaneldiv,tooltipsdiv,mapfile;
var resizeToFitWindow=true;

// debugging
var debug_on=false;
var debug=null;

function main_init() {
  mapdiv=document.getElementById("map");
  loadingdiv=document.getElementById("loading");
  headerdiv=document.getElementById("header");
  updatediv=document.getElementById("update");
  panelsdiv=document.getElementById("panels");
  toolspaneldiv=document.getElementById("tools_panel");
  groupspaneldiv=document.getElementById("groups_panel");
  tooltipsdiv=document.getElementById("tooltips");
  mapfile=document.getElementById("mapfile");
  
  mapdiv.onmousedown=null;
  mapdiv.onmousemove=null;
  mapdiv.onmouseup=null;
  
	// key objects
	queue=new Queue();
	map=new Map();
  panels=new Panels();
  rubber=new RubberBand();
  currentMapState=new MapState();
	currentLayerState=new LayerState();
	infoTabs=new TabGroup(false);
	
  // initialize the objects
  queue.init();
  map.init();
  panels.init();
  rubber.init();
  currentLayerState.init();
  
	panels.currentPanel="groups";
	panels.onPanelClick("groups");
	
  debug=new Debug();
  if (debug_on) {
    debug.init();
  }
}

/*****************************************************************************
 * the following function, format_number, was shamelessly stolen from the
 * good folks at http://www.mredkj.com/javascript/numberFormat.html
 ****************************************************************************/ 
function format_number(num) {
  num += '';  // cast it to a string
  var x = num.split('.');
  var x1 = x[0];
  var x2 = (x.length > 1) ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

// automatic resizing of the map to the available window area can be overridden
// by calling resize with 2 arguments: width and height -- in that order
function resize() {
  var mapWidth=null,mapHeight=null;
  var top=0,left=0,headerHeight=0;
  var totalWidth,totalHeight;
  
  if (self.innerWidth) {
    totalWidth=self.innerWidth-1;
    totalHeight=self.innerHeight-1;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    totalWidth=document.documentElement.clientWidth-1;
    totalHeight=document.documentElement.clientHeight-1;
  } else if (document.body.clientWidth) {
    totalWidth=document.body.clientWidth-1;
    totalHeight=document.body.clientHeight-1;
  } else {
    totalWidth=document.body.offsetWidth-1;
    totalHeight=document.body.offsetHeight-1;
  }

  if (headerdiv.style && headerdiv.style.height)
    headerHeight=parseInt(headerdiv.style.height);
  else if (headerdiv.clientHeight)
    headerHeight=headerdiv.clientHeight;
  else
    headerHeight=headerdiv.offsetHeight;
  headerHeight+=1;
  top=headerHeight;

  panelsdiv.style.top=top+"px";
  if (panelsdiv.style && panelsdiv.style.height)
    top+=parseInt(panelsdiv.style.height);
  else if (panelsdiv.clientHeight)
    top+=panelsdiv.clientHeight;
  else
    top+=panelsdiv.offsetHeight;
  if (panelsdiv.style && panelsdiv.style.width)
    left=parseInt(panelsdiv.style.width);
  else if (panelsdiv.clientWidth)
    left=panelsdiv.clientWidth;
  else
    left=panelsdiv.offsetWidth;

  toolspaneldiv.style.top=top+"px";
  toolspaneldiv.style.height=(totalHeight-top-2)+"px";
  groupspaneldiv.style.top=top+"px";
  groupspaneldiv.style.height=(totalHeight-top-2)+"px";
  mapdiv.style.top=headerHeight+"px";

  if (arguments.length==2) {
    mapWidth=arguments[0];
    mapHeight=arguments[1];
  } else if (resizeToFitWindow) {    
    mapWidth=totalWidth-left-2;
    mapHeight=totalHeight-headerHeight-2;
  }
  
  if (mapWidth && mapHeight) {
    mapdiv.style.width=mapWidth+"px";
    mapdiv.style.height=mapHeight+"px";
    
    if (loadingdiv.style && loadingdiv.style.height)
      var loadingHeight=parseInt(loadingdiv.style.height);
    else if (headerdiv.clientHeight)
      var loadingHeight=loadingdiv.clientHeight;
    else
      var loadingHeight=loadingdiv.offsetHeight;
    
    loadingdiv.style.top=(mapHeight/2 + headerHeight - loadingHeight/2) + "px";
    loadingdiv.style.left=(mapWidth/2 + parseInt(mapdiv.style.left) - loadingHeight/2) + "px";
    
    if (map && map.isDrawn) {
      map.resizeMap();
    }
  }
  
  window.onmouseup=null;
}
