/*
	window.js needs window.css
	© 2009 | WDS New Media GmbH
	Pascal Vorsmann
	
	Version 2010-09-01

*/
var global_depth=0;
var global_id = new Array();
var mydrag = new Array();
var MXcord = null;
var MYcord = null;
Event.observe(document, 'click', recordCoordinates);
	  
// Callback function to handle the event.
function recordCoordinates(event) {
  MXcord = Event.pointerX(event);
  MYcord = Event.pointerY(event);
}

// 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: 
			par = (global_id.length)-1;
			if(global_id[par]){
				//alert("My ID: "+global_id[par]);
				HideWindow(global_id[par]);
			}
		break;
	}
}	

// Funktion für die nächste freie Ebene 
function getNextHighestDepth(){
	global_depth+=10;
	return global_depth;
}

// Function zum Generieren eines Fensters
function DownloadCenter(url){
	// Fenstergröße ermitteln für Overlay
	pageDimensions = getPageDimensions();
	// Nächste freie Ebene bestimmen
	depth = getNextHighestDepth();
	global_id.push(depth);
	// Dummy-Code
	window_code = '<div id="zoom">'+
					'<table cellpadding="0" cellspacing="0" style="border:0px; width: 100%; height: 100%;" id="zoom_table">'+
						'<tbody>'+
							'<tr>'+
								'<td id="mm_'+depth+'" class="mm">'+
									'<div id="zoom_content">'+
										'<iframe id="iframe_'+depth+'" name="iframe_'+depth+'" src="about:blank" width="100%" height="100%" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true"></iframe>'+
									'</div>'+
				                '</td>'+
							'</tr>'+
						'</tbody>'+
					'</table>'+
					'<a style="position: absolute; top: 15px; left: 570px;" id="zoom_close" title="Fenster Schliessen" href="JavaScript:;" OnClick="HideWindow(\''+depth+'\'); return false;" class="more">'+
						'<img src="images/close.gif" width="18" height="18" border="0" />'+
					'</a>'+
				'</div>';
	// Window
	win = document.createElement('div');
	win.setAttribute('id', 'mywindow'+depth);
	//win.setAttribute((document.all ? 'className' : 'class'), 'mywindow');
	win.innerHTML = window_code;
	
	// Window In Seite nach Body einsetzen
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(win);
	
	// AB HIER KÖNNEN DANN ELEMENTE ÜBER DAS DOM ERGO PROTOTYPE ANGESPROCHEN WERDEN

	// Zindex für Overlay und Fenster
	zindexwin	= 1000+depth;
	zindexover	= 1000+(depth-1);
	// Overlay auf 100% höhe bringen
	$('overlayWeb').setStyle({ 'backgroundColor':'#ffffff', 'filter':'alpha(opacity=75)', '-moz-opacity':'0.75', 'opacity':'0.75', 'width':'100%', 'display':'block', 'top':'0px', 'left':'0px', 'position':'absolute', 'height': pageDimensions['height']+'px', 'zIndex':zindexover });
	
	$('mywindow'+depth).setStyle({ 'position':'absolute', 'display': 'block', 'width':'600px', 'height':'400px', 'top':'150px', 'left':'50%', 'marginLeft':'-300px', 'zIndex':zindexwin}); 
	$('mywindow'+depth).appear({ from: 0.0, to: 1.0, duration: 1.0 });
	$('zoom_close').setStyle({'textDecoration':'none','width':'18px', 'height':'18px'});
	// Iframe höhe Berechnen!
	$('iframe_'+depth).setStyle({'height':'400px'});
		
	// URL im Fenster laden!
	$('iframe_'+depth).src=url;
	
	// Abbruch per ESC starten
	monitorKeyboard();
}
function HideWindow(depth){
	// Prüfen nach Anzahl und ggf. Overlay stehen lassen und fenster nur löschen wenn es keines gibt das übergeordnet ist getNextHighestDepth-1>depth
	// Fenster und Overlay löschen
	Element.remove($('mywindow'+depth));
	$('overlayWeb').setStyle({ 'display':'none'});
	global_depth-=10;
	global_id.pop();
}


