var ibre = {
  params: location.href.toQueryParams(),
  path: location.href.substring(0, location.href.lastIndexOf("/") + 1),
  errors: [],
  calendar: {},
	datasource: {
		nodes: [],
    namedNodes: {},
		values: {},
    _parsed: false,
    numLoaded: 0,
    loading: false,
    _numLoading: 0
	},
  parsed: false,
  namedNodes: {},
  _percentageDS: 36,
  _percentageTPL: 40,
  _percentageParse: 70,
  _percentageStep: 0.2,
  _makeEventHandler: function (node, name) {
    name = "on" + name;
    var js = node.getAttribute(name);
    if (js)
      eval("node." + name + " = function (event) { with (this) { " + js + " } }.bind(node);");
  },
  _callEventHandler: function (node, name) {
    name = "on" + name;
    if (node[name]) return node[name]({targetElement: node});
  },
  console: function (str) {
    if (!ibre._consoleNode) {
      ibre._consoleNode = document.createElement("table");
      ibre._consoleNode.border = 1;
      ibre._consoleNode.createTHead().insertRow(0).insertCell(0).innerHTML = "<a href='javascript: ibre.console.clear()'>Clear</a>";
      if (!ibre._consoleNode.tBodies.length) ibre._consoleNode.appendChild(document.createElement("tbody"));
      //new Insertion.Top(ibre._consoleNode, "<thead><tr><td><a href='javascript: ibre.console.clear()'>Clear</a></td></tr></thead><tbody></tbody>")
      document.body.appendChild(ibre._consoleNode);
    }
    var node;
    if (typeof str == "object") {
      function showObj(obj, node) {
        node.innerHTML = "";
        var table = jsToDom("table", {border: "1", cellpadding: "4", cellspacing: "0"}, node);
        table.style.borderCollapse = "collapse";
        table = jsToDom("tbody", null, table);
        var s = [];
        for (var a in obj) {
          try {
            if (typeof obj[a] != "function") s.push(a);
          }
          catch (e) {
            s.push(a);
          }
        }
        if (!(obj instanceof Array)) s.sort();
        for (var i = 0; i < s.length; i++) {
          var tr = jsToDom("tr", null, table);
          var tdName = jsToDom("td", {align: "right", width: "1"}, tr);
          var field = s[i];
          var value;
          var valueStr;
          try {
            value = obj[field];
            valueStr = new String(value);
          }
          catch (e) {
            value = e;
            valueStr = e.toString();
          }
          var tdValue = jsToDom("td", null, tr);
          switch (typeof value) {
            case "object":
              if (value == null) {
                setInnerText(tdName, field);
                setInnerText(tdValue, "null");
                tdValue.style.fontStyle = "italic";
                break;
              }
            //case "array":
              var link = jsToDom("a", {href: "javascript: ;"}, tdName);
              tdValue.libreValue = value;
              setInnerText(link, field);
              setInnerText(tdValue, valueStr);
              /*link.onclick = function () {alert(3);
                showObj(this.libreValue, this);
              }.bind(td);*/
              Event.observe(link, "click", function () {
                showObj(this.libreValue, this);
              }.bind(tdValue));
              //td.style.cursor = "pointer";*/
              break;
            default:
              setInnerText(tdName, field);
              setInnerText(tdValue, valueStr);
          }
        }
      }
      node = document.createElement("div");
      showObj(str, node);
    }
    else {
      node = document.createElement("pre");
      node.appendChild(document.createTextNode(new String(str).replace("\n", "\n\r")));
    }
    ibre._consoleNode.tBodies[0].insertRow(-1).insertCell(0).appendChild(node);
  },
  event: {
    _listeners: [],
    add: function (element, eventName) {
      var item = {element: element, eventName: eventName};
      if (!this._listeners.contains(item)) this._listeners.push(item);
    }
  },
  all: {},
  set: function (node, property, value) {
    switch (node.tagName) {
      case "INPUT":
        if (node.type != "image") break;
      case "IMG":
        if (node.rollover && property == "src") node.src = node.originalSrc = value;
        break;
    }
  }/*,
  repetitionTemplates: {}*/
};
ibre.console.clear = function() {
  Element.remove(ibre._consoleNode);
  ibre._consoleNode = null;
}
var ie = /MSIE/.test(navigator.userAgent);
var moz = !ie && navigator.product == "Gecko";
function include(src) {
  //if (ie) {
    document.write("<script type='text/javascript' src='" + src + "'></script>");
  /*}
  else {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = src;
    document.head.appendChild(script);
  //}*/
}
String.prototype.endsWith = function (str) {
  return this.substring(this.length - str.length) == str;
};
(function () {
  document.head = document.getElementsByTagName("head")[0];
  var basePath = "";
  //var scripts = document.getElementsByTagName("script");
  var scripts = document.head.childNodes;
  var commonFile = "1ibre.js";
  for (var i = 0; i < scripts.length; i++) {
    var src = scripts[i].src;
    if (src && src.endsWith(commonFile)) {
      basePath = src.substring(0, src.length - commonFile.length);
      break;
    }
  }
  ibre.basePath = basePath;
  //include(basePath + "prototype.js");
  //include(basePath + "scriptaculous/scriptaculous.js");
  //include(basePath + "ieemu/ieemu.js");
  //include(basePath + "sarissa/sarissa.js");
  include(basePath + "mathcontext.js");
  include(basePath + "bigdecimal.js");
  if (!window.Calendar) {
    include(basePath + "jscalendar/calendar.js");
    include(basePath + "jscalendar/lang/calendar-es.js");
    include(basePath + "jscalendar/calendar-setup.js");
  }
  include(basePath + "lib/init.js");
  include(basePath + "lib/lang.js");
  include(basePath + "lib/dom.js");
  include(basePath + "lib/ajax.js");
  include(basePath + "lib/grammar.js");
  include(basePath + "json/json.js");
  //include(basePath + "xslt/xslt.js");
  include(basePath + "whatwg/whatwg.js");
  include(basePath + "whatwg/lang/en.js");
  include(basePath + "whatwg/parser.js");
  include(basePath + "whatwg/css.js");
  include(basePath + "whatwg/validate.js");
})();

