
if (window['console'] === undefined) {
  window.console = {log: Prototype.emptyFunction};
}

Kwo = {

  "registry": {},
  "Class": {},
  "Composer": {},
  "Visitor": {},
  "dialogs": [],
  "callbacks": {},
  "flags": {},
  "scripts": {},

  "encodeToHex": function(str) {
    var r="";
    var e=str.length;
    var c=0;
    var h;
    while(c<e){
        h=str.charCodeAt(c++).toString(16);
        while(h.length<3) h="0"+h;
        r+=h;
    }
    return r;
  },

  "getDialog": function(name) {
//    if (!Object.isHash(Kwo.dialogs)) { Kwo.dialogs = new Hash(); }
    name = name || Kwo.dialogs.keys().pop();
    return Kwo.dialogs.get(name);
  },

  "setDialog": function(dialog) {
//    if (!Object.isHash(Kwo.dialogs)) { Kwo.dialogs = new Hash(); }
    Kwo.dialogs.set(dialog.name, dialog);
  },

  "hasDialog": function(name) {
    return !Object.isUndefined(Kwo.dialogs.get(name));
  },

  "getEditor": function() {
    return Kwo.registry["_editor"]
  },

  "setEditor": function(editor) {
    Kwo.registry["_editor"] = editor;
    return Kwo.registry["_editor"];
  },

  "setContext": function (key, value) {
    window["_context"][key] = value;
  },

  "onViewportChange": function(e) {
    if (Kwo.dialogs === null) return ;
    Kwo.dialogs.each(function (pair) {
      pair.value.place();
    });
  },

  "F": function(model) {
    model = model.toLowerCase();
    if (model in Kwo.registry) return Kwo.registry[model];
    Kwo.registry[model] = new Kwo.Class.Obj(model);
    return Kwo.registry[model];
  },

  "hasError": function(res) {
    if (Object.isUndefined(res)) return false;
    return res["error"] >= 1;
  },

  "mergeArgs": function() {
    var h = new Hash({}), n = arguments.length, arg;
    for (var i = 0; i < n; i++) {
      arg = arguments[i];
      if (arg === undefined || arg === null || arg === false || arg === true) {
        continue;
      }
      if (Object.isString(arg)) {
        arg = arg.toQueryParams();
        h.update(arg);
      }
      else if (typeof arg == "object") {
        if (Object.isArray(arg)) {
          arg.each(function (item) {
            h.update(Kwo.mergeArgs(item));
          });
        }
        else if (Object.isElement(arg)) {
          if (arg.tagName.toUpperCase() == "FORM") {
            arg = $(arg).serialize(true);
          }
          else if ($(arg)) {
            if ("form" in arg && arg.form) {
              arg = Element.extend(arg.form).serialize(true);
            }
            else {
              var tmp = $(arg).up("form");
              arg = tmp ? tmp.serialize(true) : {};
            }
          }
          h.update(arg);
        }
        else {
          h.update(arg);
        }
      }
    }
    return h;
  },

"exec": function(name, value) {
    if (name == "createlink") {
      if (value == false) {
        this.saveRange();
        new Kwo.Prompt({title: "Adresse du site ?", prefix: "http://"}, this.exec.bind(this).curry(name));
        return ;
      }
      if (!Object.isString(value) || value.legnth < 5) return ;
      if (!(value.toLowerCase().startsWith("http://") || value.toLowerCase().startsWith("https://"))) {
        value = "http://" + value;
      }
      if (value.toLowerCase().startsWith("http://http://")) {
        value = value.substr(7);
      }
      this.restoreRange();
      if (Prototype.Browser.IE && this.range) {
        this.range.select();
      }
      this.doc.execCommand(name, false, value);
    }
    else if (name == "paste") {
      if (value == false) {
        this.saveRange();
        var d = new Kwo.Dialog("/sys/editor.paste", {});
        d.onSubmit = function (elt) {
          this.exec(name, $F("input-paste"));
          d.close();
        }.bind(this);
//        d.close();
//        d = null;
        return ;
      }
      this.restoreRange();
      this.insertHTML(value.replace(/\n/g, "<br/>"));
    }
    else if (name == "image") {
      if (value == false) {
        this.saveRange();
        new Kwo.FileDialog(this.exec.bind(this));
        return ;
      }
      this.restoreRange();
      this.insertHTML('<img src="/' + value + '" />');
    }
    else {
      this.win.focus();
      this.doc.execCommand(name, false, value);
    }
    if (Prototype.Browser.Gecko || Prototype.Browser.WebKit) {
      this.win.getSelection().collapseToEnd();
    }
    this.store();
  },

  "exec": function(action, args, options) {
    options = options || {};

    if ("disable" in options) {
      if (options["disable"] === true) {
        options["disable"] = args;
      }
      options["disable"] = $(options["disable"]);
      if (options["disable"].hasClassName("kwo-disabled")) {
        return ;
      }
    }

    if ("confirm" in options) {
      var msg;
      if (Object.isElement(options["confirm"])) {
        msg = $(options["confirm"]).getAttribute("data-confirm");
      }
      else {
        msg = options["confirm"] == true ? "êtes vous sûr ?" : options["confirm"];
      }
      if (msg.length >= 1 && !confirm(msg.ucfirst())) { return false; }
    }

    if ("varnish" in options) {
      var params = Kwo.mergeArgs(args, {"__token": 1});
      var method = 'get';
    } else {
      var params = Kwo.mergeArgs(args, {"__token": Math.random()});
      var method = 'post';
    }

    if ("referrer" in options) {
      var referrer = options["referrer"];
    } else {
      var referrer = window.location.href;
    }

    if ("container" in options) {
      if (!$(options["container"])) {
        alert("Oops! No Container (AJAX):"+options["container"]);
        return false;
      }
      if (window["_scope"] === "back" && $($(options["container"]).parentNode).hasClassName("deck")) {
        $(options["container"]).parentNode.raise(options["container"]);
      }
      new Ajax.Updater(options["container"],
                       action,
                       {"parameters": params.toObject(),
                        "evalScripts": true,
                        "method": method,
                        "onComplete": function () {
                          if ("callback" in options) { options["callback"].call(null); };
                        },
                        "requestHeaders": {"x-kwo-referer": referrer,
                                           "X-KWO-Request": "update"}});
      return false;
    }

    var opts = {
      "requestHeaders": {"x-kwo-referer": referrer,
                         "X-KWO-Request": "exec"},
      "asynchronous": "async" in options ? options["async"] : true,
      "evalJS": false,
      "evalJSON": false,
      "parameters": params.toObject(),
      "onCreate": function() {
        if (window.top.$("loading")) { window.top.$("loading").show(); }
        if ("toggle" in options) { $(options.toggle).toggle(); }
        if ("disable" in options) {
          options["disable"].addClassName("kwo-disabled");
          if (options["disable"].tagName.toUpperCase() == "FORM") {
            options["disable"].disable();
          }
        }
      },
      "onSuccess": function(t) {
        var res = t.responseText.evalJSON();
        if (options["callback"] == true) {
          if ((action.indexOf("/") == -1 && window.location.href.indexOf("/account/") != -1) ||
              action.indexOf("/account") != -1) {
            options["callback"] = Kwo.Account.refresh;
          }
          else {
            options["callback"] = Kwo.callback;
          }
        }
        if (Kwo.hasError(res)) {
          if ("callback" in options && !Object.isElement(options["callback"])) {
            options["callback"].call(null, res);
          }
          else if (res["error"] == 401) {
            return new Kwo.Class.Auth();
          }
          else {
            Kwo.error(res);
          }
        }
        else {
          if ("callback" in options) {
            if (options["callback"] === null || Object.isUndefined(options["callback"])) {

            }
            else if (Object.isElement(options["callback"])) {
              var elt = $(options["callback"]);
              if (elt.tagName.toUpperCase() == "SELECT") {
                elt.length = 0;
                $H(res["result"]["values"]).each(function (pair) {
                  elt.insert("<option value='"+pair.key+"'>"+pair.value+"</option>");
                });
                elt.selectedIndex = 0;
              }
              else {
                if (!elt.visible()) {
                  elt.show();
                }
                elt.update(res["result"]["callback_msg"]);
                elt.addClassName("node-updated");
                if (elt.hasClassName("vanish")) {
                  elt.addClassName("vanish-on");
                  setTimeout(function () {
                    elt.removeClassName("vanish-on");
                    elt.hide(); }, 3000);
                }
                if (elt.hasClassName("dialog-close")) {
                  setTimeout(function () { Kwo.getDialog().close(); }, 2000);
                }
              }
            }
            else {
              options["callback"].call(null, res);
            }
          }
          if ("reset" in options) {
            options["reset"] = Object.isElement(options["reset"])
                             ? $(options["reset"])
                             : $(args);
            options["reset"].reset();
          }
        }
      },
      "on404": function(t) {
        Kwo.warn("Oops!\nAJAX Error : " + t.statusText + " was not found");
      },
      "onFailure": function(t) {
        Kwo.warn("Oops!\nAJAX Failure [" + t.status + "] : " + t.statusText);
      },
      "onException": function(t, e) {
        Kwo.warn("Oops!\nAJAX Exception [" + e.name + "] : " + e.message);
      },
      "onComplete": function(t) {
        if ("toggle" in options) { $(options.toggle).toggle(); }
        if ("disable" in options) {
          options["disable"].removeClassName("kwo-disabled");
          if (options["disable"].tagName.toUpperCase() == "FORM") {
            options["disable"].enable();
          }
        }
      }
    };

    new Ajax.Request(action, opts);

    return false;
  },

  "go": function(action, args, options) {
    var url = action;
    options = options || {};

    if (!Object.isString(action) && "result" in action && "callback_url" in action["result"]) {
      url = action["result"]["callback_url"];
    }

    if ("confirm" in options) {
      var msg;
      if (Object.isElement(options["confirm"])) {
        msg = $(options["confirm"]).getAttribute("confirm");
      }
      else {
        msg = options["confirm"] == true ? "OK ?" : options["confirm"];
      }
      if (msg.length >= 2 && !confirm(msg.ucfirst())) return ;
    }


    if (args !== undefined && args != null) {
      args = Kwo.mergeArgs(args);
      url = action + "?" + args.toQueryString();
    }

    if ("target" in options) {
      if (options["target"] == "blank") {
        window.open(url);
      }
      else {
        $(options["target"]).src = url;
      }
      return ;
    }

    if ("popup" in options) {
      options["popup"] = "object" == typeof options["popup"] ? options["popup"] : {};
      if ("blank" in options["popup"]) {
        return window.open(url);
      }
      options["popup"]["width"] = options["popup"]["width"] || "400";
      options["popup"]["height"] = options["popup"]["height"] || "550";
      options["popup"]["name"] = options["popup"]["name"] || "_blank";
      return window.open(url,
                         options["popup"]["name"],
                         "width="+options["popup"]["width"] + "," +
                         "height="+options["popup"]["height"] + "," +
                         "directories=no," +
                         "status=no,menubar=no,scrollbars=yes," +
                         "resizable=no,copyhistory=no,hotkeys=no," +
                         "toolbar=no,location=no");
    }
    window.location.href = url;
      /* window.location.replace(url); */
    return false;
  },

  "anchor": function(name) {
    document.anchors.item(name).scrollIntoView();
    //document.anchors[name].focus();
    return false;
  },

  "callback": function(h) {
    if (Kwo.hasError(h)) {
      return Kwo.error(h);
    }
    if ("callback_msg" in h["result"]) {
      if ("callback_container" in h["result"]) {
        $(h["result"]["callback_container"]).update(h["result"]["callback_msg"]);
      }
      else {
        Kwo.warn(h);
      }
    }
    if ("callback_url" in h["result"]) {
      if (h["result"]["callback_url"] == "reload") {
        Kwo.reload();
      }
      else {
        Kwo.go(h["result"]["callback_url"]);
      }
      return ;
    }
    if (!("callback_msg" in h["result"]) && !("callback_url" in h["result"])) {
//      Kwo.reload();
    }
  },

  "home": function(h) {
    window.location.href= "/";
  },

  "reload": function() {
    window.location.reload();
  },

  "error": function(args) {
    if (typeof args == "object" && "result" in args && "msg" in args["result"]) {
      args = args["result"]["msg"];
    }
    if (args instanceof Array) {
      var out = "Oops!\n";
      args.each(function(item) {
        out += " - " + item + "\n";
      });
      alert(out);
    }
    else {
    	alert(args.ucfirst());
    }
    return false;
  },

  "warn": function(args) {
    if (typeof args == "object" && "result" in args && "callback_msg" in args["result"]) {
      args = args["result"]["callback_msg"];
    }
    if (args instanceof Array) {
      var out = "";
      args.each(function(item) {
        out += item + "\n";
      });
      alert(out);
    }
    else {
      alert(args.ucfirst());
    }
    return false;
  },

  "isAuth": function() {
    return "_user_id" in window && window._user_id >= 1;
  },

  "load": function(src, callback, args) {
    if (src.indexOf("/") == -1) {
      src = "/app/" + src + "/controller.js";
    }
    if (!Object.isUndefined(callback) && !Object.isUndefined(args)) {
      callback = callback.curry(args);
    }
    if (Kwo.scripts[src] == true) {
      callback();
      return ;
    }
    var script = new Element("script",
                             {"type": "text/javascript",
                              "src": src});
    if (!Object.isUndefined(callback)) {
      if (Prototype.Browser.IE) {
        script.onreadystatechange = function() {
          if (script.readyState == "loaded" ||
              script.readyState == "complete"){
            script.onreadystatechange = null;
            callback();
          }
        }
      }
      else {
        script.onload = callback;
      }
    }
    Kwo.scripts[src] = true;
    $$("head")[0].insert(script);
  }

};

