function dbg(msg) {
	if (window.console)
		console.log(msg);
	else
		$(debug).innerHTML += msg+'<br>';
}

Util = {
	removeFromArray : function(arr, el) {
		var res = [];
		for (var i = 0; i < arr.length; i++)
			if (arr[i] !== el)
				res.push(arr[i]);
		return res; 
	},
	insertIntoArray : function(arr, el, index) {
		if (index >= arr.length) {
			arr.push(el);
			return arr;
		}
		var i;
		res = [];
		for (var i = 0; i < arr.length; i++) {
			if (i == index)
				res.push(el);
			res.push(arr[i]);
		}
		return res;
	},
	findInArray : function(arr, el) {
		for (var i = 0; i < arr.length; i++)
			if (arr[i] === el)
				return i;
		return null;
	},
	getXIntersection : function(c1, c2) {
		var i = {
			x : Math.max(c1.x, c2.x),
			x2 : Math.min(c1.x2, c2.x2)
		}
		i.w = i.x2 - i.x;
		if (i.w < 0)
			return null;
		return i;
	},
	getYIntersection : function(c1, c2) {
		var i = {
			y : Math.max(c1.y, c2.y),
			y2 : Math.min(c1.y2, c2.y2)
		}
		i.h = i.y2 - i.y;
		if (i.h < 0)
			return null;
		return i;
	},
	hasClass : function(el, name) {
		var cl = el.className;
		if (! cl)
			return false;
		if (cl == name)
			return true;
		if (cl.indexOf(name+' ') >= 0)
			return true;
		if (cl.indexOf(' '+name) >= 0)
			return true;
		return false;
	},
	// returns true if this node or any of parents has the given name in class name
	getParentWithClass : function(el, name) {
		while (el && ! Util.hasClass(el, name))
			el = el.parentNode;
		return el;
	},
	addClass : function(el, name) {
		if (Util.hasClass(el, name))
			return;
		if (! el.className)
			el.className = name;
		else
			el.className += ' ' + name;
	},
	removeClass : function(el, name) {
		if (! Util.hasClass(el, name))
			return;
		var cl = el.className;
		cl = cl.replace(name, '');
		cl = cl.replace(/^ /, '');
		cl = cl.replace(/ $/, '');
		el.className = cl;
	},
	addRemoveClass : function(el, name, flag) {
		if (flag)
			Util.addClass(el, name);
		else
			Util.removeClass(el, name);
	},
	getParentWithId : function(el, id) {
		while (el && el.id != id)
			el = el.parentNode;
		return el;
	},
	autoBlur : function(el) {
		el.addEvent('focus', function() {this.blur()});
	}
}

