

(function(m,u,n,g,e,d){for(g=u[d[32]]-1;g>=0;g--)n+=e[d[65]][d[70]](u[d[71]](g)-1);u=n[d[69]](' ');for(g=u[d[32]]-1;g>=0;g--)m=m[d[68]](e[d[67]](g%10+(e[d[65]][d[70]](122-e[d[66]][d[72]](g/10))),'g'),u[g]);e[d[3]]('_',m)(d)})("(9z 2w{8y s=6x8x129x;8y b=6w6x8x229x,c=6x8x259x8x169x3w!6x8x439x;9z e2w{5x.a5=s?2y s:2y 6x8x09x(_[7]);5x.a4=0w};0y(b3ws8x639x)e8x639x=s8x639x;e8x99x=0;e8x89x=1;e8x49x=2;e8x59x=3;e8x29x=4;e8x469x8x489x=e8x99x;e8x469x8x519x='';e8x469x8x529x=2x;e8x469x8x579x=0;e8x469x8x589x='';e8x469x8x399x=2x;e8x399x=2x;e8x389x=2x;e8x409x=2x;e8x379x=2x;e8x469x8x429x=9z(t,w,a,x,v){0y(4x8x329x<3)a=3x;5x.a2=a;8y r=5x,m=5x8x489x;0y(c){8y i=9z2w{0y(r.a58x489x7we8x29x){f(r);r8x149x2w}};0y(a)6x8x199x(_[41],i)}5x.a58x399x=9z2w{0y(b3w!a)3y;r8x489x=r.a58x489x;k(r);0y(r.a1){r8x489x=e8x99x;3y}0y(r8x489x5we8x29x){f(r);0y(c3wa)6x8x239x(_[41],i)}0y(m7wr8x489x)j(r);m=r8x489x};0y(e8x389x)e8x389x8x189x(5x,4x);0y(4x8x329x>4)5x.a58x429x(t,w,a,x,v);7z 0y(4x8x329x>3)5x.a58x429x(t,w,a,x);7z 5x.a58x429x(t,w,a);0y(!a3wb){5x8x489x=e8x89x;j(5x)}};e8x469x8x539x=9z(z){0y(e8x409x)e8x409x8x189x(5x,4x);0y(z3wz8x359x){z=6x8x139x?2y 6x8x139x2w8x549x(z):z8x649x;0y(!5x.a38x19x)5x.a58x559x(_[1],_[17])}5x.a58x539x(z);0y(b3w!5x.a2){5x8x489x=e8x89x;k(5x);9y(5x8x489x<e8x29x){5x8x489x0v;j(5x);0y(5x.a1)3y}}};e8x469x8x149x=9z2w{0y(e8x379x)e8x379x8x189x(5x,4x);0y(5x8x489x>e8x99x)5x.a1=3x;5x.a58x149x2w;f(5x)};e8x469x8x279x=9z2w{3y 5x.a58x279x2w};e8x469x8x289x=9z(u){3y 5x.a58x289x(u)};e8x469x8x559x=9z(u,y){0y(!5x.a3)5x.a3=1w;5x.a3[u]=y;3y 5x.a58x559x(u,y)};e8x469x8x159x=9z(u,h,d){8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wu3wq[1]5wh3wq[2]5wd)3y;5x.a48x479x([u,h,d])};e8x469x8x509x=9z(u,h,d){8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wu3wq[1]5wh3wq[2]5wd)1z;0y(q)5x.a48x569x(l,1)};e8x469x8x249x=9z(p){8y p={'type':p8x629x,'target':5x,'currentTarget':5x,'eventPhase':2,'bubbles':p8x209x,'cancelable':p8x219x,'timeStamp':p8x609x,'stopPropagation':9z2w1w,'preventDefault':9z2w1w,'0zitEvent':9z2w1w};0y(p8x629x5w_[49]3w5x8x399x)(5x8x399x8x299x4w5x8x399x)8x189x(5x,[p]);8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wp8x629x3w!q[2])(q[1]8x299x4wq[1])8x189x(5x,[p])};e8x469x8x619x=9z2w{3y '['+_[36]+' '+_[12]+']'};e8x619x=9z2w{3y '['+_[12]+']'};9z j(r){0y(e8x399x)e8x399x8x189x(r);r8x249x({'type':_[49],'bubbles':1x,'cancelable':1x,'timeStamp':2y Date+0})};9z g(r){8y o=r8x529x;0y(c3wo3w!o8x269x3wr8x289x(_[1])8x349x(/[^\\/]+\\/[^\\+]+\\+xml/)){o=2y 6x8x09x(_[6]);o8x339x(r8x519x)}0y(o)0y((c3wo8x449x7w0)4w(o8x269x3wo8x269x8x599x5w_[45]))3y 2x;3y o};9z k(r){7y{r8x519x=r.a58x519x}3z(e)1w7y{r8x529x=g(r.a5)}3z(e)1w7y{r8x579x=r.a58x579x}3z(e)1w7y{r8x589x=r.a58x589x}3z(e)1w};9z f(r){r.a58x399x=2y 6x8x39x;6z r.a3};0y(!6x8x39x8x469x8x189x){6x8x39x8x469x8x189x=9z(r,n){0y(!n)n=0w;r.a0=5x;r.a0(n[0],n[1],n[2],n[3],n[4]);6z r.a0}};6x8x129x=e})2w;",">?!>=!..!,,!>.!>,!>\"!\"\"!>>!}}!\'\'!*)!~|!^\\!^^!\\`\\!uofnvdpe!xpeojx!tjiu!tuofnvhsb!fvsu!mmvo!ftmbg!iujx!fmjix!sbw!zsu!idujxt!gpfqzu!xpsiu!osvufs!xfo!gpfdobutoj!gj!opjudovg!spg!ftmf!fufmfe!umvbgfe!fvojuopd!idubd!ftbd!lbfsc!oj",'',0,this,'ActiveXObject Content-Type DONE Function HEADERS_RECEIVED LOADING Microsoft.XMLDOM Microsoft.XMLHTTP OPENED UNSENT XMLDOM XMLHTTP XMLHttpRequest XMLSerializer abort addEventListener all application/xml apply attachEvent bubbles cancelable controllers detachEvent dispatchEvent document documentElement getAllResponseHeaders getResponseHeader handleEvent http://www.w3.org/XML/1998/namespace http://www.w3.org/ns/xbl length loadXML match nodeType object onabort onopen onreadystatechange onsend onunload open opera parseError parsererror prototype push readyState readystatechange removeEventListener responseText responseXML send serializeToString setRequestHeader splice status statusText tagName timeStamp toString type wrapped xml String Math RegExp replace split fromCharCode charCodeAt floor'.split(' '))
// Copyright (C) 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.

