/**
* Author Jonathan Samples
* Created September 9, 2008
*
* This class will serve to manage all of the pop-up DHTML windows that we use 
*/

function WindowManager(){
	this.numWindows = 0;
	this.windows = new Array(null);
	this.openWindowIndex = false;	
	this.hasArrow = true;
	this.lastAnchor = false;
	this.iFrameString = "";
	this.inRealIframe = true;			// only used in WhiteOutWindow which is an iframe solution specific call - hence default to true
	wm = this;
}

WindowManager.prototype.createNewCallOutWindow = function(contentEle, width, pointAt, closeable, orientation){
	this.numWindows++;
	var newWindow = new CallOutWindow(contentEle, this.numWindows, width, pointAt, closeable, orientation);
	this.windows.push(newWindow);
	
	// Return the index of the new window in the array for reference
	return this.numWindows;
}

WindowManager.prototype.createNewBlackOutWindow = function(contentEle, width, xPos, yPos, closeable){
	this.numWindows++;
	var newWindow = new BlackOutWindow(contentEle, this.numWindows, width, xPos, yPos, closeable);
	this.windows.push(newWindow);
	
	// Return the index of the new window in the array for reference
	return this.numWindows;
}

WindowManager.prototype.createNewWhiteOutWindow = function(contentEle, width, xPos, yPos, closeable){
	this.numWindows++;
	var newWindow = new WhiteOutWindow(contentEle, this.numWindows, width, xPos, yPos, closeable);
	this.windows.push(newWindow);
	
	// Return the index of the new window in the array for reference
	return this.numWindows;
}

WindowManager.prototype.showWindow = function(index){
	if(this.openWindowIndex && this.openWindowIndex != 0)
		this.windows[this.openWindowIndex].hide();
	this.openWindowIndex = index;	
	this.windows[index].show();
	this.hideSelectBoxes(index);
}

WindowManager.prototype.flyWindow = function(index, direction){
	var win = this.windows[index];
	var wh = win.windowHolderEle;
	
	win.position();
	var endX = wh.offsetLeft;
	var endY = wh.offsetTop;
	var startX = endX;
	var startY = -500;
	var scrollDim = getScrollOffset();
	var viewDim = getViewDimensions();
	
	switch(direction){
		case 1:
			startX = viewDim[0]+500;
			startY = endY;
			break;
		case 2:
			startX = endX;
			startY = viewDim[1]+scrollDim[1]+500;
			break;
		case 3:
			startX = -500;
			startY = endY;
			break;
		case 4:
			startX = endX;
			startY = -500;
			break;
	}
	
	var newPointTo = document.createElement("div");
	newPointTo.style.position = "absolute";
	newPointTo.style.height = "1px";
	newPointTo.style.width = "1px";
	newPointTo.style.left = startX+"px";
	newPointTo.style.top = startY+"px";
	document.body.appendChild(newPointTo);
	
	win.pointAt = newPointTo;
	win.show();
	
	moveDiv(wh.id, startX, startY, endX, endY, 20);
	moveDiv(win.shadowElt.id, startX-5, startY-4, endX-5, endY-5, 20);
	
}

WindowManager.prototype.hideWindow = function(index){
	this.openWindowIndex = false;
	this.showSelectBoxes(index);
	this.windows[index].hide();
}

WindowManager.prototype.closeOpenWindow = function(){
	if(this.openWindowIndex)
		this.hideWindow(this.openWindowIndex);
}


WindowManager.prototype.setPosition = function(index, xPos, yPos){
	this.windows[index].xPosition = xPos;
	this.windows[index].yPosition = yPos;
}

