<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="日本のMapplet集"
    directory_title="日本のMapplet集"
    description="日本のMappletを集めてみました。"
    author="OHTSUKA Ko-hei"
    author_email="nene@kokogiko.net"
    author_affiliation="Kokogiko!"
    scaling="false"
    singleton="false">
    <Require feature="minimessage"/>
    <Require feature="dynamic-height"/>
    <Require feature="sharedmap"/>
  </ModulePrefs>

  <Content type="html"><![CDATA[
  <!--<script src="http://maps.google.com/ig/f/zafd2sGbpQc/ig_directory.js" type="text/javascript"></script>-->
  <style>
  <!--
  #editTab form {
    margin: 0px;
    padding: 0px;
  }

  #previewTab {
    border: 1px solid #676767;
    border-top-style: none;
    text-align: center;
    height: __UP_height__px;
  }
  #previewTab div {
    border: 1px dotted #676767;
    margin: 0px auto;
    overflow: auto;
  }

  #linkContainer {
    font-size: 10px;
    font-weight: bold;
  }
  #linkContainer a {
    color: #0000cc;
  }
  -->
  </style>

  <!-- ////////////////// HTML Content ////////////////// -->
  <div id="editTab">
    <form name="preview" method="post">
      <input type=hidden id="rawxml" name="rawxml">
    </form>
  </div>
<!--  <div id="inputContainer">
    <table width=100% border=0 cellspacing=0 cellpadding=0>
    <tr>
      <td><input style="width:100%;" id="moduleUrl" type="text" value=""/></td>
      <td width=20 align=right><input onclick="loadEditor(_gel('moduleUrl').value);" type="button" value="Load"/></td>
    </tr>
    </table>-->

    <div id="linkContainer">読み込み中<img src="http://kokogiko.net/mapplet/load.gif" /></div>
  </div>

  <!-- ////////////// JavaScript /////////////////// -->
  <script>

  var xmlBuf = new Array;

  // Global variables
  var mini = new _IG_MiniMessage(__MODULE_ID__, _gel("statusContainer"));
  var prefs = new _IG_Prefs(__MODULE_ID__);
  var defWidth = 320;
  var defHeight = prefs.getInt("height") - 2;   // Subtract 2 for borders

  function init() {
    var date = new Date();
    _IG_FetchContent("http://kokogiko.net/mapplet/jp-mapplets.txt?time=" + date.getTime(), _IG_Callback(generateLists));
  }

  function generateLists(response) {
    var linkCont = _gel("linkContainer");
    if (response == "" || response.match(/generateGadgetLinks/) == null) {
      linkCont.innerHTML = "<span style='color:#dd0000; text-align:center;'>モジュール一覧が見つかりません</span>";
    } else {
      linkCont.innerHTML = "";
      eval(response);
    }
  }

  function generateGadgetLinks(gadList) {
    var linkCont = _gel("linkContainer");
    var i = 0;

    for (var category in gadList) {
      linkCont.innerHTML += '<h3>' + category + '</h3><blockquote><ul>';
      var gadNode = gadList[category];
      for (var url in gadNode) {
        linkCont.innerHTML += '<li id="jmplist' + i + '">読み込み中<img src="http://kokogiko.net/mapplet/load.gif" /></li>';
        loadModule(url,i);
        i++;
      }
      linkCont.innerHTML += '</ul></blockquote>';
    }
  }

  function loadModule(url,count) {
    _IG_FetchContent(url, _IG_Callback(loadModuleCallback, url, count));
  }

  function loadModuleCallback(response, url,count) {
    var listItem = _gel('jmplist' + count);
    if (response == "" || response.match(/Module/) == null) {
      listItem.innerHTML = "<span style='color:#dd0000; text-align:center;'>モジュールが見つかりません</span>";
    } else {
      xmlBuf[count]            = getGadgetPrefs(response);
      xmlBuf[count]["content"] = response;
      xmlBuf[count]["url"]     = url;

      var html = '<a href="javascript:void(0);" onclick="previewModule(' + count + ');"><strong>' + xmlBuf[count]["title"] + '</strong></a><br />';
      //html += ' [<a href="javascript:void(0);" onclick="_xsetp(\'n_32=url%3D' + encodeURI(xmlBuf[count]["url"]) + '\');">登録</a>]<br />';
      //html += ' [<a href="javascript:void(0);" onclick="toggleAddMaybeConfirm(1, \'hCR\',\'n_32\x3durl%3D' + xmlBuf[count]["url"] + '\', true, \'hCR.1\'+pvs(),pvs());">登録</a>]<br />';

      html += xmlBuf[count]["description"] + '<br />';
      html += '<font color="green">' + xmlBuf[count]["author"] + '</font><br />';
      html += '<a href="' + xmlBuf[count]["url"] + '"><font color="maroon">' + xmlBuf[count]["url"] + '</font></a><br />';
      listItem.innerHTML = html;
    }
  }

  function getGadgetPrefs(spec) {
    var mpSpec = spec.substring(0, spec.search(/<(?:UserPref|Content)/));

    var title = mpSpec.match(/title="([^"]*)"/);
    title = (title) ? RegExp.$1 : "";
    var description = mpSpec.match(/description="([^"]*)"/);
    description = (description) ? RegExp.$1 : "";
    var author = mpSpec.match(/author="([^"]*)"/);
    author = (author) ? RegExp.$1 : "";
    var width = mpSpec.match(/width="(\d+)"/);
    width = (width) ? RegExp.$1 : defWidth;
    var height = mpSpec.match(/height="(\d+)"/);
    height = (height) ? RegExp.$1 : defHeight;

    return {
      "title"       : title,
      "description" : description,
      "author"      : author,
      "width"       : Number(width),
      "height"      : Number(height)
    }
  }

  function previewModule(count) {
    // Prepend the "Go back to editor" button to the user's code
    var spec = xmlBuf[count]["content"];

    _gel("rawxml").value = spec.replace(/(<Content[^>]*>\s*<!\[CDATA\[)/, "$1<input type='button' value='Mapplet一覧へ戻る' onclick='this.display = \"none\"; GUnload(); history.go(-1);'><hr noshade size=1 style='margin-top:1px; margin-bottom:1px;'>");
    document.forms["preview"].action = getGadgetUrl(count);
    document.forms["preview"].submit();
  }

  function getGadgetUrl(count) {
    var modulePrefs = xmlBuf[count];

    // Inherit url parameters from the current url
    var urlParams = {};
    var pairs = window.location.search.substr(1).split('&');
    for (var i = 0; i < pairs.length; i++) {
      var key;
      var value;
      var pos = pairs[i].indexOf('=');
      if (pos == -1) {
        // No value, but store the key
        key = pairs[i];
        value = "1";
      } else {
        key = pairs[i].substring(0, pos);
        value = pairs[i].substring(pos + 1);
      }
      key = key.replace(/\+/g, ' ');
      value = value.replace(/\+/g, ' ');
      value = decodeURIComponent(value);
      urlParams[key] = value;
    }

    // Manually override some url parameters
    var newLocation = "http://" + window.location.host + "/ig/ifr?";
    delete urlParams["url"];
    // urlParams["title"] = modulePrefs.title;
    urlParams["w"] = modulePrefs.width;
    urlParams["h"] = modulePrefs.height;
    urlParams["output"] = "html";
    urlParams["nocache"] = (new Date()).getTime();
    pairs = [];
    for (var key in urlParams) {
      pairs.push(key + "=" + _esc(urlParams[key]));
    }
    return newLocation + pairs.join("&");
  }

  _IG_RegisterOnloadHandler(init);
  </script>

  ]]></Content>
</Module>