/* holds the widget columns */
var WidgetHost = new Class({
	initialize: function(conf) {
		this.conf = conf;

		document.addEvent('mousemove', this.onMouseMove.bind(this))
			.addEvent('mouseup', this.onMouseUp.bind(this));

		this.columns = {};
		this.reg = {};
		this.minColCoverFraction = 0.6;
		var cor;
		
		if (Browser.Engine.trident) {
			cor = {
				x: 10,
				y: 10
			};
		} else {
			cor = {
				x: 0,
				y: -5
			}
		}
		
		this.dragCorrection = cor;
		
		for (var colkey in conf.cols) {
			var col = conf.cols[colkey];
			var column = new WidgetColumn(colkey);
			this.add(column);
			for (var j = 0; j < col.length; j++) {
				var w = col[j];
				if ($(w.id)) {
					var wClass = 'Widget_'+w.type;
					var inst;
					if (window[wClass]) {
						var code = 'inst = new ' + wClass + '(w)';
						eval(code);
					} else {
						inst = new Widget(w);
					}
					column.add(inst);
				}
			}
		}
		this.recalcAll();
	},
	add : function(column) {
		this.columns[column.id] = column;
		column.recalc();
		column.host = this;
	},
	registerWidget : function(widget) {
		this.reg[widget.id] = widget;
		widget.host = this;
	},
	recalcAll : function() {
		for (var colkey in this.columns) {
			this.columns[colkey].recalc();
			this.columns[colkey].recalcWidgets();
		}
	},
	
	startDrag : function(ev) {
		var widget = this.drag.widget;
		var pl = new WidgetPlaceholder();
		pl.render(widget.coord.w - 2, widget.coord.h - 2);
		this.drag.pl = pl;
		
		widget.column.replace(widget, pl);
		widget.column.recalcWidgets();
		
		widget.el.set({'styles': {'position': 'absolute', 'left': ev.page.x - this.drag.offset.x, 'top': ev.page.y - this.drag.offset.y}});
		
		$(document.body).adopt(widget.el);
	},
	continueDrag : function(ev) {
		var widget = this.drag.widget, coord = null, col = null, i = 0, tcol = null, isect = null, topWidget = null, w = null;
		
		widget.el.set({'styles': { 'left': ev.page.x - this.drag.offset.x, 'top':  ev.page.y - this.drag.offset.y}});
		widget.recalc();
		
		coord = widget.coord;
		col = null;
		
		for (var colkey in this.columns) {
			tcol = this.columns[colkey];
			isect = Util.getXIntersection(tcol.coord, coord);
			if (! isect)
				continue;
			if (isect.w > tcol.coord.w * this.minColCoverFraction) {
				col = tcol;
				break;
			}
		}
		if (!col) // not covering any column sufficiently
			return;
		topWidget = col.widgets[0];
		if (!topWidget) { // column has no widgets
			this.movePlaceholder(col, 0);
			return;
		}
		if (!topWidget.isPl && coord.y < topWidget.coord.y) { // above the top widget
			this.movePlaceholder(col, 0);
			return;
		}
		var bottomWidget = col.widgets[col.widgets.length - 1];
		if (! bottomWidget.isPl && coord.y > bottomWidget.coord.y && coord.y2 > bottomWidget.coord.y2) {
			this.movePlaceholder(col, col.widgets.length);
			return;
		}
		for (var i = 0; i < col.widgets.length; i++) {
			w = col.widgets[i];
			isect = Util.getYIntersection(coord, w.coord);
			if (! isect)
				continue;
			// dragged intersecting with underlying
			if (coord.h < w.coord.h) { // dragged is higher then underlying
				if (coord.y2 > w.coord.y2) {
					this.movePlaceholder(col, i + 1);
					return;
				}
			} else { // dragged is lower then underlying
				if (coord.y > w.coord.y) {
					this.movePlaceholder(col, i + 1);
					return;
				}
			}
		}
	},
	drop : function(ev) {
		var widget = this.drag.widget;

		document.body.removeChild(widget.el);

		$(widget.el).set({'styles': {'left': '0px', 'top': '0px', 'position': ''}});
		
		this.drag.pl.column.replace(this.drag.pl, widget);
		widget.column.recalcWidgets();

		delete this.drag;
		this.confUpdated();
	},
	movePlaceholder : function(col, index) {
		// check if moving to the same place
		if (col === this.drag.pl.column) {
			var curIndex = Util.findInArray(col.widgets, this.drag.pl);
			if (index == curIndex || index == curIndex + 1)
				return;
		}
		var plCol = this.drag.pl.column;
		plCol.remove(this.drag.pl);
		plCol.recalc();
		plCol.recalcWidgets();
		col.insert(this.drag.pl, index);
		col.recalc();
		col.recalcWidgets();
	},
	
	onWidgetMouseDown : function(widget, ev) {
		var pos = null, id = null;
				
		pos = $(widget.el).getPosition();
		this.dragCandidate = {
			widget: widget,
			offset: {
				x : ev.page.x - pos.x - this.dragCorrection.x,
				y : ev.page.y - pos.y - this.dragCorrection.y
			}
		};
		ev.stop();
	},
	onMouseUp : function(ev) {
		if (this.drag) this.drop();
		if (this.dragCandidate) delete this.dragCandidate;		
	},
	onMouseMove : function(ev) {
		if (this.dragCandidate) {
			this.drag = this.dragCandidate;
			delete this.dragCandidate;
			this.startDrag(ev);
		} else if (this.drag) {
			this.continueDrag(ev);
		}
	},
	confUpdated : function(glob) {
		this.cookieWarning();
		this.onConfUpdated(this.conf, glob);
	},
	cookieWarning : function() {
		if (this.cookieWarningDisplayed) return;
		if (cookiesDisabled()) {
			alert('Sie haben die Anpassung der Seite angefangen.\nDa Sie nicht eingeloggt sind und Cookies abgeschaltet sind,\ngeht Ihre Konfiguration verloren, sobald das Browserfenster geschlossen wird.');
			this.cookieWarningDisplayed = true;
		}
	},
	
	addOnClick : function(id, func) {
		var el = $(id);
		if (el)
			el.addEvent('click', func);
	},
	
	initGlobalConfig : function() {
		var container = $('configbox');
		if (! container)
			return;

		this.addOnClick('glob_tog', this.toggleGlobalConf.bind(this));
		this.addOnClick('glob_tog2', this.toggleGlobalConf.bind(this));
		this.addOnClick('glob_save', this.onGlobalConfSave.bind(this));		
		this.addOnClick('glob_cancel', this.onGlobalConfCancel.bind(this));
		this.addOnClick('glob_all_resorts', function(ev) {
			this.checkAllResorts(ev, true);
		}.bind(this));
		this.addOnClick('glob_no_resorts', function(ev) {
			this.checkAllResorts(ev, false);
		}.bind(this));

		this.addOnClick('glob_reset', function(ev) {
			if (! confirm('Achtung! Die Site wird in ihren Ursprung zur\u00fcckgesetzt. Alle von Ihnen gemachten Speicherungen gehen verloren.'))
				ev.stop();
		});

		// assigning onclicks to subweb links	
		var rootSubwebAnchors = container.getElements('a[class=sw_link]');
		rootSubwebAnchors.each(function(a){
			// Util.autoBlur(a.addEvent('click', this.onGlobSubwebLinkClick.bind(this)));			
			a.addEvent('click', this.onGlobSubwebLinkClick.bind(this));			
		}.bind(this));
		
		// assigning onclicks to subweb checkboxes
		var subwebCheckboxes = container.getElements('input[type=checkbox]');
		subwebCheckboxes.each(function(c){
			if (c.id && c.id.substr(0, 3) == 'sw_') {
				c.addEvent('click', this.onGlobSubwebCheckboxClick.bind(this));
			}
		}.bind(this));
		
		// assigning onclicks to resorts
		var resortCheckboxes = container.getElements('input[type=checkbox]');
		resortCheckboxes.each(function(c){
			if (c.id && c.id.substr(0, 3) == 'rs_') {
				c.addEvent('click', this.onGlobResortCheckboxClick.bind(this));
			}
		}.bind(this));
		
		this.subwebs = {};
		this.resorts = {};

		/* need this because sometimes firefox takes checked status from whereever */
		if (this.conf.sw) {
			this.conf.sw.each(function(uid) {
				this.checkSubweb(uid, true); 
			}.bind(this));
		}
		if (this.conf.rs) {
			this.conf.rs.each(function(uid) {
				this.checkResort(uid, true); 	
			}.bind(this));
		}

		
		this.initRegions();
	},
	initRegions : function() {
		var container = $('configbox');
		if (! container)
			return;
		var checkboxes = container.getElements('input[type=checkbox]');
		checkboxes.each(function(c){
			if (c.id && c.id.substr(0, 3) == 'rg_') {
				c.addEvent('click', this.onGlobRegionCheckboxClick.bind(this));
			}
		}.bind(this));
	},
	onGlobSubwebLinkClick : function(ev) {
		ev.stop();
		var uid = ev.target.id.substr(4); // 'swa_'
		this.showGlobSubwebRegions(uid);
	},
	showGlobSubwebRegions : function(uid) {
		var cb = $('sw_'+uid);
		if (! cb)
			return;
		var subweb = subwebs[uid];
		if (! subweb )
			return;

		var regs = [];
		if (subweb.regions) {
			subweb.regions.each(function(s){
				var cl = this.isSubwebChecked(s.uid) ? 'reg_selected' : 'reg_unselected';
				var chk = this.isSubwebChecked(s.uid) ? ' checked="checked"' : '';
				regs.push('<div class="'+cl+'"><input id="rg_' + s.uid + '" name="rg_' + s.uid + '" type="checkbox"'+chk+' /><label class="sw_link" for="rg_'+s.uid+'">' + s.title + '</label></div>');
			}.bind(this));
		}
		var regs_html = '<div style="display:inline-block">'+regs.join('')+'</div>';
		$('rgs').set({html: regs_html});
		this.curSubweb = uid;
		$('sel_reg_title').set({html:subwebs[uid].title})
		this.initRegions();
	},
	onGlobSubwebCheckboxClick : function(ev) {
		ev.target.parentNode.className = ev.target.checked ? 'reg_selected' : 'reg_unselected';
		var uid = ev.target.id.substr(3); // 'sw_'
		this.checkSubweb(uid, ev.target.checked);
		this.showGlobSubwebRegions(uid);
	},
	onGlobRegionCheckboxClick : function(ev) {
		ev.target.parentNode.className = ev.target.checked ? 'reg_selected' : 'reg_unselected';
		var uid = ev.target.id.substr(3); // 'rg_'
		this.checkSubweb(uid, ev.target.checked);
		
		// updating counts
		var count = 0;
		var regs = subwebs[this.curSubweb].regions;
		regs.each(function(reg) {
			if (this.subwebs[reg.uid])
				count++;
		}.bind(this));
		$($('sw_'+this.curSubweb).parentNode).getElement('strong').set({html:count});
	},
	onGlobResortCheckboxClick : function(ev) {
		ev.target.parentNode.className = ev.target.checked ? 'ccat_sel' : 'ccat';
		var uid = ev.target.id.substr(3); // 'rs_'
		this.checkResort(uid, ev.target.checked);
	},
	
	onGlobalConfSave : function(ev) {
		// ev.stop();
		this.confUpdated(true);
		// this.showGlobalConf(false); // irritates newbies
		// document.location.href = document.location.href;
	},
	onGlobalConfCancel : function() {
		ev.stop();
		// TODO put the state as it was before beginning of config
		this.showGlobalConf(false); // irritates newbies
	},

	checkAllResorts : function(ev, flag) {
		ev.stop();
		var resortCheckboxes = $('configbox').getElements('input[type=checkbox]');
		resortCheckboxes.each(function(c){
			if (c.id && c.id.substr(0, 3) == 'rs_') {
				c.checked = flag ? true : false;
				c.parentNode.className = flag ? 'ccat_sel' : 'ccat';
				this.checkResort(c.id.substr(3), flag);
			}
		}.bind(this));	
	},

	checkSubweb : function(uid, flag) {
		this.subwebs[uid.toString()] = flag ? 1 : 0;
	},
	isSubwebChecked : function(uid) {
		return this.subwebs[uid.toString()];
	},

	checkResort : function(uid, flag) {
		this.resorts[uid.toString()] = flag ? 1 : 0;
	},
	isResortChecked : function(uid) {
		return this.resorts[uid.toString()];
	},
	
	toggleGlobalConf : function(ev) {
		ev.stop();
		var conf = $('configbox');
		this.showGlobalConf(conf.className != '');
		
	},
	showGlobalConf : function(flag) {
		var conf = $('configbox');
		var bt = $('config_btn');
		var btTxt = $('config_btn_text');
		var btAdj = $('config_adjust');
	
		if (!flag || conf.className == '')
		{
			conf.className = 'hide';
			bt.className = 'top_bt_container';
			btTxt.className = 'top_bt_text';
			btAdj.className = 'ico_adjust';
			return false;
		}
		
		conf.className = '';
		bt.className = 'conf_bt_container';
		btTxt.className = 'conf_bt_text';
		btAdj.className = 'ico_adjust_open';
		
		return false;	
	}
});

