
/**
 * sosWindow Klasse (Popup ersatz)
 * @author mor_dark
 */
var all_sos_windows = new Array();

/**
 * Window Construct
 * settings
 	name => Name
 	title => Titel in der Titelleiste
 	body => HTML
 	width => Integer
 	height => Integer
 */
function sosWindow(settings)
{
	this.settings = settings;
	this.x;
	this.y;
	
	this.oDiv = null;
	this.oDivShadow = null;
	this.oDivTitle 	= null;
	this.oDivBody 	= null;
	this.oClose 	= null;
	this.oMoveable 	= null;
	this.oDivLoader = null;
	
	this.move = false;
	
	this.tempX;
	this.tempY;
}

/**
 * Erstellt ein Window - Popup �hnlich
 */
sosWindow.prototype.create = function()
{
	this.oDiv = document.createElement('div');
	this.oDivShadow = this.oDiv.cloneNode(true);
	this.oDivTitle = this.oDiv.cloneNode(true);
	this.oDivBody = this.oDiv.cloneNode(true);
	
	var oDivShadow_rd = this.oDiv.cloneNode(true);
	var oDivShadow_lt = this.oDiv.cloneNode(true);
	var oDivShadow_bl = this.oDiv.cloneNode(true);
	var oDivShadow_rt = this.oDiv.cloneNode(true);

	this.oDivLoader = this.oDiv.cloneNode(true);
	this.oDivLoader.className = 'sosWindowLoader';	
	
	this.oClose = document.createElement('img');
	
	this.oDiv.className = 'sosWindow';
	this.oDiv.style.position = 'absolute';
	this.oDiv.style.visibility = 'hidden';
	this.oDiv.style.zIndex = 10000;
	
	if(this.settings.height)
	{
		this.oDiv.style.height = this.settings.height + 'px';
		this.oDivBody.style.height = this.settings.height-20 + 'px';
	}
	
	if(this.settings.width)
	{
		this.oDiv.style.width = this.settings.width + 'px';
		this.oDivBody.style.width = this.settings.width + 'px';
	}
	
	if (window.innerHeight)
	{
		var maxHeight = window.innerHeight;
		var maxWidth = window.innerWidth;
	} else 	
	{
		var maxHeight = document.body.clientHeight;
		var maxWidth = document.body.clientWidth;
	}
	
	this.oDivTitle.className = 'sosWindowTitle';
	this.oDivTitle.innerHTML = '<span>'+this.settings.title+'</span>';
	this.oDivTitle.style.cursor = 'move';
	this.oDivTitle.style.height = '20px';
	this.oDivTitle.style.width = this.oDiv.style.width;
	
	this.oDivBody.className = 'sosWindowBody';
	
	if(this.settings.body)
		this.setBody(this.settings.body);
	
	this.oClose.src = _sosSystem.settings.sTK + 'window/images/close.png';
	this.oClose.style.position = 'absolute';
	this.oClose.style.right = '5px';
	this.oClose.style.top = '2px';
	this.oClose.style.cursor = 'pointer';
	this.oClose.style.zIndex = 1000;
	
	var self = this;
	this.oClose.onclick = function()
	{
		self.close();
	}

	this.oDiv.appendChild(this.oClose);
	this.oDiv.appendChild(this.oDivTitle);
	this.oDiv.appendChild(this.oDivBody);

	this.oDivTitle.onmousedown = function(event)
	{
		self.onmousedown(event);
	}
	
	this.oDivTitle.onmouseup = function(event)
	{
		self.onmouseup(event);
	}
	
	all_sos_windows[this.settings.name] = this;
	
	// ins body einbinden
	document.body.appendChild(this.oDiv);
	document.body.appendChild(this.oDivLoader);
	
	// POS
	this.y = _sosSystem.getScrollXY()[1]+100;
	this.x = ((maxWidth-this.oDiv.offsetWidth) / 2);
	
	this.oDiv.style.top = this.y + 'px';
	this.oDiv.style.left = this.x + 'px';
	
	// Schatten
	this.oDivShadow.style.position = 'absolute';
	this.oDivShadow.style.visibility = 'hidden';
	this.oDivShadow.style.zIndex = 9999;
	this.oDivShadow.style.width = this.oDiv.offsetWidth + 14 + "px";
	this.oDivShadow.style.height = this.oDiv.offsetHeight + 14 + "px";
	this.oDivShadow.style.top = this.y-7 + 'px';
	this.oDivShadow.style.left = this.x-7 + 'px';
	
	oDivShadow_rd.style.backgroundImage = "url('" + _sosSystem.settings.sTK + "window/images/shadow.png')";
	oDivShadow_rd.style.backgroundRepeat = "no-repeat";
	oDivShadow_rd.style.backgroundPosition = "bottom right";
	oDivShadow_rd.style.position = "absolute";
	oDivShadow_rd.style.right = "0px";
	oDivShadow_rd.style.bottom = "0px";
	oDivShadow_rd.style.width = this.oDiv.offsetWidth+7 +"px";
	oDivShadow_rd.style.height = this.oDiv.offsetHeight+7 +"px";
	oDivShadow_rd.innerHTML = "&nbsp;"
	
	oDivShadow_lt.style.backgroundImage = "url('" + _sosSystem.settings.sTK + "window/images/shadow_l.png')";
	oDivShadow_lt.style.backgroundRepeat = "no-repeat";
	oDivShadow_lt.style.backgroundPosition = "top left";
	oDivShadow_lt.style.position = "absolute";
	oDivShadow_lt.style.left = "0px";
	oDivShadow_lt.style.top = "0px";
	oDivShadow_lt.style.width = this.oDiv.offsetWidth+7 +"px";
	oDivShadow_lt.style.height = this.oDiv.offsetHeight+7 +"px";
	oDivShadow_lt.innerHTML = "&nbsp;"
	
	oDivShadow_bl.style.backgroundImage = "url('" + _sosSystem.settings.sTK + "window/images/shadow_bl.png')";
	oDivShadow_bl.style.backgroundRepeat = "no-repeat";
	oDivShadow_bl.style.backgroundPosition = "bottom left";
	oDivShadow_bl.style.position = "absolute";
	oDivShadow_bl.style.left = "0px";
	oDivShadow_bl.style.bottom = "0px";
	oDivShadow_bl.style.width = "8px";
	oDivShadow_bl.style.height = "8px";
	oDivShadow_bl.innerHTML = "&nbsp;"
	
	oDivShadow_rt.style.backgroundImage = "url('" + _sosSystem.settings.sTK + "window/images/shadow_rt.png')";
	oDivShadow_rt.style.backgroundRepeat = "no-repeat";
	oDivShadow_rt.style.backgroundPosition = "top right";
	oDivShadow_rt.style.position = "absolute";
	oDivShadow_rt.style.right = "0px";
	oDivShadow_rt.style.top = "0px";
	oDivShadow_rt.style.width = "8px";
	oDivShadow_rt.style.height = "8px";
	oDivShadow_rt.innerHTML = "&nbsp;"
	
	this.oDivShadow.appendChild(oDivShadow_lt);
	this.oDivShadow.appendChild(oDivShadow_bl);
	this.oDivShadow.appendChild(oDivShadow_rd);
	this.oDivShadow.appendChild(oDivShadow_rt);
	
	document.body.appendChild(this.oDivShadow);
	
	this.oDiv.style.visibility = 'visible';
	this.oDivShadow.style.visibility = 'visible';
}