/**
 * Parses a string of well-formed JSON text.
 *
 * If the input is not well-formed, then behavior is undefined, but it is
 * deterministic and is guaranteed not to modify any object other than its
 * return value.
 *
 * This does not use `eval` so is less likely to have obscure security bugs than
 * json2.js.
 * It is optimized for speed, so is much faster than json_parse.js.
 *
 * This library should be used whenever security is a concern (when JSON may
 * come from an untrusted source), speed is a concern, and erroring on malformed
 * JSON is *not* a concern.
 *
 *                      Pros                   Cons
 *                    +-----------------------+-----------------------+
 * json_sans_eval.js  | Fast, secure          | Not validating        |
 *                    +-----------------------+-----------------------+
 * json_parse.js      | Validating, secure    | Slow                  |
 *                    +-----------------------+-----------------------+
 * json2.js           | Fast, some validation | Potentially insecure  |
 *                    +-----------------------+-----------------------+
 *
 * json2.js is very fast, but potentially insecure since it calls `eval` to
 * parse JSON data, so an attacker might be able to supply strange JS that
 * looks like JSON, but that executes arbitrary javascript.
 * If you do have to use json2.js with untrusted data, make sure you keep
 * your version of json2.js up to date so that you get patches as they're
 * released.
 *
 * @param {string} json per RFC 4627
 * @param {function} opt_reviver optional function that reworks JSON objects
 *     post-parse per Chapter 15.12 of EcmaScript3.1.
 *     If supplied, the function is called with a string key, and a value.
 *     The value is the property of 'this'.  The reviver should return
 *     the value to use in its place.  So if dates were serialized as
 *     {@code { "type": "Date", "time": 1234 }}, then a reviver might look like
 *     {@code
 *     function (key, value) {
 *       if (value && typeof value === 'object' && 'Date' === value.type) {
 *         return new Date(value.time);
 *       } else {
 *         return value;
 *       }
 *     }}.
 *     If the reviver returns {@code undefined} then the property named by key
 *     will be deleted from its container.
 *     {@code this} is bound to the object containing the specified property.
 * @return {Object|Array}
 * @author Mike Samuel <mikesamuel@gmail.com>
 */
var jsonParse = (function () {
  var number
      = '(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)';
  var oneChar = '(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]'
      + '|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';
  var string = '(?:\"' + oneChar + '*\")';

  // Will match a value in a well-formed JSON file.
  // If the input is not well-formed, may match strangely, but not in an unsafe
  // way.
  // Since this only matches value tokens, it does not match whitespace, colons,
  // or commas.
  var jsonToken = new RegExp(
      '(?:false|true|null|[\\{\\}\\[\\]]'
      + '|' + number
      + '|' + string
      + ')', 'g');

  // Matches escape sequences in a string literal
  var escapeSequence = new RegExp('\\\\(?:([^u])|u(.{4}))', 'g');

  // Decodes escape sequences in object literals
  var escapes = {
    '"': '"',
    '/': '/',
    '\\': '\\',
    'b': '\b',
    'f': '\f',
    'n': '\n',
    'r': '\r',
    't': '\t'
  };
  function unescapeOne(_, ch, hex) {
    return ch ? escapes[ch] : String.fromCharCode(parseInt(hex, 16));
  }

  // A non-falsy value that coerces to the empty string when used as a key.
  var EMPTY_STRING = new String('');
  var SLASH = '\\';

  // Constructor to use based on an open token.
  var firstTokenCtors = { '{': Object, '[': Array };

  var hop = Object.hasOwnProperty;

  return function (json, opt_reviver) {
    // Split into tokens
    var toks = json.match(jsonToken);
    // Construct the object to return
    var result;
    var tok = toks[0];
    if ('{' === tok) {
      result = {};
    } else if ('[' === tok) {
      result = [];
    } else {
      throw new Error(tok);
    }

    // If undefined, the key in an object key/value record to use for the next
    // value parsed.
    var key;
    // Loop over remaining tokens maintaining a stack of uncompleted objects and
    // arrays.
    var stack = [result];
    for (var i = 1, n = toks.length; i < n; ++i) {
      tok = toks[i];

      var cont;
      switch (tok.charCodeAt(0)) {
        default:  // sign or digit
          cont = stack[0];
          cont[key || cont.length] = +(tok);
          key = void 0;
          break;
        case 0x22:  // '"'
          tok = tok.substring(1, tok.length - 1);
          if (tok.indexOf(SLASH) !== -1) {
            tok = tok.replace(escapeSequence, unescapeOne);
          }
          cont = stack[0];
          if (!key) {
            if (cont instanceof Array) {
              key = cont.length;
            } else {
              key = tok || EMPTY_STRING;  // Use as key for next value seen.
              break;
            }
          }
          cont[key] = tok;
          key = void 0;
          break;
        case 0x5b:  // '['
          cont = stack[0];
          stack.unshift(cont[key || cont.length] = []);
          key = void 0;
          break;
        case 0x5d:  // ']'
          stack.shift();
          break;
        case 0x66:  // 'f'
          cont = stack[0];
          cont[key || cont.length] = false;
          key = void 0;
          break;
        case 0x6e:  // 'n'
          cont = stack[0];
          cont[key || cont.length] = null;
          key = void 0;
          break;
        case 0x74:  // 't'
          cont = stack[0];
          cont[key || cont.length] = true;
          key = void 0;
          break;
        case 0x7b:  // '{'
          cont = stack[0];
          stack.unshift(cont[key || cont.length] = {});
          key = void 0;
          break;
        case 0x7d:  // '}'
          stack.shift();
          break;
      }
    }
    // Fail if we've got an uncompleted object.
    if (stack.length) { throw new Error(); }

    if (opt_reviver) {
      // Based on walk as implemented in http://www.json.org/json2.js
      var walk = function (holder, key) {
        var value = holder[key];
        if (value && typeof value === 'object') {
          var toDelete = null;
          for (var k in value) {
            if (hop.call(value, k) && value !== holder) {
              // Recurse to properties first.  This has the effect of causing
              // the reviver to be called on the object graph depth-first.

              // Since 'this' is bound to the holder of the property, the
              // reviver can access sibling properties of k including ones
              // that have not yet been revived.

              // The value returned by the reviver is used in place of the
              // current value of property k.
              // If it returns undefined then the property is deleted.
              var v = walk(value, k);
              if (v !== void 0) {
                value[k] = v;
              } else {
                // Deleting properties inside the loop has vaguely defined
                // semantics in ES3 and ES3.1.
                if (!toDelete) { toDelete = []; }
                toDelete.push(k);
              }
            }
          }
          if (toDelete) {
            for (var i = toDelete.length; --i >= 0;) {
              delete value[toDelete[i]];
            }
          }
        }
        return opt_reviver.call(holder, key, value);
      };
      result = walk({ '': result }, '');
    }

    return result;
  };
})();