WindowManager.prototype.hideSelectBoxes = function(index){
	var cfwindow = this.windows[index];
	var ele = cfwindow.shadowElt?cfwindow.shadowElt:cfwindow.windowHolderEle;
	var winX1 = ele.offsetLeft;
	var winY1 = ele.offsetTop;
	var winX2 = winX1+ele.offsetWidth;
	var winY2 = winY1+ele.offsetHeight;
	
	var dropDowns = document.getElementsByTagName("select");
	for(var i = 0; i < dropDowns.length; i++){
		var ddPos = getAbsolutePosition(dropDowns[i]);
		var ddX1 = ddPos[0];
		var ddY1 = ddPos[1]; 
		var ddX2 = ddX1+dropDowns[i].offsetWidth;
		var ddY2 = ddY1+dropDowns[i].offsetHeight;
		// If they intersect then at least one corner must be under the window
		if((ddX1 > winX1 && ddX1 < winX2 && ddY1 > winY1 && ddY1 < winY2) ||
			(ddX1 > winX1 && ddX1 < winX2 && ddY2 > winY1 && ddY2 < winY2) ||
			(ddX2 > winX1 && ddX2 < winX2 && ddY1 > winY1 && ddY1 < winY2) ||
			(ddX2 > winX1 && ddX2 < winX2 && ddY2 > winY1 && ddY2 < winY2))
			dropDowns[i].style.visibility = "hidden";
	}
	
	var dropDowns = cfwindow.windowHolderEle.getElementsByTagName("select");
	for(var i = 0; i < dropDowns.length; i++)
		dropDowns[i].style.visibility = "visible";
}

WindowManager.prototype.showSelectBoxes = function(index){
	var dropDowns = document.getElementsByTagName("select");
	var unassociatedNameStart = "un_card_";
	for(var i = 0; i < dropDowns.length; i++)
	{
		if (dropDowns[i].id.substring(0, unassociatedNameStart.length) != unassociatedNameStart)
		{
			dropDowns[i].style.visibility = "visible";
		}	
	}	
}

WindowManager.prototype.setHasArrow = function(hasArrow)
{
	this.hasArrow = hasArrow;
}	

WindowManager.prototype.setInRealIframe = function(inIframe)
{
	this.inRealIframe = inIframe;
}	

WindowManager.prototype.setLastAnchor = function(lastAnchor)
{
	this.lastAnchor = lastAnchor;
}

WindowManager.prototype.hasLastAnchor = function()
{
	return (this.lastAnchor != false);
}	

WindowManager.prototype.setIframeMerchantString = function(merchantId)
{
	this.iFrameString = "/" + merchantId;
}	

/**
 * This is taken from truecode.blogspot.com, it allows for true inheritance
 */
//Defines the top level Class
function Class() { }
Class.prototype.construct = function() {};
Class.extend = function(def) {
  var classDef = function() {
      if (arguments[0] !== Class) { this.construct.apply(this, arguments); }
  };

  var proto = new this(Class);
  var superClass = this.prototype;

  for (var n in def) {
      var item = def[n];                      
      if (item instanceof Function) item.$ = superClass;
      proto[n] = item;
  }

  classDef.prototype = proto;

  //Give this new class the same static extend method    
  classDef.extend = this.extend;      
  return classDef;
};

/**
 * Constructor for the Window class
 * This will be the base class for all other window types
 * It creates the basic window DOM, subclasses will be responsible for tayloring
 * position, look, shadow, etc
 */
