var CWindowModal = Class.create();
var CWindowModalManager = Class.create();
var windowModalHTMLContent = '    <div id="[##Id]" class="greyTransparency50 smallWindow">'
							+'      <table width="100%" height="100%"><tr><td align="center" valign="middle">'
							+'        <table cellspacing="0" cellpadding="0">'
							+'          <tr>'
							+'            <td class="smallWindowTopLeft"></td>'
							+'            <td class="smallWindowTop"></td>'
							+'            <td class="smallWindowTopRight"></td>'
							+'          </tr>'
							+'          <tr>'
							+'            <td class="smallWindowLeft"></td>'
							+'            <td class="smallWindowCenter">'
							+'              [##Content]'
							+'            </td>'
							+'            <td class="smallWindowRight"></td>'
							+'          </tr>'
							+'          <tr>'
							+'            <td class="smallWindowBottomLeft"></td>'
							+'            <td class="smallWindowBottom"></td>'
							+'            <td class="smallWindowBottomRight"></td>'
							+'          </tr>'
							+'        </table>'
							+'      </td></tr></table>'
							+'    </div>';

CWindowModal.prototype = {
	'initialize' : function(id, options){
		this.id = id;
		this.applyOptions(options);
	},
	'applyOptions' : function(options){
		if(Object.isUndefined(this.options)){
			this.options = {
				"isPersistent": false,
				"onClose": function(){},
				"onOpen": function(){},
				"onMap": false
			};
		}
		Object.extend(this.options, options || {});
	},
	'open' : function(){
		$(this.id).style.display = 'block';
		if(Object.isFunction(this.options.onOpen))this.options.onOpen();
	},
	'close' : function(){
		$(this.id).style.display = 'none';
		if(Object.isFunction(this.options.onClose))this.options.onClose();
	}
};


CWindowModalManager.prototype = {
	'initialize' : function(name){
		this.name = name;
		this.windowList = new Array();
		this.activeWindowPile = new Array();
		this.formToCreate = '';
		this.formToCreateOptions = {};
	},
	'add' : function(newWindow, options){
		if(newWindow.id){
		  theWindow = newWindow;
		} else {
		  theWindow = new CWindowModal(newWindow, options);
		}
		this.windowList[theWindow.id] = theWindow;
	},
	'open' : function(id, options){
		if(Object.isUndefined(this.windowList[id])){
		  if(null==$(id)){
			openWaitingForm();
			this.formToCreate = id;
			this.formToCreateOptions = options || {};
			Object.extend(this.formToCreateOptions, {
			  'scriptID' : loadTemplate(id, this.name+'.createAndOpen')
			});
			return 1;
		  }
		  this.add(id, options);
		} else {
			this.windowList[id].applyOptions(options);
		}
		if(id!='waitingForm') controller.requestController('track', 'section=WindowOpen&name='+id);
		if(this.windowList[id].options.onMap) this.closeAllWindows();
		this.activeWindowPile.unshift(id);
		this.windowList[id].open();
	},
	'closeAndDestroy' : function(id){
		this.windowList[id].close();
		if($(this.windowList[id].options.scriptID))$(this.windowList[id].options.scriptID).remove();
		delete this.windowList[id];
		$(id).remove();
	},
	'createAndOpen' : function(JSONObject){
		if(this.formToCreate != ''){
		  if(JSONObject.error_code){
	//        this.closeActiveWindow();
			closeWaitingForm();
			openErrorForm('Error '+JSONObject.error_code, JSONObject.error_msg);
			this.formToCreate = '';
		  } else {
			var htmlContent = windowModalHTMLContent;
			htmlContent = htmlContent.replace('[##Id]', this.formToCreate);
			htmlContent = htmlContent.replace('[##Content]',
							'<img width="19px" height="19px" src="./Img/closeButton.gif" class="button" style="float: right; margin-right:-7px; margin-top:-7px;" onclick="closeWindowModal();" />'
							+JSONObject.content);
			$('privateChat').insert({"before":htmlContent});
	//        this.closeActiveWindow();
			closeWaitingForm();
			this.open(this.formToCreate, this.formToCreateOptions);
			this.formToCreate = '';
			this.formToCreateOptions = {};
		  }
		}
	},
	'closeActiveWindow' : function(){
	if(this.activeWindowPile.length>0){
	  var thisWindow;

	  do thisWindow = this.activeWindowPile.shift(); while(!(this.windowList[thisWindow]) && this.activeWindowPile.length>0);
	  
	  if(this.windowList[thisWindow]){
		if(this.windowList[thisWindow].options.isPersistent){
		  this.windowList[thisWindow].close();
		} else {
		  this.closeAndDestroy(thisWindow);
		}
	  }
	}
  },
  'getWindow' : function(id){
	return this.windowList[id];
  },
  'closeAllWindows' : function(){
	while(this.activeWindowPile.length!=0) this.closeActiveWindow();
  }
};


// INIT Window Manager
windowModalManager = new CWindowModalManager('windowModalManager');