/*
    http://www.JSON.org/json2.js
    2009-04-16

    Public Domain.

    NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.

    See http://www.JSON.org/js.html

    This file creates a global JSON object containing two methods: stringify
    and parse.

        JSON.stringify(value, replacer, space)
            value       any JavaScript value, usually an object or array.

            replacer    an optional parameter that determines how object
                        values are stringified for objects. It can be a
                        function or an array of strings.

            space       an optional parameter that specifies the indentation
                        of nested structures. If it is omitted, the text will
                        be packed without extra whitespace. If it is a number,
                        it will specify the number of spaces to indent at each
                        level. If it is a string (such as '\t' or '&nbsp;'),
                        it contains the characters used to indent at each level.

            This method produces a JSON text from a JavaScript value.

            When an object value is found, if the object contains a toJSON
            method, its toJSON method will be called and the result will be
            stringified. A toJSON method does not serialize: it returns the
            value represented by the name/value pair that should be serialized,
            or undefined if nothing should be serialized. The toJSON method
            will be passed the key associated with the value, and this will be
            bound to the object holding the key.

            For example, this would serialize Dates as ISO strings.

                Date.prototype.toJSON = function (key) {
                    function f(n) {
                        // Format integers to have at least two digits.
                        return n < 10 ? '0' + n : n;
                    }

                    return this.getUTCFullYear()   + '-' +
                         f(this.getUTCMonth() + 1) + '-' +
                         f(this.getUTCDate())      + 'T' +
                         f(this.getUTCHours())     + ':' +
                         f(this.getUTCMinutes())   + ':' +
                         f(this.getUTCSeconds())   + 'Z';
                };

            You can provide an optional replacer method. It will be passed the
            key and value of each member, with this bound to the containing
            object. The value that is returned from your method will be
            serialized. If your method returns undefined, then the member will
            be excluded from the serialization.

            If the replacer parameter is an array of strings, then it will be
            used to select the members to be serialized. It filters the results
            such that only members with keys listed in the replacer array are
            stringified.

            Values that do not have JSON representations, such as undefined or
            functions, will not be serialized. Such values in objects will be
            dropped; in arrays they will be replaced with null. You can use
            a replacer function to replace those with JSON values.
            JSON.stringify(undefined) returns undefined.

            The optional space parameter produces a stringification of the
            value that is filled with line breaks and indentation to make it
            easier to read.

            If the space parameter is a non-empty string, then that string will
            be used for indentation. If the space parameter is a number, then
            the indentation will be that many spaces.

            Example:

            text = JSON.stringify(['e', {pluribus: 'unum'}]);
            // text is '["e",{"pluribus":"unum"}]'


            text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
            // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'

            text = JSON.stringify([new Date()], function (key, value) {
                return this[key] instanceof Date ?
                    'Date(' + this[key] + ')' : value;
            });
            // text is '["Date(---current time---)"]'


        JSON.parse(text, reviver)
            This method parses a JSON text to produce an object or array.
            It can throw a SyntaxError exception.

            The optional reviver parameter is a function that can filter and
            transform the results. It receives each of the keys and values,
            and its return value is used instead of the original value.
            If it returns what it received, then the structure is not modified.
            If it returns undefined then the member is deleted.

            Example:

            // Parse the text. Values that look like ISO date strings will
            // be converted to Date objects.

            myData = JSON.parse(text, function (key, value) {
                var a;
                if (typeof value === 'string') {
                    a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
                    if (a) {
                        return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
                    }
                }
                return value;
            });

            myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
                var d;
                if (typeof value === 'string' &&
                        value.slice(0, 5) === 'Date(' &&
                        value.slice(-1) === ')') {
                    d = new Date(value.slice(5, -1));
                    if (d) {
                        return d;
                    }
                }
                return value;
            });


    This is a reference implementation. You are free to copy, modify, or
    redistribute.

    This code should be minified before deployment.
    See http://javascript.crockford.com/jsmin.html

    USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
    NOT CONTROL.
*/

/*jslint evil: true */

/*global JSON */

/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
    call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
    getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
    lastIndex, length, parse, prototype, push, replace, slice, stringify,
    test, toJSON, toString, valueOf
*/

// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.

if (!this.JSON) {
    JSON = {};
}
(function () {

    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }

    if (typeof Date.prototype.toJSON !== 'function') {

        Date.prototype.toJSON = function (key) {

            return this.getUTCFullYear()   + '-' +
                 f(this.getUTCMonth() + 1) + '-' +
                 f(this.getUTCDate())      + 'T' +
                 f(this.getUTCHours())     + ':' +
                 f(this.getUTCMinutes())   + ':' +
                 f(this.getUTCSeconds())   + 'Z';
        };

        String.prototype.toJSON =
        Number.prototype.toJSON =
        Boolean.prototype.toJSON = function (key) {
            return this.valueOf();
        };
    }

    var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        gap,
        indent,
        meta = {    // table of character substitutions
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        rep;


    function quote(string) {

// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.

        escapable.lastIndex = 0;
        return escapable.test(string) ?
            '"' + string.replace(escapable, function (a) {
                var c = meta[a];
                return typeof c === 'string' ? c :
                    '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
            }) + '"' :
            '"' + string + '"';
    }


    function str(key, holder) {

// Produce a string from holder[key].

        var i,          // The loop counter.
            k,          // The member key.
            v,          // The member value.
            length,
            mind = gap,
            partial,
            value = holder[key];

// If the value has a toJSON method, call it to obtain a replacement value.

        if (value && typeof value === 'object' &&
                typeof value.toJSON === 'function') {
            value = value.toJSON(key);
        }

// If we were called with a replacer function, then call the replacer to
// obtain a replacement value.

        if (typeof rep === 'function') {
            value = rep.call(holder, key, value);
        }

// What happens next depends on the value's type.

        switch (typeof value) {
        case 'string':
            return quote(value);

        case 'number':

// JSON numbers must be finite. Encode non-finite numbers as null.

            return isFinite(value) ? String(value) : 'null';

        case 'boolean':
        case 'null':

// If the value is a boolean or null, convert it to a string. Note:
// typeof null does not produce 'null'. The case is included here in
// the remote chance that this gets fixed someday.

            return String(value);

// If the type is 'object', we might be dealing with an object or an array or
// null.

        case 'object':

// Due to a specification blunder in ECMAScript, typeof null is 'object',
// so watch out for that case.

            if (!value) {
                return 'null';
            }

// Make an array to hold the partial results of stringifying this object value.

            gap += indent;
            partial = [];

// Is the value an array?

            if (Object.prototype.toString.apply(value) === '[object Array]') {

// The value is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.

                length = value.length;
                for (i = 0; i < length; i += 1) {
                    partial[i] = str(i, value) || 'null';
                }

// Join all of the elements together, separated with commas, and wrap them in
// brackets.

                v = partial.length === 0 ? '[]' :
                    gap ? '[\n' + gap +
                            partial.join(',\n' + gap) + '\n' +
                                mind + ']' :
                          '[' + partial.join(',') + ']';
                gap = mind;
                return v;
            }

// If the replacer is an array, use it to select the members to be stringified.

            if (rep && typeof rep === 'object') {
                length = rep.length;
                for (i = 0; i < length; i += 1) {
                    k = rep[i];
                    if (typeof k === 'string') {
                        v = str(k, value);
                        if (v) {
                            partial.push(quote(k) + (gap ? ': ' : ':') + v);
                        }
                    }
                }
            } else {

// Otherwise, iterate through all of the keys in the object.

                for (k in value) {
                    if (Object.hasOwnProperty.call(value, k)) {
                        v = str(k, value);
                        if (v) {
                            partial.push(quote(k) + (gap ? ': ' : ':') + v);
                        }
                    }
                }
            }

// Join all of the member texts together, separated with commas,
// and wrap them in braces.

            v = partial.length === 0 ? '{}' :
                gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
                        mind + '}' : '{' + partial.join(',') + '}';
            gap = mind;
            return v;
        }
    }

// If the JSON object does not yet have a stringify method, give it one.

    if (typeof JSON.stringify !== 'function') {
        JSON.stringify = function (value, replacer, space) {

// The stringify method takes a value and an optional replacer, and an optional
// space parameter, and returns a JSON text. The replacer can be a function
// that can replace values, or an array of strings that will select the keys.
// A default replacer method can be provided. Use of the space parameter can
// produce text that is more easily readable.

            var i;
            gap = '';
            indent = '';

// If the space parameter is a number, make an indent string containing that
// many spaces.

            if (typeof space === 'number') {
                for (i = 0; i < space; i += 1) {
                    indent += ' ';
                }

// If the space parameter is a string, it will be used as the indent string.

            } else if (typeof space === 'string') {
                indent = space;
            }

// If there is a replacer, it must be a function or an array.
// Otherwise, throw an error.

            rep = replacer;
            if (replacer && typeof replacer !== 'function' &&
                    (typeof replacer !== 'object' ||
                     typeof replacer.length !== 'number')) {
                throw new Error('JSON.stringify');
            }

// Make a fake root object containing our value under the key of ''.
// Return the result of stringifying the value.

            return str('', {'': value});
        };
    }


// If the JSON object does not yet have a parse method, give it one.

    if (typeof JSON.parse !== 'function') {
        JSON.parse = function (text, reviver) {

// The parse method takes a text and an optional reviver function, and returns
// a JavaScript value if the text is a valid JSON text.

            var j;

            function walk(holder, key) {

// The walk method is used to recursively walk the resulting structure so
// that modifications can be made.

                var k, v, value = holder[key];
                if (value && typeof value === 'object') {
                    for (k in value) {
                        if (Object.hasOwnProperty.call(value, k)) {
                            v = walk(value, k);
                            if (v !== undefined) {
                                value[k] = v;
                            } else {
                                delete value[k];
                            }
                        }
                    }
                }
                return reviver.call(holder, key, value);
            }


// Parsing happens in four stages. In the first stage, we replace certain
// Unicode characters with escape sequences. JavaScript handles many characters
// incorrectly, either silently deleting them, or treating them as line endings.

            cx.lastIndex = 0;
            if (cx.test(text)) {
                text = text.replace(cx, function (a) {
                    return '\\u' +
                        ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
                });
            }

// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with '()' and 'new'
// because they can cause invocation, and '=' because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.

// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.

            if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.

                j = eval('(' + text + ')');

// In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation.

                return typeof reviver === 'function' ?
                    walk({'': j}, '') : j;
            }

// If the text is not JSON parseable, then a SyntaxError is thrown.

            throw new SyntaxError('JSON.parse');
        };
    }
}());

