/**************************************************************************\
BUILD 4
Copyright (c) Enigma Interactive 2005

Filename:		editor_main.js
Description:	Provides the overall edit area interface controller.

notes:			the register sender section is for the ajax files.
History
ver	date		who			comment
-----------------------------------------------------------------------------
1	15/12/05	AJL			Created
\**************************************************************************/

try { document.execCommand("BackgroundImageCache",false,true); } catch(e) {};

var CONTROLLERS = CC = { };

CC.messenger = window.CM = OO.create(MESSENGER.MessageController);
CC.init = function() {
	
	window.$CHAIN = function(chain, noWait, waitMess) { CC.messenger.chainRequest(chain, noWait, waitMess); };
	window.$QSEND = function(noWait, waitMess) { CC.messenger.queueSend(noWait, waitMess); };
	window.$QR = function(label, message, params) { CC.messenger.queueRequest(label, message, params); return false; };
	window.$CR = function(label, message, params, noWait, waitMess, errorMess, async) { CC.messenger.sendRequest(label, message, params, noWait, waitMess, errorMess, async); return false; };
	window.$AR = function(label, message, params) { CC.messenger.asyncRequest(label, message, params); };
	window.$DR = function(d, label, message, params, noWait, waitMess, errorMess) { CC.messenger.delayedRequest(d, label, message, params, noWait, waitMess, errorMess); return false; };
	window.$CM = function(type, message, params) { CC.messenger.sendMessage(type, message, params); return false; };
	window.$DM = function(d, type, message, params) { CC.messenger.delayedMessage(d, type, message, params); return false; };
	window.$QM = function() { for (var i=0,a;(a=arguments[i]);i++) $CM.apply(this, a.split(',')); };
	window.$URL = function(location, params) { CC.content.go(location, params); return false; };
	window.$DURL = function(delay, location, params) { CC.content.setTimeout("go", delay, CC.content.go, location, params); return false; };
	window.$FLOAT = function(location, params, tmpURL) { CC.content.loadFloat(location, params, tmpURL); return false; };
	window.$LISTENER = function(o, types) { CC.messenger.registerListener(o, types); }
	window.$SENDER = function(t, u) { CC.messenger.registerSender(t, u); }
	window.$QS = function() { for (var i=0,a;(a=arguments[i]);i++) $SENDER.apply(this, a.split(',')); };
	window.$CURRENT = function() { return CC.content.currentLocation; }
	window.$PARAMS = function(type) { return CC.content.getClientParams(type); }

	$QS('chain,chain','content,content/content', 'user,user/user', 'utility,general/utility', 'design_menu,menus/design_menu', 
		'base_template,site_design/base_template', 'layout_preview,site_design/layout_preview', 'edit_layout,site_design/edit_layout',
		'edit_image,site_design/edit_image', 'layout_colour,site_design/layout_colour', 'page_layout,page_editor/page_layout', 
		'page_editor,page_editor/page_editor', 'content_editor,page_editor/content_editor', 'documents_editor,page_editor/documents_editor', 
		'file_upload,dialogs/file_upload', 'article_editor,page_editor/article_editor', 'image_editor,dialogs/image_editor', 
		'event_article_editor,page_editor/article_event_editor', 'site_organiser,site_organiser/site_organiser', 
		'site_wizard,/site_wizard/site_wizard', 'forgot_password,/dialogs/forgotten_password', 'template_details,/site_design/template_details',
		'change_email,/dialogs/change_email', 'create_site,site_manager/create_site', 'site_manager,site_manager/site_manager', 
		'site_eshop,/site_eshop/site_eshop', 'site_eshop_product,/site_eshop/site_eshop_product', 'site_eshop_invoice,/site_eshop/site_eshop_invoice', 
		'eshop_editor,page_editor/eshop_editor', 'product_layout,/page_editor/product_layout', 'legalPack,/site_legal/legalPack', 
		'site_publisher,/site_manager/publisher', 'contentFeed,/page_editor/contentfeed', 'topic_editor,page_editor/topic_editor',
		'link_chooser,/dialogs/link_chooser','colourpicker,dialogs/colourpicker','help,dialogs/help','flash_editor,page_editor/flash_editor','advice,dialogs/advice',
		'pageproperties,dialogs/pageproperties', 'helpers,global/helpers', 'session,global/session', 'link_editor,page_editor/links_editor', 'youtube_editor,page_editor/youtube_editor', 'gallery_editor,page_editor/gallery_editor');

	CC.content = OO.create(CONTENT.ContentController);
	CC.dialogs = OO.create(DIALOG.MainController);
	CC.alerts = OO.create(DIALOG.AlertController);
	CC.user = OO.create(USER.UserController);
	CC.wait = OO.create(DIALOG.waitController);

	window.CONST = CC.oConstants = OO.create(CC.cConstants);
	
	EVENT.attachEvents(window, 'unload', CC.unloadWindowFn);
}

CC.unloadWindowFn = function() {
	var list = [ 'content', 'dialogs', 'alerts', 'user', 'wait' ];
	var i = list.length-1;
	do { 
		OO.dispose(CC[list[i]]);
		delete CC[list[i]];
	} while (i--);
	
	delete CC.messenger;
	
	window.CC = null;
	
	window.DIALOG = null;
	window.USER = null;
	window.CONTENT = null;
}

PAGE.onload(function() { CC.init(); });

CC.cConstants = function() {
	this.inherit(OO.Listener);
}
CC.cConstants.prototype = {
	initSelf: function() {
		CM.registerListener(this, 'constants');
		CM.sendRequest('utility', 'requestConstants');
		this.constats = { };
	},
	
	listenToMessage: function(message, param) {
		switch (message) {
			case 'setConstants'	:	this.setupConstants(param); break;
		}
	},
	
	setupConstants: function(param) {
		var key;
		
		for (key in param.constants) if (typeof(this[key]) == 'undefined')this[key] = param.constants[key];
	}
	
}

ERROR = {
	handle: function(e) {
		console.log(e);
	}	
}