////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 1998-99 by Mead & Company
////////////////////////////////////////////////////////////////////////////////

function zpmOpen(url, pageName, wstyle) {
  var mgr = zpmObject(pageName);
  if ( mgr ) { mgr.window.focus(); return mgr.window; }
  return zpmObject.openCustom(url, wstyle);
}

function zpmOpenModal(url, pageName, wstyle) {
  var mgr = zpmObject(pageName);
  if ( mgr ) { mgr.window.focus(); return mgr.window; }
  return zpmObject.openModal(url, wstyle, pageName);
}

function zpmOpenStyleCSS(url, pageName, wstyle, css) {
  var mgr = zpmObject(pageName);
  if ( mgr ) { 
    zpmObject.SetBrowserWstyle(mgr, wstyle);
    mgr.document.styleSheets(0).href = css;
    mgr.window.focus(); 
    return mgr.window; 
  }
  return zpmOpenNotify(url, pageName, wstyle, function(doc) {
    doc.styleSheets(0).href = css;
  });
}

function zpmOpenStyleBody(url, pageName, wstyle, bodyClass) {
  var mgr = zpmObject(pageName);
  if ( mgr ) { 
    zpmObject.SetBrowserWstyle(mgr, wstyle);
    mgr.document.body.className = bodyClass;
    mgr.window.focus(); 
    return mgr.window; 
  }
  return zpmOpenNotify(url, pageName, wstyle, function(doc) {
    doc.body.className = bodyClass
  });
}

function zpmOpenReplace(url, pageName, wstyle) {
  var mgr = zpmObject(pageName);
  if ( mgr ) { 
    mgr.window.navigate(url);
    mgr.window.focus(); 
    return mgr.window;
  }
  return zpmObject.openCustom(url, wstyle, pageName);
}

function zpmOpenNotify(url, pageName, wstyle, notify) {
  var wnd = zpmObject.openCustom(url, wstyle, pageName);
  zpmObject.factory.OnDocumentComplete(wnd, failsafe(function() {
    notify(wnd.document);
  }));
  return wnd;
}

function zpmClose(pageName) {
  var mgr = zpmObject(pageName);
  if ( mgr ) zpmObject.Close(mgr);
}

function zpmGetChild(pageName) { // check for parent relationship
  var mgr = zpmObject(pageName);
  return zpmObject.IsChild(mgr)? mgr: null;
}

function zpmCloseChildren() {
  zpmCloseModalDialog();
  var enumer = new Enumerator(zpmObject);
  while ( !enumer.atEnd() ) {
    var mgr = enumer.item(); 
    if ( zpmObject.IsChild(mgr) ) 
      mgr.Quit(); // async
    enumer.moveNext();
  }
}

function zpmCloseAll() {
  zpmCloseModalDialog();
  var enumer = new Enumerator(zpmObject);
  while ( !enumer.atEnd() ) {
    zpmObject.Close(enumer.item());
    enumer.moveNext();
  }
}

function zpmCloseModalDialog() {
	if ( window.zpmModalDialog != null ) 
    window.zpmModalDialog.close();
}

////////////////////////////////////////////////////////////////////////////////
// Pending

function zpmIsPending(elem) {
  for ( i in window.zpmPending )
    if ( window.zpmPending[i].elem === elem )
      return true;
  return false;
}

function zpmAddPending(elem, pageName) {
  if ( zpmIsPending(elem) ) return false;
  var mgr = zpmObject(pageName);
  if ( mgr ) { mgr.window.focus(); return false; }
  var v = new Object;
  v.elem = elem;
  v.pageName = pageName;
  v.cursor = elem.style.cursor;
  if ( !window.zpmPending ) 
    window.zpmPending = new Array;
  window.zpmPending[window.zpmPending.length++] = v;
  elem.style.cursor="wait";
  return true;
}

function zpmRemovePending(pageName) {
  for ( i in window.zpmPending ) {
    var v = window.zpmPending[i];
    if ( v.pageName == pageName ) {
      v.elem.style.cursor = v.cursor;
      if ( zpmObject ) zpmObject.ShowCursor();
      delete window.zpmPending[i];
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
// helpers

function failsafe(func) {
  @cc_on
  @if ( @_jscript_version >= 5 )
    return function() {
      try { func(); }
      catch(e) { alert("master.js: "+e.description+"\n"+func); }
    }
  @else
    return function() { func() }
  @end
}

function closure(obj, method) {
  return function() { return obj[method]() } }

////////////////////////////////////////////////////////////////////////////////
// function QuickDictionary()
//
//	A quick and dirty implementation of a dictionary. E.g:
//
//  dic = new QuickDictionary();
//  dic.Add("anykey", "anyitem");
//  item = dic.Item("anykey");

function QuickDictionary()
{
	this.Add = function(key, item) { 
    this.dict[key] = this.count; 
    this.items[this.count] = item; 
    this.count++;;
  }
	this.Exists = function(key) {
    return this.dict[key] != void 0;
  }
	this.Item = function(key) {
    return this.items[this.dict[key]];
  }
	this.RemoveAll = function() {
    this.dict = new Object;
    this.items.length = 0;
    this.count = 0;
  }
  this.RemoveAll();
}