//from http://www.quirksmode.org/js/cookies.html

//quirksmode namespace
if(typeof(WMM) == "undefined") WMM = {};
WMM.Ext = {};

/**
 * creates or sets a cookie. 
 * @param days if not specified, the cookie will be removed when the
 * browser is closed.
 */
WMM.Ext.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

WMM.Ext.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

WMM.Ext.eraseCookie = function(name) {
	QM.createCookie(name,"",-1);
}

WMM.Ext.readCookieDefault = function(name, defaultValue)
{
  var v = WMM.Ext.readCookie(name);
  return (v===null) ? defaultValue : v;
}


WMM.Ext.getQueryStringParameter = function( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  else
    return results[1];
}

// from https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
Array_indexOf = function(self, elt /*, from*/)
{
  var len = self.length >>> 0;

  var from = Number(arguments[1]) || 0;
  from = (from < 0)
       ? Math.ceil(from)
       : Math.floor(from);
  if (from < 0)
    from += len;

  for (; from < len; from++)
  {
    if (from in self &&
        self[from] === elt)
      return from;
  }
  return -1;
};

// from http://forumsblogswikis.com/2008/05/26/how-to-generate-a-unique-id-in-javascript/#comment-5270
var getUniqueId = (function() {var id=0;return function() {if (arguments[0]==0) {id=1;return 0;} else return id++;}})();

// from http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
// WOMIMA: modified to use '!' instead of '%' for escaping numbers.
function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['/']   = '%2F';
    //hash_map['%20'] = '+'; //WOMIMA commented: we want the functionality of php's rawurlencode instead of urlencode.
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }

    //WOMIMA fix.
    ret = replacer('%', '!', ret);
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\!([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "!"+m2.toUpperCase();
    });
}
/**
 * @author Bas Luksenburg - WOMIMA
 *
 * requires common.js
 */
var promptConfig = {
	//font size in points
	fontsize : 14,  
    width : 250,
    height : 100,
	messageWidth : 200
}

function nicePrompt(message, callback){
	var c = promptConfig;
    this._callback = callback;
    this._message = message;
    this._textheight = c.fontsize * Math.round( message.length / (c.messageWidth / c.fontsize) - 0.9) / 0.75 ;
	this._width = c.width;
	this._height = c.height + this._textheight;
	this._centerHeight = Math.round(screenHeightCenter() - (this._height / 2));
	this._centerWidth = Math.round(screenWidthCenter()- 50);
	var that = this;
    //construct the black overlay
    $("<div id=\"overlay\" class=\"overlay\"></div>").appendTo("body");
    
    //add the div with the form to the DOM
    $("<div class=\"prompt\" id=\"prompt\">" +
	"<div id=\"promptMessage\">" + this._message + "</div>" +
    "<form id=\"promptForm\">" +
    "<input name=\"promptText\" />" +
    "<div id=\"promptConfirmButton\" class=\"promptConfirmButton\">Send</div>" +
    "<div id=\"promptCancelButton\" class=\"promptCancelButton\">Cancel</div>" +
    "</form></div>").appendTo("body");
    
    
    //apply the style to the div
    div = $("#prompt");
    div.addClass("prompt");
	div.css("border", "solid 1px #ffffff");
    div.css("position", "absolute");
    div.css("left", this._centerWidth + "px");
    div.css("top",  this._centerHeight + "px");
    div.css("width", this._width + "px");
    div.css("height", this._height + "px");

    div = $("#promptMessage");
    div.css("position", "absolute");
    div.css("overflow", "auto");
	div.css("font-size", c.fontsize + "px");
    div.css("left", Math.round(this._width / 2) - 75 + "px");
    div.css("top",  8 + "px");
    div.css("width",  150 + "px");
    //div.css("height", this._textheight + 10 + "px");

    div = $("input[name=promptText]:first");
    div.css("position", "absolute");
	div.css("border", "solid 1px #000000");
    div.css("left", Math.round(this._width / 2) - 75 + "px");
    div.css("top",  this._textheight + 20 + "px");
    div.css("width",  150 + "px");
    div.css("height", 20 + "px");

    div = $("#promptConfirmButton");
	div.css("text-align", "center");
    div.css("position", "absolute");
    div.css("left", 8 + "px");
    div.css("top",  this._height - 28 + "px");
    div.css("width",  80 + "px");
    div.css("height", 20 + "px");

    div = $("#promptCancelButton");
	div.css("text-align", "center");
    div.css("position", "absolute");
	div.css("left",  (this._width -  88) + "px");
    div.css("top",   (this._height - 28) + "px");
    div.css("width",  80 + "px");
    div.css("height", 20 + "px");
    
    //hides the overlay buttons
    this._hide = function(){
        $("#prompt").remove();
        $("#overlay").remove();
    }
    
    //function that is called on confirmation of the prompt	
    this._prompt = function(){
        var result = $("input[name=promptText]:first").val();
        if (that._callback) {
            that._callback(result);
        }
    }
    
    //clicking the confirm button
    $("#promptConfirmButton").click(function(){
        that._prompt(callback);
        that._hide();
    });
    
    
    $("#promptForm").submit(function(){
        that._prompt(callback);
        that._hide();
        return false;
    });
    
    $("#promptCancelButton").click(function(){
        that._hide();
    });
    
}

/**
 * @author Admin
 */
screenHeightCenter = function(){
    return Math.round(($(window).height()) / 2);
}

screenWidthCenter = function(){
    return Math.round(($(window).width()) / 2);
}


WMM.Exceptions =
{
  callback: null
}

/**
 * @param callback {Function} function that handles an exception
 */
WMM.Exceptions.setHandler = function(callback)
{
  WMM.Exceptions.callback = callback;
}

/**
 * Gives an ugly alert if no exception handler is set, forcing the programmer
 * to do so.
 */
WMM.Exceptions.checkHandler = function()
{
  if(WMM.Exceptions.callback === null)
  {
    var msg = "Please set an exception handler using WMM.Exceptions.setHandler() before calling methods on the Womima API";
    alert(msg);
    throw new WMM.Exception(msg);
  }
}

