/*
 * Copyright (c) 2008 FIZON GmbH
 * All rights reserved.
 *
 * $Id: pajah.js 2276 2008-10-22 14:59:32Z as $
 */
function PaJaH()
{
	var httpRequest, loadLayerDiv;
	var thisObj = this;
	var timeoutObj = null;
	var loadLayerItem = false;


	/**
	 * construct()
	 */
	this.construct = function()
	{
		if (typeof PaJaH.Objs == 'undefined') {
			PaJaH.Objs = [];

			PaJaH.LoadLayers = document.createElement('div');
			PaJaH.LoadLayers.id = 'pajah-loadlayers';
			document.getElementsByTagName('body')[0].appendChild(PaJaH.LoadLayers);

			PaJaH.LoadLayer = document.createElement('div');
			PaJaH.LoadLayer.className = 'pajah-loadlayer';
			PaJaH.LoadLayer.innerHTML
				= '<img width="32" height="32" alt="load" src="styles/'+templatedir+'/images/load.gif"/>';
		}

		this.id = PaJaH.Objs.length;
		PaJaH.Objs.push(this);

		httpRequest = this.getHttpRequest();
		loadLayerDiv = PaJaH.LoadLayer.cloneNode(true);
	}


	/**
	 * getHttpRequest()
	 */
	this.getHttpRequest = function()
	{
		var hr = null;

		// Mozilla, Opera, Safari sowie Internet Explorer 7
		if (typeof XMLHttpRequest != 'undefined')
			hr = new XMLHttpRequest();

		if (! hr) {
			// Internet Explorer 6 und aelter
			try {
				hr = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
					hr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					hr = null;
				}
			}
		}

		return hr;
	}


	/**
	 * request()
	 */
	this.request = function(method, url, values, handlerFunction)
	{
		var isHandlerFunction = (typeof(handlerFunction) == 'function');

		if (this.isRequest())
			this.abortRequest();

		this.startRequest();

		httpRequest.open(method, url, isHandlerFunction);
		httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset='+encoding);
		httpRequest.setRequestHeader('Content-length', (values) ? values.length : 0);

		if (isHandlerFunction) {

			httpRequest.onreadystatechange = function()
			{
				if (httpRequest.readyState != 4)
					return null;

				thisObj.endRequest();

				try {
					if (httpRequest.status != 200 && httpRequest.status != 304) {
						requestError(httpRequest);
					}
				}
				// XXX abgebrochener Versuch muss noch behandelt werden
				catch (e) {

				}

				handlerFunction(httpRequest);
			}
		}

		httpRequest.send((values) ? values : null);

		if (! isHandlerFunction) {
			thisObj.endRequest();

			if (httpRequest.status != 200 && httpRequest.status != 304) {
				requestError(httpRequest);
				return false;
			}

			return httpRequest;
		}
	}


	/**
	 * abort()
	 */
	this.abortRequest = function()
	{
		httpRequest.abort();
		this.removeLoadLayer();
		this.endRequest();
	}


	/**
	 * setTimeout()
	 */
	this.startRequest = function()
	{
		timeoutObj = window.setTimeout("PaJaH.AbortRequest("+this.id+")", 30000);
	}


	/**
	 * clearTimeout()
	 */
	this.endRequest = function()
	{
		if (timeoutObj == null)
			return;

		window.clearTimeout(timeoutObj);
		timeoutObj = null;
	}


	/**
	 * isRequest()
	 */
	this.isRequest = function()
	{
		return timeoutObj != null;
	}


	/**
	 * getRequest()
	 */
	this.getRequest = function(url, handlerFunction)
	{
		return this.request('GET', url, null, handlerFunction);
	}


	/**
	 * postRequest()
	 */
	this.postRequest = function(url, postValues, handlerFunction)
	{
		var postStr = '';
		for(var name in postValues) 
			postStr += escape(name) + '=' + escape(postValues[name]) + '&';

		return this.request('POST', url, postStr, handlerFunction);
	}


	/**
	 * sendForm()
	 */
	this.sendForm = function(form, handlerFunction)
	{
		var postValues = new Object();
		var url = form.action == '' ? '?' : form.action;
		var item;

		for (var i = 0; i < form.elements.length; i++) {
			item = form.elements[i];

			if (item.name == '')
				continue;
			if (item.type == "checkbox" && ! item.checked)
				continue;
			if (item.type == "reset")
				continue;

			postValues[item.name] = item.value;
		}

		this.postRequest(url, postValues,
			function(hr) {
				thisObj.removeLoadLayer();
				
				if (typeof(handlerFunction) == 'function')
					handlerFunction(hr);
			}
		);
		
		this.addLoadLayer(form);
	}


	/**
	 * getResponseText()
	 */
	this.getResponseText = function(url)
	{
		var hr = this.getRequest(url);
		if (! hr)
			return false;

		return hr.responseText;
	}



	/**
	 * replaceInnerHTML()
	 */
	this.replaceInnerHTML = function(url, item, handlerFunction)
	{

		this.getRequest(url, 
			function(hr)
			{ 
				item.innerHTML = hr.responseText;

				if (typeof(handlerFunction) == 'function') 
					handlerFunction(hr);

				thisObj.removeLoadLayer();
			}
				
		);

		this.addLoadLayer(item);
	}


	/**
	 * addLoadLayer()
	 */
	this.addLoadLayer = function(item)
	{
		loadLayerItem = item;

		/* z-position */
		var index = findZIndex(loadLayerItem);
		index = index ? index + 1 : 50;
		loadLayerDiv.style.zIndex = index;
		
		/* und anzeigen */
		PaJaH.LoadLayers.appendChild(loadLayerDiv);

		this.watchLoadLayerPosition();
	}


	/**
	 * positionLoadLayer()
	 */
	this.positionLoadLayer = function()
	{
		if (! loadLayerItem)
			return false;

		/* die zu ueberdekende position finden */
		var xy = findPos(loadLayerItem);

		/* breite und hohe - 2px damit dieser inline liegt */
		loadLayerDiv.style.width = loadLayerItem.clientWidth-2+'px';
		loadLayerDiv.style.height = loadLayerItem.clientHeight-2+'px';
		
		/* xxx: ploeder hack damit das load-bild mittig ist */
		loadLayerDiv.style.lineHeight = loadLayerDiv.style.height;

		/* positionieren */
		loadLayerDiv.style.left = xy[0]+1+'px';
		loadLayerDiv.style.top = xy[1]+1+'px';
	}


	/**
	 * removeLoadLayer()
	 */
	this.removeLoadLayer = function()
	{
		loadLayerItem = false;
		return PaJaH.LoadLayers.removeChild(loadLayerDiv);
	}




	/**
	 * watchLoadLayerPosition()
	 */
	this.watchLoadLayerPosition = function()
	{
		if (! loadLayerItem)
			return;

		this.positionLoadLayer();

		window.setTimeout("PaJaH.WatchLoadLayerPosition("+this.id+")", 100);
	}



	/***************/
	/*** PRIVATE ***/
	/***************/

	/**
	 * private findPos()
	 */
	function findPos(obj)
	{
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft, curtop];
	}


	/**
	 * findZIndex()
	 */
	function findZIndex(obj)
	{
		do {
			if (obj.tagName == 'BODY')
				return false;

			if (obj.style.zIndex)
				return obj.style.zIndex;

		} while (obj = obj.parentNode)
	}


	/**
	 * requestError()
	 */
	function requestError(hr)
	{
		alert("Error: PaJaH::getRequest => request_error("+hr.status+"): "+hr.statusText);
	}


	this.construct();
}


