/**
 * global Lex utility object.
 * usage:
 * Lex.dbkey=dbkey-of-lexicon-contentstore
 * Lex.loadPage(key-to-home-document)
 */
var Lex={

	init: function(el, dbkey, tml){
		this.el=Ext.get("lexikon"),
		this.tml=tml;
		this.dbkey=dbkey;
		Ext.UpdateManager.defaults.loadScripts=true;
	},

	/**
	 * convert all links in given Element
	 * @param {Object} el 
	 */	
	convertLinks: function(el){
		el.select("a[linkType=int]").each(function(el){
			var href=el.dom.href
			var parts = href.split("/");
			var key=parts[parts.length-1];
			var keyparts=key.split(".");
			key = keyparts[0];
			el.dom.href="#";
			el.on("click", function(a,b,c){
				Lex.loadPage(c);
			}, this, key);
		})
	},
	
	/**
	 * Load new page with given content key
	 * @param {Object} contentkey
	 */
	loadPage: function(contentkey, params){
		var opts={dbkey:this.dbkey, contentkey:contentkey};
		for(p in params)
			opts[p]=params[p];
		this.el.load(this.tml, opts, this.convertLinks)
	},
	
	/**
	 * Load new Page and execute query
	 * @param {Object} query
	 */
	loadQuery: function(query, params){
		var opts={dbkey:this.dbkey, query:query};
		for(p in params)
			opts[p]=params[p];
		this.el.load(this.tml, opts, this.convertLinks)
	}
	
};