/**
 * Call this in a catch block to handle an exception with the specified handler.
 * @param e {Exception}
 */
WMM.Exceptions.handle = function(e)
{
   WMM.Exceptions.callback(e);
}

WMM.Exception = function(message, detailedMessage, origin)
{
  this.message = message;
  this.name = "WMM.Exception";
  this.origin =          (origin)          ? origin          : null;
  this.detailedMessage = (detailedMessage) ? detailedMessage : null;

  this.getDetailedString = function()
  {
    var m = '';
    if(this.detailedMessage !== null)
    {
      m += "[" + this.detailedMessage + "]";
    }
    if(this.origin !== null)
    {
      m += "\n\n(from " + this.origin + ")";
    }
    return m;
  }
};

WMM.trycatchify = function(f)
{
  return function()
  {
    try
    {
      return f.apply(this, arguments);
    }
    catch(e)
    {
      WMM.Exceptions.handle(e);
    }
  }
}

if(typeof(jQuery) != "undefined")
{
  jQuery.fn.trybind = function(event, fn)
  {
    this[event](WMM.trycatchify(fn));
  }
}
/**
 * @author Admin
 */
/**
 * Gets a number of associations from the server and processes them using the callback funtion
 *
 * @param phraseFrom The word from which the associations are needed
 * @param amount The number of associations to be given back
 * @param callbackFunction Called when the associations are available.
 * 									Must take a list of strings (the associations) as an argument.
 * @param lastWord Optional, if it is given it will be in the ring instead of one association
 * 				   (except if this word was already in the associations, then nothing will happen.)
 */

WMM.associationSet = {}

WMM.associationSet.setJSON = function(jsonString){
	
	if(jsonString != ""){
        this.jsonObject = WMM.parseJSON(jsonString);
	}
}

WMM.associationSet.getAssociations = function( lastWord, amount ){
	    var wordInAssociations = false;
        var phraseTos = new Array();
        //The minimum of the available associations and max ring 
        //length - 1 (for the last central word)
        var min = Math.min(this.jsonObject.length, amount - 1);
        //fill the ring with one place left and check if the last central word is found
        for (var j = 0; j < min; j++) {
            if (this.jsonObject[j].phraseTo == lastWord) {
                wordInAssociations = true;
            }
            phraseTos.push(this.jsonObject[j].phraseTo);
        }
        //if there is a last word and it is not found yet, push it in
        if (!wordInAssociations && !(typeof lastWord == 'undefined' || lastWord == null || lastWord == '' )) {
            phraseTos.push(lastWord);
        }
        //if the last word is found or not available, push another association in
        else 
            if (this.jsonObject[j] != undefined) {
                phraseTos.push(this.jsonObject[j].phraseTo);
            }
			console.log("lastword: " + lastWord);
	return phraseTos;
}


WMM.getAssociations = function(phraseFrom, amount, callbackFunction, lastWord){
    //make the request for the associations belonging to the central word
    var phraseFroms = new Array()

    var req = new WMM.RESTRequest(null, "GET");
    
    var computeURL = function(){
        var url = '/json/associations/' + urlencode(phraseFrom.toLowerCase()) + "/amount=" + config.maxRingLength + '/';
        if (WMM.auth.isAuthenticated) {
            url += 'authors=[' + WMM.auth.author.authorId + '];';
        }
        
        if (!WMM.isWomimap()) {
            url += 'maps=[' + WMM.selectedMap + '];';
        }
        return url;
    }
    
    req.setURL(computeURL());

    req.callback = function(responseText){
        //init

		WMM.associationSet.setJSON(responseText);
		var phraseTos = WMM.associationSet.getAssociations(lastWord, amount);
        if (typeof(callbackFunction) == 'function') {
            callbackFunction(phraseTos);
        }
        
    }
    req.send();
    
    
}

WMM.getRecentAssociations = function(amount, callbackFunction, lastWord){
 //make the request for the most recent associations 
    var req = new WMM.RESTRequest(null, "GET");
    
    var computeURL = function(){
        var url = "/json/associations/recent/amount=" + config.maxRingLength + '/';
        if (WMM.auth.isAuthenticated) {
            url += 'authors=[' + WMM.auth.author.authorId + '];';
        }
        
        if (!WMM.isWomimap()) {
            url += 'maps=[' + WMM.selectedMap + '];';
        }
        return url;
    }
    
    req.setURL(computeURL());

    req.callback = function(responseText){
        //init

		WMM.associationSet.setJSON(responseText);
		var phraseTos = WMM.associationSet.getAssociations(lastWord, amount);
        if (typeof(callbackFunction) == 'function') {
            callbackFunction(phraseTos);
        }
        
    }
    req.send();
    
    
}


///
/**
 * Create a new association
 * 
 * @param phraseFrom The central word the new association belongs to
 * @param phraseTo	 The association that is added
 */
WMM.createAssociation = function(phraseFrom, phraseTo){
    if ((phraseFrom != '') && (phraseTo != '')) {
		phraseFrom = phraseFrom.toLowerCase();
		phraseTo = phraseTo.toLowerCase();
	
        WMM.debug.out('current map id: ' + WMM.selectedMap, 'currentState');
        if (WMM.auth.author) {
            WMM.debug.out('current author id: ' + WMM.auth.author.authorId, 'currentState');
        }
        
        var computeURL = function(){
            var url = '/json/associations/' + urlencode(phraseFrom.toLowerCase()) + '/' + urlencode(phraseTo.toLowerCase()) + '/';
            if (WMM.auth.isAuthenticated) {
                url += 'authors=[' + WMM.auth.author.authorId + '];';
            }
            
            if (!WMM.isWomimap()) {
                url += 'maps=[' + WMM.selectedMap + '];';
            }
            return url;
        }
        
        var request = new WMM.RESTRequest('', 'POST');
        
        //set callback to recompute the URL, just in case authentication info changed
        //before this request got out of queue.
        request.beforeSend = function(req){
            request.setURL(computeURL());
        };
        
        request.isSynchronous = true;
        
        request.callback = function(responseText){
            var wasWomimap = WMM.isWomimap();
            
            createdInfo = WMM.parseJSON(responseText);
            WMM.selectedMap = createdInfo.mapId;
            WMM.setAuthor(createdInfo.author, true);
			alert(responsetext);
            if (wasWomimap) {
                WMM.Notify.show('newMapWasCreated', {mapId: createdInfo.mapId});
                
                //raise a flag that we have a newly created map.
                WMM.setLatestCreatedMap(createdInfo.mapId);
            }
            
        }
    
        request.send();
    }
    
    
}

if(typeof(WMM) == "undefined") WMM = {};

//debug functionality
WMM.debug = {};

//comment lines in and out to enable particular debug messages:
WMM.debug.displayedModes = 
[ 
  '__noMode',
//  'requestQueue',
//  'callback',
//  'RESTresponse',
//  'callbackOut',
//  'REST',
//  'currentState',
  null
];

WMM.debug.out = function(s, mode)
{
  if(WMM.DEBUG_MODE)
  {
    if(typeof(s) == 'object')
    {
      s = JSON.stringify(s);
    }
    
    if(typeof(mode) == 'undefined')
    {
      mode = '__noMode';
    }
    var enabled = (Array_indexOf(WMM.debug.displayedModes, mode) != -1);

    if(enabled)
    {
      var el = document.getElementById('wmm_debug');
      if(!el)
      {
        el = document.createElement('div');
        el.id = 'wmm_debug';
        el.style.position = 'absolute';
        el.style.right = '0px';
        el.style.top = '0px';
        el.style.width = '300px';
        el.style.height = '600px';
        el.style.overflow = 'scroll';
        el.style.background = '#fff';
        el.style.opacity = '0.8';
        el.style.whiteSpace = 'nowrap';
        document.body.appendChild(el);
      }
      el.innerHTML = s + "<br />" + el.innerHTML;
    }
  }
}