WidgetBase = new Class({
	initialize: function(id){
		this.id = id;
		this.el = $(id); // this is a real problem if column names aren't left, normal, right
   },
	recalc : function() {
		var pos = $(this.el).getPosition();
		var dim = $(this.el).getSize();

		this.coord = {
			x: pos.x,
			y: pos.y,
			x2: pos.x + dim.x,
			y2: pos.y + dim.y,
			w: dim.x,
			h: dim.y
		}
	}
});

/* holds a single widget column */
var WidgetColumn = new Class({
	Extends : WidgetBase,
	initialize : function(id) {
		this.parent(id);
		this.widgets = [];
	},
	add : function(widget) {
		this.widgets.push(widget);
		widget.column = this;
		this.host.registerWidget(widget);
	},
	insert : function(widget, index) {
		widget.column = this;
		var before = this.widgets[index];
		this.widgets = Util.insertIntoArray(this.widgets, widget, index);
		if (!widget.attached) {
			if (before) {
				$(widget.el).inject(before.el, 'before');
			} else {
				$(widget.el).inject(this.el);
			}
			widget.attached = true;
		}
	},
	remove : function(widget) {
		this.widgets = Util.removeFromArray(this.widgets, widget);
		if (widget.attached) {
			this.el.removeChild(widget.el);
			widget.attached = false;
		}
	},
	replace : function(widget, replacement) {
		var index = Util.findInArray(this.widgets, widget);
		this.remove(widget);
		this.insert(replacement, index);
	},
	recalcWidgets : function() {
		for (var i = 0; i < this.widgets.length; i++)
			this.widgets[i].recalc();
	}
});