Kwo.dialogs = new Hash();

Kwo.Locale = {

  onOpen: function (elt) {
    $("kwo-locales-box").toggle();
    var pos = $(elt).cumulativeOffset();
    $("kwo-locales-box").setStyle({"top": (pos[1] + $(elt).getHeight()) + "px",
                                   "left": pos[0] + "px"});
  },

  onSet: function (locale, fallback) {
    if ($("kwo-locales-box")) $("kwo-locales-box").toggle();
    Kwo.exec("/sys/locale.set", {"locale": locale},
             {"callback": Kwo.Locale.onCallback.curry(fallback)});
  },

  onCallback: function (fallback, h) {
    if (fallback == undefined) {
      var parts = window.location.pathname.substring(1).split("/", 1);
      if (parts.length != 1 || parts[0].length != 2) {
        Kwo.reload();
        return ;
      }
      window.location.href = window.location.href.replace(new RegExp("/" + parts[0] + "(/|$)"),
                                                          "/" + h["result"]["locale"] + "/");
      return ;
    }
    Kwo.go(fallback);
  }

};

Kwo.BirthDate = {
  change: function (elt) {
    var input = $(elt).previous("INPUT"), date = {}, tmp;
    tmp = input;
    for (var i = 1; i <= 3; i++) {
      tmp = tmp.next("SELECT");
      if (tmp.className.indexOf("day") != -1) date["day"] = $F(tmp);
      else if (tmp.className.indexOf("month") != -1) date["month"] = $F(tmp);
      else date["year"] = $F(tmp);
    }
    input.value = date["year"] + "-" + date["month"] + "-" + date["day"];
  }
};

