<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright (C) 2009 eXo Platform SAS.
    
    This is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 of
    the License, or (at your option) any later version.
    
    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this software; if not, write to the Free
    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA, or see the FSF site: http://www.fsf.org.

-->

<Module>
  <ModulePrefs title="Pankow Gadget">

  <Require feature="oauth">
    <Param name="service_name">
      demo
    </Param>
    <Param name="access_url">
      http://localhost:9090/oauth-provider/access_token
    </Param>
    <Param name="access_method">
      GET
    </Param>

    <Param name="request_url">
      http://localhost:9090/oauth-provider/request_token
    </Param>
    <Param name="request_method">
      GET
    </Param>

    <Param name="authorize_url">
      http://localhost:9090/oauth-provider/authorize
    </Param>
  </Require>

  </ModulePrefs>
  <Content type="html">
      <![CDATA[

    <style>
    #main {
        margin: 0px;
        padding: 0px;
        font-size: small;
    }
    </style>

    <div id="main" style="display: none">
    </div>

    <div id="approval" style="display: none">
      <img src="http://localhost:8080/gadgets/files/samplecontainer/examples/new.gif">
      <a id="personalize"
        onclick="javascript:showOneSection('waiting'); return true"
        target="approve">Personalize this gadget</a>
    </div>

    <div id="waiting" style="display: none">
      Please click
      <a onclick="javascript:fetchData()">I've approved access</a>
      once you've approved access to your data.
    </div>

    <script type="text/javascript">
      function showOneSection(toshow) {
        var sections = [ 'main', 'approval', 'waiting' ];
        for (var i=0; i < sections.length; ++i) {
          var s = sections[i];
          var el = document.getElementById(s);
          if (s === toshow) {
            el.style.display = "block";
          } else {
            el.style.display = "none";
          }
        }
      }

      function fetchData() {
        url = "http://localhost:9090/oauth-provider/echo";
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] =
          gadgets.io.ContentType.TEXT;
        params[gadgets.io.RequestParameters.AUTHORIZATION] =
          gadgets.io.AuthorizationType.AUTHENTICATED;
        params[gadgets.io.RequestParameters.OAUTH_SERVICE] = "demo";
        params[gadgets.io.RequestParameters.METHOD] =
          gadgets.io.MethodType.GET;

        gadgets.io.makeRequest(url, function (response) {
          if (response.approvalUrl) {
            var personalize = document.getElementById('personalize');
            personalize.href = response.approvalUrl;
            showOneSection('approval');
          } else if (response.data) {
            var main = document.getElementById('main');
            var txt = document.createTextNode(response.data);
            main.appendChild(txt);
            showOneSection('main');
          } else {
            var main = document.getElementById('main');
            var whoops = document.createTextNode('Something went wrong');
            main.appendChild(whoops);
            showOneSection('main');
          }
        }, params);
      }

      gadgets.util.registerOnLoadHandler(fetchData);
    </script>
        ]]>
  </Content>
</Module>