var Window = Class.extend({
	construct: function(contentEle, handle, width, xPos, yPos, closeable){
		// Init all the member variables
		this.windowHolderEle = document.createElement('div');
		this.shadowElt = false;
		this.contentEle = contentEle;
		this.width = width;
		this.closeable = closeable;
		this.xPosition = xPos;
		this.yPosition = yPos;
		this.id = "window_"+handle;
		this.handle = handle;
		if(this.contentEle)
			this.init();
	},
	
	init: function(){
			// Build the DOM for the window
		var windowHeight = document.body.clientHeight;
		var windowWidth = document.body.clientWidth;
		
		var window_holder = this.windowHolderEle;
		window_holder.className = "window_holder";
		window_holder.id = "wh_"+this.handle;
		document.body.appendChild(window_holder);
		
		// Create the elements and set their styles
		//var stylerDiv = document.createElement("div");
		//styleDiv.className = "window_gray";
		
		var topLeft = document.createElement("div");
		topLeft.className = "w_top_left";
		var topRight = document.createElement("div");
		topRight.className = "w_top_right";
		var topCenter = document.createElement("div");
		topCenter.className = "w_top_center";
		topCenter.id = "tc_"+this.handle;
		
		var midLeft = document.createElement("div");
		midLeft.className ="w_mid_left";
		midLeft.id = "ml_"+this.handle;
		var midRight = document.createElement("div");
		midRight.className = "w_mid_right";
		midRight.id = "mr_"+this.handle;
		var midCenter = document.createElement("div");
		midCenter.className = "w_mid_center";
		
		var botLeft = document.createElement("div");
		botLeft.className = "w_bot_left";
		var botRight = document.createElement("div");
		botRight.className = "w_bot_right";
		var botCenter = document.createElement("div");
		botCenter.className = "w_bot_center";
		botCenter.id = "bc_"+this.handle;
		
		// Add them to the window_holder
		window_holder.appendChild(topLeft);
		window_holder.appendChild(topCenter);
		window_holder.appendChild(topRight);
			
		window_holder.appendChild(midLeft);
		window_holder.appendChild(midCenter);
		window_holder.appendChild(midRight);
		
		window_holder.appendChild(botLeft);
		window_holder.appendChild(botCenter);
		window_holder.appendChild(botRight);
			
		if(this.closeable){
			var closeDiv = document.createElement("div");
			closeDiv.id = "pop_close_button_"+this.handle;
			closeDiv.className = "pop_close_button";
			closeDiv.onclick = new Function("wm.hideWindow("+this.handle+");");	
			if (wm.hasArrow)
			{
				window_holder.appendChild(closeDiv);
				closeDiv.style.left = "6px";
				closeDiv.style.top = "7px"; 
				midCenter.appendChild(this.contentEle);
			}
			else
			{
				midCenter.appendChild(closeDiv);
				var newMid = document.createElement("div");
				newMid.style.margin = "0px 29px 0px 0px";
				midCenter.appendChild(newMid);
				newMid.appendChild(this.contentEle);
			}	
		}
		else
		{
			midCenter.appendChild(this.contentEle);
		}	

		// Now we have to set all the heights and widths and stuff
		window_holder.style.width = this.width+"px";
		
		var centerWidth = this.width - topLeft.offsetWidth - topRight.offsetWidth;
		topCenter.style.width = centerWidth+"px";
		midCenter.style.width = centerWidth+"px";
		botCenter.style.width = centerWidth-9+"px";
		
		var midHeight = midCenter.offsetHeight;
		if(midHeight < midLeft.offsetHeight) midHeight = midLeft.offsetHeight;
		midLeft.style.height = midHeight+"px";
		midCenter.style.height = midHeight+"px";
		midRight.style.height = midHeight+"px";
		
		
		var shadow = this.getShadowElement(midHeight, centerWidth);
		if(shadow)document.body.appendChild(shadow);
	},
	
	position: function(){
		this.windowHolderEle.style.left = this.xPosition+"px";
		this.windowHolderEle.style.top = this.yPosition+"px";
	},

	show: function(){
		this.position();
		this.windowHolderEle.style.visibility = "visible";
		if(this.shadowElt)
			this.shadowElt.style.visibility = "visible";
		var dropDowns = this.windowHolderEle.getElementsByTagName("select");
		for(var i = 0; i < dropDowns.length; i++)
		{
			dropDowns[i].style.visibility = "visible";
			dropDowns[i].style.display = "";
		}	
	},

	hide: function(){
		this.windowHolderEle.style.visibility = "hidden";
		if(this.shadowElt)
			this.shadowElt.style.visibility = "hidden";
		var dropDowns = this.windowHolderEle.getElementsByTagName("select");
		for(var i = 0; i < dropDowns.length; i++)
		{
			dropDowns[i].style.visibility = "hidden";
			dropDowns[i].style.display = "none";
		}	
	},

	getShadowElement: function(midHeight, midWidth){
		return document.createElement("div");
	}
});