Kwo.Tag = {

  "view": function (tag) {
    Kwo.Search.results(tag, 0);
  }

};

Kwo.Tooltip = {

  "elt": null,

  "hide": function(anchor, id) {
    document.stopObserving("mousemove", Kwo.Tooltip.onMouseMove);
    Kwo.Tooltip.elt.hide();
  },

  "show": function(anchor, id) {
    anchor = $(anchor);
    Kwo.Tooltip.elt = id === undefined
                    ? anchor.previous(".kwo-tooltip")
                    : $(id);
    document.observe("mousemove", Kwo.Tooltip.onMouseMove);
    Kwo.Tooltip.elt.show();
    if (window.event) Kwo.Tooltip.onMouseMove(window.event);
  },


  "onMouseMove": function(e) {
    var dim = document.viewport.getDimensions();
    var pos = document.viewport.getScrollOffsets();
    var size = Kwo.Tooltip.elt.getDimensions();
    var top = (Event.pointerY(e) - 80);
    var left = (Event.pointerX(e) - 50);

    if ($$('.inner')[0]) {
      var gap_w = parseInt((dim["width"] - $$('.inner')[0].getWidth()) / 2);
      var gap_h = parseInt((dim["height"] - $$('.inner')[0].getHeight()) / 2);
      left -= gap_w - 70;
      top -= 105;
    }

    if ((left + size["width"]) > (dim["width"] + pos["left"])) {
      left -= (left + size["width"]) - (dim["width"] + pos["left"]) + 5;
    }
    if ((top + size["height"]) > (dim["height"] + pos["top"])) {
      top -= size["height"] + 20;
    }
    Kwo.Tooltip.elt.setStyle({"top": top + "px", "left": left + "px"});
  }

};

