/**
 * Dialogsteuerung
 * benötigt Spry.Utils!
 */

// TODO: ESC zum Schließen abfangen
// TODO: addEvent


var Dialog;

Dialog = function (id, title, type) {
	
	var self=this;
	
	this.id = id;
	this.title = title;
	this.type = type;
	
	this.dialog = null;
	this.dialogmask = null;
	
	
	Spry.Utils.addLoadListener(function() {self.myListener()});
	
	
};

Dialog.prototype.myListener = function () {
	
	this.initialize();
	
};

Dialog.prototype.initialize = function() {

	var self=this;
	this.dialog = document.getElementById(this.id);
	
	
	
	
	dialogheader = document.createElement('div');
	dialogheader.className = 'header';
	
	
	dialogtitle = document.createElement('div');
	
	//dialogtitle.setAttribute('class', 'title');
	dialogtitle.className = 'title';
	
	
	dialogtitletext = document.createTextNode(this.title);
	dialogtitle.appendChild(dialogtitletext);
	
	dialogclose = document.createElement('div');
	dialogclose.className = 'close';
	
	
	this.dialogmask = document.getElementById('dialog-mask');
	
	if (!this.dialogmask) {
		dialogmask = document.createElement('div');
		dialogmask.id = 'dialog-mask';
		
		document.body.appendChild(dialogmask);
		this.dialogmask = dialogmask;
	}
	
	this.dialog.style['display'] = 'none';
	this.dialogmask.style['display'] = 'none';
	
 

	dialogheader.appendChild(dialogtitle);
	dialogheader.appendChild(dialogclose);
	
	
	this.dialog.insertBefore(dialogheader, this.dialog.firstChild);
	
	Utils.addEventListener(dialogclose, 'click', function(e) {self.hide()} );

	
	
	
}


Dialog.prototype.show = function() {

	
	if (!this.dialog) {
		this.initialize();
	}
	
	this.dialog.style['display'] = 'block';
	this.dialogmask.style['display'] = 'block';
	
	  var width = this.pageWidth();
	  var height = this.pageHeight();
	  var left = this.leftPosition();
	  var top = this.topPosition();
	  var dialogwidth = this.dialog.offsetWidth;
	  var dialogheight = this.dialog.offsetHeight;
	  
	  
	  var topposition = (top /2) + (height / 2) - (dialogheight / 2);
	  var leftposition = (left / 2) + (width / 2) - (dialogwidth / 2);
	  
	 // alert('w' + width + ' h' + height + ' left' + left + ' top' + top + ' dialogw' + dialogwidth + ' dialogh' + dialogheight + ' toppos' + topposition + ' leftposition' + leftposition);
	  
	  
	  this.dialog.style.top = topposition + "px";
	  this.dialog.style.left = leftposition + "px";
	  
	  // TODO das stimmt nur, wenn wir schon nach unten gescrollt haben
	  this.dialogmask.style.height = height + this.dialog.offsetHeight + 50 + 'px';



	  
	  
	  
}

Dialog.prototype.hide = function() {
	this.dialog.style['display'] = 'none';
	this.dialogmask.style['display'] = 'none';
	
	try { 
		this.tmphide();	
	}
	catch (e) {}
	
	
}

// TODO funktion nicht fertig. funktioniert nur mit hide, und auch nur ein evthdl!!!
Dialog.prototype.addEvent = function(type, handle) {
	 
		if (type == 'hide') {
			this.tmphide = handle;		
		}
	
};



//calculate the current window width //
Dialog.prototype.pageWidth = function() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
Dialog.prototype.pageHeight = function() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
Dialog.prototype.topPosition = function() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
Dialog.prototype.leftPosition = function() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 