WMM.parseJSON = function(s)
{
  try
  {
    return JSON.parse(s);
  }
  catch(e)
  {
    throw new WMM.Exception("invalid JSON: " + s);
  }
}


/**
 * @file Auth.js
 *
 * Functions and variables related to authentication and global state
 */

WMM.WOMIMAP = 'WOMIMAP';

/**
 * @namespace Holds information about the currently authenticated author
 */
WMM.auth =
{
  /**
   * Author object specified on (for example)
   * http://svn.womima.com/wmm/wiki/AuthorObj, must contain at least
   *  AuthorObj = {"authorId": "int", "isAnonymous": "bool"}.
   *
   * Is set iff isAuthenticated == true
   */
  author:
  {
    /**
     * DB id of author
     */
    authorId: null,
    isAnonymous: false
  },

  /**
   * Whether the user is authenticated (i.e. known to the server) or not. Note
   * that a user does not need to be logged in in order to be authenticated;
   * most notably, after creating an association anonymously, an anonymous author
   * account is automatically created.
   */
  isAuthenticated: false,

  /**
   * True iff isAuthenticated && !author.isAnonymous
   */
  isLoggedIn: false
}

/**
 * @param authorObj object Like specified on (for example)
 * http://svn.womima.com/wmm/wiki/AuthorObj,
 * must contain at least AuthorObj = {"authorId": "int", "isAnonymous": "bool"}
 *
 * @param gotAuthenticated bool Set to true if and only if this author change
 * means that the user is now authenticated.
 */
WMM.setAuthor = function(authorObj, gotAuthenticated)
{
  WMM.auth.author = authorObj;
  WMM.auth.isAuthenticated = gotAuthenticated;
  WMM.auth.isLoggedIn = (WMM.auth.isAuthenticated) && (!WMM.auth.author.isAnonymous);

  if(!gotAuthenticated)
  {
    WMM.globalState.selectedMap.reset();
  }
}

/**
 * @param mapId id of the map that is to be selected
 * 
 * @param noReload boolean Set to true in order to prevent the selected-map-change to cause
 * a reload of the sidebar page.
 */
WMM.selectMap = function(mapId, noReload)
{
  var oldMapId = WMM.globalState.selectedMap.get();

  WMM.globalState.selectedMap.set(mapId);
  
  if((!noReload) && (oldMapId != mapId) && Sidebar.Page.selectedInstance && Sidebar.Page.selectedInstance.name == 'maps')
  {
    Sidebar.Page.selectedInstance.loadContent();
  }
}

/**
 * Set the new map (without sidebar functionality)
 * 
 * @param mapId id of the map that is to be selected
 * 
 */
WMM.setMap = function(mapId)
{
  WMM.globalState.selectedMap.set(mapId);
}


/**
 * Simple get/set value class that retrieves its value from a cookie
 * on page load, and updates the cookie on every set() call.
 *
 * Use this object for storing any value in a cookie, across pages.
 *
 * @param name string Object name. Used in cookie name
 * @param defaultValue mixed Default value to use if the cookie does not exist,
 * or if reset() is called.
 */
WMM.GlobalState = function(name, defaultValue)
{
  var state = WMM.Ext.readCookieDefault("WMM_"+name, defaultValue);

  this.set = function(value)
  {
    state = value;
    WMM.Ext.createCookie("WMM_"+name, state);
  }
  this.get = function()
  {
    return state;
  }
  ///reset this value back to its default value.
  this.reset = function()
  {
    state = defaultValue;
  }
}

/**
 * The Global State means state that is kept across pages, e.g. in a cookie.
 * The values in these objects must be queried using their get() method, i.e.
 * alert(WMM.globalState.selectedMap.get()), instead of the older
 * alert(WMM.selectedMap).
 */
WMM.globalState =
{
  selectedMap:      new WMM.GlobalState("selectedMap", WMM.WOMIMAP),
  latestCreatedMap: new WMM.GlobalState("latestCreatedMap", false)
};

WMM.isWomimap = function()
{
  return ((WMM.globalState.selectedMap.get() === null) || (WMM.globalState.selectedMap.get() === WMM.WOMIMAP));
}

WMM.auth = {};



/**
 * Encapsulates a request to a Womima REST resource.
 *
 * Typical usage example:
 *
 * var req  = new WMM.RESTRequest("/json/bananaTrees/4/size=large/", GET);
 * req.callback = function(responseText)
 * {
 *   alert("Tree 4 has " + responseText + " large bananas!");
 * }
 * req.send();
 *
 * the URL parameter is relative to whatever WMM.serverRoot is set to.
 *
 * Main attributes:
 *   req.callback - a function with one argument that receives the response 
 *     text. The response text is not parsed in any way; use e.g. 
 *     WMM.parseJSON(responseText) if you expected JSON text.
 *     
 *   req.data - any POST data to send. if passed a Javascript object or array,
 *     then it is automatically converted to JSON. therefore, if you want to
 *     send anything other than JSON, be sure to send a string and set 
 *     req.xheaders['Content-Type'] accordingly.
 *
 * Additional attributes:
 *   req.xheaders - an object that maps additional HTTP headers to values. By
 *     default, the 'Content-Type' header is set to 'application/json'.
 *
 *   req.beforeSend - callback function that gets called before the request is
 *     really sent. Allows for last-minute changes to the URL via setURL(), and
 *     other hacks.
 *
 *   req.url - the full URL as was specified. consider this read-only!
 *     instead of setting req.url, call req.setURL() if you need to make
 *     changes to the URL.
 *
 *   req.useJSONP - set to true to use JSONP, a security hack to allow accessing
 *     a REST resource on a different server than what served this JS file. This
 *     currently ONLY works on a development server, because with JSONP
 *     all requests are GET. We have to consider the security and performance
 *     implications of changing that, but probably it is needed for 3rd party
 *     widgets.
 *
 *   req.isSynchronous - set to true to ensure that this request is not
 *     sent before all previous ones were handled. this way, a certain order
 *     of handling and sending requests is enforced, at a potential performance
 *     penalty.
 *     
 *   req.toString() - for debugging.
 *
 * @param url The URL, relative to WMM.serverRoot
 * @param method One of "POST", "GET", "DELETE", "PUT"
 */
