var OverlayWindow = Class.create({

	log: function(what) {
		if (!Object.isUndefined(site)) { site.log('OverlayWindow: '+what); }
	},

	initialize: function(divId,options) {
		// Options
		this.options = Object.extend({
			autoPositionOnEvent:	true,
			positionTop:			undefined,
			positionLeft:			undefined,
			minimumiSpacingTop:		0,
			overlayId:				divId+'_overlay'
		}, options || {});

		this.body = $$('BODY')[0];
		this.div = $(divId);

		if (undefined===this.div) {
			this.log('Unable to locate #'+divId);
			return false;
		}

		// Construct overlay
		this.overlay = new Klean.Overlay(divId+'_overlay',{
			background:		'#000000',
			observeResize:	false	// We'll do this ourselves
		});

		// Keep bound functions, so we can stop observing events based on them
		this.onResizeBound = this.onResize.bindAsEventListener(this);
		this.onScrollBound = this.onScroll.bindAsEventListener(this);

		this.lastDivLeft = undefined;
		this.lastDivTop = undefined;

		this.wrapper = new Element('div');
		this.wrapper.setStyle({
			'zIndex':	999,
			'position':	'absolute',
			'left':		0,
			'top':		0
			// 'backgroundColor': '#fff'
		});
		this.wrapper.hide();
		this.div.wrap(this.wrapper);

		this.div.setStyle({
			'margin':	'0 auto'
		});
		this.div.show(); // Backwards compatability
	},

	onResize: function(e) {
		// this.log('onResize()');
		this.position(true);
	},

	onScroll: function(e) {
		// this.log('onScroll()');
		this.position(true);
	},

	position: function(move) {

		// Div size
		var divWidth = this.div.getWidth();
		var divHeight = this.div.getHeight();
		var divTop = 0;
		var divLeft = 0;

		// Calculate position
		var offset = document.viewport.getScrollOffsets();
		var viewportDim = document.viewport.getDimensions();
		var bodyDim = this.body.getDimensions();

		var pageWidth = viewportDim.width;
		var pageHeight = Math.max(viewportDim.height, bodyDim.height);

		// Adjust wrapper width so container element is centered
		this.wrapper.setStyle({
			'width':	''+viewportDim.width+'px'
		});

		var position = 'fixed';
		if (this.options.positionTop !== undefined) {
			position = 'absolute';
			divTop = this.options.positionTop;
		} else {
			divTop = Math.floor((viewportDim.height-divHeight)*0.5);

			// IE6 "position: fixed" workaround
			if (Prototype.Browser.IE && (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))<=6)) {
				position = 'absolute';
				divTop += offset.top;
			}
		}

		this.wrapper.setStyle({
			'position': position,
			'top': ''+divTop+'px'
		});

		// Resize overlay?
		pageWidth = Math.max(pageWidth,divWidth+divLeft);
		pageHeight = Math.max(pageHeight,divHeight+divTop);
		if ((this.overlay.width != pageWidth) ||
			(this.overlay.height != pageHeight)) {
			this.log('Overlay needs resizing from '+this.overlay.width+'x'+this.overlay.height+' to '+pageWidth+'x'+pageHeight);
			this.overlay.setSize(pageWidth,pageHeight);
		}
	},
	show: function() {
		this.overlay.show();

		// Allow placement and measuring
		this.wrapper.removeClassName('hidden');
		this.wrapper.setOpacity(0.0001);
		this.wrapper.show();

		// Position it
		this.position(false);

		// Poke sIFR?
		if (!Object.isUndefined(sifr)) { sifr.refresh(); }

		// Poke windowResizer?
		if (!Object.isUndefined(windowResizer)) { windowResizer.resize([this.div]); }

		// Appear when callstack is empty (sIFR done etc.)
		this.show2.bind(this).defer();
	},
	show2: function() {
		this.wrapper.appear({
			duration: 0.3
		});
		this.log('startObserving');
		Event.observe(window,'scroll',this.onScrollBound);
		Event.observe(window,'resize',this.onResizeBound);
	},
	hide: function() {
		this.log('stopObserving');
		Event.stopObserving(window,'scroll',this.onScrollBound);
		Event.stopObserving(window,'resize',this.onResizeBound);

		this.wrapper.hide();
		this.overlay.hide();
	}
});

