
if (!window.CMS) {
	var CMS = {
		load: false,
		
		onload: [],
		
		addOnload: function(func, args){
			if (this.load) {
				this.exec([func, args]);
				return;
			}
			this.onload.push([func, args]);
		},
		
		exec: function(x){
			//x[0](x[1]);
			x[0].call(null, x[1]);
		},
		
		doOnload: function(){
			while (this.onload.length > 0) {
				this.exec(this.onload.pop());
			}
			this.load = true;
		}
		
	}
	window.onload = CMS.doOnload.bind(CMS);
}
CMS.Bookmark = {
	add : function(title)	{
		var bookmarks = this.get();
		if(title)	{
			bookmarks[document.location] = title;
		}
		else	{
			bookmarks[document.location] = document.title;
		}
		this.set(bookmarks);
		return;
	},
	
	remove : function(url)	{
		var bookmarks = this.get();
		delete bookmarks[url];
		this.set(bookmarks);
		this.show();
		return;
	},
	
	set : function(bookmarks)	{ 
		jimAuld.utils.cookies.set('bookmarks', Object.toJSON(bookmarks), 8760000);
	},
	
	get : function()	{
		var val = jimAuld.utils.cookies.get('bookmarks');
		if(!val)	{
			return {};
		}
		
		return eval(val.evalJSON() );
	},
	
	gotoUrl : function(url)	{
		if(window.opener)	{
			window.opener.location.href = url
		}
		else	{
			document.location.href = url;
		}
	},
	
	show : function()	{
		var container = $('bookmarks');
		if(!container)	{
			return;
		}
		// Container leeren
		var children = container.childElements();
		for(var i = 0; i < children.length; i++)	{
			children[i].remove();
		}
		
		// Liste generieren
		
		var bookmarks = this.get();
		for(var url in bookmarks)	{
			var item = new Element('li')
			var mainlink = 'javascript:CMS.Bookmark.goto("'+url+'")';
			var remlink = 'javascript:CMS.Bookmark.remove("'+url+'")';
			item.appendChild(new Element('a', { 'class': 'remove', href: 'javascript:CMS.Bookmark.remove("'+url+'")'}).update("remove"));
			item.appendChild(new Element('a', {  href: 'javascript:CMS.Bookmark.gotoUrl("'+url+'")', title: url}).update(bookmarks[url]));
			
			container.appendChild(item);
		}
	}
}