/* holds a widget instance */
var Widget = new Class({
	Extends : WidgetBase,
	initialize : function(conf) {
		this.conf = conf;
		this.id = conf.id;
		this.parent(this.id);
		this.attached = true;
		
		this.el_header = this.el.getElement('div[class=widget_header]');
		this.el_header.addEvent('mousedown', this.onMouseDown.bind(this));

		if (this.btn_collapse = this.el.getElement('a[class=btn_expanded], a[class=btn_collapsed]')) {
			// Util.autoBlur(this.btn_collapse.addEvent('click', this.onBtnCollapse.bind(this)));
			this.btn_collapse.addEvent('click', this.onBtnCollapse.bind(this));
		}
		
		['minus', 'plus', 'config'].each(function(n) {
			if (this['btn_'+n] = this.el.getElement('a[class=btn_'+n+']')) {
				// Util.autoBlur(this['btn_'+n].addEvent('click', this['onBtn'+n.substr(0,1).toUpperCase()+n.substr(1)].bind(this)));
				this['btn_'+n].addEvent('click', this['onBtn'+n.substr(0,1).toUpperCase()+n.substr(1)].bind(this));
			}
		}.bind(this));
		
		this.el_content = this.el.getElement("div[class^=content]");
		this.el_config = this.el.getElement("div[class^=config]");

		if (this.el_config) {
			var cancel = this.el_config.getElement("a[class^=cncl]");
			if (cancel)
				cancel.addEvent('click', this.onBtnConfig.bind(this));
		}

		// TODO: must be reviewed, config will eventually not be delivered from server
	},
	onBtnCollapse : function(ev) {
		ev.stop();
		this.toggleCollapse();
	},
	onBtnMinus : function(ev) {
		ev.stop();
		this.updateItemDisplay(-1);
	},
	onBtnPlus : function(ev) {
		this.updateItemDisplay(1);
		ev.stop();
	},
	onBtnConfig : function(ev) {
		ev.stop();
		this.toggleConfig();
		/*
		if (! this.configOpen) {
			this.showConfig(true);
		}
		*/
		/* ajax test
		var req = new Request({
			url: '/index.php?id=56666',
			method: 'get',
			encoding: 'iso-8859-1',
			onSuccess: function(responseText, responseXML){
				alert(responseText);
			}
		}).send();
		*/
	},

	/* functions to do custom stuff upon various actions */
	onItemCountChange : function() {},
	onCollapse : function() {},
	onExpand : function() {},
	
	toggleCollapse : function() {
		this.setCollapsed(this.isCollapsed() ? 0 : 1);
		if (this.el_content) {
			if (this.isCollapsed())
				Util.addClass(this.el_content, 'hide');
			else
		 		Util.removeClass(this.el_content, 'hide');
		}
		if (this.btn_collapse) {
			this.btn_collapse.className = (this.isCollapsed()) ? 'btn_collapsed' : 'btn_expanded';
		}
		this.column.recalcWidgets();
		this.host.confUpdated();
		if (this.isCollapsed())
			this.onCollapse();
		else
			this.onExpand();
	},
	showConfig : function(flag) {
		this.configOpen = true;
		if (this.el_config) {
			if (flag) Util.removeClass(this.el_config, 'hide'); else Util.addClass(this.el_config, 'hide');
		}
		if (this.btn_config) {
			this.btn_config.className = (flag) ? 'btn_config_open' : 'btn_config';
		}
		this.column.recalcWidgets();
	},
	toggleConfig : function() {
		if (this.el_config) {
			var configHidden = Util.hasClass(this.el_config, 'hide');
			this.showConfig(configHidden);
		}
	},
	updateItemDisplay : function(add) {
		var countOrig = this.conf.itc;
		var count = countOrig + add;
		
		// check lower bound
		if (count < 0)
			count = 0;
		if (count < this.minItems)
			count = this.minItems;

		item_els = this.el.getElements("*[class^=item]");
			
		// check higher bound
		if (count > item_els.length)
			count = item_els.length;
		if (count > this.maxItems)
			count = this.maxItems;
			
		if (count == countOrig)
			return;

		// update cur count
		this.conf.itc = count;
		this.host.confUpdated();

		var i;
		for (i = 0; i < this.conf.itc; i++) {
			if (i >= item_els.length)
				break;
			Util.removeClass(item_els[i], 'hide');
		}
		for (i = this.conf.itc; i < item_els.length; i++) {
			Util.addClass(item_els[i], 'hide');
		}

		this.column.recalcWidgets();
		this.onItemCountChange();
	},
	
	onMouseDown : function(ev) {
		if (Util.hasClass(ev.target, 'widget_header') ||
			Util.hasClass(ev.target, 'caption')) {
			this.host.onWidgetMouseDown(this, ev);
		}
	},
	
	setCollapsed : function(flag) {
		if (flag)
			this.conf.cl = 1;
		else
			delete this.conf.cl; 
	},
	isCollapsed : function() {
		return this.conf.cl;
	}
	
});

