/**
 * 
 * @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 });
		}
	});
}

/**
 * 
 * @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: "#FFF",
		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.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);
}

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

