
/*===============================================================================
	PopUpWindow.js
	John Larson
	2/07/08
	
	Component for system pop ups.
	

===============================================================================*/


var PopUpWindow = new Class({
	
	options: {
		isDraggable:		true,
		isClosable:			true,
		isResizable:		false,
		resizeLimits:		{},
		resizeModifiers:	{x: 'width', y: 'height'},
		onClose:			function() { return; },
		onOpen:				function() { return; },
		onResize:			function() { return; },
		top:				0,
		left:				0,
		contentDiv:			false,
		position:			'absolute',
		width:				'650',
		height:				'100%',
		zIndex:				'20000'
	},
	
	initialize: function(title, options) {
		this.setOptions(options);
		this.title = title;
		
		var windowDiv = new Element('div', {
			'styles':{'visibility': 'hidden',
				'position': this.options.position,
				'left': this.options.left,
				'top': this.options.top,
				'z-index': this.options.zIndex,
				'width': this.options.width,
				'height': this.options.height
				}
			});
		
		var closeIconHTML = this.options.isClosable ? '<span class="closeIcon"></span>' : '';
		var resizeIconHTML = this.options.isResizable ? '<span class="resizeIcon"></span>' : '';
		
		windowDiv.setHTML(
		'<table class="popUpTableBorder" cellspacing="0" cellpadding="0" border="0" width="' + this.options.width + '"><tr><td>' +
		'<div class="popUpWindow"><div class="popUpWindowTitleBar">' +
		'  <span class="theTitle">' + title + '</span>' + closeIconHTML +
		' </div>' +
		' <div class="content"><div class="popUpWindowContent"></div>' + resizeIconHTML + '</div>' +
		'</div></div></div></div></div></td></tr></table>');
		
		windowDiv.injectInside(this.options.injectLocation || document.body);
		windowDiv.titleBar = $E('.popUpWindowTitleBar', windowDiv);
		windowDiv.titleSpan = $E('.theTitle', windowDiv);
		windowDiv.closeIcon = $E('.closeIcon', windowDiv);
		windowDiv.contentDivHolder = $E('.popUpWindowContent', windowDiv);
		
		windowDiv.contentDiv = (this.options.contentDiv || new Element('div'));
		windowDiv.contentDiv.injectInside(windowDiv.contentDivHolder);
		
		
		this.windowShim = new IframeShim({
			element	: windowDiv,
			display	: true
		});
		
		if(this.options.isDraggable) {
			this.drag = new Drag.Move(windowDiv, {
				handle: windowDiv.titleBar,
				onDrag: function() {
					this.windowShim.position();
				}.bind(this)
			}); 
		}
		
		if(this.options.isResizable) {
			windowDiv.contentDiv.makeResizable({
				handle: $E('.resizeIcon', windowDiv),
				limit: this.options.resizeLimits,
				modifiers: {x: false, y: 'height'}, //limit the sizing to vertical
				onComplete: this.options.onResize
			});
		}
		
		this.windowDiv = windowDiv;
		
		var onCloseAction	= this.options.onClose.bind(this);
		
		if (this.options.isClosable) { // make the close icon work
			var closeAction = this.close.bind(this);
			this.windowDiv.closeIcon.addEvent('click', function() {
				closeAction();
				onCloseAction();
			}, this);
		}
		
	},
	
	setTitle: function(newTitle) {
		this.windowDiv.titleSpan.setText(newTitle);
	},
	
	getContentDiv: function() {
		return this.windowDiv.contentDivHolder;
	},
	
	setContent: function(contentDiv) {
		contentDiv.injectInside(this.windowDiv.contentDivHolder);
	},
	
	close: function(event) {
		this.windowDiv.effect('opacity').start(1,0);
		this.windowShim.hide();
	},
	
	open: function() {
		this.windowDiv.effect('opacity').start(0,1);
		this.windowShim.show();
		this.fireEvent('onOpen');
	},
		
	setPosition: function(options) {
		this.windowDiv.setPosition(options);
	}
	
	
});

PopUpWindow.implement(new Options, new Events);