/**
* Class CallOutWindow inherits from Window
*/
var CallOutWindow = Window.extend({
	construct: function(contentEle, handle, width, pointAtId, closeable, orientation){
		// Init all the member variables
		this.windowHolderEle = document.createElement('div');
		this.contentEle = contentEle;
		this.width = width;
		this.orientation = orientation;
		this.prefOrient = orientation;
		this.closeable = closeable;
		this.xPosition = false;
		this.yPosition = false;
		this.pointAtId = pointAtId;
		this.id = "window_"+handle;
		this.handle = handle;
		this.init();
	},
	
	position: function(){
		// Now we have to figure out where to put the window, he he he	
		// First set the orientation to the desired orientation, then try to orient it.
		this.orientation = this.prefOrient;
		var pointerPad = document.getElementById("pointer_pad_"+this.handle);
		if(pointerPad)
			pointerPad.parentNode.removeChild(pointerPad);
			
		var pointAt = document.getElementById(this.pointAtId);
		var pointAtPos = getAbsolutePosition(pointAt);
		this.xPosition = pointAtPos[0];
		this.yPosition = pointAtPos[1];
		
		var closeDiv = document.getElementById("pop_close_button_"+this.handle);
		var pointerImg = document.createElement("img");
		var pointerPad = document.createElement("div");
		pointerPad.appendChild(pointerImg);
		pointerPad.id = "pointer_pad_"+this.handle;
		pointerPad.style.position = "absolute";
		
		this.windowHolderEle.appendChild(pointerPad);
		// Find the document height, and the scroll height
		var docHeight = getViewDimensions()[1];
		var scrollHeight = getScrollOffset()[1];
		var docWidth = document.body.clientWidth;	
		
		var midHeight = document.getElementById("ml_"+this.handle).offsetHeight;
		var midWidth = document.getElementById("tc_"+this.handle).offsetWidth;
		
		var winHeight = this.windowHolderEle.offsetHeight;
		var winWidth = this.windowHolderEle.offsetWidth;
		
		var pointerOffset = 0;
		var winPosX = this.xPosition;
		var winPosY = this.yPosition;
		var valid = false;
		var loopCount = 0;
		var loopMax = 5;
		var iFramePad = "";
		while(!valid){
			switch(this.orientation){
				case 1: // To Right
					//alert("trying to open on the right");
					this.xPosition = pointAtPos[0]+pointAt.offsetWidth;
					this.yPosition = pointAtPos[1]+pointAt.offsetHeight/2;
					
					var padMargin = (winHeight-30)/2;
					winPosX = this.xPosition;
					winPosY = (this.yPosition-winHeight/2-pointerOffset);
					
					// The window goes too far to the right
					if(winPosX+winWidth > docWidth){
						this.orientation = 3;
						if(loopCount < loopMax) break;
					}
					
					// The window goes too high
					if(winPosY-30 < scrollHeight){ // If out of the view push in if possible
						var diff = winPosY - scrollHeight;
						if(padMargin-30 + diff > 0){
							padMargin += diff;
							winPosY = scrollHeight;
						}
						else{ // If not possible try and open the window below.
							this.orientation = 2;
							if(loopCount < loopMax) break;
						}
					}
					// The window goes to long
					else if(winPosY+winHeight+50 > docHeight+scrollHeight){
						var diff = winPosY+winHeight - (docHeight+scrollHeight);
						if(diff < padMargin-50){
							padMargin += diff;
							winPosY -= diff;
						}
						else{ // if can't adjust the pointer, try to put it on top
							this.orientation = 4;
							if(loopCount < loopMax) break;
						}
					}
					
					pointerImg.src = "/includes/templates/master/images/round_corners" + wm.iFrameString + "/popup_triangle_l.png";
					pointerPad.style.top = padMargin+"px";
					pointerPad.style.left = 0;
					if(this.closeable && wm.hasArrow){
						closeDiv.style.top = padMargin-30+"px";
						closeDiv.style.left = "0px";
					}
					//pointerPad.style.left

					valid = true;
					break;
				case 2: // Under
					//alert("trying to open under");
				
					this.xPosition = pointAtPos[0]+pointAt.offsetWidth/2;
					this.yPosition = pointAtPos[1]+pointAt.offsetHeight;
				
					var padMargin = ((winWidth-35)/2);
					winPosX = this.xPosition - winWidth/2-pointerOffset;
					winPosY = this.yPosition;
					
					// Too long
					if(winPosY+winHeight > docHeight+scrollHeight){
						this.orientation = 4;
						if(loopCount < loopMax) break;
					}
					// too far left
					if(winPosX < 0){
						if(winPosX < padMargin-10){
							padMargin -= winPosX;
							winPosX = 0;
						}
						else{
							this.orientation = 1;
							if(loopCount < loopMax) break;
						}
					}
					// too far right
					else if(winPosX+winWidth > docWidth){
						var diff = winPosX+winWidth-docWidth;
						if(diff < padMargin-10){
							padMargin += diff;
							winPosX -= diff;
						}
						else{
							this.orientation = 3;
							if(loopCount < loopMax) break;
						}
					}
					
					pointerImg.src = "/includes/templates/master/images/round_corners" + wm.iFrameString + "/popup_triangle_t.png";
					pointerPad.style.left = padMargin+"px";
					pointerPad.style.top = 0;
					if(this.closeable && wm.hasArrow){
						closeDiv.style.top = "0";
						closeDiv.style.left = padMargin+40+"px";
					}
					valid = true;
					break;
					
				case 3: // to Left
					//alert("trying to open on the left");
					
					this.xPosition = pointAtPos[0];
					this.yPosition = pointAtPos[1]+pointAt.offsetHeight/2;
			
					var padMargin = (winHeight-30)/2;
					winPosY = (this.yPosition-winHeight/2-pointerOffset);
					winPosX = this.xPosition - (winWidth);
					
					// The window goes too far to the left
					if(winPosX < 0){
						this.orientation = 1;
						if(loopCount < loopMax) break;
					}
					
					// The window goes too high
					if(winPosY < scrollHeight){ // If out of the view push in if possible
						var diff = winPosY - scrollHeight;
						if(padMargin-30 + diff > 0){
							padMargin += diff;
							winPosY = scrollHeight;
						}
						else{ // If not possible try and open the window below.
							this.orientation = 2;
							if(loopCount < loopMax) break;
						}
					}
					// The window goes to long
					else if(winPosY+winHeight > docHeight+scrollHeight){
						var diff = winPosY+winHeight - (docHeight+scrollHeight);
						if(diff+50 < padMargin-10){
							padMargin += diff;
							winPosY -= diff;
						}
						else{ // if can't adjust the pointer, try to put it on top
							this.orientation = 4;
							if(loopCount < loopMax) break;
						}
					}
					pointerImg.src = "/includes/templates/master/images/round_corners" + wm.iFrameString + "/popup_triangle_r.png";
					pointerPad.style.top = padMargin+"px";
					pointerPad.style.left = (winWidth-17)+"px";
					if(this.closeable && wm.hasArrow){
						closeDiv.style.top = padMargin-30+"px";
						closeDiv.style.left = (winWidth-30)+"px";
					}
					valid = true;
					break;
				case 4: // on top
					//alert("trying to open on top");
					
					this.xPosition = pointAtPos[0]+pointAt.offsetWidth/2;
					this.yPosition = pointAtPos[1];
			
					var padMargin = (winWidth-35)/2;
					winPosX = this.xPosition - winWidth/2-pointerOffset;
					winPosY = this.yPosition - winHeight;
					// Too short
					if(winPosY < scrollHeight){
						this.orientation = 2;
						if(loopCount < loopMax) break;
						
					}
					// too far left
					if(winPosX < 0){
						if(winPosX < padMargin-10){
							padMargin -= winPosX;
							winPosX = 0;
						}
						else{
							this.orientation = 1;
							if(loopCount < loopMax) break;
						}
					}
					// too far right
					else if(winPosX+winWidth > docWidth){
						var diff = winPosX+width-docWidth;
						if(diff < padMargin-10){
							padMargin += diff;
							winPosX -= diff;
						}
						else{
							this.orientation = 3;
							if(loopCount < loopMax) break;
						}
					}
					pointerImg.src = "/includes/templates/master/images/round_corners" + wm.iFrameString + "/popup_triangle_b.png";
					pointerPad.style.top = winHeight-22+"px";
					pointerPad.style.left = padMargin+"px";
					if(this.closeable && wm.hasArrow){
						closeDiv.style.top = winHeight-33+"px";
						closeDiv.style.left = padMargin+40+"px";
					}
					
					valid = true;
					break;
			}
			loopCount++;
		}
		
		this.windowHolderEle.style.left = winPosX+"px";
		this.windowHolderEle.style.top = winPosY+"px";	
		
		var shadow = this.shadowElt;
		if(shadow){
			shadow.style.left = winPosX-5+"px";
			shadow.style.top = winPosY-4+"px";
		}
	},

	getShadowElement: function(midHeight, midWidth){
		return false;
		if(this.shadowElt)
			return this.shadowElt;
		
		midHeight -= 35;
		midWidth -= 35;
		var cornerWidth = 40;
		var shadow = document.createElement("div");
		shadow.className = "window_shadow";
		shadow.id = "sh_"+this.handle;
		shadow.style.height = (cornerWidth*2+midHeight)+"px";
		shadow.style.width = (cornerWidth*2+midWidth)+"px";
		
		var tl = document.createElement("div");
		tl.className = "tl";
		shadow.appendChild(tl);
		
		var tc = document.createElement("div");
		tc.className = "tc";
		tc.style.width = midWidth+"px";
		shadow.appendChild(tc);
		
		var tr = document.createElement("div");
		tr.className = "tr";
		shadow.appendChild(tr);
		
		var ml = document.createElement("div");
		ml.className = "ml";
		ml.style.height = midHeight+"px";
		shadow.appendChild(ml);
		
		var mc = document.createElement("div");
		mc.className = "mc";
		mc.style.height = midHeight+"px";
		mc.style.width = midWidth+"px";
		shadow.appendChild(mc);
		
		var mr = document.createElement("div");
		mr.className = "mr";
		mr.style.height = midHeight+"px";
		shadow.appendChild(mr);
		
		var bl = document.createElement("div");
		bl.className = "bl";
		shadow.appendChild(bl);
		
		var bc = document.createElement("div");
		bc.className = "bc";
		bc.style.width = midWidth+"px";
		shadow.appendChild(bc);
		
		var br = document.createElement("div");
		br.className = "br";
		shadow.appendChild(br);
		
		this.shadowElt = shadow;
		return shadow;
	}
});