/**
 * Setzt den Window Body
 */
sosWindow.prototype.setBody = function(html)
{
	this.oDivBody.innerHTML = html;
}

/**
 * Schlie�t das Fenster
 */
sosWindow.prototype.close = function()
{
	this.oDiv.parentNode.removeChild(this.oDiv);
	this.oDivShadow.parentNode.removeChild(this.oDivShadow);
	this.oDivLoader.parentNode.removeChild(this.oDivLoader);
	
	all_sos_windows[this.settings.name] = null;
	
	
	var msg = 'Fenster wurder geschlossen 1!';
			
		_info = new sosInfo({text : msg});
		_info.create();
	
	
	
}

/**
 * Verschiebt das Fenster
 */
sosWindow.prototype.onmousemove = function(event)
{
	if(this.move == true)
	{
		this.mousePos(event);
		
		this.oMoveable.style.top = this.y - this.tempY + "px"; 
		this.oMoveable.style.left = this.x - this.tempX + "px";
	}
}

/**
 * OnMouseDown Event
 */
sosWindow.prototype.onmousedown = function(event)
{
	if(this.move == false)
	{
		this.oMoveable = document.createElement('div');
		var oMoveableStyle = this.oMoveable.style;
		oMoveableStyle.width = this.oDiv.offsetWidth + "px";
		oMoveableStyle.height = this.oDiv.offsetHeight + "px";
		oMoveableStyle.position = "absolute";
		oMoveableStyle.top = this.oDiv.style.top;
		oMoveableStyle.left = this.oDiv.style.left;
		oMoveableStyle.zIndex = 10002;
		oMoveableStyle.border = "2px solid #CFD4E6";
		oMoveableStyle.backgroundColor = "#fff";
		oMoveableStyle.cursor = "pointer";
		
		document.body.appendChild(this.oMoveable);
		
		var self = this;
		document.onmousemove = function(event)
		{
			self.onmousemove(event);
		}
		
		document.onmouseup = function(event)
		{
			self.onmouseup(event);
		}
		
		if (_sosSystem.isMSIE) 
		{
			this.tempX = window.event.clientX + document.body.scrollLeft - this.x;
			this.tempY = window.event.clientY + document.body.scrollTop - this.y;
	  	} else 
	  	{
	  		this.tempX = event.pageX - this.x;
			this.tempY = event.pageY - this.y;
	 	}
		this.mousePos(event);
	}
	
	this.move = true;
}

/**
 * OnMouseDown Event
 */
sosWindow.prototype.onmouseup = function(event)
{
	this.move = false;
	
	this.oDiv.style.top = this.oMoveable.style.top;
	this.oDiv.style.left = this.oMoveable.style.left;
	
	this.oDivShadow.style.top = parseInt(this.oMoveable.style.top) - 7 + "px";
	this.oDivShadow.style.left = parseInt(this.oMoveable.style.left) - 7 + "px";
	
	this.oMoveable.parentNode.removeChild(this.oMoveable);
	this.oMoveable = null;
	
	this.x = this.x - this.tempX;
	this.y = this.y - this.tempY;
	
	document.onmousemove = null;
	document.onmouseup = null;
}

/**
 * Setzt die Mausposition
 */
sosWindow.prototype.mousePos = function(event)
{
	this.x = document.all ? window.event.clientX : event.pageX;
	this.y = document.all ? window.event.clientY : event.pageY;
	
	/*
	if (_sosSystem.isMSIE) 
	{
		this.x = event.clientX + document.body.scrollLeft;
		this.y = event.clientY + document.body.scrollTop;
  	} else 
  	{
  		this.x = event.pageX;
		this.y = event.pageY;
 	}
 	*/
}