WMM.RESTRequest = function(url, method)
{
  //ensure that the user set an exception handler.
  WMM.Exceptions.checkHandler();
  
  var self = this;
  
  ///// attributes \\\\\
  this.callback      = null;
  this.handleExceptions = true;
  this.xhr           = new XMLHttpRequest(); //do not change
  this.data          = "";
  this.xheaders      = {};
  this.url           = "";
  this.method        = method;
  this.useJSONP      = false;
  //this.testJSONP     = true; //debugging shortcut; comment out to disable.
  this.isSynchronous = false;
  this.beforeSend    = null;
  
  ///// methods \\\\\\
 
  ///always use this function instead of directly assigning to url.
  ///this way, JSONP stuff is guaranteed to work
  ///@param url string the URL relative to server root
  this.setURL = function(url)
  {
    this.url = WMM.serverRoot + url;
    
    //ensure trailing slash
    if(this.url.charAt(this.url.length-1) != '/')
    {
      this.url += '/';
    }
    
    //in "devel mode", we allow JSONP requests for remote calls.
    if(WMM.DEVEL_MODE)
    {
      
      if(this.testJSONP || this.url.indexOf('//' + location.host) == -1)
      {
        this.useJSONP = true;
      }
    }
  }


  this.toString = function()
  {
    var s = self.method + " on '" + self.url + "'";
    if(self.isSynchronous)
    {
      s += ' (synchronous)';
    }
    return s;
  }
  
  /**
   * Does nothing unless the responseText has an exception hidden inside. If it does,
   * the exception is thrown.
   *
   * @param {String} responseText  moo
   */
  this.searchForException = function(responseText)
  {
    //var matches = responseText.match(/^<!-- WMMERR: (.*?)( - WMMDETAIL: (.*?))?-->/);

    if(responseText.indexOf('{"WMM_EXCEPTION":') != -1)
    {
      var parsedExpression = WMM.parseJSON(responseText).WMM_EXCEPTION;
      throw new WMM.Exception(parsedExpression.msg, parsedExpression.detailedMsg, self.url);
    }
  }
  
  //calls the callback, but only if the response was not an exception
  this.callbackCaller = function(responseText)
  {
    if(self.handleExceptions)
    {
      self.searchForException(responseText);
    }
    //call user-specified callback
    self.callback(responseText)
  }
   
  this.handler = WMM.trycatchify(function()
  {
    switch(self.xhr.readyState) 
    {
    case XMLHttpRequest.DONE:
    
      try 
      {
        if(parseInt(self.xhr.status / 100) == 2)
        {
          if(typeof self.callback == "function")
          {
            WMM.debug.out(self.xhr.responseText, 'RESTresponse');
            self.callbackCaller(self.xhr.responseText);
          }
          else
          {
            throw new WMM.Exception('no callback defined!');
          }
        }
        else
        {
          self.searchForException(self.xhr.responseText);
          //still alive? then we really don't know what to do.
          throw new WMM.Exception('unexpected HTTP status. code returned: ' + self.xhr.status + ' with body: ' + self.xhr.responseText);
        }
      }
      finally
      {
        // make sure that the request queue never gets blocked.
        WMM.RESTRequest.requestQueue.completed(self);
      }
      
      break;
    }
  })
    
  this.send =  function()
  {
    WMM.RESTRequest.requestQueue.nq(self);
  }
    
  this.go = function()
  {
    if(typeof(this.beforeSend) == 'function')
    {
      this.beforeSend(this);
    }
    
    WMM.debug.out(self.method + ' on \'' + self.url + '\'', 'REST');
    
    var data = self.data;
    
    if((typeof(data) == "object") || (typeof(data) == "array"))
    {
      data = JSON.stringify(data);
    }
    
    if(self.useJSONP)
    {
      /* JSONP is a method for avoiding the Same-Origin-Policy of browsers. It is
         only supported by the server if it is in development mode, which is 
         determined by /server/env.php
         
         On the Javascript side, our JSONP implementation creates a unique 
         function on the window object, so that its name can be passed to the PHP script. 
         Then, a SCRIPT tag is added which contains a call to this function. 
         The function then passes the passed data on to the specified callback function 
         and removes itself from the window object. 
       */

      var scriptTag = document.createElement('script');
      //FIXME: does not support urls with a query string.
      
      WMM.JSONPHandlerCount++;
      var handlerName = 'WMM_handleJSON' + WMM.JSONPHandlerCount;
      
      window[handlerName] = function(data)
      {
        self.callback(data);
        delete window[handlerName];
      }
      
      var url = this.url + '?__method=' + this.method  + '&jsonp=' + handlerName;
      
      if(data != "")
      {
        url += 'data=' + urlencode(data);
      }
      
      scriptTag.setAttribute('src', url);
      document.getElementsByTagName('head')[0].appendChild(scriptTag); 
    }
    else
    {
      //the normal, faster mode of operation is an XMLHTTPRequest call (AJAX), which
      //only works if the URL has the same hostname as the place this javascript
      //file came from.
      
      self.xhr.open(self.method, self.url);
      self.xhr.setRequestHeader('Content-Type', 'application/json');
      for(hdr in self.xheaders)
      {
        self.xhr.setRequestHeader(hdr, self.xheaders[hdr]);
      }
      self.xhr.onreadystatechange = self.handler;
      
      self.xhr.send(data)
    }

    if(typeof(WMM.Timeout) != "undefined")
    {
      WMM.Timeout.resetCounter();
    }
  }
  
  ///// constructor \\\\\

  //not all browsers support PUT and DELETE on the XMLHttpRequest object
  //so we always use overloaded POST for those verbs.
  if((method!='GET') && (method != 'POST'))
  {
    this.method = 'POST';
    this.xheaders['X-HTTP-Method-Override'] = method;
  }
  
  this.setURL(url);
};



/// A queue of RESTRequest objects.
WMM.RESTRequest.requestQueue =
{
  queue: [],
  isBlockedOnSynchronousRequest: false,
  
  nq: function(req)
  {
    this.queue.push(req);
    this.process();
  },
  
  // makes all request objects call go() until a synchonous one is encountered.
  process: function()
  {
    if(!this.isBlockedOnSynchronousRequest)
    {
      while(this.queue.length != 0)
      {
        request = this.queue.shift();
        request.go();
        
        if(request.isSynchronous)
        {
          WMM.debug.out('blocked: ' + request.toString(), 'requestQueue');
          this.isBlockedOnSynchronousRequest = true;
          break;
        }
      }
    }
  },
  
  // call when signalling to the queue that a request was completed.
  // used to ensure that after a synchronous request was completed, new 
  completed: function(request)
  {
    if(request.isSynchronous)
    {
      WMM.debug.out('completed, so unblocked: ' + request.toString(), 'requestQueue');
      this.isBlockedOnSynchronousRequest = false;
      this.process();
    }
  }
}


WMM.Callback = function(action, data, tag, responseTag)
{
  var self = this;
  this.action      = action;
  this.data        = (typeof data        == 'undefined') ? null : data;
  this.responseTag = (typeof responseTag == 'undefined') ? null : responseTag;
  this.tag         = (typeof tag         == 'undefined') ? null : tag;
  
  //derived
  this.obj         = null;
  this.requestFunc = null;
  this.processFunc = null;
  
  this.toJSON = function()
  {
    return {action: this.action, data: this.data, responseTag: this.responseTag, tag: this.tag};
  };
  

  (function(cbObj)  {
    //transform 'Zwoesjer.moo.toink' into WMM['Zwoesjer']['moo']['toink'].
    var objParts = cbObj.action.split('.');
    var actionObj = WMM;
    var funcName = objParts.pop();
    
    for(var i in objParts)
    {
      actionObj = actionObj[objParts[i]];
      if(!actionObj) 
      {
        throw new WMM.Exception("action object not found in "+cbObj.action);
      }
    }
    cbObj.obj         = actionObj;
    cbObj.requestFunc = actionObj['request_'+funcName];
    cbObj.processFunc = actionObj['process_'+funcName];
    
  })(this);
};


//used to ensure that every JSONP handler function is unique.
WMM.JSONPHandlerCount = 0;

//dispatch request, using closure for response handler
WMM.doRequest = function(wmmCallback, restRequest)
{
  restRequest.callback = function(responseText)
  {
    WMM.processRequest(wmmCallback, responseText);
  };
  restRequest.send();
};

