/*
	window.js needs window.css
	© 2009 | WDS New Media GmbH
	Pascal Vorsmann
	
	Version 2011-01-24

*/
// Funktion zum ermitteln der Fenstergdimension für Overlays etc
function getPageDimensions() {
	var pageDimensions =[];
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageDimensions['height'] = windowHeight;
	} else { 
		pageDimensions['height'] = yScroll;
	}

	if(xScroll < windowWidth){	
		pageDimensions['width'] = windowWidth;
	} else {
		pageDimensions['width'] = xScroll;
	}
	return pageDimensions;
}
// Funktion um Keytasten abzufangen 
function monitorKeyboard() {
	document.onkeydown = eventKeypress.bind(this); 
}
// Funktion um auf ESC (27) zu reagieren
function eventKeypress(e){
	if (e == null) {
		var keycode = event.keyCode;
	} else {
		var keycode = e.which;
	}
	switch (keycode) { 
		case 27:
			HideWindow(); 
		break;
	}
}
function HideWindow() {
	new Effect.Parallel([
		new Effect.Appear('Popup_Layer', {from:1, to:0, sync:true}),
		new Effect.Move('Popup_Layer', {x: 0, y: -1000, sync: true})
	],  { duration: 1, afterFinish: function() { HideWindow2(); }.bind(this) });
}

function HideWindow2(){
	$('overlay').hide();
	Element.remove($('Popup_Layer'));
}

function showWindow(url,h){
	if(h==undefined)
		h=800;
	pageDimensions = getPageDimensions();
	$('overlay').setStyle({ 'display':'block', 'height': pageDimensions['height']+'px', 'zIndex':'1000' });

	// Dummy-Code
	window_code = '<table border="0" cellpadding="0" cellspacing="0" style="border:0px; width:100%; height:100%;">'+
						'<tr>'+
							'<td valign="top">'+
								'<iframe id="Popup_Iframe" src="about:blank" width="100%" height="100%" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true"></iframe>'+
			                '</td>'+
						'</tr>'+
					'</table>';
	// Window
	win = document.createElement('div');
	win.setAttribute('id', 'Popup_Layer');
	win.innerHTML = window_code;
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(win);
	$('Popup_Layer').setStyle({ 'display':'none', 'width':'100%', 'height':'100%', 'position':'absolute', 'marginTop':'-'+h+'px', 'zIndex':'1005' });
	
	// Iframe höhe Berechnen!
	$('Popup_Iframe').setStyle({'height':h+'px'});
		
	// URL im Fenster laden!
	$('Popup_Iframe').src=url;
	
	// Zum seitenstart Scrollen!
	$('toppage').scrollTo();

	// Nach Laden des Popups selbiges einblenden
	var firsttime = false;
	$('Popup_Iframe').observe('load', function(){
		if(firsttime==false){
			firsttime = true;
			new Effect.Parallel([
				new Effect.Appear('Popup_Layer', {sync:true}),
				new Effect.Move('Popup_Layer', {x: 0, y: h, sync: true})
			],  { duration: 1.5 });
		}
	});
		
	// Abbruch per ESC starten
	monitorKeyboard();
}


