// VolunteerMatch window popup methods


/**
 * Show a help popup.  The help window overrides the default size.
 */

function vmPopup_HELP(url) {
  var overrides = new Array();
  overrides['width']=700;
  overrides['height']=500;

  return vmPopup(url, 'Help', overrides);
}

/**
 * Show a features popup.  The features window overrides the default size.
 */

function vmPopup_FEATURES(url) {
  var overrides = new Array();
  overrides['width']=320;
  overrides['height']=150;

  return vmPopup(url, 'Features', overrides);
}

/**
 * Show a glossary popup.  The glossary window overrides the default size.
 */

function vmPopup_GLOSSARY(url) {
  var overrides = new Array();
  overrides['width']=320;
  overrides['height']=150;

  return vmPopup(url, 'Glossary', overrides);
}

/**
 * Show a "standard" popup; this could easily be the default vmPopup method,
 * but naming it specially makes it easier to search for.
 */

function vmPopup_STANDARD(url, name) {
  var overrides = new Array();
  overrides['width']=600;
  overrides['height']=500;

  return vmPopup(url, name, overrides);
}

/**
 * Convenience method for vmPopup to explicitly take a width and height. This
 * method does not allow features overrides.
 */

function vmPopup_SIZED(url, name, width, height) {
  if (width == null || height == null) {
    throw new Error("invalid arguments; expected vmPopup_SIZED(url, name, width, height)");
  }

  var overrides = new Array();
  overrides['width']=width;
  overrides['height']=height;

  return vmPopup(url, name, overrides);
}

/**
 * Show a popup. A third argument is allowed but not required; if provided
 * it should be an associative array of features that will override the
 * default set.
 */

function vmPopup(url, name) {
  if (arguments.length < 2 || arguments.length > 3) {
    throw new Error("invalid arguments; expected vmPopup(url, name [, overrides])");
  }

  var features = vmPopupCombineFeatures(arguments[2]);

  var win = window.open(url, name, features);
  if (win.focus) {
    win.focus();
  }

  return win;
}

/**
 * Given a window, center it.  Can be used with any of the vmPopup calls, for
 * example:  vmPopupCenter(vmPopup_HELP(...))
 */

function vmPopupCenter(win) {
  if (win != null) {
    var frameWidth = 0;
    var frameHeight = 0;

    if (win.innerWidth) {
      frameWidth = win.innerWidth;
      frameHeight = win.innerHeight;
    } else if (win.document.documentElement && win.document.documentElement.clientWidth) {
      frameWidth = win.document.documentElement.clientWidth;
      frameHeight = win.document.documentElement.clientHeight;
    } else if (win.document.body) {
      frameWidth = win.document.body.clientWidth;
      frameHeight = win.document.body.clientHeight;
    } else {
      return null;
    }

    var left = win.screen.availWidth/2 - frameWidth/2;
    var top = win.screen.availHeight/2 - frameHeight/2;

    win.moveTo(left, top);
  }

  return win;
}

/**
 * FOR INTERNAL USE ONLY
 *
 * Given a set of override features (in the form of an associative array),
 * construct a set of features for the new window that include the
 * volunteermatch defaults.
 */

function vmPopupCombineFeatures(overrides) {
  var features = new Array();
  features['resizable']='yes';
  features['scrollbars']='yes';
  features['toolbar']='no';
  features['location']='no';
  features['width']=640;
  features['height']=480;

  if (overrides != null) {
    for (var key in overrides) {
      features[key] = overrides[key];
    }
  }

  var result = '';

  for (var key in features) {
    var value = features[key];
    if (value != null) {
      result += ',';
      result += key;
      result += '=';
      result += value;

      isFirst = false;
    }
  }

  return result.substring(1);
}

