/**
 * 
 * @param resultid
 * @param url
 * @return
 *
 */
function ajax_update(resultid, url) {
	new Ajax.Request(url,
	{
		method:'POST',
		onSuccess: function(transport)
		{
			$(resultid).innerHTML = transport.responseText;
			transport.responseText.evalScripts();
			$(resultid).fire('ajax:update', { 'transport':transport });
		}
	});
}
/**
 * 
 * @param resultid
 * @param url
 * @return
 *
 */
function ajax_update_inputfields(resultid, url, inputid) {
	//alert($(inputid).value);
	var value = $(inputid).value;
	url = url+"&js="+value;
	new Ajax.Request(url,
			{
				method:'POST',
				onSuccess: function(transport)
				{
					$(resultid).value = transport.responseText;
					transport.responseText.evalScripts();
					$(resultid).fire('ajax:update', { 'transport':transport });
				}
			});
	
}

/**
 * 
 * @return
 */
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

/**
 * 
 * @return
 */
function Overlay()
{
	this.visible = false;
	this.shadowDuration = 0.5;
	this.shadowOpacity = 0.7;
	
}

/**
 * 
 */
Overlay.prototype.getInstance = function()
{
	if (!Overlay.instance)
	{
		Overlay.instance = new Overlay();
	}
	return Overlay.instance;
}

/**
 * 
 */
Overlay.prototype.initOverlay = function()
{
	this.shadow = document.createElement('DIV');
	this.shadow.id = 'overlay_shadow';
	this.shadow.setStyle({
		background: "#333"
	});
	document.body.appendChild(this.shadow);
	this.controldiv = document.createElement('DIV');
	this.controldiv.id = "overlay_control";
	document.body.appendChild(this.controldiv);	

	new Draggable(this.controldiv.id, { scroll: window });
	Element.observe(window, 'scroll', this.scrollEvent.bind(this));
	Element.observe(window, 'resize', this.scrollEvent.bind(this));
	Element.observe(this.shadow, 'click', this.hide.bind(this));
}

/**
 * 
 */
Overlay.prototype.scrollEvent = function()
{
	if (this.visible)
	{
		this.shadow.setStyle({
			width: getPageSizeWithScroll()[0]+"px", 
			height: getPageSizeWithScroll()[1]+"px"
		});
	}
}

/**
 * 
 */
Overlay.prototype.setupShadow = function()
{
	Element.absolutize(this.shadow);
	this.shadow.innerHTML = '&nbsp;'
	this.shadow.setStyle({
		left: 0, 
		top: 0, 
		zIndex: 900, 
		width: getPageSizeWithScroll()[0]+"px", 
		height: getPageSizeWithScroll()[1]+"px"
	});
}

/**
 * 
 */
Overlay.prototype.setupControl = function()
{
	var dimensions = Element.getDimensions(document.body);

	Element.absolutize(this.controldiv);
	this.controldiv.setStyle({
		backgroundColor: "#174D4B",
		height: "auto",
		zIndex: 901,
		top: "20px",
		left: (dimensions['width']/4) + "px",
		width: (dimensions['width']/2) + "px",
		border: "1px solid #123",
		padding: "10px"
	});
	this.controldiv.innerHTML ='<div style="float:right"><a href="javascript:hide_overlay()">schließen [X]</a></div><div style="clear:right;"></div><div id="overlay_content"></div>';
}

Overlay.prototype.setupControlColor = function(color)
{
	var dimensions = Element.getDimensions(document.body);

	Element.absolutize(this.controldiv);
	this.controldiv.setStyle({
		backgroundColor: color,
		height: "auto",
		zIndex: 901,
		top: "20px",
		left: (dimensions['width']/4) + "px",
		width: (dimensions['width']/2) + "px",
		border: "1px solid #123",
		padding: "10px"
	});
	this.controldiv.innerHTML ='<div style="float:right"><a href="javascript:hide_overlay()">schließen [X]</a></div><div style="clear:right;"></div><div id="overlay_content"></div>';
}


/**
 * 
 */
Overlay.prototype.updateData = function()
{
	ajax_update('overlay_content', this.url);	
}

/**
 * 
 */
Overlay.prototype.show = function(url)
{
	var myOverlay = Overlay.prototype.getInstance();
	if (!myOverlay.shadow)
	{
		myOverlay.initOverlay();
	}
	myOverlay.setupShadow();
	myOverlay.setupControl();

	myOverlay.url = url;
	myOverlay.updateData();
	myOverlay.shadow.hide();
	myOverlay.controldiv.hide();
	window.scrollTo(0, 0);
	new Effect.Appear(myOverlay.shadow, 
	{ 
		duration: myOverlay.shadowDuration/2, 
		from: 0.0, 
		to: myOverlay.shadowOpacity, 
		afterFinish: function ()
		{
			new Effect.Appear(myOverlay.controldiv, {from: 0.0, to: 1, duration: myOverlay.shadowDuration/2});
		}
	});
	myOverlay.visible = true;

}

Overlay.prototype.show_direct = function(url,color)
{
	
	var myOverlay = Overlay.prototype.getInstance();
	if (!myOverlay.shadow)
	{
		myOverlay.initOverlay();
	}
	myOverlay.setupShadow();
	myOverlay.setupControlColor(color);
	
	myOverlay.url = url;
	
	$('overlay_content').innerHTML = '<img src="'+url+'" />';
	myOverlay.shadow.hide();
	myOverlay.controldiv.hide();
	window.scrollTo(0, 0);
	new Effect.Appear(myOverlay.shadow, 
	{ 
		duration: myOverlay.shadowDuration/2, 
		from: 0.0, 
		to: myOverlay.shadowOpacity, 
		afterFinish: function ()
		{
			new Effect.Appear(myOverlay.controldiv, {from: 0.0, to: 1, duration: myOverlay.shadowDuration/2});
		}
	});
	myOverlay.visible = true;

}


/**
 * 
 */
Overlay.prototype.hide = function()
{
	var myOverlay = Overlay.prototype.getInstance();
	myOverlay.visible = false;
	
	Effect.Parallel([
		new Effect.Fade(myOverlay.controldiv, {from: 1, to: 0, duration: myOverlay.shadowDuration}),
		new Effect.Fade(myOverlay.shadow, {from: myOverlay.shadowOpacity, to: 0, duration: myOverlay.shadowDuration})
	 ], {sync: true});
	myOverlay.visible = false;
}

/**
 * 
 * @param url
 * @return
 */
function show_overlay(url)
{
	Overlay.prototype.show(url);
}

function show_direct_overlay(url,color)
{
	Overlay.prototype.show_direct(url,color);
}

/**
 * 
 * @return
 */
function hide_overlay()
{
	Overlay.prototype.hide();
}


/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};
var_dump = print_r;

function show_var(obj)
{
	var debug = print_r(obj);
	var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
	sOption+="scrollbars=yes,width=550,height=300,left=100,top=25";

	var winprint=window.open("","",sOption);
	if (winprint)
	{
		winprint.document.open();
		winprint.document.write('<html><body><pre>');
		winprint.document.write(debug);
		winprint.document.write('</pre></body></html>');
		winprint.document.close();
		winprint.focus(); 
	}
}

