﻿	function DSWin()
	{
		var me = this;
		var _window = null;
		var _windowContent = null;

		var _elementID = null;
		var _elementParent = null;
		var _Clicked = false;

		var WindowClick = function(e)
		{
			_Clicked = true;
			//Event.stop(e);
		}

		// {elementid, relativeElementID, width, height, dock, offsetX, offsetY}
		me.Show = function(params,e)
		{	
		
			// first hide anythign that is currently showing...
			me.Hide();
			
			_elementID = params.elementID;
			
			var element = $(params.elementID);
			_elementParent = element.parentNode;
			
			//_windowContent.appendChild(element);
			
			// if width wasn't provided, try and get it from the element.
			if(!params.width)
			{
				params.width = Element.getDimensions(element).width;
				
				if (Element.getStyle(element,"border-left-width") != "medium")
				{
					params.width += parseInt(Element.getStyle(element,"border-left-width").replace("px",""));
				}

				if (Element.getStyle(element,"border-right-width") != "medium")
				{
					params.width += parseInt(Element.getStyle(element,"border-right-width").replace("px",""));
				}
			} 

			if(!params.height)
			{
				params.height = Element.getDimensions(element).height;
				
				if (Element.getStyle(element,"border-top-width") != "medium")
				{
					params.height += parseInt(Element.getStyle(element,"border-top-width").replace("px",""));
				}
				
				if (Element.getStyle(element,"border-bottom-width") != "medium")
				{									
					params.height += parseInt(Element.getStyle(element,"border-bottom-width").replace("px",""));
				}
			}
			
			if(params.maxheight && params.height > params.maxheight)
			{
				params.height = params.maxheight;
			}
			
			if(params.maxwidth && params.width > params.maxwidth)
			{
				params.width = params.maxwidth;
			}

			// move the element into the content node
			_windowContent.appendChild(element);


			// dimensions of relative element
			var d = Element.getDimensions($(params.relativeElementID));


			var posXMax = 0;
			var posYMax = 0;


			if(window.event)
			{
				posXMax = document.documentElement.clientWidth;
				posYMax = document.documentElement.clientHeight;
			}
			else
			{
				posXMax = window.innerWidth + window.pageXOffset;
				posYMax = window.innerHeight + window.pageYOffset;
			}


			var top = 0;
			var left = 0;
			
			if(params.dock == "right")
			{
				var pos = Position.cumulativeOffset($(params.relativeElementID));
				left = pos[0] + d.width;
				top = pos[1];


				if((top + params.height) > posYMax)
				{
					top = top - params.height + d.height;
				}
			}
			else if(params.dock == "left")
			{
				var pos = Position.cumulativeOffset($(params.relativeElementID));
				left = pos[0] - d.width;
				top = pos[1];


				if((top + params.height) > posYMax)
				{
					top = top - params.height + d.height;
				}			
			}
			else if(params.dock == "bottomleft")
			{
				left = Position.cumulativeOffset($(params.relativeElementID))[0];
				top = Position.cumulativeOffset($(params.relativeElementID))[1] + d.height;

				if((left - params.width + d.width) >= 0)
				{
					left = left - params.width + d.width;
				}

			}
			else if(params.dock == "bottomright")
			{
				left = Position.cumulativeOffset($(params.relativeElementID))[0];
				top = Position.cumulativeOffset($(params.relativeElementID))[1] + d.height;

				if((left + params.width) > posXMax)
				{
					left = left - params.width + d.width;
				}

			}

			if(params.offsetX)
				left += params.offsetX;

			if(params.offsetY)
				top += params.offsetY;

			_window.style.left = left + "px";
			_window.style.top = top + "px";
			

			_window.style.width = params.width + "px";
			_window.style.height = params.height + "px";			
		
			// commented out the show, because the effect.appear will show the element.
			//Element.show(_window);  
			
			Effect.Appear(_window,{duration:.10});
			Element.show(element);
		
			_windowContent.scrollTop = 0;
						
			element = null;
					
			// obeserve events in bubble phase
			Event.observe(document, "click", me.Hide, false);
			Event.observe(_window, "click", WindowClick, false);
			
			if(e)
				Event.stop(e);
		}
		
		me.Resize = function(params)
		{
			if (!params)
			{
				var params = {width:null,height:null,maxheight:null,maxwidth:null};
			}

			var element = $(_elementID);
											
			if(!params.width)
			{
				params.width = Element.getDimensions(element).width;
				
				if (Element.getStyle(element,"border-left-width") != "medium")
				{
					params.width += parseInt(Element.getStyle(element,"border-left-width").replace("px",""));
				}

				if (Element.getStyle(element,"border-right-width") != "medium")
				{
					params.width += parseInt(Element.getStyle(element,"border-right-width").replace("px",""));
				}
			} 

			if(!params.height)
			{
				params.height = Element.getDimensions(element).height;
				
				if (Element.getStyle(element,"border-top-width") != "medium")
				{
					params.height += parseInt(Element.getStyle(element,"border-top-width").replace("px",""));
				}
				
				if (Element.getStyle(element,"border-bottom-width") != "medium")
				{									
					params.height += parseInt(Element.getStyle(element,"border-bottom-width").replace("px",""));
				}
			}
			
			if(params.maxheight && params.height > params.maxheight)
			{
				params.height = params.maxheight;
			}
			
			if(params.maxwidth && params.width > params.maxwidth)
			{
				params.width = params.maxwidth;
			}
			
			Element.hide(_window);
			
			_window.style.width = params.width + "px";
			_window.style.height = params.height + "px";			

			Effect.Appear(_window,{duration:0});
			
		}
				
		me.Hide = function()
		{
			if(!_Clicked)
			{
				// reset element and parent...
				var element = $(_elementID);

				Event.stopObserving(document, "click", me.Hide, false);
				
				// move the element back to its original spot in the dom (or at least original parent)..
				if((element != null) && (_elementParent!=null))
				{
					_elementParent.appendChild(element);
					Element.hide(_window);
					Element.hide(element);
				}

				element = null;
				_elementParent = null;

				Position.prepare();
			}
			_Clicked = false;
		}
	

		// construct window and add to body element
		
		_window  = $C("div");
		_window.style.display = "none";
		
		Element.addClassName(_window, "dswindow");
		var m = $C("div");
		Element.addClassName(m, "m");

		var iframe = $C("iframe");
		iframe.src = "javascript:null;";
		m.appendChild(iframe);

		var mi = $C("div");
		Element.addClassName(mi, "mi");

		m.appendChild(mi);
		_window.appendChild(m);
		
		var bl = $C("div");
		Element.addClassName(bl, "bl");

		var tr = $C("div");
		Element.addClassName(tr, "tr");

		_windowContent = $C("div");
		Element.addClassName(_windowContent, "content");

		
		_window.appendChild(bl);
		_window.appendChild(tr);
		_window.appendChild(_windowContent);		
		
		document.body.appendChild(_window);
	}
	
	var win = null;
	Event.observe(window, "load", LoadDSWin);
	function LoadDSWin()
	{
		win = new DSWin();
		Event.stopObserving(window, "load", LoadDSWin);
}
