/**************************************
 *
 * basis klasse fuer dialoge
 *
 *************************************/

var allWindows = new Array();
var setDiv = '';

function eWindow(settings)
{
	this.settings = settings;
	this.created = false;
	this.mx = 0;
	this.my = 0;
	this.ox = 0;
	this.oy = 0;
	this.maindiv = null;
	this.movediv = null;
	this.dragbar = null;
	this.window_body = null;

	if(this.settings && this.settings.body)
	{
		this.window_body = this.settings.body;
	}
}

eWindow.prototype.setTitle = function(title)
{
	this.settings.title = title;
}

eWindow.prototype.show = function()
{
	if(!this.created)
		this.create();

	new Effect.Appear("popup_" + this.settings.name);

	// Aktives Popup bekommen
	egotecSystem.activePopup = this.settings.name;

	allWindows[this.settings.name] = this;
}

eWindow.prototype.hide = function()
{
	new Effect.Fade("popup_" + this.settings.name);
}

eWindow.prototype.create = function()
{
	this.maindiv = document.createElement("div");
	this.maindiv.id = "popup_" + this.settings.name;
	this.maindiv.setAttribute("class", "popup");
	this.maindiv.className = "popup";
	this.maindiv.style.position = "absolute";
	this.maindiv.style.top = this.settings.top + "px";
	this.maindiv.style.left = this.settings.left + "px";
	this.maindiv.style.height = this.settings.height + "px";
	this.maindiv.style.width = this.settings.width + "px";
	this.maindiv.style.display = "none";
	this.maindiv.setAttribute("win_obj", "main_" + this.settings.name);
	this.maindiv.style.zIndex = "10000000";

	// Div welches verschoben wird
	this.movediv 		= document.createElement("div");
	this.movediv.id 	= "popup_move_" + this.settings.name;
	this.movediv.style.position 	= "absolute";
	this.movediv.style.top 			= this.settings.top + "px";
	this.movediv.style.left 		= this.settings.left + "px";
	this.movediv.style.height 		= this.settings.height + "px";
	this.movediv.style.width 		= this.settings.width + "px";
	this.movediv.style.border 		= "2px solid #BFB9B9";
	this.movediv.style.display		= "none";
	this.movediv.style.zIndex 		= "9999999";
	this.movediv.style.MozOpacity 	= "0.7"
	this.movediv.style.filter 		= "Alpha(opacity=70)"
	this.movediv.style.backgroundColor = "#fff";
	this.movediv.setAttribute("class", "popup");
	this.movediv.setAttribute("win_obj", this.settings.name);

	//resize Bild
	/*
	var resizeImg = document.createElement("img");
	resizeImg.style.position = 'absolute';
	resizeImg.style.bottom = '1px';
	resizeImg.style.right = '1px';
	resizeImg.style.cursor = 'se-resize';
	resizeImg.src = '../js/eTK/window/img/resize.gif';
	
	//resize Bild
	var closeImg = document.createElement("img");
	closeImg.src = '../js/eTK/window/img/close.gif';
	closeImg.width	= '16';
	closeImg.height	= '16';
	closeImg.style.position = 'absolute';
	closeImg.style.top 	= '1px';
	closeImg.style.right 	= '1px';
	closeImg.style.cursor 	= 'pointer';

	closeImg.onmouseover = function() { this.src = '../js/eTK/window/img/close_over.gif'; }
	closeImg.onmouseout = function() { this.src = '../js/eTK/window/img/close.gif'; }
	closeImg.setAttribute("win_obj", this.settings.name);
	closeImg.onclick = function(win) { egotecSystem.popupClose(this.getAttribute("win_obj")) }
	*/

	var innerobj = document.createElement("div");
	innerobj.id = "inner_popup_" + this.settings.name;
	innerobj.setAttribute("class", "inner_popup");
	innerobj.className = "inner_popup";

	this.dragbar = document.createElement("div");
	this.dragbar.id = "menu_title_" + this.settings.name;
	this.dragbar.setAttribute("class", "menu_titlepop");
	this.dragbar.className = "menu_titlepop";
	this.dragbar.setAttribute("win_obj", this.settings.name);
	this.dragbar.onmousedown = function(e) { start_drag(this.getAttribute("win_obj"), e); };

	var mt_title_obj = document.createElement("span");
	mt_title_obj.id = "mt_title_" + this.settings.name;
	mt_title_obj.setAttribute("class", "mt_title");
	mt_title_obj.className = "mt_title";
	mt_title_obj.innerHTML = this.settings.title;

	if(!this.window_body)
	{
		this.createBody();
	}
	this.window_body.create();

	this.dragbar.appendChild(mt_title_obj);
	innerobj.appendChild(this.dragbar);
	innerobj.appendChild(this.window_body.frame_body);
	//innerobj.appendChild(resizeImg);
	//innerobj.appendChild(closeImg);

	this.maindiv.appendChild(innerobj);

	document.body.appendChild(this.movediv);
	document.body.appendChild(this.maindiv);

	this.created = true;
}