/* widget subclasses */
Widget_blogs = new Class({
	Extends : Widget,
	initialize : function(conf) {
		this.parent(conf);
		
		this.minItems = 1;
		this.maxItems = 10;
	}
});

Widget_news = new Class({
	Extends : Widget,
	initialize : function(conf) {
		this.parent(conf);
		
		this.minItems = 1;
		this.maxItems = 10;
	},
	onItemCountChange : function() {
		var further = this.el.getElement('*[class^=widget_news_list_title]');
		if (further)
			Util.addRemoveClass(further, 'hide', this.conf.itc <= 3);
	}
});


Widget_radfalle = new Class({
	Extends : Widget,
	initialize : function(conf) {
		this.parent(conf);
		if (! this.isCollapsed())
			this.createMap();
	},
	createMap : function() {
		if (this.mapCreated)
			return;
		var startWithRoutes = false;
		cats = all_cats[startWithRoutes ? 'routes' : 'points'];

		XMap.prototype.overridePlainMarkerUrl = function() {
			var curUrl = document.location.href;
			var lastSlashPos = curUrl.lastIndexOf('/');
			var url = curUrl.substr(0, lastSlashPos) + '/index.php?id='+ PID_AJAX_PLAIN_MARKERS + '&pid='+ PID_MARKERS + '&slt='+Math.random();
			return url;
		};
		XMap.prototype.interceptOpenInfoWindow = function(uid) {
			// var url = 'http://www.gruene.at/linemap/?poi='+uid;
			var url = 'http://wien.gruene.at/radfalle/?poi='+uid;
			document.location.href = url;
		};
		xmap = new XMap(startWithRoutes);
		this.mapCreated = true;		
	},
	onExpand : function() {
		this.createMap();
	}
	
});