Kwo.Menu = {
  "opened": null,
  "binded": {},
  "timeout": null,
  "id": null,
  "bind": function() {
    $$(".bind-menu").each(function(item) {
      if (!$("menu-" + item.id)) return ;
      $("menu-" + item.id).addClassName("menu");
      item.observe("mouseover", function() {
        if (Kwo.Menu.id != null && Kwo.Menu.id != item.id) {
          $("menu-" + Kwo.Menu.id).hide();
        }
        Kwo.Menu.id = item.id;
        if (Kwo.Menu.timeout) {
          window.clearTimeout(Kwo.Menu.timeout);
        }
        Kwo.Menu.opened = $("menu-" + item.id);
        var pos = this.cumulativeOffset();
        Kwo.Menu.opened.setStyle({"top": (pos.top + this.getHeight() + 1) + "px",
                                  "left": pos.left + "px"});
        if (!(Kwo.Menu.opened.id in Kwo.Menu.binded)) {
          Kwo.Menu.binded[Kwo.Menu.opened.id] = true;
          Kwo.Menu.opened.onmouseover = Kwo.Menu.over;
          Kwo.Menu.opened.onmouseout = Kwo.Menu.out;
        }
        Kwo.Menu.opened.show();
      });

      item.observe("mouseout", function() {
        window.clearTimeout(Kwo.Menu.timeout);
        Kwo.Menu.timeout = setTimeout(function () {
          Kwo.Menu.opened.hide();
        }, 500);
      });
    });
  },

  "reset": function() {
    window.clearTimeout(Kwo.Menu.timeout);
    if ($("menu-" + Kwo.Menu.id)) {
      $("menu-" + Kwo.Menu.id).hide();
    }
  },

  "out": function () {
    $(Kwo.Menu.id).removeClassName("selected");
    window.clearTimeout(Kwo.Menu.timeout);
    this.hide();
  },

  "over": function() {
    $(Kwo.Menu.id).addClassName("selected");
    window.clearTimeout(Kwo.Menu.timeout);
    this.show();
  }
};


Kwo.Editor = Class.create({
  "doc": null,
  "iframe": null,
  "range": null,
  "selection": null,
  "src": null,
  "version": 1.1,
  "win": null,

  "initialize": function(name) {
    if (!("execCommand" in window.document)) return ;
    var actions = {"bold": false, "italic": false,
//                   "underline": false, "increasefontsize": false, "indent":true,"outdent":true,
//                   "insertorderedlist": false, "insertunorderedlist": false,
//                   "createlink": false, "insertimage": false, "paste": false,
//                   "createlink": false, "paste": false,
                   "removeformat": false,
                   "insertimage": false};
    this.src = $(name);
    this.src.hide();
    this.iframe = new Element("iframe", {"designmode": "on", "name": "_" + name, "id": "_" + name});
    this.iframe.setStyle({"height": this.src.getStyle("height"),
                          "width": this.src.getStyle("width"),
                          "border": "1px solid #d6d6d4"});
    this.iframe.addClassName("richtext");
    this.src.insert({"after": this.iframe});
    if (Prototype.Browser.IE) {
      this.win = window.frames["_" + name];
      this.doc = this.win.document;
    }
    else {
      this.win = this.iframe.contentWindow;
      this.doc = this.iframe.contentDocument;
    }
    if (!Prototype.Browser.Gecko) {
      this.doc.designMode = "on";
    }
    this.doc.open("text/html");
    this.doc.write("<html><head><style>"
                   + "BODY { background:" + this.src.getStyle("background-color") + "; cursor: text; height: 100%; "
                   + "       border:none; color:#777; font-family:monospace; margin:0; padding:0 4px; }"
                   + "BLOCKQUOTE { border-left:2px solid #eee; padding-left:4px; margin-left:4px; }"
                   + "</style></head><body></body></html>");
    this.doc.close();
    if (Prototype.Browser.Gecko) {
      this.doc.designMode = "on";
    }
    if (this.src.getValue().length >= 1) {
      this.doc.body.innerHTML = this.src.getValue();
    }
    if (Prototype.Browser.Gecko || Prototype.Browser.WebKit) {
      this.doc.execCommand("styleWithCSS", false, false);
    }
    this.iframe.observe("mouseout", this.store.bindAsEventListener(this));
    if (this.doc.addEventListener) {
      this.doc.addEventListener("keyup", this.store.bindAsEventListener(this), false);
    }
    else {
      this.doc.attachEvent("onkeyup", this.store.bindAsEventListener(this));
    }
    var toolbar = new Element("div").setStyle({"width": this.src.getStyle("width")});
    toolbar.addClassName("kwo-toolbar");
    this.src.insert({"after": toolbar});
    var img;
    for (key in actions) {
      if ((Prototype.Browser.IE ||  Prototype.Browser.WebKit) &&
          key == "increasefontsize") continue ;
      img = new Element("img", {"src": "/app/sys/pix/editor/" + key + ".png"});
      img.observe("click", this.exec.bind(this, key, actions[key]));
      toolbar.insert(img);
    };
  },

  "store": function() {
    var source = this.doc.body.innerHTML;
    source = source.replace(/<br class\="webkit-block-placeholder">/gi, "<br />");
    source = source.replace(/<span class="Apple-style-span">(.*)<\/span>/gi, "$1");
    source = source.replace(/ class="Apple-style-span"/gi, "");
    source = source.replace(/ style="">/gi, "");
    source = source.replace(/<span style="font-weight: bold;">(.*)<\/span>/gi, "<strong>$1</strong>");
    source = source.replace(/<span style="font-style: italic;">(.*)<\/span>/gi, "<em>$1</em>");
    source = source.replace(/<br>/gi, "<br />");
    source = source.replace(/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|li|p)/gi, "</$1");
    source = source.replace(/<b(\s+|>)/g, "<strong$1");
    source = source.replace(/<\/b(\s+|>)/g, "</strong$1");
    source = source.replace(/<i(\s+|>)/g, "<em$1");
    source = source.replace(/<\/i(\s+|>)/g, "</em$1");
    source = source.replace(/(<[^\/]>|<[^\/][^>]*[^\/]>)\s*<\/[^>]*>/gi, "");
    source = source.replace(/\.\.\/\.\.\/var\/docs/g, "var/docs");
    this.src.value = source;
    //this.log();
  },

  "exec": function(name, value) {
    if (name == "createlink") {
      if (value == false) {
        this.saveRange();
        new Kwo.Prompt({title: "Adresse du site ?", prefix: "http://"}, this.exec.bind(this).curry(name));
        return ;
      }
      if (!Object.isString(value) || value.legnth < 5) return ;
      if (!(value.toLowerCase().startsWith("http://") || value.toLowerCase().startsWith("https://"))) {
        value = "http://" + value;
      }
      if (value.toLowerCase().startsWith("http://http://")) {
        value = value.substr(7);
      }
      this.restoreRange();
      if (Prototype.Browser.IE && this.range) {
        this.range.select();
      }
      this.doc.execCommand(name, false, value);
    }
    else if (name == "paste") {
      if (value == false) {
        this.saveRange();
        var d = new Kwo.Dialog("/sys/editor.paste", {});
        d.onSubmit = function (elt) {
          this.exec(name, $F("input-paste"));
          d.close();
        }.bind(this);
//        d.close();
//        d = null;
        return ;
      }
      this.restoreRange();
      this.insertHTML(value.replace(/\n/g, "<br/>"));
    }
    else if (name == "image" || name == "insertimage") {
      if (value == false) {
        this.saveRange();
        new Kwo.FileDialog(this.exec.bind(this));
        return ;
      }
      this.restoreRange();
      this.insertHTML('<img src="/' + value + '" />');
    }
    else {
      this.win.focus();
      this.doc.execCommand(name, false, value);
    }
    if (Prototype.Browser.Gecko || Prototype.Browser.WebKit) {
      this.win.getSelection().collapseToEnd();
    }
    this.store();
  },

  "saveRange": function() {
    if (Prototype.Browser.IE) {
      this.range = null;
      if (this.doc.selection.createRange().text.length >= 1) {
        this.range = this.doc.selection.createRange();
      }
    }
    else if (Prototype.Browser.Gecko) {
      this.selection = this.win.getSelection();
      this.range = this.selection.getRangeAt(0).cloneRange();
      console.log(this.range);
    }
  },

  "restoreRange": function() {
    this.win.focus();
    if (!Prototype.Browser.Gecko) return ;
    var selection = this.win.getSelection();
    selection.removeAllRanges();
    if (this.range) {
      selection.addRange(this.range);
    }
  },

  "insertHTML": function(content) {
    this.win.focus();
    if (Prototype.Browser.IE) {
      if (this.range === null) {
        this.range = this.doc.selection.createRange();
      }
      this.range.pasteHTML(content);
      this.range.collapse(false);
      this.range = null;
    }
    else {
      this.doc.execCommand("insertHTML", false, content);
    }
  },

  "log": function() {
    console.log(this.doc.body.innerHTML);
    console.log(this.src.value);
  }

});

