/*[version]2008-04-22 20:02[/version]*/
/**
Requires: Acu.js, Geometry.js

Extreme Schwäche: wird Browser-Fenster verkleinert,
bleibt die Dokumentgröße gleich, da sie ja vom Interceptor bestimmt wird.
Möglichkeit der Korrektur wäre über die Veränderung in der Position der Scrollbar.

Sample-CSS-Klasse:
#interceptor{
	background-color: black;
	position: absolute;
	top: 0px;
	left: 0px;
	z-index: 3;
}

*/
// Optional: Event-Array, opacity (default: 0.75).
function APEInterceptor( obj /*, eventArray, opacity*/ ){
	if( typeof obj == "string" ) obj = document.getElementById( obj );
	if( ! obj ){
		throw "[APEInterceptor] Creating interceptor failed: no such DOM Element.\n";
	}
	this.domElement = obj;
	this.domId = this.domElement.id;
	this.opacity = 0;
	this.interceptEvents( arguments[1] );
	this.setOpacity( arguments[2] );

	var self = this;
	this.resizeHandler = function( e ){
		if( !e ) e = window.event;
		self.resize();
	};
}

APEInterceptor.DEFAULT_OPACITY = 0.75;
APEInterceptor.DEFAULT_EVENTS = ['click','focus','mousedown','mouseup','mousemove'];
APEInterceptor.FIXED_WIDTH = 0;
APEInterceptor.FIXED_HEIGHT = 0;

APEInterceptor.prototype.getOpacity = function(){
	return this.opacity;
};
APEInterceptor.prototype.setOpacity = function( opacity ){
	opacity = isNaN(opacity) ? APEInterceptor.DEFAULT_OPACITY : opacity;
	opacity = (opacity<0) ? 0 : opacity;
	opacity = (opacity>1) ? 1 : opacity;

	Acu.setOpacity( this.domElement, opacity );
	this.opacity = opacity;
};

APEInterceptor.prototype.interceptEvents = function( eventArray ){
	eventArray = eventArray ? eventArray : APEInterceptor.DEFAULT_EVENTS;

	var intercept = function( e ){
		if( !e ) e = window.event;

		if( e.stopPropagation ){
			e.stopPropagation();
		}else if( e.cabcelBubble ){
			e.cancelBubble = true;
		}else{
			return false;
		}
	};

	for( var i=0; i<eventArray.length; i++ ){
		Acu.registerEvent( this.domElement, eventArray[i], intercept );
	}
};

APEInterceptor.prototype.launch = function(){
	if( !Geometry.loaded ) Geometry.load();
	this.resize();
	this.domElement.style.display = '';

	Acu.registerEvent( window, "resize", this.resizeHandler );
	//Acu.registerEvent( window, "scroll", this.resizeHandler );
};

APEInterceptor.prototype.shutDown = function(){
	this.domElement.style.display = 'none';
	Acu.unregisterEvent( window, "resize", this.resizeHandler );
	//Acu.unregisterEvent( window, "scroll", this.resizeHandler );
};

APEInterceptor.prototype.resize = function(){
	// Breite dich über's ganze Dokument aus.
	// Ist der Viewport breiter oder höher, nehmen wir ihn als Maßstab.
	// Davor schrumpfen wir uns, damit wir die Dokumentbreite nicht beeinflussen.
	var dw = Geometry.getDocumentWidth();
	var dh = Geometry.getDocumentHeight();
	var vpw = Geometry.getViewportWidth();
	var vph = Geometry.getViewportHeight();
	var w = (dw<vpw) ? vpw : dw;
	var h = (dh<vph) ? vph : dh;
	this.domElement.style.width = w + "px";
	this.domElement.style.height = h + "px";
};
