var baseZ = 777;
var currentWinCount = 0;

var WS_CLOSE = 0x0001;
var WS_MINIMIZE = 0x0002;
var WS_MAXIMIZE = 0x0004;

var parentContainer = null;
var imagePrefix = '';
var lastClick = new Position();

function setImagePrefix(prefix)
{	
	imagePrefix = prefix;		
}

function getImagePrefix()
{	
	return imagePrefix;
}

function Position()
{
	this.xPos = 100;
	this.yPos = 100;
}

function registerLastClickPosition()
{
	document.onclick= storeLastCoordinates;
}

function storeLastCoordinates( evt )
{
	var e = window.event ? window.event : evt;
	if (e.pageX || e.pageY) 	{
		lastClick.xPos = e.pageX;
		lastClick.yPos = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		lastClick.xPos = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		lastClick.yPos = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
}


function isAgentIE()
{
   if ( navigator.userAgent.indexOf("MSIE") < 0 )
   {
   		return false;
   }
   return true;
}

function isValid( argument )
{
	if ( typeof argument !== 'undefined' && argument !== null )
	{
		return true;
	}
	return false;
}


function getParentContainer()
{
	return ( parentContainer ) ? parentContainer : document.body;
}

function setParentContainer(pc)
{
	parentContainer = pc;
}

function getContainerFrame()
{
	return ( parentContainer ) ? parentContainer : document.body;
}


function EmbeddedWindow(title, winStyle, oContentDiv, x, y, w, h, product, contextPath)
{
	// save arguments

	this.title = title;
	this.oContent = oContentDiv;
	this.windowStyle = winStyle;

	this.windowContainer = document.createElement("div");
	this.windowContainer.style.position="absolute";
	this.windowContainer.style.width = parseInt(w) + "px";
	this.windowContainer.style.height = parseInt(h) + "px";
	this.windowContainer.style.left = x + "px";
	this.windowContainer.style.top = y + "px";
	this.windowContainer.id= "affWin-" + title;

	this.windowContainer.style.zIndex  = baseZ+currentWinCount;

	// link from the table to the EmbeddedWindow object
	this.windowContainer.theWindow = this;
	this.theWindow = this;

    this.closeIcon = null;

	// if anywhere in the table are is clicked, bring the window to front.
	this.windowContainer.onmousedown = EmbeddedWindow.prototype.onBringToFront;

	// append to document body
	getParentContainer().appendChild(this.windowContainer);
	
	// create table for window with title-bar and content
	this.windowBody = document.createElement("table");
	this.windowBody.className = "defaultWin";

	if (  isValid(top.frames["main"] )&& 
		( top.frames["main"].document.body !== getParentContainer() ))
	{
		this.windowBody.className = "defaultWinNoShadow";
	}
	
	this.windowBody.cellPadding=1;
	this.windowBody.cellSpacing=0;
	this.windowBody.border=0;
	this.windowBody.style.width= this.windowContainer.style.width;

	// add row for title bar
	this.titleRow = this.windowBody.insertRow(0);
	this.titleRow.className = "windowTitle";
	this.titleRow.theWindow = this;

	// title	
	var oTitleTD = this.titleRow.insertCell(0);
	oTitleTD.verticalAlign="center";	 
	oTitleTD.style.paddingLeft = "12px";	 
	if ( isAgentIE() == false )
	{
		oTitleTD.width="100%";
	}
	oTitleTD.innerHTML = title;
	oTitleTD.theWindow = this;
	this.titleCell = oTitleTD;
	
	this.windowContainer.setTitle = EmbeddedWindow.prototype.setTitle;
	
	oTitleTD.onmousedown = EmbeddedWindow.prototype.onMouseDown;
	
	// minimize
	winControlsTD = this.titleRow.insertCell(1);
	winControlsTD.theWindow = this;
	winControlsTD.style.textAlign="right";
	winControlsTD.onmousedown = EmbeddedWindow.prototype.onMouseDown;
	winControlsTD.style.paddingRight=0;
    
	if ( this.windowStyle == 0  )
	{
			var trans = document.createElement("img");
			var source = document.URL.indexOf(product);
			var sourcecontextPath = document.URL.indexOf(contextPath);
			if ( sourcecontextPath > 0)
				trans.src= document.URL.substring( 0, source ) + "blank.gif";
			else
				trans.src= document.URL.substring( 0, source ) + contextPath + "blank.gif";
			
			
			trans.height = 14;
			winControlsTD.appendChild( trans );
	}
	else
	{
			var winControls= document.createElement("table");
			winControls.border=0;
			var wctrlRow = winControls.insertRow(0)
			var wctrlCloseCell = wctrlRow.insertCell(0);
			wctrlCloseCell.style.paddingRight=0;

			if ( this.windowStyle & WS_CLOSE  )
			{
				// close
				var clsImg = document.createElement("img");
				var source = document.URL.indexOf(product);
				var sourcecontextPath = document.URL.indexOf(contextPath);
				if ( sourcecontextPath > 0)
					clsImg.src = document.URL.substring( 0, source ) + "close.gif";
				else
					clsImg.src = document.URL.substring( 0, source ) + contextPath + "/close.gif";
				clsImg.border=0;
				clsImg.title="schliessen";
				clsImg.style.cursor="pointer";
				clsImg.width=13;
				clsImg.height=13;
				wctrlCloseCell.appendChild( clsImg );
				wctrlCloseCell.onclick = EmbeddedWindow.prototype.onClose;
				wctrlCloseCell.theWindow = this;
			    this.closeIcon = wctrlCloseCell;
			}
			winControlsTD.appendChild( winControls );
	}	
	// add row for window content
	// a single cell the same width as the title bar row
	this.bodyRow = this.windowBody.insertRow(1);
	this.bodyRow.style.height= "100%";

	var oTD = this.bodyRow.insertCell(0);
	oTD.colSpan = 2;

	this.contentCanvas = document.createElement("div");
	this.contentCanvas.style.border=0;
	this.contentCanvas.style.width = "100%";
	this.contentCanvas.style.height = "100%";
	this.contentCanvas.style.position="relative";
	this.contentCanvas.className="windowCanvas";

	oTD.appendChild( this.contentCanvas );
	
	this.contentCanvas.appendChild(this.oContent);

	this.windowContainer.appendChild(this.windowBody);

	currentWinCount++;

	return this.windowContainer;
}

EmbeddedWindow.prototype.setTitle = function(title)
{
	if ( this.theWindow == null ) return;
	this.theWindow.titleCell.innerHTML = title;
}

EmbeddedWindow.prototype.onBringToFront = function()
{
	this.theWindow.bringToFront();
}

EmbeddedWindow.prototype.bringToFront = function()
{
	   for ( i = 0; i < getParentContainer().childNodes.length; i++ )
	   {
			var thisNode = getParentContainer().childNodes[i];
			if (	thisNode.style != null 
					&&  thisNode.id.indexOf( "affWin") >= 0 
					&& thisNode.id.indexOf( "Debug") < 0 
					&&  thisNode != this.windowContainer )
			{
				var thisZ = getParentContainer().childNodes[i].style.zIndex;
				var newZ = Math.max( thisZ+1, this.windowContainer.style.zIndex );
				newZ  = Math.min( newZ , (baseZ + 1000));
				this.windowContainer.style.zIndex = newZ;
			}
	}
}

EmbeddedWindow.prototype.onMouseDown = function()
{
	if ( this.theWindow == null ) return;
	this.theWindow.mouseDown();
}

EmbeddedWindow.prototype.mouseDown = function()
{
	// record that an onmousedown has just occurred
	if ( this.isMaximized )
	{
		return;
	}

	this.bDown = true;

//	this.oContent.style.display="none";

	// link from body to this EmbeddedWindow object
	getParentContainer().theWindow = this;

	// save body mouse handlers
	this.saveMouseMove = getParentContainer().onmousemove;
	this.saveMouseUp = getParentContainer().onmouseup;

	// set new handlers.
	getParentContainer().onmousemove = EmbeddedWindow.prototype.bodyOnMouseMove;
	getParentContainer().onmouseup = EmbeddedWindow.prototype.bodyOnMouseUp;
}

EmbeddedWindow.prototype.bodyOnMouseMove = function(evt)
{
	if ( this.theWindow == null ) return;
	var e = window.event ? window.event : evt;
	this.theWindow.onMouseMove(e);
}

EmbeddedWindow.prototype.onMouseMove = function(evt)
{
	// if mouse not down, stop the move (for IE only)
	if ( (document.all) && !(evt.button & 1) )
	{
		this.onMouseUp();
		return;
	}
	if ( this.bDown )
	{
		this.dx = parseInt(this.windowContainer.style.left, 10) - evt.clientX;
		this.dy = parseInt(this.windowContainer.style.top, 10) - evt.clientY;
		this.bDown = false;
	}
	else
	{
		this.windowContainer.style.left = Math.max((this.dx + evt.clientX),0) + "px";
		this.windowContainer.style.top = Math.max((this.dy + evt.clientY),0) + "px";
		this.saveX = this.windowContainer.style.left;
		this.saveY = this.windowContainer.style.top;
	}
}

EmbeddedWindow.prototype.bodyOnMouseUp = function()
{
	if ( this.theWindow == null ) return;
	this.theWindow.onMouseUp();
}

EmbeddedWindow.prototype.onMouseUp = function()
{
	this.oContent.style.display="";

	getParentContainer().onmouseup = this.saveMouseUp;
	getParentContainer().onmousemove = this.saveMouseMove;
	getParentContainer().theWindow = null;
}

EmbeddedWindow.prototype.close = function()
{	
	// remove content from browser document
	this.oContent.parentNode.removeChild(this.oContent);
	
	// remove from browser document
	this.windowContainer.parentNode.removeChild(this.windowContainer);

	currentWinCount--;
}

EmbeddedWindow.prototype.onClose = function()
{
	this.theWindow.close();
}




function createWindow( url, title, x, y, w, h, winStyle )
{
	var frame = document.createElement("iframe");
	frame.frameBorder=0;
	frame.marginWidth=0;
	frame.marginHeight=0;
	frame.style.border=0;
    frame.scrolling="auto";
	frame.style.width = "100%";
	frame.style.height = "100%";
	frame.src=url;

	var useStyle = winStyle ? winStyle : (WS_CLOSE | WS_MINIMIZE );

	var newWin = new EmbeddedWindow(title, useStyle , frame, parseInt(x), parseInt(y), parseInt(w), parseInt(h) );
	newWin.theWindow.windowBody.style.filter = null;

	return newWin;
}


function createWindowFromDiv(  contentDiv , left, top, width, height, product, contextPath)
{
        var newWin = new EmbeddedWindow("Bitte w&auml;hlen:", WS_CLOSE, contentDiv, left, top, width, height, product, contextPath);
//        setSinglePopup( newWin );
        return newWin;
}