Kwo.Dialog = Class.create({

  "args": null,
  "bind": null,
  "className": null,
  "width": null,
  "height": null,
  "layout": null,
  "name": null,
  "opts": null,
  "overlay": null,
  "shadow": null,
  "support": null,

  initialize: function(paint_method, args, opts) {
    opts = opts || {};
    this.args = args || this.args;
    if (this.opts === null) {
      this.opts = opts;
    }
    this.name = this.opts["name"] || this.name || "dialog";
    this.className = this.opts["className"] || this.className || "";
    if (this.layout) {
      this.className +=  "layout-" + this.layout;
    }
    this.width = this.opts["width"] || this.width || 500;
    this.height = this.opts["height"] || this.height || 300;
    if (this.overlay === null) {
      if (Kwo.dialogs.size() < 1) {
        $(document.body).makeClipping();
      }
      this.overlay = new Element("div").setStyle("display:none; opacity:0.2; position:absolute; top:0; left:0;").addClassName("dialog-overlay");
      this.shadow = new Element("div").setStyle("display:none; position:absolute; top:0; left:0;").addClassName("dialog-shadow");
      this.support = new Element("div").setStyle("display:none;");
      this.support.addClassName("dialog-support dialog-" + this.name + " " + this.className);
      //this.shadow.appendChild(new Element("img", {"src": "/app/sys/pix/empty.gif"})).observe("click", this.close);
      this.shadow.appendChild(new Element("div").addClassName("dialog-close")).observe("click", this.close);
      this.place();
//      return ;
      document.body.appendChild(this.overlay);
      document.body.appendChild(this.shadow);
      this.shadow.appendChild(this.support);
      this.overlay.observe("click", this.close);
      if ("_scope" in window && window["_scope"] == "back") {
        new Draggable(this.shadow,{starteffect:function(){},endeffect:function(){}});
        new Draggable(this.support,{starteffect:function(){},endeffect:function(){},snap:[0,0]});
      }
    }
//    this.opts = opts;
//    this.place();
    this.bind = this.place.bindAsEventListener(this);
    Event.observe(window, "resize", Kwo.onViewportChange);
/*  Event.observe(window, "scroll", Kwo.onViewportChange);
    Event.observe(this.overlay, "scroll", Kwo.onViewportChange); */
    Kwo.setDialog(this);
    if (Prototype.Browser.IE && navigator.userAgent.indexOf("MSIE 6") > -1) {
      $$("SELECT").invoke("hide");
    }
    this.support.update();
    if (Object.isFunction(paint_method)) {
      paint_method.call(this, this.args);
    }
    else if (!Object.isUndefined(paint_method)) {
      Kwo.exec(paint_method, this.args, {async: true, container: this.support});
    }
    $(this.overlay, this.shadow, this.support).invoke("show");

    //UAD
    Event.observe(document, "keyup", UAD.Dialog.escapeHandler);
    _gaq.push(['_trackEvent','popup', paint_method]);
  },

  onDrag: function (evt) {
    console.log(Event.element(evt));
/*
    var dt = evt.dataTransfer;
    dt.setData("application/x-moz-node", this.shadow);
    dt.setData("text/plain", "http://www.mozilla.org");*/
    var dt = evt.dataTransfer;
    dt.setData("Text", "Dropped in zone!");
    return true;
  },

  onDrop: function (evt) {
    var dt = evt.dataTransfer;
    return false;
  },

  place: function () {
    var dimensions = document.viewport.getDimensions();
    var offsets = document.viewport.getScrollOffsets();
    var left = offsets.left + ((dimensions.width / 2) - (this.width / 2));
    var top = offsets.top + ((dimensions.height / 2) - (this.height / 2));
    top = top < 0 ? 0 : top;
    left = left < 0 ? 0 : left;
    top += 15;
    this.overlay.setStyle({"width": dimensions.width + "px",
                           "height": dimensions.height + "px",
                           "left": offsets.left + "px",
                           "top": offsets.top + "px"});
    this.shadow.setStyle({"width": this.width + "px",
                          "height": this.height + "px",
                          "left": left + "px",
                          "top": top + "px"});
    this.support.setStyle({"width": this.width + "px",
                           "height": this.height + "px"});
  },

  apply: function(value) {
    if (Object.isUndefined(this.callback)) alert(value);
    else if (Object.isElement(this.callback)) this.callback.value = value;
    else this.callback(value);
    this.close();
  },

  close: function() {
    var dialog = Kwo.getDialog();
    if (Kwo.dialogs.size() == 1) {
      $(document.body).undoClipping();
    }
    dialog.shadow.remove();
    dialog.overlay.remove();
    Kwo.dialogs.unset(dialog.name);
    if (Prototype.Browser.IE && navigator.userAgent.indexOf("MSIE 6") > -1) {
      $$("SELECT").invoke("show");
    }
    if (_scope != "back") {
      Event.stopObserving('keyup', UAD.Dialog.escapeHandler);
    }
  }

});

