
/*===============================================================================
	PopUpWindow.js
	John Larson
	2/07/08
	
	Component for system pop ups.
	

===============================================================================*/


var PopUpWindow = new Class({
	
	Implements: [Options, Events],
	
	options: {
		isDraggable:		true,
		isClosable:			true,
		isResizable:		false,
		resizeLimits:		{},
		resizeModifiers:	{x: 'width', y: 'height'},
		closeAction:		function() { return; },
		onOpen:				function() { return; },
		onResize:			function() { return; },
		top:				0,
		left:				0,
		contentDiv:			false,
		width:				'250px',
		URL:				null
	},
	
	initialize: function(title, options) {
		this.setOptions(options);
		this.title = title;
		
		var windowDiv = new Element('div', {
			'styles':{'visibility': 'hidden',
				'position': 'absolute',
				'z-index': 4000}
			});
		
		this.isOpen = false;
		windowDiv.setStyle('left', this.options.left);
		windowDiv.setStyle('top', this.options.top);
		windowDiv.setStyle('width', this.options.width);
		
		var onCloseAction = this.options.closeAction.bind(this);
		
		var closeIconHTML = this.options.isClosable ? '<span class="closeIcon"></span>' : '';
		var resizeIconHTML = this.options.isResizable ? '<span class="resizeIcon"></span>' : '';
		
		windowDiv.setHTML(
		'<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td>' +
		'<div class="ds1UR"><div class="ds1LL"><div class="ds1M"><div class="ds1C">' +
		'<div class="popUpWindow"><div class="popUpWindowTitleBar">' +
		'<span class="theTitle">' + title + '</span>' + closeIconHTML +
		'</div>' +
		'<div class="popUpWindowContent"><div class="popUpWindowContentDivHolder"></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('.popUpWindowContentDivHolder', windowDiv);
		
		windowDiv.contentDiv = (this.options.contentDiv || new Element('div'));
		windowDiv.contentDiv.injectInside(windowDiv.contentDivHolder);
		
		
		if(window.IframeShim)
			this.windowShim = new IframeShim({
				element	: windowDiv,
				display	: false
			});
		
		if(this.options.isDraggable) {
			this.drag = new Drag.Move(windowDiv, {
				handle: windowDiv.titleBar,
				onDrag: function() {
					if(this.windowShim)
						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;
		
		if (this.options.isClosable) { // make the close icon work
			var closeAction = this.close.bind(this);
			this.windowDiv.closeIcon.addEvent('click', function() {
				closeAction();
				onCloseAction();
			}, this);
		}
		
		if(this.options.URL) {
			new Ajax(this.options.URL, {
				method: 'post',
				data: '_popUpData=necessaryToAvoidInvalidPost',
				update: this.windowDiv.contentDivHolder,
				evalScripts: true
			}).request();
		}
		
	},
	
	setTitle: function(newTitle) {
		this.windowDiv.titleSpan.setText(newTitle);
	},
	
	getWindowDiv: function() {
		return this.windowDiv;
	},
	
	getContentDiv: function() {
		return this.windowDiv.contentDivHolder;
	},
	
	setContent: function(contentDiv) {
		contentDiv.injectInside(this.windowDiv.contentDivHolder);
	},
	
	close: function(event) {
		if(!this.isOpen)
			return;
		
		this.windowDiv.effect('opacity').start(1,0);
		if(this.windowShim) this.windowShim.hide();
		this.isOpen = false;
	},
	
	open: function() {
		if(this.isOpen)
			return;
		this.windowDiv.effect('opacity').start(0,1);
		if(this.windowShim) this.windowShim.show();
		this.fireEvent('onOpen');
		this.isOpen = true;
	},
		
	setPosition: function(options) {
		this.windowDiv.setPosition(options);
	}
	
	
});

