/******************************************************
	Index:
	 
	0) Init
	1) Forms
		1.1) Form <Kontakt> Send
		1.2) Form <ERROR> Send
		
******************************************************/



/* --------------------------------------------------------------------------------------------- */
/* 0) Init
/* --------------------------------------------------------------------------------------------- */
if(!window.TopinApi)
	window.TopinApi = {};

window.TopinApi.ajaxhandler = {};

function AJAXGarbageCollector(storagearea) {
	this.storagearea = storagearea;
	this.preitems = [];
	this.items = [];
	this.interval = null;

	window.TopinApi.ajaxgarbagecollector = this;

	this.start();
}

AJAXGarbageCollector.prototype.add = function(item) {
	this.preitems.push(item);
}

AJAXGarbageCollector.prototype.start = function() {
	this.stop();

	this.interval = setInterval(function() {
		window.TopinApi.ajaxgarbagecollector.run();
	}, 30000);
}

AJAXGarbageCollector.prototype.stop = function() {
	if(this.interval != null) {
		clearInterval(this.interval);
		this.interval = null;

		this.run();
		this.run();
	}
}

AJAXGarbageCollector.prototype.run = function() {
	for(var i = 0; i < this.items.length; i++)
		delete this.storagearea[this.items[i]];

	this.items = this.preitems;
	this.preitems = [];
}

new AJAXGarbageCollector(window.TopinApi.ajaxhandler);

function AJAX(method, callback, data) {
	this.url = 'http://' + GLOBAL['topin_host'] + '/ajax/api.php';

	this.ajaxhandler = 'ah' + Math.round(Math.random() * 1000000);

	if(callback)
		this.callback = callback;
	else
		this.callback = this.success;

	this.data = data;

	this.parameter = '';

	this.setParam('method', 'topin.' + method);
}

AJAX.prototype.success = function(s, d) {
	alert(s);
}

AJAX.prototype.parseJSON = function(text) {
	if(text.length == 0)
		return null;

	var rex = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/
	var reprex = /"(\\.|[^"\\])*"/g

	if(rex.test(text.replace(reprex, '')) == true)
		return null;

	var obj;

	try {
		obj = eval('(' + text + ')');
	}
	catch(e) {
		obj = null;
	}

	return obj;
}

AJAX.prototype.setParam = function(parameter, value) {
	this.parameter = this.parameter + '&' + encodeURIComponent(parameter) + '=' + encodeURIComponent(value);
}

AJAX.prototype.setForm = function(fid) {
	var f = document.getElementById(fid);
	if(!f)
		return;

	var p = '';

	for(var i = 0; i < f.elements.length; i++) {
		switch(f.elements[i].type) {
			case 'hidden':
			case 'text':
			case 'password':
			case 'textarea':
				p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'checkbox':
			case 'radio':
				if(f.elements[i].checked)
					p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'select-one':
				p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'select-multiple':
				for(var j = 0; j < f.elements[i].options.length; j++) {
					if(f.elements[i].options[j].selected)
						p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].options[j].value);
				}
				break;
			case 'button':
			case 'submit':
			case 'reset':
			default:
				break;
		}
	}

	this.parameter = this.parameter + p;
}

AJAX.prototype.send = function() {
	this.setParam('callback', 'TopinApi.ajaxhandler.' + this.ajaxhandler + '.f');

	window.TopinApi.ajaxhandler[this.ajaxhandler] = {};
	var ah = window.TopinApi.ajaxhandler[this.ajaxhandler];

	var self = this;

	ah.callback = this.callback;
	ah.data = this.data;
	ah.f = function(response) {
		var handler = window.TopinApi.ajaxhandler[self.ajaxhandler];

		if(response.status != 'ok')
			return;

		handler.callback(response['data'], handler.data);

		window.TopinApi.ajaxgarbagecollector.add(self.ajaxhandler);

		return;
	};

	var script = document.createElement('script');
	script.setAttribute('src', this.url + '?' + this.parameter);
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);

	return;
}
	
/* --------------------------------------------------------------------------------------------- */
/* 1) Forms
/* --------------------------------------------------------------------------------------------- */

	ajax = {};

// 1.1) Form <Kontakt> Send
// .......................................................................
	ajax.sendForm = function(form, callback){
		var a = new AJAX('email.send_feedback', function(response, params) {
			params(response['feedback']);
		}, callback);
		for(var i = 0; form.elements[i]; i++)
			a.setParam('feedback[' + form.elements[i].title + ']', form.elements[i].value);
		a.send();

		return;
	}


// 1.2) Form <ERROR> Send
// .......................................................................
	ajax.sendErrorMessage = function(referer, message){
		var a = new AJAX('email.send_error', function(response, params) {});
		a.setParam('referer', referer);
		a.setParam('message', message);
		a.send();
	}