Kwo.Prompt = Class.create(Kwo.Dialog, {

  initialize: function($super, args, callback) {
    this.name = "prompt";
    this.callback = callback;
    if (Object.isString(args)) {
      args = {"title": args};
    }
    $super(this.refresh, args, {height: 150});
  },

  refresh: function(args) {
    Kwo.exec("/sys/dialog.prompt", args, {container: this.support});
  }

});

Kwo.FileDialog = Class.create(Kwo.Dialog, {

  "initialize": function($super, callback, opts) {
    this.callback = callback;
    this.name = "file";
    opts = opts || {};
    this.opts = opts;
    $super("/community/files.dialog", null);
  },

  "onUploadCompleted": function(file_path) {
    if (this.opts["mode"] == "upload") {
      this.apply(file_path);
    }
    else {
      this.refresh();
    }
  },

  "refresh": function() {
    this.support.innerHTML = "";
    Kwo.exec("/community/files.dialog", null,
             {"container": this.support});
  },

  "preview": function(path) {
    path = "/" + path;
    if (path.indexOf(".jpg") > 0 || path.indexOf(".jpeg") > 0 ||
        path.indexOf(".png") > 0 || path.indexOf(".gif") > 0) {
      $("thumb").hide();
      var img = new Image();
      img.onload = function() {
        var max = 230;
        $("thumb").style.display = "block";
        $("thumb").src = this.src;
        if (this.width <= max && this.height <= max) {
          $("thumb").width = this.width;
          $("thumb").height = this.height;
        }
        else if (this.width > max || this.height > max) {
          if (this.width > this.height) {
            $("thumb").width = max;
            $("thumb").height = Math.ceil(this.height * (max / this.width));
          }
          else {
            $("thumb").width = Math.ceil(this.width * (max / this.height));
            $("thumb").height = max;
          }
        }
        $("thumb").show();
      };
      img.src = path;
    }
    else {
      $("thumb").hide();
    }
  },

  "apply": function(path) {
    if (Object.isUndefined(this.callback)) {
      return alert(path);
    }
    else if (Object.isElement(this.callback)) {
      if ("name_only" in this.opts) {
        path = path.basename();
      }
      $(this.callback).value = path;
    }
    else if (Object.isFunction(this.callback)) {
      this.callback("insertimage", path);
    }
    this.close();
  }

});

Kwo.Class.Upload = Class.create(Kwo.Dialog, {

  initialize: function($super, callback, opts) {
    this.name = "upload";
    this.callback = callback;
    this.opts = opts || {};
    this.opts["width"] = 400;
    this.opts["height"] = 200;
    var args = {};
    if ("filter" in this.opts) {
      args["filter"] = this.opts["filter"];
    }
    $super("/sys/upload.select", args);
  },

  onUploadCompleted: function(args) {
    if (Object.isString(args)) {
      this.apply(args);
    }
  },

  onBeforeSubmit: function(elt) {
    this.form = $(elt);
    if (this.form.down("INPUT[type=file]").value.blank()) {
      return ;
    }
    Kwo.exec("/sys/upload.check",
             {"file": this.form.down("INPUT[type=file]").value,
              "replace": this.form.down("INPUT[type=checkbox]").checked ? 1 : 0,
              "filter": this.opts["filter"]},
             {callback:this.onSubmit.bind(this)});
  },

  onSubmit: function(res) {
    if (res["error"] == 2) {
      this.form.down("LABEL").show();
      return ;
    }
    else if (res["error"] >= 1) {
      return this.form.down("DIV.form-error").update(res["result"]["msg"]);
//      return Kwo.error(res);
    }
    this.form.down("DIV.form-error").hide();
    $(this.form, "loading-box").invoke("toggle");
    this.form.down("input[name=filter]").value = this.opts["filter"];
    this.form.action = "/sys/upload";
    this.form.stopObserving("submit");
    this.form.down("DIV.form-error").hide();
    this.form.submit();
  },

  refresh: function(args) {
    alert(args["error"]);
    $("loading-box", this.form).invoke("toggle");
    this.form.reset();
  },

  apply: function(path) {
    if (Object.isUndefined(this.callback)) {
      return alert(path);
    }
    else if (Object.isElement(this.callback)) {
      var previous = $(this.callback).previous("INPUT[type=hidden]");
      if (!Object.isUndefined(previous)) {
        previous.value = path;
      }
      var cb = $(this.callback);
      cb.value = path.basename();
      if (!cb.value.blank()) {
        cb.next('div.form-error', 0).hide();
        cb.removeClassName('error');
      }
      this.close();
    }
    else if (Object.isFunction(this.callback)) {
      this.close();
      this.callback(path);
    }
  }
});


