var m = {

	d : document,
	w : window,
	checkflag : false,

	req : null,
	events : new Array(),

	init : function() {
		m.evAdd(null, 'ajaxstart', m.toggleWaiting);
		m.evAdd(null, 'ajaxend', m.toggleWaiting);
	},

	id : function(id) {
		id = this.d.getElementById(id);
		if (!id)
			return false;
		return id;
	},

	evAdd : function(obj, event, funcname) {
		if (event != 'load' && event != 'mousedown' && event != 'mousemove'
				&& event != 'mouseup') {
			var ev = {
				obj : obj,
				ev : event,
				func : funcname
			};
			m.events.push(ev);
		} else {
			if (obj.addEventListener) {
				obj.addEventListener(event, funcname, false);
			} else if (obj.attachEvent) {
				if (obj == m.w
						&& (event.indexOf('mouse') != -1 || event
								.indexOf('click') != -1))
					obj = m.d;
				obj.attachEvent('on' + event, funcname);
			}
		}
	},

	evCall : function(event, source) {
		var i;
		var length = this.events.length;
		for (i = 0; i < length; ++i) {
			if (m.events[i].ev == event) {
				m.events[i].func(m.events[i]);
			}
		}
	},

	query : function(data, url, callback) {
		var xhttp = null;
		this.req = {
			quest : data,
			status : null,
			state : null,
			response : null
		};

		if (window.ActiveXObject) {
			try {
				// IE 6 and higher
				xhttp = new ActiveXObject("MSXML2.XMLHTTP");
			} catch (e) {
				try {
					// IE 5
					xhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					xhttp = false;
				}
			}
		} else if (window.XMLHttpRequest) {
			try {
				// Mozilla, Opera, Safari ...
				xhttp = new XMLHttpRequest();
			} catch (e) {
				xhttp = false;
			}
		} else {
			return false;
		}
		if (!xhttp)
			return false;
		xhttp.onreadystatechange = function() {
			m.req.state = xhttp.readyState;
			if (xhttp.readyState == 4 && xhttp.status == 200) {
				m.req.status = xhttp.status;
				if (typeof (callback) == 'function') {
					m.req.response = xhttp.responseText;
					m.evCall('ajaxend', null);
					callback(xhttp.responseText);
				} else {
					return false;
				}
			}
		};
		m.evCall('ajaxstart', null);
		xhttp.open('POST', m.d.location + '?ajax=true', true);
		xhttp.setRequestHeader('Content-Type',
				'application/x-www-form-urlencoded');
		xhttp.send('ajax=true&' + data);
		return true;
	},

	toggle : function(id, showstyle) {
		if (!showstyle)
			showstyle = '';
		if (!m.id(id))
			return false;
		if (m.id(id).style.display == showstyle) {
			m.id(id).style.display = 'none';
		} else {
			m.id(id).style.display = showstyle;
		}
	},

	toggleWaiting : function() {
		m.toggle('wait', 'block');
	},

	close : function(id) {
		var obj = m.id(id);
		var parent = obj.parentNode;
		parent.innerHTML = '';
	},

	reloadImage : function() {
		fader.fade_out('pictureofday', m.reloadPictureOfDay);
	},

	reloadPictureOfDay : function() {
		m.query('mode=reload', '', m.reloadedImage);
	},

	reloadedImage : function(ret) {
		var im = new Image();
		im.src = ret;
		m.id('reisebild').src = im.src;
		fader.fade_in('pictureofday');
	},

	showPreview : function(id) {
		this.query('preview=true&id=' + id, '', box.open);
	},

	addImageFilter : function(id, name, value) {
		this.query('imgid=' + id + '&imagefilter=' + name + '&value=' + value,
				'', m.imageFilterAdded);
	},

	imageFilterAdded : function(ret) {
		var im = new Image();
		im.src = ret;
		m.id('imageedit').src = im.src;
	},

	fadeMessages : function() {
		setTimeout('fader.fade_out(\'ok\', m.close)', 3000);
		setTimeout('fader.fade_out(\'fail\', m.close)', 3000);
		setTimeout('fader.fade_out(\'info\', m.close)', 3000);
	},

	selectAllCheckboxes : function(id) {
		var obj = this.id('formular')[id];

		if (!this.checkflag) {
			for (i = 0; i < obj.length; i++) {
				obj[i].checked = true;
			}
			this.checkflag = true;
			return true;
		} else {
			for (i = 0; i < obj.length; i++) {
				obj[i].checked = false;
			}
			this.checkflag = false;
			return true;
		}
	},

	autocomplete : function(field) {
		var value;
		value = m.id('form_' + field).value;
		this.tocomplete = field;
		this.query('autocomplete=true&field=' + field + '&text=' + value, '',
				m.autocompleted);
	},

	autocompleted : function(ret) {
		if (!m.tocomplete)
			return false;
		var el = m.id('completition_' + m.tocomplete);
		if (ret == '') {
			el.innerHTML = '';
			el.style.display = 'none';
			return false;
		}
		el.innerHTML = ret;
		el.style.display = 'block';
		return true;
	}
};

