/**
 * Class that allows to have the login part loaded by ajax.
 * @author Nils Blattner <nb@cabag.ch>
 */
var AjaxLogin = function (root, url) {
	if (!root) throw new Exception('AjaxLogin: No root found!');
	if (!url) throw new Exception('AjaxLogin: No url found!');
	
	// content of the ajax request
	this.content = '';
	
	// will be set to true if dom:loaded as the element $(root) exists after that time
	this.domLoaded = !!$(root);
	
	// id string of the root element
	this.root = root;
	
	// prepare the url
	var parts1 = url.split('?');
	// url to call in ajax request
	this.url = parts1[0];
	
	this.parameters = {};
	
	if (parts1.length > 1) {
		var parts2 = parts1[1].split('&');
		
		for (var c = 0; c < parts2.length; c++) {
			var keyValParts = parts2[c].split('=');
			this.parameters[keyValParts[0]] = keyValParts[1];
		}
	}
	
	/**
	 * Function that handles the dom:loaded event.
	 *
	 * @return void
	 */
	this.handleDomLoaded = function () {
		this.domLoaded = true;
		
		if (this.content != '') {
			this.writeContent();
		}
	};
	
	// this has to be after handleDomLoaded was defined!
	if (!this.domLoaded) {
		document.observe('dom:loaded', this.handleDomLoaded.bind(this));
	}
	
	/**
	 * Writes the content taken from ajax to the root.
	 *
	 * @return void
	 */
	this.writeContent = function () {
		if (this.domLoaded) {
			this.root = $(this.root);
			
			scripts = new Array();
			
			var scriptRegex = /\<script[^\>]*type="text\/javascript[^\>]*\>([^\<]*)\<\/script\>/gi;
			
			// extract all the script tags
			while ((m = scriptRegex.exec(this.content)) != null) {
				if (m[1]) scripts.push(m[1]);
			}
			
			// remove the scripts from the html
			this.content = this.content.replace(scriptRegex, '');
			
			this.root.innerHTML = this.content;
			
			for (var c = 0; c < scripts.length; c++) {
				try {
					var tFunc = new Function(scripts[c]);
					tFunc();
				} catch (e) {
					/* alert(e); */
				}
			}
			
			$(document).fire('ajax_login:loaded');
			
			//window.alert(!/class="tx-felofin-pi1/.test(this.content));
			if(!/class="tx-felogin-pi1/.test(this.content) && !/class="error"/.test(this.content)) {
				d = document.getElementById('communitylogin');
				d.removeClassName('open');
			}
		}
	};
	
	/**
	 * Retrieves the url's content and stores or writes it.
	 *
	 * @return void
	 */
	this.getUrlContent = function (parameters, asPost) {
		var method = 'get';
		if (asPost) method = 'post';
		
		var requestParameters = {
			method: method,
			onSuccess: function(response) {	
				this.content = response.responseText;
				this.writeContent();
			}.bind(this),
			onFailure: function(response) {
				//alert(response.responseText);
				return '';
			}
		};
		
		requestParameters.parameters = this.parameters;
		
		for (var key in parameters) {
			requestParameters.parameters[key] = parameters[key];
		}
		
		var ajax = new Ajax.Request(this.url, requestParameters);
	};
	
	/**
	 * Reloads the ajax content.
	 *
	 * @return void
	 */
	this.reload = function () {
		this.getUrlContent();
	};
	
	
	/**
	 * Reloads the ajax content with some post parameters.
	 *
	 * @parameter json Parameters to send with the post.
	 * @return void
	 */
	this.post = function (parameters) {
		this.getUrlContent(parameters, true);
	};
	
	/**
	 * Takes a form and posts it.
	 *
	 * @param DOM The form.
	 * @return void
	 */
	this.postForm = function (form) {
		form = $(form);
		if (!form) return;
		var elements = form.getElements();
		var parameters = {};
		
		elements.each(
			function (el) {
				if (el.name) {
					parameters[el.name] = el.getValue();
				}
			}
		);
		
		var isPostRegex = /post/i;
		var isPost = false;
		
		if (isPostRegex.test(form.method)) isPost = true;
		
		this.getUrlContent(parameters, isPost);
	};
	
	
	// fetch the ajax content
	this.getUrlContent();
}

