function Bookee () {

  var _name = _name;

  this.addLink = function(index) {
    Kwo.exec("/bk/bookee.add_link", $("add_link_form"+index));
  }

  this.alterLink = function(bookee_id, link_id) {
    Kwo.exec("/bk/bookee.alter_link", $H({"bookee_id": bookee_id, "link_id": link_id}));
  }

  this.goLink = function(url, id) {
    Kwo.go("/bk/bookee.redirect", {"url": url, "link_id": id}, {"target": "blank"});
  }

  this.edit = function(id) {
    var params = (id === undefined) ? null : "bookee_id="+id
    if (id===undefined) id = 0;
    Kwo.go("/bk/bookee.edit", params);
  }

  this.removeLink = function(bookee_id, link_id) {
    if (!confirm("OK ?")) return;
    Kwo.exec("/bk/bookee.remove_link", $H({"bookee_id": bookee_id, "link_id": link_id}));
  }

  this.removeLinks = function(args) {
    if (!confirm("Supprimer les liens / Delete selected links ?")) return;
    Kwo.exec("/bk/bookee.remove_link", args);
  }
  
  this.updateLink = function(link_id) {
    Kwo.exec("/bk/bookee.add_link", {"bookee_id": $F("bookee_id"), "link_id": link_id, "link_name": $F("name"+link_id), "link_url": $F("url"+link_id)});
  }

  this.search = function(query) {
    if (query === undefined) query = $F("query");
    if ($('all_lang').checked) {
      Kwo.go("/bk/bookee.search", {"query": query, "lang":"all"});
    } else {
      Kwo.go("/bk/bookee.search", {"query": query});
    }
  }

  this.remove = function() {
    //clearSizeColumn();
    if (!confirm("Etes-vous sûr de vouloir supprimer ce bookee ? / Are you sure you want to delete this bookee ?")) return;
    Kwo.exec("/bk/bookee.remove", $("bookee_form"));
    resizeColumn();
  }

  this.removeShared = function() {
    //clearSizeColumn();
    if (!confirm("Etes-vous sûr de vouloir supprimer ce bookee partagé ? / Are you sure you want to delete this shared bookee ?")) return;
    Kwo.exec("/bk/bookee.remove_shared", $("bookee_form"));
    //resizeColumn();
  }

  this.store = function() {
    Kwo.exec("/bk/bookee.store", $("bookee_form"));
  }

  this.toggleAddLink = function(index) {
    clearSizeColumn();
    //$("add_link_open"+index, "add_link_close"+index).invoke("toggle");
    $("add_link_open"+index).toggle();
    resizeColumn();
  }
}

function Town () {

  var _name = _name;

  this.addItem = function(item_type, item_id) {
    if ($('bk_'+item_id)) {
      //Kwo.warn('Ce bookee est déjà ouvert / This bookee is already open.');
      if ($('bk_'+item_id)) new Effect.ScrollTo('bk_'+item_id,{offset:-9});
      return ;
    }
    if (window['_user_id'] < 1) {
      Kwo.warn("Merci de vous identifier ou de créer votre compte si vous n\'êtes pas encore membre de Bookeetown.\nPlease register as a new member or identify yourself as an existing member in order to use this bookee.");
      return ;
    }
    // if (!confirm("OK ?")) return;
    Kwo.exec("/bk/town.add_item", {"item_type": item_type, "item_id": item_id});
  }

  this.home = function() {
    Kwo.go("/");
  }

  this.minimizeItem = function(index) {
    clearSizeColumn();
    $("minimize"+index, "maximize"+index).invoke('toggle');
    $("item_"+index).toggle();
    resizeColumn();
  }

  this.removeItem = function(item_type, item_id) {
    Kwo.exec("/bk/town.remove_item", {"item_type": item_type, "item_id": item_id} );
  }

  this.moveItem = function(index) {
    Kwo.exec("/bk/town.move_item", "index="+index);
  }

  this.storeAccount = function(elt) {
    Kwo.exec("/bk/town.store_account", elt);
  }

  this.orderItem = function () {
    Kwo.exec("/bk/town.order_item");
  }

  this.showTop5 = function () {
    if ($("top5Box")) {
      if ($("top5Box").style.display == "none") {
        $("top5Box").style.display = "block";
        setCookie("bookee_top5","block");
      } else {
        $("top5Box").style.display = "none";
        setCookie("bookee_top5","none");
      }
    } else {
      Kwo.go("/");
      setCookie("bookee_top5","block");
    }
  }

  this.hideTop5 = function () {
    $("top5Box").style.display = "none";
    setCookie("bookee_top5","none");
  }

  this.showForecast = function () {
    if ($("forecastBox")) {
      if ($("forecastBox").style.display == "none") {
        $("forecastBox").style.display = "block";
        setCookie("bookee_forecast","block");
      } else {
        $("forecastBox").style.display = "none";
        setCookie("bookee_forecast","none");
      }
    } else {
      Kwo.go("/");
      setCookie("bookee_forecast","block");
    }
  }

  this.hideForecast = function () {
    $("forecastBox").style.display = "none";
    setCookie("bookee_forecast","none");
  }

}

bookee = new Bookee();
town   = new Town();

function sortUpdate () {
  resizeColumn();
  var res = new Hash();
  var col1 = $('column-left').getElementsByClassName('bookee');
  var col2 = $('column-right').getElementsByClassName('bookee');
  for (key in col1) if (col1[key].id && col1[key].id.match('bk_')) {
    res[col1[key].id.replace("bk_","")] = {'col_num': 1, 'col_order': key };
  }
  for (key in col2) if (col2[key].id && col2[key].id.match('bk_')) {
    res[col2[key].id.replace("bk_","")] = {"col_num": 2, "col_order": key};
  }
  Kwo.exec('/bk/town.save_state',{"state": res.toJSON()});
}

function clearSizeColumn () {
  $('column-left').style.height = '';
  $('column-right').style.height = '';
}

function resizeColumn () {
  $('column-left').style.height = '';
  $('column-right').style.height = '';
  if ($('column-right').offsetHeight < $('column-left').offsetHeight) {
    $('column-right').style.height = $('column-left').offsetHeight - 2 + "px";
  } else if ($('column-right').offsetHeight > $('column-left').offsetHeight) {
    $('column-left').style.height = $('column-right').offsetHeight - 2 + "px";
  }
}

function increaseColumn (id) {
  //var col = $("bk_"+id).up('.column');
  if ($('column-right').offsetHeight < $('column-left').offsetHeight) {
    $('column-right').style.height = $('column-left').offsetHeight + $("bk_"+id).offsetHeight + 10 + "px";
    $('column-left').style.height = $('column-left').offsetHeight + $("bk_"+id).offsetHeight + 10 + "px";
  } else {
    $('column-left').style.height = $('column-right').offsetHeight + $("bk_"+id).offsetHeight + 10 + "px";
    $('column-right').style.height = $('column-right').offsetHeight + $("bk_"+id).offsetHeight + 10 + "px";
  }
}

function sendBookmark (btn) {
  Kwo.exec('/bk/bookmark.add',btn);
}
