/*
City of Saint Paul Open Source License

LICENSE

Copyright © 2006 City of Saint Paul, Minnesota. 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following terms and conditions:

1.The above copyright notice and this permission notice shall be included in all copies of this Software or works derived from this Software.
2.Whenever a Recipient distributes or redistributes the Software, a copy of this agreement must be included with each copy of the Software
3.If Recipient distributes or redistributes the Software in any form other than source code, Recipient must also make the source code freely available, and must provide with each copy of the Software information on how to obtain the source code in a reasonable manner on or through a medium customarily used for software exchange
4.A Recipient may not make any representation in the Software or in any promotional, advertising or other material that may be construed as an endorsement by the City of Saint Paul of any product or service provided by Recipient, or that may seek to obtain commercial advantage by the fact of the City of Saint Paul’s participation in this Agreement.

DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION

No Warranty:  The Software is provided “as is” without any warranty of any kind, either expressed, implied, or statutory, including, but not limited to, any warranty that the Software will conform to specifications, any implied warranties of merchantability, fitness for a particular purpose, or freedom from infringement, any warranty that the Software will be error free, or any warranty that documentation, if provided, will conform to the Software. This agreement does not, in any manner, constitute an endorsement by the City of Saint Paul or any prior recipient of any results, resulting designs, hardware, software products or any other applications resulting from use of the Software. Further, the City of Saint Paul disclaims all warranties and liabilities regarding third-party software, if present in the original Software, and distributes it “as is.”

Waiver and Indemnity:  Recipient agrees to waive any and all claims against the City of Saint Paul, its contractors and subcontractors, as well as any prior recipient.  If recipient's use of the Software results in any liabilities, demands, damages, expenses or losses arising from such use, including any damages from products based on, or resulting from, recipient's use of the Software, recipient shall indemnify and hold harmless the City of Saint Paul, it’s contractors and subcontractors, as well as any prior recipient, to the extent permitted by law. Recipient's sole remedy for any such matter shall be the immediate, unilateral termination of this agreement.

SUPPORT STATEMENT 

The Software is freely provided by the City of Saint Paul as a public service.  The recipient is herby notified that the provision and distribution of the Software should in no way be construed to mean that the City of Saint Paul is obligated to provide any level of support for the Software, now or in the future.  Support in this context shall be construed to mean providing coordination, technical assistance, responding to questions, releasing bug fixes, providing future releases, or otherwise obligating the City of Saint Paul to commit resources to maintain this Software or assist in its implementation by any person or entity.  The City of Saint Paul may, at it’s sole option, and without notice or obligation to the recipients, provide varying levels of support for the Software or terminate the agreement.  Provision of such support shall not alter the condition that the Software is provided without support as stated above.
*/

