
/*===============================================================================
	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'},
		onClose:			$empty,
		onOpen:				$empty,
		onResize:			$empty,
		top:				0,
		left:				0,
		contentDiv:			false,
		injectLocation:		null,
		width:				'250px',
		zIndex:				4000,
		URL:				null
	},
	
	initialize: function(title, options) {
		this.setOptions(options);
		this.title = title;
		
		var windowDiv = new Element('div', {
			'styles':{'visibility': 'hidden',
				'position': 'absolute',
				'z-index': this.options.zIndex}
			});
		
		this.isOpen = false;
		windowDiv.setStyle('left', this.options.left);
		windowDiv.setStyle('top', this.options.top);
		windowDiv.setStyle('width', this.options.width);
		
		var closeIconHTML = this.options.isClosable ? '<span class="closeIcon"></span>' : '';
		var resizeIconHTML = this.options.isResizable ? '<span class="resizeIcon"></span>' : '';
		
		windowDiv.set('html',
		'<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.injectBottom(this.options.injectLocation || document.body);
		windowDiv.titleBar = windowDiv.getElement('.popUpWindowTitleBar');
		windowDiv.titleSpan = windowDiv.getElement('.theTitle');
		windowDiv.closeIcon = windowDiv.getElement('.closeIcon');
		windowDiv.contentDivHolder = windowDiv.getElement('.popUpWindowContentDivHolder');
		
		windowDiv.contentDiv = ($(this.options.contentDiv) || new Element('div'));
		windowDiv.contentDiv.injectBottom(windowDiv.contentDivHolder);
		if(windowDiv.contentDiv.style.display == 'none')
			windowDiv.contentDiv.setStyle('display', 'block');
		
		if(window.IframeShim  &&  Browser.Engine.trident4)
			this.windowShim = new IframeShim(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();
			}, this);
		}
		
		if(this.options.URL) {
			new Request.HTML({ url: this.options.URL,
				method: 'post',
				data: '_popUpData=necessaryToAvoidInvalidPost',
				update: this.windowDiv.contentDivHolder,
				evalScripts: true
			}).send();
		}
		
	},
	
	setTitle: function(newTitle) {
		this.windowDiv.titleSpan.set('text', newTitle);
	},
	
	getWindowDiv: function() {
		return this.windowDiv;
	},
	
	getContentDiv: function() {
		return this.windowDiv.contentDivHolder;
	},
	
	setContent: function(contentDiv) {
		this.windowDiv.contentDivHolder.empty().adopt(contentDiv);
	},
	
	setContentHTML: function(contentHTML) {
		this.windowDiv.contentDivHolder.set('html', contentHTML);
	},
	
	setWidth: function(newWidth) {
		this.windowDiv.setStyle('width', newWidth);
	},
	
	close: function(event) {
		if(!this.isOpen)
			return;
		this.isOpen = false;
		
		this.fireEvent('close');
		this.windowDiv.fade('out');
		if(this.windowShim) this.windowShim.hide();
	},
	
	open: function() {
		if(this.isOpen)
			return;
		this.windowDiv.fade('in');
		if(this.windowShim) this.windowShim.show();
		this.fireEvent('onOpen');
		this.isOpen = true;
	},
	
	toggle: function() {
		if(this.isOpen)
			this.close();
		else
			this.open();
	},
		
	setPosition: function(options) {
		this.windowDiv.position(options);
	},
	
	openURL: function(URL, newTitle, onComplete) {
		var me = this;
		new Request.HTML({ url: URL,
			method: 'post',
			data: '_popUpData=',
			update: this.windowDiv.contentDivHolder,
			evalScripts: true,
			onComplete: function() {
				me.open();
				if(newTitle)
					me.setTitle(newTitle);
				if(onComplete)
					onComplete(this.response.text);
			}
		}).send();
	}
	
	
});