eWindow.prototype.createBody = function()
{
	this.window_body = new eFrame(this.settings);
}

eWindow.prototype.start = function(e)
{ // drag n drop beginnt
	this.mx = document.all ? window.event.clientX : e.pageX;
	this.my = document.all ? window.event.clientY : e.pageY;

	this.movediv.style.zIndex++; // nach oben
	//this.ox = this.mx - this.maindiv.offsetLeft;
	this.ox = this.mx - this.movediv.offsetLeft;
	this.oy = this.my - this.movediv.offsetTop;
}

eWindow.prototype.drag = function(e)
{
	this.mx = document.all ? window.event.clientX : e.pageX;
	this.my = document.all ? window.event.clientY : e.pageY;

	//this.maindiv.style.left = (this.mx - this.ox) + "px";
	this.movediv.style.left = (this.mx - this.ox) + "px";
	this.movediv.style.top = (this.my - this.oy) + "px";
}

eWindow.prototype.stop = function(e)
{ // drag n drop hoert auf
	document.onmousemove = null;
	document.onmouseup = null;
}

eWindow.prototype.destroy = function()
{
	document.body.removeChild(this.maindiv);
}

/**************************************
 *
 * helper functions
 *
 **************************************/

function start_drag(win, e)
{
	// Ein Div auf den kompletten Bereich legen -> IE markiert sonst ab und zu alles
	var winDiv = document.createElement("div");
	winDiv.style.position 	= 'absolute';
	winDiv.style.width 		= '100%';
	winDiv.style.height 	= '100%';
	winDiv.style.top 		= '0px';
	winDiv.style.left 		= '0px';
	winDiv.style.zIndex 	= '9999998';
	winDiv.id	 			= 'windDiv';
	winDiv.style.MozOpacity = "0"
	winDiv.style.filter 	= "Alpha(opacity=0)"
	winDiv.style.backgroundColor = '#fff';

	document.body.appendChild(winDiv);

	// Das Move fenster anzeigen
	allWindows[win].movediv.style.display = '';
	allWindows[win].movediv.style.zIndex = '10000000';

	document.onmousemove = function(e) { move_drag(win, e); };
	document.onmouseup = function(e) { stop_drag(win, e); };
	allWindows[win].start(e);
}

function stop_drag(win, e)
{
	allWindows[win].maindiv.style.top = allWindows[win].movediv.style.top;
	allWindows[win].maindiv.style.left = allWindows[win].movediv.style.left;

	allWindows[win].stop(e);

	// Wieder die richtige Ordnunger der 2 Fenster herstellen
	allWindows[win].maindiv.style.zIndex = "10000000";
	allWindows[win].movediv.style.zIndex = "9999999";
	allWindows[win].movediv.style.display = "none";

	// gelegtes 100% Fenster wieder entfernen
	document.body.removeChild(document.getElementById('windDiv'));
}

function move_drag(win, e)
{
	allWindows[win].drag(e);
}

function hide_window(win)
{
	allWindows[win].hide();
}