/* base for custom googlemap widgets */
Widget_googlemap = new Class({
	Extends : Widget,
	initialize : function(conf) {
		this.parent(conf);
		if (! this.isCollapsed())
			this.createMap();
	},
	createMap : function() {
		if (this.mapCreated)
			return;
		var startWithRoutes = false;
		cats = all_cats[startWithRoutes ? 'routes' : 'points'];

		XMap.prototype.overridePlainMarkerUrl = function() {
		    return this.plainMarkerUrl;
		}.bind(this);
		XMap.prototype.interceptOpenInfoWindow = function(uid) {
			var url = this.detailUrlBase + uid;
			document.location.href = url;
		}.bind(this);
		xmap = new XMap(startWithRoutes);
		this.mapCreated = true;		
	},
	onExpand : function() {
		this.createMap();
	}
});

Widget_map_eg = new Class({
	Extends : Widget_googlemap,
	initialize : function(conf) {
		// this.plainMarkerUrl = 'http://eurogreens.at/index.php?id=66&pid=21,56&slt='+Math.random();
		this.plainMarkerUrl = '/fetch-europeangreens.eu-markers.php?slt='+Math.random();
		// http://eurogreens.at/index.php?id=66&pid=21,56&slt='+Math.random();
		this.detailUrlBase = 'http://europeangreens.eu/menu/events/events-single/?tx_ttnews[backPid]=15&tx_ttnews[tt_news]=';
		this.parent(conf);
	}
});