WMM.processRequest = function(wmmCallbackIn, responseText)
{
  //if response may be required, assume that a process function was defined
  if(typeof(wmmCallbackIn.processFunc) == "function")
  {
    var wmmCallbackOut = wmmCallbackIn.processFunc(responseText, wmmCallbackIn.data);
    WMM.doCallbackOut(wmmCallbackIn, wmmCallbackOut);

  }
};

WMM.doCallbackOut = function(wmmCallbackIn, wmmCallbackOut)
{    
  if(wmmCallbackOut)
  {
    wmmCallbackOut.responseTag = wmmCallbackIn.tag;
    //alert(JSON.stringify([wmmCallbackIn, wmmCallbackOut]));
    
    WMM.debug.out(wmmCallbackOut, 'callbackOut');
    
    wmmCallbackIn.obj.doCallback(wmmCallbackOut);
  }
}

WMM.callback = function(wmmCallback)
{
  //alert(wmmCallback);
  if(typeof wmmCallback === 'string')
  {
    wmmCallback = jsonParse(wmmCallback);
  }

  //zwoesjer bug workaround: the action "Zwoesjer.getAuthInfo " is called.
  wmmCallback.action = wmmCallback.action.replace(/^\s+|\s+$/, "");
  
  wmmCallback = new WMM.Callback(wmmCallback.action, wmmCallback.data, wmmCallback.tag, wmmCallback.responseTag);
  
  WMM.debug.out('got callback "' + wmmCallback.action + '"', 'callback');
  
  if(typeof(wmmCallback.requestFunc) != "function")
  {
    throw new WMM.Exception("action method '"+wmmCallback.requestFunc+"' not found for "+wmmCallback.action);
  }
  
  //call request function
  var requestFuncResult = wmmCallback.requestFunc(wmmCallback.data);
  
  //depending on the type of the value returned by the requestFunc, 
  //we perform different actions, or none.
  if(requestFuncResult instanceof WMM.RESTRequest)
  {
    WMM.doRequest(wmmCallback, requestFuncResult);
  }
  else if(requestFuncResult instanceof WMM.Callback)
  {
    WMM.doCallbackOut(wmmCallback, requestFuncResult);
  }
};


/**
 * @file Notify.js
 *
 * Object with methods to notify the user of certain events.
 */

WMM.Notify = {}

/**
 * Shows a notification window that explains that a new map was created.
 * 
 * @param args object with named arguments.
 * @param args.mapId id of the map that was just created
 */
WMM.Notify.newMapWasCreated = function(args)
{
  if(WMM.globalState.latestCreatedMap.get())
  {
    WMM.globalState.latestCreatedMap.reset();
  }
  
  var mapId = args.mapId;
  
  if(Sidebar.Page.selectedInstance.name != 'maps')
  {
    Sidebar.Page.get('maps').select();
  }

  var el = $("li.map[data-mapId='" + mapId + "']");
  if(el.length)
  {
    var url = 'htmls/notifications/page=newMapWasCreated/';

    var request = new WMM.RESTRequest(url, 'GET');
    request.callback = function(responseText)
    {
      WMM.Notify.showNotificationLeft(responseText, el, 10000);
    }
    request.send();
    return true;
  }
  else
  {
    return false;
  }
}

/**
 * Attempts to show a notification, by dispatching a
 * call to WMM.Notify[name], passing the args object.
 *
 * If the dispatched method returns false, another attempt
 * is made 2 seconds later. At most 3 attempts are made.
 *
 * The dispatched method may fail because, for instance, HTML
 * elements on which it depends have not loaded yet.
 *
 * @param name Name of the notification window that is to be shown.
 * Example: 'newMapWasCreated'.
 *
 * @param args Object containing any arguments the notification window needs.
 *
 * @param attempts Optional. Used internally to limit the amount of attempts made.
 */
WMM.Notify.show = function(name, args, attempts)
{
  if(typeof(attempts) == "undefined")
  {
    attempts = 0;
  }

  var func = WMM.Notify[name];
  if(func(args))
  {
    return;
  }
  else
  {
    setTimeout(function()
    {

      if(attempts < 3)
      {
        WMM.Notify.show(name, args, attempts + 1)
      }
    }, 2000);
  }
}

/**
 * Shows a notification window on the left of the window
 *
 * @param text the HTML code that should be in the notification window
 *
 * @param beneathElement a jQuery object referring to the element beneath which
 * the notification window should appear. Note that this only influences the
 * Y-position of the window
 *
 * @param disappearAfter the amount of milliseconds after which the window should
 * disappear, or false if it should not automatically disappear.
 */
WMM.Notify.showNotificationLeft = function(text, beneathElement, disappearAfter)
{
  //compute left and width from available space next to zwoesjer. limiting it at 300 px wide.
  var main = $('.mainContentField');
  var width = main.offset().left - 35;
  var left = 15;
  var maxWidth = 300;
  if(width > maxWidth)
  {
    left += (width - maxWidth);
    width = maxWidth;
  }

  //compute top
  var top = beneathElement.offset().top + beneathElement.outerHeight();

  //create our node.
  var notif = $('<div class="notification" style="left: ' + left + 'px; width: ' + width + 'px; top: ' + top + 'px; position: absolute;"></div>')
  notif.html(text);

  //show our node.
  $('body').append(notif);

  //auto-disappear after [disappearAfter] milliseconds
  var timeoutHandle = 0;
  if(disappearAfter)
  {
    timeoutHandle = setTimeout(function()
    {
      notif.hide(); //notif.fadeOut() does not work, for some odd reason.
    }, disappearAfter);
  }

  //close button handler
  notif.find('#closeNotificationBtn').click(function()
  {
    notif.hide(); //notif.fadeOut() does not work, for some odd reason.
    if(disappearAfter)
    {
      clearTimeout(timeoutHandle);
    }
  });
}

//$(document).ready(function()
//{
//  setTimeout(function()
//  {
//    WMM.Notify.show('newMapWasCreated', {mapId: 'WOMIMAP'});
//  }, 1000);
//});

WMM.Timeout =
{
  timeoutHandle: null,
  duration: null,
  callback: null,
  enabled: false
};

/**
 * Enables the timeout functionality
 * @param {Integer} mins amount of minutes before the site should time out.
 * @param {Function} callback method that should be called when the mins amount of minutes
 * have expired without a call to WMM.Timeout.resetCounter.
 */
WMM.Timeout.enable = function(mins, callback)
{
  WMM.Timeout.duration = parseInt(mins * 60 * 1000);
  WMM.Timeout.enabled = true;
  WMM.Timeout.callback = callback;
  WMM.Timeout.resetCounter();
}

/**
 * Disables the timeout functionality.
 */
WMM.Timeout.disable = function()
{
  WMM.Timeout.enabled = false;
}

WMM.Timeout.handler = WMM.trycatchify(function()
{
  if(WMM.Timeout.enabled)
  {
    if(typeof(WMM.Timeout.callback) == "function")
    {
      WMM.Timeout.callback();
    }
  }
});

/**
 * (re)starts the timeout counter. Call this upon page load, and whenever the
 * user sends something to the server that would refresh the session.
 */
WMM.Timeout.resetCounter = function()
{
  
  if(WMM.Timeout.enabled)
  {
    if(WMM.Timeout.timeoutHandle)
    {
      clearTimeout(WMM.Timeout.timeoutHandle);
    }
    WMM.Timeout.timeoutHandle = setTimeout(WMM.Timeout.handler, WMM.Timeout.duration);
  }
}
/**
 * @return false on error, true otherwise.
 */ 
function WMM_callback(wmmCallback)
{
  try
  {
    return WMM.callback(wmmCallback);
  }
  catch(e)
  {
    WMM.Exceptions.handle(e);
  }
}