var fader = {

	fade_in : function(id, delay, o) {
		if (o > 100) return;
		if (!delay) delay = 50;
		if (o == null) o = 0;
		this.setOpacity(document.getElementById(id), o);
		o += 20;
		setTimeout("fader.fade_in('" + id + "'," + delay + "," + o + ")", delay);
	},

	fade_out : function(id, callback, delay, o) {
		if (!delay) delay = 50;
		if (o == null) o = 100;
		var element = document.getElementById(id);
		if (!element) return;
		if (o < 0) {
			element.style.zIndex = -20;
			if (typeof (callback) == 'function') {
				callback(id);
			}
			return true;
		}
		this.setOpacity(element, o);
		o -= 20;
		setTimeout("fader.fade_out('" + id + "'," + callback + "," + delay + "," + o + ")", delay);
	},

	setOpacity : function(obj, opacity) {
		if (!obj) return;
		opacity = (opacity == 100) ? 99.999 : opacity;
		obj.style.filter = "alpha(opacity=" + opacity + ")";
		obj.style.KHTMLOpacity = opacity / 100;
		obj.style.MozOpacity = opacity / 100;
		obj.style.opacity = opacity / 100;
	}
};

var editor = {

	obj : null,
	topobj : null,
	top : 0,
	height : 0,
	browser : '',

	init : function() {

		editor.browser = navigator.appName;
		var content = m.id('editor_nachricht').value;

		// if IE
		if (editor.browser == "Microsoft Internet Explorer") {

			var id = "editordiv";
			if (!m.id(id))
				return;

			this.obj = m.id(id);
			editor.obj = this.obj;

			this.obj.style.display = 'inline';
			this.obj.innerHTML = content;
		}
		// other browsers
		else {

			var id = "editor";
			if (!m.id(id))
				return;

			this.obj = m.id(id).contentWindow.document;
			editor.obj = this.obj;

			m.id(id).style.display = 'inline';

			var el = document.createElement("link");
			el.href = '/css/editor.css';
			el.rel = 'stylesheet';
			el.type = 'text/css';

			var head = this.obj.getElementsByTagName('head')[0];
			head.appendChild(el);

			var body = this.obj.getElementsByTagName('body')[0];
			body.innerHTML = content;
		}
		this.obj.designMode = "On";
	},

	format : function(command, parms, value) {
		if (editor.browser == "Microsoft Internet Explorer") {
			m.id("editordiv").focus();
			m.d.execCommand(command, false, parms);
		} else {
			m.id("editor").focus();
			this.obj.execCommand(command, false, parms);
		}
	},

	addImage : function() {
		var path = prompt('Bild-Pfad angeben:');
		if (!path)
			return;
		this.format('insertImage', path);
	},

	addLink : function() {
		var path = prompt('Link-Pfad (URL) angeben:');
		if (!path)
			return;
		this.format('createLink', path);
	},

	getContent : function() {
		if (editor.browser == "Microsoft Internet Explorer") {
			var content = m.id("editordiv").innerHTML;
		} else {
			var body = m.id("editor").contentWindow.document
					.getElementsByTagName('body')[0];
			var content = body.innerHTML;
		}
		m.id('editor_nachricht').value = content;
	}	
};

m.evAdd(m.w, 'load', m.init);
m.evAdd(m.w, 'load', m.fadeMessages);
m.evAdd(m.w, 'load', editor.init);