/** 
* Class BlackOutWindow inherits from Window
*/

var BlackOutWindow = Window.extend({
	construct: function(contentEle, handle, width, xPos, yPos, closeable){
		// Init all the member variables
		this.windowHolderEle = document.createElement('div');
		this.shadowElt = false;
		this.contentEle = contentEle;
		this.width = width;
		this.closeable = closeable;
		this.xPosition = xPos;
		this.yPosition = yPos;
		this.id = "window_"+handle;
		this.handle = handle;
		this.init();
	},
	
	show: function(){
		arguments.callee.$.show.call(this); // Super class call
		document.body.style.overflow = "hidden";
	},
	
	hide: function(){
		arguments.callee.$.hide.call(this); // Super class call
		document.body.style.overflow = "auto";
	},
	
	// Simply positions the window in the center of the viewable area
	position: function(){
		var scrollDimension = getScrollOffset();
		var viewDimension = getViewDimensions();
		var tleft = (viewDimension[0]-this.width)/2+scrollDimension[0];
		if (tleft < 0)
		{
			tleft = 10;
		}	
		this.windowHolderEle.style.left = tleft +"px";
		
		var tright = (viewDimension[1]-this.windowHolderEle.offsetHeight)/2+scrollDimension[1];
		if (tright < 0)
		{
			tright = 10;
		}	
		
		if (wm.iFrameString.length > 0)
		{
			tright = 320;
			if (wm.hasLastAnchor())
			{
				var pointAt = document.getElementById(wm.lastAnchor);
				var pointAtPos = getAbsolutePosition(pointAt);

				var tright = pointAtPos[1];
				// alert(tright + " :: " + this.windowHolderEle.offsetHeight + " :: " + scrollDimension[1] + " :: " + viewDimension[1]);
				tright = pointAtPos[1] - (this.windowHolderEle.offsetHeight / 2) + 10;
				if (tright < 0)
				{
					tright = 10;
				}	
				if (tright + this.windowHolderEle.offsetHeight > viewDimension[1])
				{
					tright = viewDimension[1] - this.windowHolderEle.offsetHeight - 10;
				}	
			}
		}
		
		this.windowHolderEle.style.top = tright +"px";
		
		//shadow.style.width = viewDimension[0]+"px";
		var shadow = this.getShadowElement(0,0);
		if (shadow)
		{
			shadow.style.width = "100%";
			shadow.style.height = viewDimension[1]+"px";
			shadow.style.top = scrollDimension[1]+"px";
		}	
	},

	getShadowElement: function(midHeight, midWidth){
		if(this.shadowElt)
			return this.shadowElt;
		
		var shadow = document.createElement("div");
		shadow.className = "blackout_shadow";
		
		document.body.appendChild(shadow);
		this.shadowElt = shadow;
		
		return shadow;
	}
});

