<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <!-- 
   Copyright 2008 Google Inc. 

   Licensed under the Apache License, Version 2.0 (the "License"); 
   you may not use this file except in compliance with the License. 
   You may obtain a copy of the License at 
   
       http://www.apache.org/licenses/LICENSE-2.0 

   Unless required by applicable law or agreed to in writing, software 
   distributed under the License is distributed on an "AS IS" BASIS, 
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   See the License for the specific language governing permissions and 
   limitations under the License.
  -->
  <ModulePrefs title="LameRankings (1.0)" 
    description="Rank things with a runoff algorithm">
    <Require feature="dynamic-height" />
    <Require feature="views" />
    <Require feature="opensocial-0.7" />
  </ModulePrefs>
  <Content type="html">
    <![CDATA[
    <script>
var lr = {
  /**
   * Url of the directory where these files are stored. Should not end in /
   * @type string
   */
  baseURL : gadgets.util.getUrlParameters()["url"].replace(/lamerankings\.xml/, ""),
  
  /**
   * Loads the supplied URL as a javascript file and calls callback when 
   * the file is finished loading
   */
  loadScript : function(href, callback) {
    if (href.indexOf("http") != 0) {
      href = [ this.baseURL, href ].join("");
    }
    var script = document.createElement("script");
    script.src = href;
    if (typeof(callback) == "function") {
      script.onload = callback;
    }
    document.body.appendChild(script);
  },
  
  loadCss : function(href) {
    if (href.indexOf("http") != 0) {
      href = [ this.baseURL, href ].join("");
    }
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = href;
    document.body.appendChild(link);
  },
  
  /**
   * Returns a closure that will call the supplied function on the supplied
   * object and forward any parameters passed to it
   */
  bind : function(func, obj) {
    return function() {
      func.apply(obj, arguments);
    };
  }, 
  
  /**
   * Logs a message to an appropriate location 
   */
  log : function() {
    if (window.console && console.log) {
      console.log.apply(console, arguments);
    }
  }
};

/**
 * Class that calls a specified function after its own callback function has
 * been called a set number of times
 */
lr.AggregateCallback = function(num, onfinished) {
  this.current_ = 0;
  this.total_ = num;
  this.onfinished_ = onfinished;
};

/**
 * Prototype for AggregateCallback
 */
lr.AggregateCallback.prototype = {
  current_ : 0,
  total_ : null,
  onfinished_ : null,
  callback : function() {
    this.current_++;
    lr.log("Loaded", this.current_, "callback / ", this.total_);
    if (this.current_ == this.total_) {
      this.onfinished_();
      this.onfinished_ = null;
    }
  }
};

//Create a new AggregateCallback that waits for four callbacks and calls 
//lr.init when all four have been activated
var ac = new lr.AggregateCallback(7, function() { lr.init(); });
//Load w23.js
lr.loadScript("/w23.js", lr.bind(ac.callback, ac));
//Load jquery.js
lr.loadScript("/jquery.js", lr.bind(ac.callback, ac));
//Load interface.js
lr.loadScript("/interface.js", lr.bind(ac.callback, ac));
//Load lamerankings.js
lr.loadScript("/lamerankings.js", lr.bind(ac.callback, ac));
//Load cr-opensocial.js
lr.loadScript("/cr-opensocial.js", lr.bind(ac.callback, ac));
//Load lr-lamerank.js
lr.loadScript("/lr-lamerank.js", lr.bind(ac.callback, ac));
//Wait for OpenSocial to have loaded
gadgets.util.registerOnLoadHandler(lr.bind(ac.callback, ac));

//Load CSS
lr.loadCss("/lamerankings.base.css");

    </script>
    <div id="main">
      <table id="tablewrap">
        <colgroup>
          <col id="column1"></col>
          <col id="column2"></col>
        <tr>
          <td valign="top" colspan="2" id="question-wrap"></td>
        </tr>
        <tr>
          <td valign="top" id="addanswer-wrap"></td>
          <td valign="top" rowspan="2" id="ranked-wrap"></td> 
        </tr>
        <tr>
          <td valign="top" id="ranking-wrap"></td>
        </tr>
      </table>
    </div>
    ]]>
  </Content>
</Module>