Kwo.Class.Abuse = Class.create(Kwo.Dialog, {

  "initialize": function($super, item_key) {
    this.name = "abuse";
    this.className = "layout-hbox";
    this.width = 600;
    this.height = 400;
    this.args = {"item_key": item_key};
    $super(this.refresh, this.args);
  },

  "refresh": function(args) {
    Kwo.exec("/abuse/abuse.compose", args,
             {container: this.support});
  },

  "apply": function(elt) {
    elt = $(elt);
    Kwo.exec("/abuse/abuse.report", elt,
             {disable:elt, callback:$("abuse-button")});
//    $(args).reset()
//    $("abuse-button").update($("abuse-button").readAttribute("confirm").ucfirst());
//    return false;
  }

});


Kwo.Datepicker = Class.create(Kwo.Dialog, {

  "initialize": function($super, input) {
    this.name = "date";
    this.callback = input;
    $super(this.refresh, {"date": $F(input)}, {"height":221, "width":300});
//    Kwo.setDialog("date", this);
  },

  "refresh": function(args) {
    Kwo.exec("/sys/date.select", args, {"container": this.support});
  }

});

Kwo.Datetimepicker = Class.create(Kwo.Dialog, {

  "initialize": function($super, input) {
    this.name = "date";
    this.callback = input;
    $super(this.refresh, {"datetime": $F(input)}, {"height": 230});
//    Kwo.setDialog("datetime", this);
  },

  "refresh": function(args) {
    Kwo.exec("/sys/datetime.select", args, {"container": this.support});
  }

});

Kwo.Colorpicker = Class.create(Kwo.Dialog, {

  "initialize": function($super, input, opts) {
    this.name = "color";
    this.input = input;
    opts = opts || {};
    opts["width"] = 220;
    opts["height"] = 180;
    $super("/sys/dialog.colorpicker", null, opts);
//    Kwo.setDialog("color", this);
  },

  "put": function(value) {
    if ("goal" in this.opts) {
      if (this.opts["goal"] == "bgcolor") {
        Kwo.getEditor().setBgColor("#"+value);
      }
      else {
        Kwo.getEditor().setFgColor("#"+value);
      }
    }
    else {
      $(this.input).setValue(value);
    }
    this.close();
  }

});

Kwo.Class.Snippet = Class.create(Kwo.Dialog, {

  initialize: function($super, code, opts) {
    this.name = "snippet";
    this.opts = opts || {};
    this.args = {"code": code};
    $super("/sys/snippet", this.args, {width:500 , height:400});
  }

});

Kwo.Class.Auth = Class.create(Kwo.Dialog, {

  initialize: function($super, opts) {
    this.name = "auth";
    this.width = 800;
    this.height = 500;
    this.opts = opts;
    $super("/community/signup");
  },

  onCallback: function() {
    if ("callback" in this.opts) {
      this.opts["callback"].call();
    }
    this.close();
  }

});

Kwo.Class.Feed = Class.create(Kwo.Dialog, {

  initialize: function($super, args) {
    this.name = "feed";
    this.className = "layout-hbox";
    this.args = args;
    $super(this.refresh, this.args, {width: 400, height: 310});
  },

  refresh: function() {
    Kwo.exec("/sys/feed.consume", this.args,
             {container: this.support});
  }

});

Kwo.Class.Valuation = Class.create(Kwo.Dialog, {

  initialize: function($super, args) {
    this.name = "valuation";
    this.className = "layout-hbox";
    this.args = {item_key: args};
    this.width = 700;
    this.height = 400;
    $super(this.refresh, this.args);
  },

  onSubmit: function(elt) {
    Kwo.exec("/rating/valuation.save", [elt, {item_key: this.args["item_key"]}],
             {disable:elt, callback:this.onCallback.bind(this)});
  },

  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    this.close();
    Kwo.reload();
  },

  bindStars: function() {
    $$(".valuation-stars")[0].observe("click", this.onClick);
  },

  onClick: function(evt) {
    var elt = evt.element();
    if (!elt.hasClassName("star")) return ;
    elt.up("TR").select("IMG.star").each(function (img) {
      img.src = img.src.sub("-on", "-off");
    });
    elt.src = elt.src.sub("-off", "-on");
    elt.previousSiblings().each(function (img) {
      img.src = img.src.sub("-off", "-on");
    });
    elt.up("TR").down("SPAN").update(elt.readAttribute("data-value") + " / 5");
    elt.up("TR").down("INPUT").value = elt.readAttribute("data-value");
  },

  refresh: function() {
    Kwo.exec("/rating/valuation", this.args,
             {container: this.support});
  }

});

Kwo.Geolocpicker = Class.create(Kwo.Dialog, {

  "initialize": function($super, input, opts) {
    this.name = "geoloc";
    this.input = input;
    $super("/sys/dialog.geoloc", null, {"width": 640, "height":480});
    Kwo.setDialog("geoloc", this);
  },

  "getPoint": function () {
    if ($(this.input).up("tr") && $(this.input).up("tr").down("input.back")) {
      return new Array($(this.input).up("tr").down("input.latitude").value,
                       $(this.input).up("tr").down("input.longitude").value,
                       $(this.input).up("tr").down("input.zoom").value);
    } else {
      if (this.input.value.match(",")) {
        return this.input.value.split(",");
      }
    }
    return false;
  },

  "setValue": function(value) {
    if ($(this.input).up("tr") && $(this.input).up("tr").down("input.back")) {
      var values = value.split(",");
      $(this.input).up("tr").down("input.latitude").value = values[0];
      $(this.input).up("tr").down("input.longitude").value = values[1];
      $(this.input).up("tr").down("input.zoom").value = values[2];
    } else {
      $(this.input).setValue(value);
    }
    this.close();
  }

});

