var mainlimesLastElement;

function explain(element, url) {
	mainlimesLastElement = element;
	var contentDiv = document.createElement("div");
	if (url.match('lang=en')) {
		contentDiv.appendChild(document.createTextNode('Loading description of “'+element.innerHTML+'”…'));
	} else {
		contentDiv.appendChild(document.createTextNode('Lade Beschreibung von „'+element.innerHTML+'“…'));
	}
	drawBox(element, contentDiv)
	new Ajax(url, {onSuccess:updateCitation});
}

function updateCitation(data) {
	var contentDiv = document.createElement("div");
	var headline = document.createElement("p");
	headline.style.fontWeight = 'bold';
	headline.appendChild(document.createTextNode(data['title']));
	contentDiv.appendChild(headline);
	if  (data['subtitle']) {
		var subheadline = document.createElement("p");
		headline.style.fontStyle = 'italic';
		subheadline.appendChild(document.createTextNode(data['subtitle']));
		contentDiv.appendChild(subheadline);
	}
	var descriptionP = document.createElement("p");
	descriptionP.innerHTML=data['description'];
	descriptionP.style.margin = 0;
	contentDiv.appendChild(descriptionP);
	drawBox(mainlimesLastElement, contentDiv);
}

function drawBox(element, contentDiv) {
	var citation = document.getElementById('citation_element');
	if (citation) {
		citation.parentNode.removeChild(citation);
	}
	if (!element) return;
	citation = document.createElement("div");
	citation.id = 'citation_element';
	citation.appendChild(contentDiv);
	citation.style.position = "absolute";
	citation.style.paddingBottom = "31px";
	citation.style.background = "url(/template/pix/citation-bottom.png) no-repeat bottom";
	contentDiv.style.background = "url(/template/pix/citation-top.png) no-repeat top";
	contentDiv.style.paddingTop = "35px";
	contentDiv.style.paddingLeft = "32px";
	contentDiv.style.paddingRight = "32px";
	contentDiv.style.paddingBottom = "0px";
	contentDiv.style.width = "268px";
	element.offsetParent.appendChild(citation);
	citation.style.top = (element.offsetTop - citation.offsetHeight + 3) + "px";
	citation.style.left = (element.offsetLeft + element.offsetWidth/2 - 61) + "px";
	closeButton = document.createElement("div");
	closeButton.style.position="absolute";
	closeButton.style.top=0;
	closeButton.style.right=0;
	closeButton.style.width="50px";
	closeButton.style.height="50px";
	closeButton.style.cursor="pointer";
	closeButton.onclick=function(e){drawBox()};
	contentDiv.appendChild(closeButton);
}

/* These are the basic AJAX functions... */
if (!Function.prototype.apply)
{
	// Based on code from http://www.youngpup.net/
	Function.prototype.apply = function(object, parameters)
	{
		var parameterStrings = new Array();
	    if (!object)
			object = window;
	    if (!parameters)
			parameters = new Array();
    
	    for (var i = 0; i < parameters.length; i++)
	      	parameterStrings[i] = 'parameters[' + i + ']';
    
	    object.__apply__ = this;
	    var result = eval('object.__apply__(' + parameterStrings.join(', ') + ')');
	    object.__apply__ = null;
    
    	return result;
	}
}
Function.prototype.bind = function(object)
{
	var __method = this;
	return function()
	{
		__method.apply(object, arguments);
	}
}


function Ajax( url, options )
{
	//Most important things first... let's try making a request object!
	try {this.transport = new ActiveXObject('Msxml2.XMLHTTP');}
	catch (e)
	{
		try {this.transport = newActiveXObject('Microsoft.XMLHTTP');}
		catch (e2)
		{
			try {this.transport = new XMLHttpRequest();}
			catch (e3)
			{
				return false;
			}
		}
	}
	
	this.isSuccess = function()
	{
		return this.transport.status == undefined
	        || this.transport.status == 0 
	        || (this.transport.status >= 200 && this.transport.status < 300);
	}
	this.onStateChange = function( a )
	{
		var result;
		if (this.transport.readyState==4)
		{
			//Immediatly parse the JSON result
			try {eval('result = '+this.transport.responseText);}
			catch(e) {result=false;}
			
			(this[this.isSuccess()?'onSuccess':'onFailure'])(result);

			//Avoid a memory leak in IE
			this.transport.onreadystatechange = nullFunction;
		}
	}
	
	if (typeof options != 'object') options={};
	
	this.onSuccess = options.onSuccess || nullFunction;
	this.onFailure = options.onFailure || nullFunction;
	
	if (options.method=='get' && typeof options.parameters == 'string')
		url = url+'?'+options.parameters;
	
	this.transport.open( options.method || 'get', url, true );
	this.transport.onreadystatechange = this.onStateChange.bind(this);
	
	if (options.method=='post')
		this.transport.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	
	//Avoid a mozilla bug...
	if (this.transport.overrideMimeType)
		this.transport.setRequestHeader('Connection','Close');
	
	this.transport.send(options.method=='post'?(options.postBody||options.parameters):null);
}

//Create a null function, so we don't always have to specify an empty function 
function nullFunction () {}