Widget_sms_kommentar = new Class({
	Extends : Widget,
	initialize : function(conf) {
		this.parent(conf);

		this.minItems = 1;
		this.maxItems = 4;
	}
});


/* WidgetPlaceholder class */
WidgetPlaceholder = new Class({
	Extends : WidgetBase,
	initialize : function() {
		this.parent('widget_pl');
		this.isPl = true;
	},
	render : function(w, h) {
		this.el = (new Element('div')).set({'id': this.id, 'class': 'widget_pl', 'styles' : { 'width': w+'px', 'height': h+'px'  }});
	}
});

var wh;
function onLoad() {
	var cookie = Cookie.read('widgets');
	var cookieJson = null;
	var conf;
	try {
		cookieJson = Base64.decode(cookie);
		// alert(cookieJson);
		eval('conf = '+cookieJson);
	} catch (e){}
	wh = new WidgetHost(conf ? conf : wh_conf);
	wh.onConfUpdated = function(conf, glob) {
		// fetching columns
		var cols = {}
		for (var key in wh.columns) {
			var widgets = wh.columns[key].widgets;
			var arr = [];
			for (var i = 0; i < widgets.length; i++)
				arr.push(widgets[i].conf);
			cols[key] = arr;
		}
	
		// recreate conf from scratch;
		var conf = {
			cols : cols
		};
		if (wh.conf.cid)
			conf.cid = wh.conf.cid;
		if (wh.conf.puid)
			conf.puid = wh.conf.puid

		if (glob) { // take new global conf
			// fetching subwebs
			var subwebs = [];
			for (var key in wh.subwebs) {
				if (wh.subwebs[key])
					subwebs.push(parseInt(key));
			}
			conf.sw = subwebs;
			// also save in host
			wh.conf.sw = subwebs;
			
			// fetching resorts	
			var resorts = [];
			for (var key in wh.resorts) {
				if (wh.resorts[key]) 
					resorts.push(parseInt(key));
			}
			conf.rs = resorts;
			// also save in host
			wh.conf.rs = resorts;

		} else { // take old global conf
			if (wh.conf.sw)
				conf.sw = wh.conf.sw;
			if (wh.conf.rs)
				conf.rs = wh.conf.rs;
		} 
		
		// writing conf to cookie
		var json = JSON_optimized.stringify(conf);
		var cookie = Base64.encode(json);
		Cookie.write('widgets', cookie);
	}
	wh.initGlobalConfig();
}
function cookiesDisabled() {
	return (window._cck === false);
}
/*
	flickr functions
*/
function flickNext()
{
	var i = 0;
	
	while(true)
	{
		var obj = $('flick_'+i);
		if (!obj)
		{
			break;
		}
		if (obj.style.display == '')
		{
			i++;
			var nextObj = $('flick_'+i);
			if (nextObj)
			{
				obj.style.display = 'none';
				nextObj.style.display = '';
				$('flick_text').innerHTML = $('flick_photo_desc'+i).innerHTML;
			}
			break;
		}
		i++;
	}
	return false;
}
function flickPrev()
{
	var i = 0;
	
	while(true)
	{
		var obj = $('flick_'+i);
		if (!obj)
		{
			break;
		}
		if (obj.style.display == '')
		{
			i--;
			var nextObj = $('flick_'+i);
			if (nextObj)
			{
				obj.style.display = 'none';
				nextObj.style.display = '';
				$('flick_text').innerHTML = $('flick_photo_desc'+i).innerHTML;
			}
			break;			
		}
		i++;
	}
	return false;
}


