/*
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.
*/

/*
 * This Class handles the contents, movements, and management of a single popup object.
 */

function Popup(Id, ParentLayer, Title, Contents) {
	var renderedObject;
	var cursorPosition;
	var Origin;
	var StartPoint;
	
	var TailDots = 10;

	this.showClose = false;

	this.ondrag = function() {
		// Do nothing by default
	}

	this.onunload = function () {
		/*
		 * Do nothing by default
		 */
	}


	this.getParent = function() {
		return ParentLayer;
	}

	this.isRendered = function () {
		return(renderedObject && renderedObject.parentNode);
	}

	this.defaultStyleTitle = function(title) {
		title.style.position = 'relative';
		title.style.left = '0px';
		title.style.top = '0px';
		title.style.width = '100%';
		title.style.height = '16px';
		title.style.fontSize = '12px';
		title.style.fontWeight = 'bold';
		title.style.color = 'white';
		title.style.background = 'black';
		title.style.overflow = 'hidden';
	}

	this.defaultStyleContents = function(contents) {
		contents.style.position = 'relative';
		contents.style.left = '0px';
		contents.style.top = '0px';
		contents.style.background = '#FFF';
		contents.style.width = '100%';
		contents.style.fontSize = '12px';

		contents.style.overflow = 'hidden';
	}

	this.styleTitle = function(title) {
	}

	this.styleContents = function(contents) {
	}

	this.stylePopup = function(x,y) {
		renderedObject.style.position = 'absolute';
		renderedObject.style.background = '#999';
		renderedObject.style.top = y+'px';
		renderedObject.style.left = x+'px';
		renderedObject.style.width = '150px';
		renderedObject.style.border = 'solid 2px #999';
		renderedObject.style.zIndex = 1000;
	}

	this.styleKillBox = function(killbox) {
		killbox.style.position = 'absolute';
		killbox.style.top = '0px';
		killbox.style.right = '0px';
		killbox.style.color = 'red';
		killbox.style.border = 'solid 1px red';
		killbox.style.fontSize = '10px';
		killbox.style.cursor = 'pointer';
	}

	this.renderKillBox = function() {
		var killbox = document.createElement('div');
		killbox.appendChild(document.createTextNode('X'));
		killbox.owner = this;
		killbox.onclick = function () {
			this.owner.removePopup();
		}

		this.styleKillBox(killbox);
		if(this.showClose) {
			renderedObject.appendChild(killbox);
		}

	}

	this.renderPopup = function(x,y) {
		renderedObject = document.createElement('div');
		var title = document.createElement('div');
		var contents = document.createElement('div');
		
		title.onmousedown = startDrag;
//		title.onmousemove = whileDragging;
//		title.onmouseup = endDrag;
//		title.onmouseexit = whileDragging;
		renderedObject.onmouseenter = whileDragging;
		contents.onmousemove = whileDragging;

		title.appendChild(document.createTextNode(Title));
		//contents.appendChild(document.createTextNode(Contents));
		contents.innerHTML = Contents;

		this.stylePopup(x,y);
		this.defaultStyleTitle(title);
		this.defaultStyleContents(contents);

		this.styleTitle(title);
		this.styleContents(contents);

		this.renderKillBox();

		renderedObject.appendChild(title);
		renderedObject.appendChild(contents);
	
		renderedObject.id = Id;
		contents.onmouseexit = function () { alert('Left Me'); }

	}

	var TailPoints = new Array();

	function renderPoint(i,x,y) {
		if(!TailPoints[i]) {
			TailPoints[i] = document.createElement('div');
			TailPoints[i].style.background = 'red';
			TailPoints[i].style.position = 'absolute';
			TailPoints[i].style.height = '2px';
			TailPoints[i].style.width = '2px';
			TailPoints[i].style.overflow = 'hidden';
			renderedObject.parentNode.appendChild(TailPoints[i]);	
			setOpacity(TailPoints[i],'50');
		}
		TailPoints[i].style.left = x+'px';
		TailPoints[i].style.top = y+'px';

	}

	function renderLine(x,y) {
		var tail = new LineSegment(Origin, new Point(x,y));
		var distanceOverX = x - Origin.x ;
		var width = renderedObject.offsetWidth  / (TailDots + 2);
		var height = renderedObject.offsetHeight / (TailDots + 2);
		var incr = distanceOverX / TailDots;
		var baseX = Origin.x;
		if(distanceOverX != 0) {
			renderPoint(0, baseX, tail.eval(baseX));	
			for(var i = 1; i < TailDots; i++) {
				renderPoint(i, baseX+incr*i, tail.eval(baseX+incr*i));	
				TailPoints[i].style.width = width * i;
				TailPoints[i].style.height = height * i;
			}	
		} else { // What happens when the popup is vertically aligned with the origin.
			renderPoint(0, Origin.x, Origin.y);
			var distanceOverY = y - Origin.y;
			incr = distanceOverY / TailDots;
			for(var i = 1; i < TailDots; i++) {
				renderPoint(i, baseX, Origin.y + incr * i);
				TailPoints[i].style.width = width * i;
				TailPoints[i].style.height = height * i;
			}	
		}
	}

	function removeLine() {
		if(TailPoints.length > 0 ) {
			for(var i = TailDots - 1; i >= 0; i--) {
				if(TailPoints[i] && TailPoints[i].parentNode) {
					var p = TailPoints[i].parentNode;
					p.removeChild(TailPoints[i]);
					delete TailPoints[i];
				}
			}
		}
	}

	var myself = this;
	function startDrag(event) {
		if(window.event) { event = window.event; }
		StartPoint = new Point(renderedObject.offsetLeft, renderedObject.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);
		}

		myself.ondrag();
	}

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

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

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

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

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

	this.getPopup = function(x,y,orig) {
		if(renderedObject == null) {
			this.renderPopup(x,y);
			/*if(!orig) {
				Origin = new Point(x,y);
			} else {
				Origin = orig;
			}*/
			Origin = orig;
			return renderedObject;
		} else {
			return renderedObject;
		}
	}

	this.popup = function(Parent, x, y) {
		var check = document.getElementById(Id);
		if(!check) {
		}
	}

	this.removePopup = function () {
		if(renderedObject.parentNode) {
			var p = renderedObject.parentNode;
			p.removeChild(renderedObject);
		}
		delete renderedObject;
		renderedObject = null;
		this.onunload();
		removeLine();
	}	

	this.getId = function() {
		return Id;
	}

}
