/* Copyright (C) 2002-2011 by Home of the Brave
   Web http://home.of.the.brave.de
   E-Mail info@brave.de */
/* $Revision: 1.3 $ $Date: 2011/07/22 12:24:46 $ */

/*
 * Handle bookmark links.
 * owner: Simon.Leidig@brave.de
 */

var Bookmarks = {
	getVar: function (m,k) {
		return (Bookmarks.vars || (Bookmarks.vars = {
			url:	escape(Bookmarks.getURL()),
			title:	escape(document.title),
			lang:	escape(document.documentElement.lang)
		}))[k] || '';
	},
	getURL: function () {
		return location.href.replace(/^https:/,'http:').replace(/;jsessionid=[\w%+-]*/,'');
	},
	init: function (id) {
		var a = document.getElementById('bookmark_'+id);
		if (a) ( Bookmarks['init_'+id] || Bookmarks.initLink ).call(a);
	},
	initLink: function () {
		this.href = this.href.replace(/\{(\w+)\}/g,Bookmarks.getVar);
		this.style.display = 'inline';
	},
	init_browser: function () {
		if (!(window.sidebar || window.opera || window.external)) return;
		this.onclick = function () {
			var u = Bookmarks.getURL();
			var t = document.title;
			if (window.sidebar) window.sidebar.addPanel(t,u,'');
			else if (window.opera) {
				var a = document.createElement('A');
				a.href	= u;
				a.title	= t;
				a.rel	= 'sidebar';
				a.target= '_search';
				a.click();
			}
			else if (window.external) window.external.AddFavorite(u,t);
			return false;
		};
		Bookmarks.initLink.call(this);
	},
	init_tinyurl: function () {
		if (!window.$) return;
		var s = this;
		var j = window.$(s);
		Bookmarks.initLink.call(s);
		s.onclick = function () {
			s.className += ' bookmark_tinyurl_loading';
			window.$.getJSON(s.href,function (r) {
				var t = document.createElement('INPUT');
				t.type			= 'text';
				t.defaultValue	=
				t.value			= r.tinyurl;
				t.onfocus		= function () {this.select()};
				t.onchange		= function () {this.value = this.defaultValue};
				t.className		= 'input small bookmark_tinyurl_processed';
				t.style.width	= j.width()+'px';
				j.replaceWith(t);
				window.$(t).focus();
			});
			s.onclick = null;
			return false;
		};
	}
}

