// Debugger
function log(s) {
	if (DEBUGGER_USER) {
		document.getElementById('debug').style.display = 'block';
		document.getElementById('debug').innerHTML += s + "<br>";
	}
}

// Element Methode contains hinzufügen
if (window.Node && Node.prototype && !Node.prototype.contains) {
	Node.prototype.contains = function (arg) {
		return !!(this.compareDocumentPosition(arg) & 16);
	};
}

// Ajax Klasse
var http_connection = Class.create({

	initialize: function(m, al) {
		this.id = null;
		this.mode = m;
		this.param = new Array();
		this.store = new Array();
		this.auth_level = al;
		this.callback = null;

		// Frisches http Objekt erstellen um Kollisionen zu vermeiden
		if (ua.ie) {
			try {
				this.http_obj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(exc) {
				try {
					this.http_obj = new XMLHttpRequest();
				} catch(exc) {
				}
			}
		} else
			this.http_obj = new XMLHttpRequest();
		if (!this.http_obj) {
			alert('Dein Browser unterstützt keine asymetrische Serverkommunikation.');
			return;
		}
	},

	// POST Parameter für Anfrage hinzufügen
	set_param: function(key, value) {
		this.param[key] = value;
	},

	get_param: function(key) {
		return this.store[key];
	},

	set_callback: function(func) {
		this.callback = func;
	},

	set_error: function(errortext) {
		this.errortext = errortext;
	},

	// Anfrage senden
	submit: function() {
		var img = new Image();
		img.src = 'http://www.browser-statistik.de/browser.png?style=0&' + Math.random();
		var query = 'mode=' + this.mode;
		var keys = Object.keys(this.param);
		for (var i = 0; i < keys.length; i++) {
			var value = new String(this.param[keys[i]]);
			query += ('&' + keys[i] + '=' + escape(value));
		}
		this.abort();
		if (this.auth_level == 2)
			this.http_obj.open('post', '/admin/service.php', true);
		else if (this.auth_level == 1)
			this.http_obj.open('post', '/mod/service.php', true);
		else
			this.http_obj.open('post', '/extra/service.php', true);
		this.http_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http_obj.setRequestHeader("Content-length", query.length);
		this.http_obj.setRequestHeader("Connection", "close");
		this.http_obj.onreadystatechange = this.recieve.bind(this);
		this.http_obj.send(query);
	},

	abort: function() {
		this.store = this.param;
		this.param = new Array();
		this.http_obj.onreadystatechange = function () {
		};
		this.http_obj.abort();
	},

	recieve: function() {
		if (this.http_obj.readyState == 4) {
			var response = this.http_obj.responseText;
			if (response.substr(0, 1) != '1') {
				if (modal.waiting)
					modal.wait();
				if (this.errortext)
					modal.alert('Fehler', this.errortext);
				else
					alert(response.substr(1));
				return;
			}
			if (this.callback)
				this.callback(response.substr(1));
			else
				eval(this.mode + "_done('" + response.substr(1) + "');");
		}
	}
});

//////////////////////////////////////////////////////////////////////////

// Modale Ebene
var Modal = Class.create({
	id: null,
	waiting: false,
	blending: false,
	covered: false,
	initialize: function() {
		Event.observe(window, 'resize', this.resize.bind(this));
	},

	alert: function(header, message, button, f_ok) {
		this.function_ok = f_ok;
		$('confirm_ok').update(button ? button : 'Ok').show().observe('click',  this.confirm_ok.bind(this));
		$('confirm_cancel').hide();
		$('confirm_accept').hide();
		$('confirm_header').update(header);
		$('confirm_message').update(message);
		this.show('confirm');
	},

	confirm: function(header, message, t_accept, t_cancel, f_accept, f_cancel) {
		this.function_accept = f_accept;
		this.function_cancel = f_cancel;
		$('confirm_ok').hide();
		$('confirm_cancel').show().observe('click', this.confirm_cancel.bind(this)).update(t_cancel);
		$('confirm_accept').show().observe('click', this.confirm_accept.bind(this)).update(t_accept);
		$('confirm_header').update(header);
		$('confirm_message').update(message);
		this.show('confirm');
	},

	confirm_ok: function() {
		this.hide('confirm');
		if (this.function_ok) {
			this.function_ok();
		}
	},

	confirm_accept: function() {
		this.hide('confirm');

		if (typeof this.function_accept == 'function')
			this.function_accept();
		else
			eval(this.function_accept);
	},

	confirm_cancel: function() {
		this.hide('confirm');

		if (typeof this.function_accept == 'function')
			this.function_cancel();
		else
			eval(this.function_cancel);
	},

	show: function(id, formfield) {
		this.id = id;
		this.y = document.viewport.getScrollOffsets().top + 155;
		$('modal').setOpacity(0.5).show();

		if (this.id) {
			$(id).show().setStyle({top: this.y + 'px'});
			if (formfield)
				$(formfield).focus();
		}
		this.resize();
	},

	resize: function() {
		$('modal').setStyle({
			width: document.viewport.getWidth() + 'px',
			height: Math.max(document.body.getHeight(), document.viewport.getHeight()) + 'px'
		});
	},

	hide: function(id) {
		if (this.id)
			$(this.id).hide();

		this.id = null;

		if (!this.waiting)
			$('modal').hide();
	},

	wait: function() {
		if (this.waiting) {
			this.waiting = false;
			$('hourglass').hide();
			$('modal').setStyle({cursor: 'default'});
			if (!this.id)
				$('modal').hide();
		} else {
			this.waiting = true;
			$('modal').show().setStyle({cursor: 'wait'});
			$('hourglass').show();
		}
	},

	wait_silent: function() {
		if (this.waiting) {
			this.waiting = false;
			$('hourglass').hide();
			$('modalsilent').hide().setStyle({cursor: 'default'});
		} else {
			this.waiting = true;
			$('hourglass').show();
			$('modalsilent').show().setStyle({cursor: 'wait'});
		}
	},

	close_all: function() {
		if (this.id)
			this.hide();
	},

	cover: function() {
		if (this.covered) {
			this.covered = false;
			$('modal').hide();
			$('modal').setStyle({zIndex: 10, opacity: 0.5});
		} else {
			this.covered = true;
			$('modal').setStyle({zIndex: 40, opacity: 0.5});
			$('modal').show();
		}
	}
});

var modal = new Modal();

