/**************************************************************************\
BUILD 4
Copyright (c) Enigma Interactive 2005

Filename:		content_client.js
Description:	Provides the overall edit area interface controller.

History
ver	date		who			comment
-----------------------------------------------------------------------------
1	15/12/05	AJL			Created
\**************************************************************************/


var DIALOG = { };

DIALOG.MainController = function() {
	this.inherit(OO.Listener);
	this.inherit(OO.Timer);
}

DIALOG.MainController.prototype = {
	initSelf: function() {
		this.dialogs = [ ];
		this.dialogsById = { };
		this.canvas = DOM.get('sbDialogLayer');
		this.modelessCanvas = DOM.get('sbModelessDialogLayer');
		this.counter = 0;
		this.dialogParams = { };
		
		CC.messenger.registerListener(this, 'content');
		CC.messenger.registerListener(this, 'dialog');
	},

	load: function(item, controller, modeless, params) {
		var dialog = this.createDialog(controller);
		var canvas = (modeless) ? this.modelessCanvas : this.canvas;
		
		this.counter++;
		dialog.load(item);
		if (params) dialog.setup(params);
		
		if (canvas.childNodes.length == 1)
			DHTML.show(canvas);
		
		dialog.id = 'sbDialog'+this.counter;
		this.dialogs.push(dialog);
		this.dialogsById[dialog.id] = dialog;
		dialog.display(canvas);

		return dialog;
	},
	
	listenToMessage: function(message, params) {
		switch (message) {
			case 'closeDialog'	: this.closeDialog(params.originator); break;
			case 'dialogParams'	: this.setDialogParams(params.label, params.params); break;
		}
	},
	
	closeDialog: function(id) {
		var dialog = this.dialogsById[id];

		if (!dialog) return;
		dialog.closeDialog();
		delete this.dialogsById[id];
		delete this.dialogs[this.dialogs.indexOf(dialog)];
		
		dialog.canvas.currentItemId = 0;
	},
	
	createDialog: function(controller) {
		var dialog = DHTML.create('div', controller);
		
		return dialog;
	},
	
	setDialogParams: function(label, params) { this.dialogParams[label] = params; },
	
	getDialogParams: function(label, doClear) { var result = this.dialogParams[label] || { }; if (doClear) this.clearDialogParams(label); return result;  },
	
	clearDialogParams: function(label) { this.dialogParams[label] = null; }
	
}

DIALOG.Dialog = function() {
	this.inherit(DHTML.Anim);
	this.inherit(OO.Listener);
	this.inherit(DHTML.Drag);
}
DIALOG.Dialog.prototype = {
	initSelf: function() { 
		this.className = 'sbDialog';
		this.hide();
	},
	
	load: function(item) {
		var logo = DHTML.structure({ e: { className: 'sbLogo' } });
		var header = DHTML.structure({ e: { id: 'sbDialogHeader', className: 'sbDialogHeader' }});
		var bg = DHTML.structure({ e: { className: 'sbDialogBack' } });
		
		item = item || this.defaultInfo;
		item.content = item.content || '';
		this.setDragHandle(header);
		this.extras = item.params;
		this.className = 'sbDialog ' + item.label;
		this.content = DHTML.structure({ e: { className: 'sbContent', innerHTML: item.content }, c: DHTML.Anim });		
		this.appendChild(bg);
		this.appendChild(logo);
		this.appendChild(header);
		this.header = header;
		if (this.title) this.setTitle(this.title);
	},
	
	display: function(canvas) {
		var oThis = this;
		
		this.setConstrain('sbBaseLayer');
		this.canvas = canvas;
		this.canvas.appendChild(this);

		if (this.extras.height) this.setHeight(this.extras.height);
		if (this.extras.width) this.setWidth(this.extras.width);
		this.center(1, 1, 'sbBaseLayer');
		this.setHeight(1);
		//this.setOpacity(100);
		this.showContent();
		this.extras.height = this.extras.height || (this.content.offsetHeight+25);
		this.show();
		//this.animate({ O: 200 }, 100, 50);
		this.animate({ H: this.extras.height }, 250);
	},
	
	setTitle: function(title) {
		while (this.header.firstChild) this.header.removeChild(this.header.firstChild);
		this.header.appendChild(DOM.structure({ e: { className: 'title' }, x: title } ));
	},
	
	showContent: function() {
		this.appendChild(this.content);
	},

	closeDialog: function() {
		//this.removeChild(this.content);
		this.animate({H: 1 }, 250);
		//this.animate({O: 0 }, 100, 250);
		
		this.setTimeout('closeDialog', 300, function() { this.stopAnimation(), DHTML.removeSelf(this); if (this.canvas.childNodes.length == 1) DHTML.hide(this.canvas); });

		U.Cleaner.clean(this, 2000);
	}
	
}

DIALOG.AlertController = function() {
	this.inherit(OO.Listener);
	this.inherit(OO.Timer);
	this.canvas = DOM.get('sbDialogLayer');
}
DIALOG.AlertController.prototype = {
	initSelf: function() {
		CC.messenger.registerListener(this, 'content');
	},
	
	listenToMessage: function(message, params) { 
		switch (message) {
			case 'alertDisplay'	:	this.alert(params); break;
		}
	},
	
	alert: function(params) {
		this.mess = DHTML.structure({ e: { className: 'sbAlertHolder', innerHTML: params.text }, c: DHTML.Anim });
		this.mess.hide();
		this.CQ(this.mess);
		this.canvas.appendChild(this.mess);
		this.mess.center(0, 1);
		this.mess.fontSize(10);
		this.mess.setOpacity(100);
		DHTML.show(this.canvas);
		this.mess.show();
		this.mess.animate({ F: 120, oY: -60, O: 0 }, 500);
		this.setTimeout('hideCanvas', 1300, this.hideCanvas);	
	},
	
	hideCanvas: function() { this.mess.hide(); this.mess.stopAnimation(); DHTML.removeSelf(this.mess); DHTML.hide(this.canvas); }
}