var WhiteOutWindow = BlackOutWindow.extend({
	construct: function(contentEle, handle, width, pointAtId, closeable){
		// Init all the member variables
		this.windowHolderEle = document.createElement('div');
		this.shadowElt = false;
		this.contentEle = contentEle;
		this.width = width;
		this.closeable = closeable;
		this.xPosition = 0;
		this.yPosition = 0;
		this.pointAtId = pointAtId;
		this.id = "window_"+handle;
		this.handle = handle;
		this.init();
	},
	
	// Simply positions the window in the center of the viewable area
	position: function(){
		// alert("white pos " + wm.inRealIframe);
		var pointAt = document.getElementById(this.pointAtId);
		var pointAtPos = getAbsolutePosition(pointAt);

		var scrollDimension = getScrollOffset();
		var viewDimension = getViewDimensions();
		var tleft = (viewDimension[0]-this.width)/2+scrollDimension[0];
		if (tleft < 0)
		{
			tleft = 10;
		}	
		this.windowHolderEle.style.left = tleft +"px";
		
		var scrollDimension = getScrollOffset();
		var viewDimension = getViewDimensions();

		var ttop = pointAtPos[1];
		if (!wm.inRealIframe)
		{
			ttop = (viewDimension[1]-this.windowHolderEle.offsetHeight)/2+scrollDimension[1];
		}	
		else
		{
			if ((ttop + this.windowHolderEle.offsetHeight + scrollDimension[1]) > viewDimension[1])
			{
				ttop = viewDimension[1] - this.windowHolderEle.offsetHeight - 10;
			}	
		}
		if (ttop < 0)
		{
			ttop = 10;
		}	

		this.windowHolderEle.style.top = ttop+"px";

		
		//shadow.style.width = viewDimension[0]+"px";
		var shadow = this.getShadowElement(0,0);
		if (shadow)
		{
			shadow.style.width = "100%";
			shadow.style.height = viewDimension[1]+"px";
			shadow.style.top = scrollDimension[1]+"px";
		}	
	}
});