Kwo.Elt = {

  onImageCallback: function(path) {
    this.value = path.basename();
    //this.hide();
    if (this.next("img")) {
      this.next().src = path;
      this.next().show();
    } else {
      var img_target = this.up("div").previous().down("img.elt-image-target");
      //img_target.src = "/" + path;
      img_target.src = path;
      img_target.show();
    }
  },

  onImageDelete: function(elt) {
    var elt = $(elt);
    Kwo.exec("/account/uad/image.delete",
             {"item": elt.readAttribute("data-item"), "field": elt.readAttribute("data-field")},
             {callback: Kwo.Elt.onImageDeleteCallback.curry(elt), confirm:true});
  },

  onImageDeleteCallback: function(elt, res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    var elt = $(elt);
    var img = elt.up(".elt-control").down("img.elt-image-target");
    elt.remove();
    img.src = "/app/sys/pix/empty.gif";
    img.removeAttribute("onclick");
  },

  onTagAdd: function(elt) {
    if (elt.value.blank() || elt.value == elt.title) return ;
    var tags = elt.getValue().stripTags().split(",");
    var input = elt.previous(0).getValue().split(",");
    elt.clear();
    var tag, span, spans = elt.next("DIV");
    for (var i = 0; i < tags.length; i++) {
      tag = tags[i].strip();
      if (tag.blank() || input.indexOf(tag) != -1) continue ;
      span = new Element("SPAN", {"onclick": "Kwo.Elt.onTagEdit($(this))"}).update(tag);
      spans.insert(span);
    }
    Kwo.Elt.onTagStore(spans);
  },

  onTagEdit: function(elt) {
    var spans = elt.up();
    spans.previous("INPUT").value = elt.innerHTML;
    elt.remove();
    Kwo.Elt.onTagStore(spans);
  },

  onTagFocus: function(elt) {
    elt = $(elt);
    if (elt.value == elt.title) {
      elt.clear();
    }
    elt.next().show()
  },

  onTagStore: function(elt) {
    var tags = [];
    elt.select("SPAN").each(function (span) {
      tags.push(span.innerHTML);
    });
    elt.previous("INPUT", 1).value = tags.join(",");
  },

  onTooltipHide: function(elt) {
    elt.up("DIV.elt").select("DIV.elt-tooltip")[0].hide();
  },

  onTooltipShow: function(elt) {
    var tooltip = elt.up("DIV.elt").select("DIV.elt-tooltip")[0];
    if (!tooltip.hasClassName("elt-widget")) {
      tooltip.addClassName("elt-widget");
      var pointer = new Element("DIV").addClassName("elt-tooltip-pointer-down elt-widget");
      pointer.insert(new Element("DIV").addClassName("elt-tooltip-pointer-down-inner"));
      tooltip.insert(pointer);
    }
    var pos = elt.viewportOffset();
    var dim = tooltip.getDimensions();
    var top  = pos["top"] - (dim["height"] + 16);
    var left = (pos["left"] - Math.ceil(dim["width"] / 2)) + 5;
    tooltip.setStyle({"top": top + "px", "left": left + "px"});
    tooltip.show();
  },

  onNodeSelect: function(elt, target_id) {
    elt = $(elt);
    var target = $(target_id);
    if (elt.getValue() == 0) {
      target.length = 0;
      target.update("<option value='0'>-</option>");
      return ;
    }
    Kwo.exec("/tree/node.children", {"node_id": elt.getValue()},
             {callback: target});
  }

};

Kwo.Currency = {

  onSelect: function(arg) {
    $("kwo-currencies").toggle();
    var pos = $(arg).cumulativeOffset();
    $("kwo-currencies").setStyle({top: (pos[1] + $(arg).getHeight()) + "px",
                                  left: pos[0] + "px"});
  },

  onSubmit: function(code) {
    Kwo.exec("/sys/currency.set", {"code": code},
             {callback: true});
  }

};




window.bookmark = function() {
  if ("sidebar" in window) {
    window.sidebar.addPanel(document.title, document.location, "");
    return ;
  }
  if ("external" in window) {
    window.external.AddFavorite(document.location, document.title);
    return ;
  }
  var msg = Prototype.Browser.Opera ? "CTRL-T" : "CTRL-D";
  alert(msg);
}

Object.extend(String.prototype, {

  intval: function() {
    return this.length < 1 ? 0 : parseInt(this);
  },

  toggle: function() {
    return parseInt(this) == 1 ? "0" : "1";
  },

  ucfirst: function() {
   return this.charAt(0).toUpperCase() + this.substring(1);
  },

  basename: function() {
    return this.match(/[^\/\\]+$/);
  },

  dirname: function() {
    return this.match(/(.*)[\/\\]/)[1];
  },

  isImage: function() {
    return this.match(/\.(gif|jpeg|jpg|png)$/i);
  }

});


Element.addMethods({

  turn: function(elt) {
    elt = $(elt);
    var img;
    if (elt.tagName.toUpperCase() == "IMG") {
      img = elt;
    }
    else {
      img = elt.down("IMG");
      if (Object.isUndefined(img)) return elt;
    }
    if (img.src.match("-off.")) {
      img.src = img.src.sub("-off.", "-on.", 1);
    }
    else {
      img.src = img.src.sub("-on.", "-off.", 1);
    }
    return elt;
  }

});

Kwo.Bindings = {

  onAppend: function(evt, handler) {
    var elt = evt.element();
    if (elt.hasClassName("infinite-completed")) return ;
    if ((elt.scrollHeight - 50) > (elt.getHeight() + elt.cumulativeScrollOffset()["top"])) return ;
    handler.call(elt);
  }

};

