/*
 * OpenHandle Utils, 0.2.1
 *
 * <p>Copyright (C) 2008 Tony Hammond</p>
 *
 * <p><a rel="license" href="http://creativecommons.org/licenses/GPL/2.0/">
 * <img alt="Creative Commons License" style="border-width:0"
 * src="http://i.creativecommons.org/l/GPL/2.0/88x62.png" /></a><br />
 * This work is licensed under a <a rel="license"
 * href="http://creativecommons.org/licenses/GPL/2.0/">Creative Commons GNU
 * General Public License License</a>.</p>
 *
 * @author <a href="mailto:tony.hammond@gmail.com">Tony Hammond</a>
 *
 */

OpenHandle.Utils = function() {

  var getHandleParam = function() {
    // escape '[' and ']' in name
    // var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    // var re = new RegExp("[\\?&]" + name + "=([^&#]*)");
    var re = new RegExp("[\\?&]handle=([^&#]*)");
    var results = re.exec(window.location.href);
    return (results == null) ? "" : results[1];
  };
  
  // Get random handle from OpenHandle/CrossRef table
  var getHandleRandom = function() {
    var handles = [
      //
      // OpenHandle Handle Gallery
      '0.NA/0',
      '0.NA/10101',
      '0.SERV/10',
      // '0.TYPE/URL',
      '200/14',
      // '100.2/ADA300359',
      '100.10/newregistry',
      'loc.gmd/g3850.ct000509',
      '2000.01/A3D8BE7457C943FFB66ED4583059A8BA',
      '10101/nature',
      '10100/10.1038/nature06264',
      '10.1038/nature06264',
      // '10.1130/B25510.1',
      '10.2277/0521853575',
      '10.1392/ONIX_DOI_MR',
      '1721.1/29448',
      '10000.2/m.donoghue',
      //
      // CrossRef DOI Guidelines
      '10.1090/S0002-9939-00-05422-8',
      '10.1103/PhysRevLett.88.088302',
      '10.1046/j.1432-1327.2001.02263.x',
      '10.1101/gr.10.12.1841',
      // '10.1130/0091-7613(2001)',
      '10.1109/16.8842',
      '10.1023/A:1003629312096',
      '10.1162/003355300554872',
      '10.1038/26989',
      '10.1093/ageing/29.1.57',
      '10.1126/science.286.5445.1679e',
      '10.1098/rspa.2001.0787',
      '10.1086/301055',
      '10.1006/jmbi.1998.2354',
      '10.1063/1.125173',
      '10.1021/cm960127g'
    ];
    return handles[Math.floor(Math.random() * handles.length)];
  };

  var listMethods = function() { var a = []; for (method in this) { a.push(method); } return a; };

  return {
    getHandleParam: getHandleParam,
    getHandleRandom: getHandleRandom,
    listMethods: listMethods
  }

};