DIALOG.waitController = function() {
	this.inherit(OO.Listener);
	this.inherit(OO.Timer);
}
DIALOG.waitController.prototype = {
	initSelf: function() {
		CC.messenger.registerListener(this, 'pleaseWait');
		this.canvas = DOM.get('sbAlertLayer');
		this.initWaitEle();
		this.on = false;
		this.level = 0;
		EVENT.attachEvents(document.body, 'mousemove', this.mouseMoveFN, this);
	},

	listenToMessage: function(message, params) { 
		switch (message) {
			case 'show'		:	this.show(false, params); break;
			case 'hide'		:	this.hide(); break;
			case 'on'		:	this.doOn(params); break;
			case 'off'		:	this.doOff(); break;
		}
	},

	initWaitEle: function() {
		this.waitEle = DOM.structure({ cn: 'sbWaitHolder', e: { blockMouse: true }, y: { display: 'none' } });
		this.canvas.appendChild(this.waitEle);
		this.waitEle.innerHTML = '<img src="/_fc/images/wait/slider.gif" />';
	},
	
	show: function(hideImg, mess) {
		mess = mess || 'Be right with you. You are my first priority. I exist only to serve.';
		if (this.on) return;
		if (!this.waitEle) this.initWaitEle();
		
		this.canvas.blockMouse = true;
		EVENT.attachEvents(this.waitEle, 'dblclick', this.doOff, this);
		if (hideImg) DHTML.display(this.waitEle, 'none');
		else {
			DHTML.display(this.waitEle, 'block');
		}
		
		DHTML.show(this.canvas);
		this.on = true;
	},
	
	hide: function() {
		if (!this.on) return;
		DHTML.display(this.waitEle, 'none');
		DHTML.hide(this.canvas);
		EVT.unlock(this, 'mouseover,mouseup,mousedown,mousemove,click,keyup,keydown,keypress');
		this.on = false;
	},
	
	doOn: function(mess) {
		
		if (this.level++) return;
		
		//this.setTimeout('imgOn', 10, this.imgOn);
		this.show(true, mess);
		this.imgOn();
		DIALOG.waiting = true;
	},
	
	doOff: function() {
		if (!this.level) return this.hide();
		if (--this.level) return;
		
		this.hide();
		DIALOG.waiting = false;
	},
	
	imgOn: function() {
		if (!this.waitEle) return;
		DHTML.moveTo(this.waitEle, this.lastX, this.lastY);
		DHTML.display(this.waitEle, 'block');
		EVT.lock(this, 'mouseover,mouseup,mousedown,mousemove,click,keyup,keydown,keypress');
	},
	
	mouseMoveFN: function(e) {
		if (this.on) DHTML.moveTo(this.waitEle, e.pageX, e.pageY);
		this.lastX = e.pageX;
		this.lastY = e.pageY;
	},
	
	disposeSelf: function() {
		if (this.waitEle) OO.dispose(this.waitEle);
	}
}

DIALOG.alertCM = function(text, type, message, params, icon, yes) {
	yes = yes || 'Yes';
	
	var params = {	body: text, buttons: { confirm: yes }, confirmRequest: { type: type, message: message, params: params }, autoSize: true };
	if (icon) params.image = icon;
	
	$FLOAT('/globalDialog/confirm', params);
}
	
DIALOG.confirmCR = function(text, type, message, params, icon, yes, no) {
	yes = yes || 'Yes';
	no = no || 'No';
	
	var params = {	body: text, buttons: { confirm: yes, cancel: no }, confirmRequest: { type: type, message: message, params: params }, autoSize: true };
	if (icon) params.image = icon;
	
	$FLOAT('/globalDialog/confirm', params);
}
	
DIALOG.confirmCM = function(text, type, message, params, icon, yes, no) {
	yes = yes || 'Yes';
	no = no || 'No';
	
	var params = {	body: text, buttons: { confirm: yes, cancel: no }, confirmMessage: { type: type, message: message, params: params }, autoSize: true };
	if (icon) params.image = icon;
	
	$FLOAT('/globalDialog/confirm', params);
}

DIALOG.chooseCM = function(text, type, yesMess, noMess, yesParams, noParams, icon, yes, no) {
	yes = yes || 'Yes';
	no = no || 'No';
	icon = icon || 'query';
	noMess = noMess || yesMess;
	noParams = noParams || yesParams;
	
	var params = {	body: text, buttons: { confirm: yes, cancel: no }, confirmMessage: { type: type, message: yesMess, params: yesParams }, cancelMessage: { type: type, message: noMess, params: noParams } }
	if (icon) params.image = icon;
	
	$FLOAT('/globalDialog/confirm', params);
}
	
DIALOG.chooseCR = function(text, type, yesMess, noMess, yesParams, noParams, icon, yes, no) {
	yes = yes || 'Yes';
	no = no || 'No';
	icon = icon || 'query';
	noMess = noMess || yesMess;
	noParams = noParams || yesParams;
	
	var params = {	body: text, buttons: { confirm: yes, cancel: no }, confirmRequest: { type: type, message: yesMess, params: yesParams }, cancelRequest: { type: type, message: noMess, params: noParams } }
	if (icon) params.image = icon;
	
	$FLOAT('/globalDialog/confirm', params);
}

	