/**
 * PaJaH.GetRequestById()
 */
PaJaH.GetRequestById = function(id) {
	return PaJaH.Objs[id];
}


/**
 * PaJaH.AbortRequest()
 */
PaJaH.AbortRequest = function(id)
{
	return (PaJaH.GetRequestById(id)).abortRequest();
}


/**
 * PaJaH.WatchLoadLayerPosition()
 */
PaJaH.WatchLoadLayerPosition = function(id)
{
	return (PaJaH.GetRequestById(id)).watchLoadLayerPosition();
}


/**
 * Statische Methoden als Aliase.
 */
PaJaH.GetHttpRequest	= function() 				{ return (new PaJaH()).getHttpRequest(); }
PaJaH.Request 			= function(a1, a2, a3, a4) 	{ return (new PaJaH()).request(a1, a2, a3, a4); }
PaJaH.GetRequest 		= function(a1, a2) 			{ return (new PaJaH()).getRequest(a1, a2); }
PaJaH.PostRequest 		= function(a1, a2, a3) 		{ return (new PaJaH()).postRequest(a1, a2, a3); }
PaJaH.SendForm 			= function(a1, a2) 			{ return (new PaJaH()).sendForm(a1, a2); }
PaJaH.GetResponseText 	= function(a1) 				{ return (new PaJaH()).getResponseText(a1); }
PaJaH.ReplaceInnerHTML 	= function(a1, a2, a3) 		{ return (new PaJaH()).replaceInnerHTML(a1, a2, a3); }
