/*///////////////////////////////////////////////////////////////////////
//window.js
//Written by Electric Solutions GbR
//Copyright 2002-2009 by Electric Solutions GbR
//Author: R. Grosseck
//support@electric-solutions.de
//www.electric-solutions.de
///////////////////////////////////////////////////////////////////////*/

(function Dialog(){
	var isactive;					//Screen aktiv
	var dialogobj;							//HauptScreen Objekt
	var updatedivcontent;	//Bool um dialog_content auch bei resize anzupassen
}

(Dialog = {

	Init : function(screendiv) {		
		this.dialogobj=this.getObj(screendiv);
		addEvent(this.dialogobj, "click", 
			function(event){
				var e = (event.target) ? event.target : event.srcElement;
				if(e.getAttributeNode("id") && e.getAttributeNode("id").nodeValue == 'quickviewlayer')
				Dialog.HideDialog();
			}
		);
		this.dialogobj.style.height = document.getElementsByTagName("html")[0].scrollHeight + "px";
		this.isactive=false;
		this.updatedivcontent=false;
	},
	
	getObj : function (name) {
		if(browser.isIE && browser.version == 4)
		return document.all[name];
		else if(browser.isNS && browser.version == 4)
		return document.layers[name];
		else
		return document.getElementById(name);
	},

	ShowDialog : function(){
		this.UpdateDialog('scroll');
		if(browser.isIE){
			this._setVisible(this.dialogobj, true);
		}
		else{
			this._setVisible(this.getObj('dialog'), false);
			this.BlendDialog(this.dialogobj, 'in', 0);
		}
		this.isactive=true;
	},

	HideDialog : function(){
		if(browser.isIE)
		this._setVisible(this.dialogobj, false);
		else
		this.BlendDialog(this.dialogobj, 'out', 10);
	},
	
	BlendDialog : function(obj, t, s){
		if(t == 'out' && s <= 0){
			this._setVisible(this.dialogobj, false);
			this.isactive=false;
			this.dialogobj.innerHTML="";
			Dialog._setOpacity(this.dialogobj, 10);
		}
		else if(t == 'in' && s >= 10){
			this._setOpacity(this.dialogobj, 10);
			this._setVisible(this.getObj('dialog'), true);
		}
		else	{
			Dialog._setOpacity(this.dialogobj, s);
			if(t == 'in'){
				this._setVisible(this.dialogobj, true);
				s++;
			}
			else {
				this._setVisible(this.getObj('dialog'), false);
				s--;
			}
			window.setTimeout("Dialog.BlendDialog('"+this.dialogobj+"', '"+t+"',"+s+")", 50);
		}
	},

	getWinHeight : function(){
		var winheight = 0;
	  if (window.innerHeight) winheight = window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) 
			winheight = document.documentElement.clientHeight;
		else if (document.body && document.body.clientHeight) 
			winheight = document.body.clientHeight;		
		return winheight;
	},
	
	getWinWidth : function(){
	  var winwidth = 0;
	  if (window.innerWidth)
	  	winwidth = window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth) 
			winwidth = document.documentElement.clientWidth;
		else if (document.body && document.body.offsetWidth) 
			winwidth = document.body.offsetWidth;		
		return winwidth;
	},
	
	getDivHeight : function(obj){
	  var divheight =obj.offsetHeight;
		return divheight;
	},
	
	getDivWidth : function(obj){
	  var divwidth =obj.offsetWidth;
		return divwidth;
	},
	
	SetDivWidth : function(div, x){
		this.getObj(div).style.width=x+'px';
	},
	
	SetDivHeight : function(div, x){
		this.getObj(div).style.height=x+'px';
	},
	
	CenterDialogWindow : function(obj){
		var a=Math.round(this.getWinHeight()/2);
		var b=Math.round(parseInt(this.getDivHeight(obj))/2);;
		var c=a-b;
		obj.style.top=c+"px";
		if(browser.isIE && browser.version < 7){
			a=Math.round(this.getWinWidth()/2);
			b=Math.round(parseInt(this.getDivWidth(obj))/2);
			c=a-b;		
			obj.style.left=c+"px";
		}
	},
	
	SetDialogWidth : function(x){
		this.getObj('dialog').style.width=x;
	},
	
	SetDialogHeight : function(x){
		this.getObj('dialog').style.Height=x;
	},
	
	SetDialogContent : function(content){
		this.getObj('dialog_content').innerHTML = content;
		this.UpdateDialog('scroll');
	},
	
	DialogTemplate : function(title){
		var output;
		output="<div id=\"dialog\">";
		output+="<div id=\"dialog_header\">";
		output+="<div id=\"dialog_header_title\">" + title + "</div>";
		output+="<div id=\"dialog_header_close\"><a href=\"javascript:void(0)\" onclick=\"Dialog.HideDialog()\" title=\"Schließen\"><img src=\"/img/button_close.png\" border=\"0\"></a></div>";		
		output+="<div style=\"clear:both\"></div>";
		output+="</div>";		
		output+="<div id=\"dialog_content\">";	
		output+="placeholder";
		output+="</div>";
		output+="</div>";
		return output;
	},
	
	Dialog : function(title,text,width,height){
		this.dialogobj.innerHTML=this.DialogTemplate(title).replace(/placeholder/g, text);
		if(width)
		this.SetDialogWidth(width);
		if(height)
		this.SetDialogHeight(height);		
		this.CenterDialogWindow(this.getObj('dialog'));
		//this.SetDivHeight('dialog_content', this.getDivHeight(this.getObj('dialog'))-this.getDivHeight(this.getObj('dialog_header'))-10);
		this.ShowDialog();
	},

	ShowDialogError : function(error){
		var output, odiv;
		output="<div id=\"dialog\">";
		output+="<div id=\"dialog_header_error\">";
		output+="<div id=\"dialog_header_title\">Fehler</div>";
		output+="<div id=\"dialog_header_close\"><a href=\"javascript:void(0)\" onclick=\"Dialog.HideDialog()\"><img src=\"/img/button_close.png\" border=\"0\"></a></div>";		
		output+="<div style=\"clear:both\"></div>";
		output+="</div>";		
		output+="<div id=\"dialog_content\">";	
		output+="<div id=\"dialog_image\"><img src=\"images/icon_stop.png\" width=\"48\" height=\"48\"></div>";
		output+="<div id=\"dialog_text\">";
		output+="<p>" + error + "</p>";		
		output+="</div>";
		output+="<div style=\"clear:both\"></div>";
		output+="</div>";
		output+="<div id=\"dialog_button\"><input type=\"button\" name=\"close\" value=\"Schließen\" class=\"button\" onclick=\"Dialog.HideDialog()\"></div>";
		output+="</div>";		
		this.dialogobj.innerHTML=output;
		this.CenterDialogWindow(Dialog.getObj('dialog'));
		this.ShowDialog();		
	},
	
	setUpdateDivContent : function(bool){
		this.updatedivcontent=bool;	
	},
	
	UpdateDialog : function(type){
		if(type == 'scroll'){
			var d=document.getElementById("dialog");
			if(d){
				var a=Math.round(this.getWinHeight()/2);
				var b=Math.round(parseInt(this.getDivHeight(d))/2);
				var c=(browser.isIE == true ? document.documentElement.scrollTop : window.pageYOffset)+(a-b);
				d.style.top=c+"px";
			}
		}
		else {
			if(this.isactive == true){
				this.dialogobj.style.width=this.getWinWidth()+"px";
				this.dialogobj.style.height=document.getElementsByTagName("html")[0].scrollHeight + "px";
				this.CenterDialogWindow(this.getObj('dialog'));
			}
			if(this.updatedivcontent == true){
				this.SetDivHeight('dialog_content', this.getDivHeight(this.getObj('dialog'))-this.getDivHeight(this.getObj('dialog_header'))-10);
			}
		}
	},
	
	_setOpacity : function(obj, value){
		obj.style.opacity = value/10;
		obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	},
	
	_setVisible : function(obj, visible){
		if (browser.isNS && browser.version == 4)
		obj.visibility = visible == true ? "show" : "hide";
		else
		obj.style.visibility= visible == true ? "visible" : "hidden";
	}
}));

addEvent(window, "load", function(){Dialog.Init('quickviewlayer')});
addEvent(window, "resize", function(){Dialog.UpdateDialog('resize')});
addEvent(window, "scroll", function(){Dialog.UpdateDialog('scroll')});
