/* [version]2007-12-25 18:27[/version] */
// Requires Module Acu.js
var Geometry = {};
Geometry.loaded = false;

Geometry.load = function(){
	if( window.screenLeft ){	// IE and others
		Geometry.getWindowX = function(){ return window.screenLeft; };
		Geometry.getWindowY = function(){ return window.screenTop; };
	}else if( window.screenX ){	// Firefox and others
		Geometry.getWindowX = function(){ return window.screenX; };
		Geometry.getWindowY = function(){ return window.screenY; };
	}

	if( window.innerWidth ){	// All browsers but IE
		Geometry.getViewportWidth = function(){ return window.innerWidth; };
		Geometry.getViewportHeight = function(){ return window.innerHeight; };
		Geometry.getHorizontalScroll = function(){ return window.pageXOffset; };
		Geometry.getVerticalScroll = function(){ return window.pageYOffset; };
	}else if( document.documentElement && document.documentElement.clientWidth ){
		// These functions are for IE 6 when there is a DOCTYPE
		Geometry.getViewportWidth = function(){ return document.documentElement.clientWidth; };
		Geometry.getViewportHeight = function(){ return document.documentElement.clientHeight; };
		Geometry.getHorizontalScroll = function(){ return document.documentElement.scrollLeft; };
		Geometry.getVerticalScroll = function(){ return document.documentElement.scrollTop; };
	}else if( document.body.clientWidth ){
		// These are for IE4, IE5 and IE6 without a DOCTYPE
		Geometry.getViewportWidth = function(){ return document.body.clientWidth; };
		Geometry.getViewportHeight = function(){ return document.body.clientHeight; };
		Geometry.getHorizontalScroll = function(){ return document.body.scrollLeft; };
		Geometry.getVerticalScroll = function(){ return document.body.scrollTop; };
	}

	// These functions return the size of the document. They are not window
	// related, but they are useful to have here anyway.
	if( document.documentElement && document.documentElement.scrollWidth ){
		Geometry.getDocumentWidth = function(){ return document.documentElement.scrollWidth; };
		Geometry.getDocumentHeight = function(){ return document.documentElement.scrollHeight; };
	}else if( document.body.scrollWidth ){
		Geometry.getDocumentWidth = function(){ return document.body.scrollWidth; };
		Geometry.getDocumentHeight = function(){ return document.body.scrollHeight; };
	}

	Geometry.loaded = true;

	//alert( 'done loading Geometry' );
};


// Zentriert ein absolut positioniertes Element auf dem Bildschirm,
// gegebenenfalls mit Korrektur um offsetX, offsetY (Argumente 4 & 5).
// Zentriert wird immer vertikal über dem Viewport, horizontal
// über dem Dokument. Dh. wurde nach rechts/links gescrollt, befindet
// sich das DOM-Element nicht in der Mitte.
// Um die Berechnung zu beschleuningen und falls bekannt, können die
// Höhe und Breite als 2. und 3. Argument übergeben werden.
Geometry.centerDomElement = function( obj ){
	var width = arguments[1] ? arguments[1] : 0;
	var height = arguments[2] ? arguments[2] : 0;
	var offsetX = arguments[3] ? arguments[3] : 0;
	var offsetY = arguments[4] ? arguments[4] : 0;

	if( typeof obj == "string" ) obj = document.getElementById( obj );
	if( !obj ) return;
	if( !Geometry.loaded ) Geometry.load();

	var totalWidth = (width > 0) ? width : Acu.getDomElementTotalWidth( obj );
	var totalHeight = (height > 0) ? height : Acu.getDomElementTotalHeight( obj );

	var maxLeft = Geometry.getDocumentWidth() - totalWidth;
	var maxTop  = Geometry.getDocumentHeight() - totalHeight;

	// Vertikal zentriere über dem Dokument.
	var left = Math.round( (Geometry.getDocumentWidth() - totalWidth) / 2 );
	var top = Geometry.getVerticalScroll() + Math.round( (Geometry.getViewportHeight() - totalHeight) / 2 );

	left = (left > maxLeft) ? maxLeft : left;
	left = (left < 0) ? 0 : left;
	top = (top > maxTop) ? maxTop : top;
	top = (top < 0) ? 0 : top;

	obj.style.left = left + 'px';
	obj.style.top = top + 'px';

	// Könnte man später mal was draus machen.
	//window.onscroll = centerPopup;
	//window.onresize = centerPopup;
};


