if (window.opener) {
  gaia = opener.gaia;
  gaiaWS = opener.gaiaWS;
  gaiaViewer = opener.gaiaViewer;
  gaiaDate = opener.gaiaDate;
  // install the new member fucntions into this window's version of Date
  gaiaDate.installDateMemberFunctions(new Date);
  gaiaSprintf = opener.gaiaSprintf;
 }


// ***** ***** *****
// CONSTANTS AND GLOBAL VARIABLES
// ***** ***** *****
if (!window.gaiaToolbox)
  var gaiaToolbox = { };

gaiaToolbox.windows = {
  "select_channels": {
    "windowName": "select_channels",
    "url": "select_channels",
    "multipleCopies": false},
  "mapTool": {
    "windowName": "mapTool",
    "windowOptions": "width=870,height=840,status=yes,resizable=yes",
    "url": "maptool",
    "multipleCopies": false},
  "movie": {"windowName": "movie",
	    "windowOptions": "width=400,height=400,status=yes,resizable=yes",
	    "url": "movie",
    "multipleCopies": true},
  "showStatus": {"windowName": "showStatus",
		 "windowOptions": "status=yes,resizable=yes,scrollbars=yes",
		 "url": "show_status",
		 "multipleCopies": false},
  "lanlSopa": {"windowName":"lanlSopa",
	       "url": "http://leadbelly.lanl.gov/lanl_ep_data/cgi-bin/ep_plot_choose_3.cgi?PlotType=LANL_GEO_LoE_00-24&Year=%Y&Month=%m&Day=%d&PlotSize=2",
	       "multipleCopies": false}
};

// ***** ***** *****
// FUNCTIONS
// ***** ***** *****

// Open a toolbox window. Store reference to the window object in
// the current window
if (gaiaToolbox.windowlist == null)
  gaiaToolbox.windowlist = { };

gaiaToolbox.openWindow = function (tool) {
  // Allow URL to be a (UTC) strftime format string, and expand based on
  // selected time
  var url = gaiaViewer.state.time.UTCstrftime(gaiaToolbox.windows[tool].url);
  var w;
  if (gaiaToolbox.windows[tool].multipleCopies == true
      || gaiaToolbox.windowlist[tool] == null 
      || gaiaToolbox.windowlist[tool].closed) {
    // open new window
    if (gaiaToolbox.windows[tool].windowOptions)
      w = window.open(url, gaiaToolbox.windows[tool].windowName,
		      gaiaToolbox.windows[tool].windowOptions);
    else
      w = window.open(url, gaiaToolbox.windows[tool].windowName);

    if (!w)
      // popups must be disabled
      window.location = gaiaWS.config.enable_popups_url;

    // remember the new window
    if (gaiaToolbox.windows[tool].multipleCopies == true) {
      if (!w.opener.gaiaToolbox.windowlist)
	gaiaToolbox.windowlist[tool] = new Array;
      gaiaToolbox.windowlist[tool].push(w);
    }
    else 
      w.opener.gaiaToolbox.windowlist[tool] = w;
      
    // create reference to useful data in the tool's window
    //w.gaiaToolbox = gaiaToolbox;
    //w.gaiaWS = gaiaWS;
  }
  else {
    w = gaiaToolbox.windowlist[tool];
    window.blur();
    w.focus();
  }
  return w;

}
  

  gaiaToolbox.selectFromForm = function () {
    return gaiaToolbox.openWindow("select_channels");
  };

gaiaToolbox.openMapTool = function () {
  return gaiaToolbox.openWindow("mapTool");
};


// This is slightly different, as we allow for multiple copies to be open
// gaiaToolbox.openMoviePlayer = function () {
//   return gaiaToolbox.openWindow("movie");
// };


gaiaToolbox.openShowStatus = function () {
  return gaiaToolbox.openWindow("showStatus");
};


// Export all of our data to the viewer window
gaiaViewer.gaiaToolbox = gaiaToolbox;

 


// window.addEventListener('beforeunload', function () { gaiaViewer.windowlist.viewer.alert('on beforeunload')}, true)

gaiaToolbox.pageOnLoad = function () {

  // Create the channelOrder control
  var chanOrdContainer = document.getElementById("channel-order-container");
  var chanOrdControl = gaiaViewer.channelOrderControl(document);
  chanOrdControl.onclick = function() { 
   gaiaViewer.channelOrderControlOnChange(this); };
  chanOrdContainer.appendChild(chanOrdControl);
  gaiaViewer.registerCallback(window,
			      function () {
				gaiaViewer.channelOrderControlRefresh(chanOrdControl, gaiaViewer.state.channelOrder); },
			      ["channelOrder"], 
			      "gaiaViewer.channelOrderControlRefresh");

 
 
 
  // Create the orientation control
  var orientContainer = document.getElementById("orientation-container");
  var orientControl = gaiaViewer.orientationControl(document);
  orientControl.onclick = function() { 
    gaiaViewer.orientationControlOnChange(this); };
  orientContainer.appendChild(orientControl);
  gaiaViewer.registerCallback(window,
			      function () {
				gaiaViewer.orientationControlRefresh(orientControl, gaiaViewer.state.orientation); },
			      ["orientation"], 
			      "gaiaViewer.orientationControlRefresh");

};