function Dialog(w,h,name) {
	var DialogObject;
	var DialogContent;

	DialogObject = document.createElement('div');
	DialogObject.className = 'Dialog';

	var x,y;

	/*
	 * Start the Dialog Centered
	 */
	var winSize = getWindowSize();
	x = (winSize.x - w) / 2;
	y = (winSize.y - h) / 2;

	DialogObject.style.top = y+'px';
	DialogObject.style.left = x+'px';
	DialogObject.style.width = w+'px';
	DialogObject.style.height = h+'px';

	var TitleBar = document.createElement('div');
	TitleBar.className = 'TitleBar';
	TitleBar.appendChild(document.createTextNode(name));	
	TitleBar.style.width = w+'px';


	TitleBar.onmousedown = startDrag;

	DialogObject.appendChild(TitleBar);
	var ControlBar;
 
	function toggleChildren() {
		var children = new Array();
		children[0] = ControlBar;
		children[1] = DialogContent;

		/*
 		 * There is some code below dealing with the overflow.  FireFox has some quirky issue where if there
		 * is a div set to overflow: scroll, it does some weird buffering.  IE doesn't do it, so I don't make IE
		 * run that code.
		 */
		for(var i = 0; i < children.length; i++) {
			if(children[i]) {
				if(children[i].style.visibility == 'visible') {
					children[i].style.visibility = 'hidden'; 
					if(!document.all) {
						children[i].style._overflow = children[i].style.overflow;
						children[i].style.overflow = 'hidden';
					}
				} else {
					children[i].style.visibility = 'visible'; 

					if(!document.all) {
						children[i].style.overflow = children[i].style._overflow;
						delete children[i].style._overflow;
					}
				}
			}
		}	
	}

	this.closeDialog = function() {
		var parent = DialogObject.parentNode;
		parent.removeChild(DialogObject);
	}

	this.bar = function() {
		//logDebugMessage('Dialog', false, 'Barred! '+DialogObject.style.height);
		if(DialogObject.style.height == h+'px') {
			DialogObject.style.height = '20px';
			toggleChildren();
		} else {
			DialogObject.style.height = h+'px';
			toggleChildren();
		}
	}


	TitleBar.ondblclick = this.bar;
	this.addWinControls = function() {
		var closeButton = document.createElement('div');
		closeButton.appendChild(document.createTextNode('X'));
		closeButton.className = 'CloseButton';

		closeButton.onclick = function () {
			var parent = DialogObject.parentNode;
			parent.removeChild(DialogObject);
		}
		TitleBar.appendChild(closeButton);

		var barButton = document.createElement('div');
		barButton.appendChild(document.createTextNode('-'));
		barButton.className = 'BarButton';
		barButton.onclick = this.bar;
		TitleBar.appendChild(barButton);
	}

	this.createEmptyControlBar = function() {
		if(!ControlBar) {
			ControlBar = document.createElement('div');
			ControlBar.className = 'Controls';
			ControlBar.style.width = w+'px';
			ControlBar.style.visibility = 'visible';
			DialogObject.appendChild(ControlBar);
		}
	} 

	this.getControlBar = function() {
		return ControlBar;
	}

	this.addCloseBar = function() {
		this.createEmptyControlBar();
		var closeButton = document.createElement('input');
		closeButton.type = 'button';
		closeButton.value = 'Close';
		closeButton.onclick = function () {
			var parent = DialogObject.parentNode;
			parent.removeChild(DialogObject);
		}
		closeButton.style.position = 'absolute';
		closeButton.style.right = '0px';
		closeButton.style.top = '5px';
		ControlBar.style.height = '30px';
		ControlBar.appendChild(closeButton);
	}
		
	this.getDialog = function() {
		this.getContent();
		DialogObject.onmouseover = function () { AllPopups.removeVisiblePopups(); }
		return DialogObject;
	}

	this.resetContent = function() {
		DialogObject.removeChild(DialogContent);
		delete DialogContent;
		return this.getContent();
	}

	this.getContent = function() {
		if(!DialogContent) {
			DialogContent = document.createElement('div');
			DialogContent.className = 'Content';
			DialogContent.style.left = '0px';
			DialogContent.style.top = '20px';
			DialogContent.style.width = w+'px';
			DialogContent.style.visibility = 'visible';
			this.resetContentHeight();
			DialogObject.appendChild(DialogContent);
		}
		return DialogContent;
	}

	this.resetContentHeight = function() {
		var contentH = h-20; // Account for the TitleBar
		if(ControlBar) {
			contentH -= parseInt(ControlBar.style.height); // Account for the Control Bar
			//logDebugMessage('Dialog',false,'Control Bar H: '+ControlBar.style.height);
		}
		//logDebugMessage('Dialog',false,'Content Height: '+contentH);
		DialogContent.style.height = contentH+'px';
	}

	/*
	 * This is the dragging code for the Dialog Boxes
	 * Really, I should integrate this with the "Draggable" package, but I haven't yet.
	 */

	var cursorPosition;
	function startDrag(event) {
		if(window.event) { event = window.event; }
		StartPoint = new Point(DialogObject.offsetLeft, DialogObject.offsetTop);
		cursorPosition = new Point(event.clientX - StartPoint.x, event.clientY - StartPoint.y);
	
		if(document.all) {	
			document.attachEvent("onmousemove", whileDragging);
			document.attachEvent("onmouseup",   endDrag);
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else {
			document.addEventListener("mousemove",whileDragging,true);
			document.addEventListener("mouseup",endDrag,true);
		}

	}

	function whileDragging(event) {
		if(window.event) { event = window.event; }
		if(cursorPosition != null) {
			DialogObject.style.left = (event.clientX - cursorPosition.x)+'px';
			DialogObject.style.top = (event.clientY - cursorPosition.y)+'px';
		}
	}

	function endDrag(event) {
		if(window.event) { event = window.event; }
		cursorPosition = null;
		if(DialogObject.offsetLeft < 0) { DialogObject.style.left = '0px'; }
		if(DialogObject.offsetTop < 0) { DialogObject.style.top = '0px'; }

		var winSize = getWindowSize(); // From WindowSize.js

		if((DialogObject.offsetLeft + DialogObject.offsetWidth) > winSize.x) { DialogObject.style.left = (winSize.x - DialogObject.offsetWidth) + 'px'; }
		if((DialogObject.offsetTop + DialogObject.offsetHeight) > winSize.y) { DialogObject.style.top = (winSize.y - DialogObject.offsetHeight) + 'px'; }

		if (document.all) {
			document.detachEvent("onmousemove", whileDragging);
			document.detachEvent("onmouseup",   endDrag);
		} else {
			document.removeEventListener("mousemove", whileDragging,   true);
			document.removeEventListener("mouseup",   endDrag, true);
		}
	}


}
