ASC.namespace('ASC.Chrysler.Configurator.DialogManager');

ASC.Chrysler.Configurator.DialogManager = ASC.extend(ASC.util.Observable, {
	
	deltaDlg: null,
	mustHaveDlg: null,
	moreInfoDlg: null,
	
	constructor: function(options)
	{
		if (!options) { options = {}; }
		
		ASC.Chrysler.Configurator.DialogManager.superclass.constructor.apply(this);
		
		this.addEvents({
			select: true,
			cancel: true,
			complete: true
		});
		
		if (options.events) {
			this.on(options.events);
		}
	},
	
	showMustHave: function (configResponse)
	{
		var me = this;
		var events = {
			optionSelected: {
				fn: function (optCode) {
					me.destroyDialog(me.mustHaveDlg, ['select', optCode]);
				}
			},
			
			cancel: {
				fn: function () {
					me.destroyDialog(me.mustHaveDlg, ['cancel']);
				}
			},
			
			hide: {
				fn: function () {
					me.destroyDialog(me.msutHaveDlg, ['cancel']);
				}
			}  
		};
		
		this.mustHaveDlg = new ASC.Chrysler.Configurator.Dialogs.MustHave(configResponse);
		this.mustHaveDlg.on(events);
		this.mustHaveDlg.show();
	},
	
	showDelta: function (configResponse,selectedCode)
	{
		var me = this;
		var events = {
			select: {
				fn: function (configResponse) {
					me.destroyDialog(me.deltaDlg, ['complete', configResponse]);
				}
			},
			
			cancel: {
				fn: function () {
					me.destroyDialog(me.deltaDlg, ['cancel']);
				}
			},
			
			hide: {
				fn: function () {
					me.destroyDialog(me.deltaDlg, ['cancel']);
				}
			}			
		};
		this.deltaDlg = new ASC.Chrysler.Configurator.Dialogs.Delta(configResponse,selectedCode);
		this.deltaDlg.on(events);
		this.deltaDlg.show();
	},
	
	showMoreInfo: function (response)
	{
		var me = this;
		var events = {
			hide: function () {
				me.destroyDialog(me.moreInfoDlg);
			}
		};
		
		this.moreInfoDlg = new ASC.Chrysler.Configurator.Dialogs.MoreInfo(response);
		this.moreInfoDlg.on(events);
		this.moreInfoDlg.show();
	},
	
	//private
	destroyDialog: function (/*Ext.Window*/ win, /*Array*/ postDestroyEvent) 
	{
		var me = this;
				
		//The window is being destoryed do not notify any event subscribers of anymore events
		win.suspendEvents();
				
		var fireEvent = function ()
		{ 
			//wait until the window is hidden
			if (!win.isVisible()) {	
				win.destroy();
				delete win;
				
				if (typeof postDestroyEvent !== 'undefined') {			
					me.fireEvent.apply(me, postDestroyEvent);
				}
			} else {					
				setTimeout(fireEvent, 50);
			}
		};			
		fireEvent();		
		
		win.hide();
	}
});

