function Bookee () {

  var _name = _name;

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

  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.editSubBookee = function(id, id_sub) {    
    if (id >= 1 && id_sub >= 1) {
		Kwo.go("/bk/bookee.edit", {"bookee_id": id, "sub_bookee_id": id_sub});
	}
  }
  
  this.customize = function(id) {
    var params = (id === undefined) ? null : "bookee_id="+id
    if (id===undefined) id = 0;
    Kwo.go("/bk/bookee.customize", 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.searchInAllBookees = function(query) {
    if (query === undefined) query = $F("queryAll");
    if ($('all_lang').checked) {
      Kwo.go("/bk/bookee.search", {"query": query, "mybookees": 0, "lang":"all"});
    } else {
      Kwo.go("/bk/bookee.search", {"query": query, "mybookees": 0});
    }
  }
  
  this.searchInMyBookees = function(query) {
    if (query === undefined) query = $F("queryMy");
    if ($('all_lang').checked) {
      Kwo.go("/bk/bookee.search", {"query": query, "mybookees": 1, "lang":"all"});
    } else {
      Kwo.go("/bk/bookee.search", {"query": query, "mybookees": 1});
    }
  }  

  this.remove = function() {
  	//FIXME KG 2010-05-20
	if ($('removed_bookee_id').value >= 1) {
		//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();
	} else {
		alert('Veuillez sélectionner un bookee / Please select a bookee');
	}    
  }

  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.removeSubBookee = function(parent_id, bookee_id) {
  	if (!confirm("Etes-vous sûr de vouloir supprimer ce sous bookee ? / Are you sure you want to delete this sub bookee ?")) return;
	Kwo.exec("/bk/bookee.remove_sub_bookee", $H({"bookee_id": bookee_id, "parent_id": parent_id}));
  }

  this.toggleAddLink = function(index) {
    clearSizeColumn();
    //$("add_link_open"+index, "add_link_close"+index).invoke("toggle");
    $("add_link_open"+index).toggle();
    resizeColumn();
  }
  
  this.toggleAddSubBookee = function(index) {
    clearSizeColumn();
    $("add_sub_bookee_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 Bookee.\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});
  }
  
  //FIXME KG 2010-05-24
  this.addTopBookee = function(item_type, item_id, col_num, col_order, color) {
  	if ($('bk_'+item_id)) {
      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 Bookee.\nPlease register as a new member or identify yourself as an existing member in order to use this bookee.");
      return ;
    }
	Kwo.exec("/bk/town.add_item", {"item_type": item_type, "item_id": item_id, "col_num": col_num, "col_order": col_order, "color": color});
  }
  
  //FIXME KG 2010-09-17
  this.scrollTopBookee = function(index) {  	
	if ($('bk_'+index)) {
      new Effect.ScrollTo('bk_'+index,{offset:-9});
    }
  }

  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.hideBox = function (name) {
    $(name).style.display = "none";
    setCookie(name,"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);
}

function sortLeftBarUpdate(){
	var res = new Hash();
	var col = $('leftbar1').getElementsByClassName('boxHandle');
	for (key in col) {
		res[col[key].id] = {"col_order": key};
	}
	Kwo.exec('/bk/town.save_order', {"order": res.toJSON()});
}
