var pb_applications_Actions = Class.create({
	executeCommand: function(appname,actionClass,command,parameters,onDone) {
		var actionFile = pb.core.system.getLibraryPath() + 'plasticbriqFramework/actions/_application_action.php';
		parameters.appname = appname;
		parameters.actionClass = actionClass;
		parameters.command = command;

		pb.core.actions.execute(actionFile,command,parameters,onDone);
	},
	
	executeAndPutResultIntoContainer: function(appname,actionClass,command,parameters,container,useInnerHTML,append,taskId) {
		var actionFile = pb.core.system.getLibraryPath() + 'plasticbriqFramework/actions/_application_action.php';
		parameters.appname = appname;
		parameters.actionClass = actionClass;
		
		pb.core.actions.executeAndPutResultIntoContainer(actionFile,command,parameters,container,useInnerHTML,append,taskId);
	},
	
	// Es una envoltura del periodicalUpdater de prototype, solo que incluye el comando y la plantilla
	periodicalUpdater: function(appname,actionClass,command,parameters,containerId,frequency,decay) {
		var actionFile = pb.core.system.getLibraryPath() + 'plasticbriqFramework/actions/_application_action.php';
		parameters.command = command;
		parameters.style = pb.core.system.getCurrentStyle();
		parameters.appname = appname;
		parameters.actionClass = actionClass;
		pb.core.actions.periodicalUpdater(actionFile,command,parameters,containerId,frequency,decay);
	}
}); // pb_applications_Actions

var pb_applications_Application = Class.create({
	name:'',
	mainActionClass:'',

	init: function(appName,actionClass) {
		this.name = appName;
		this.mainActionClass = actionClass;
	},
	
	getName: function() { return this.name; },
	getMainActionClass: function() { return this.mainActionClass; },
	
	executeCommand: function(command,parameters,onDone,actionClass) {
		if (actionClass) {
			pb.applications.actions.executeCommand(this.name,actionClass,command,parameters,onDone);
		}
		else {
			pb.applications.actions.executeCommand(this.name,this.mainActionClass,command,parameters,onDone);
		}
	},
	
	executeAndPutResultIntoContainer: function(command,parameters,container,useInnerHTML,append, actionClass,taskId) {
		if (actionClass) {
			pb.applications.actions.executeAndPutResultIntoContainer(this.name,actionClass,command,parameters,container,useInnerHTML,append,taskId);
		}
		else {
			pb.applications.actions.executeAndPutResultIntoContainer(this.name,this.mainActionClass,command,parameters,container,useInnerHTML,append,taskId);
		}
	},
	
	periodicalUpdater: function(command,parameters,containerId,frequency,decay,actionClass) {
		if (actionClass) {
			pb.applications.actions.periodicalUpdater(this.name,actionClass,command,parameters,containerId,frequency,decay);
		}
		else {
			pb.applications.actions.periodicalUpdater(this.name,this.mainActionClass,command,parameters,containerId,frequency,decay);
		}
	}
}); // pb_applications_Application

var pb_applications_UserBar = Class.create({
	load: function(containerName) {
		
	},
	
	clearSelection: function(){
		//pb.controls.controlManager.mouseUp('userBar_messagesButton')
	},
	
	showLogInWindow: function() {
		pb.applications.loginWindow.showLogInWindow({type:'noacl'});
	},
	
	showUserPreferences: function(windowTitle) {
		var prefWindow = pb.applications.modalWindowManager.getItem('userPreferencesWindow');
		if (!prefWindow) {
			prefWindow = new pb_applications_ModalWindow('userPreferencesWindow',windowTitle,600,550,600,550);
			prefWindow.setResizable(true);
			pb.applications.modalWindowManager.addItem(prefWindow);
		}
		prefWindow.open();
		prefWindow.setContent(pb.core.loaderAnimation.getLoaderContainerText('userPreferencesWindowLoader'));
		prefWindow.setContentWithURL(this.getActionFile(),
									'showUserPreferences',{},
									new pb_core_Task('loadUserPreferencesWindow','Loading user preferences'));
	},
	
	showSystemSettings: function(windowTitle) {
		var settingsWin = pb.applications.modalWindowManager.getItem('systemSettingsWindow');
		if (!settingsWin) {
			settingsWin = new pb_applications_ModalWindow('systemSettingsWindow',windowTitle,850,550,850,550);
			settingsWin.setResizable(true);
			pb.applications.modalWindowManager.addItem(settingsWin);
		}
		settingsWin.open();
		settingsWin.setContent(pb.core.loaderAnimation.getLoaderContainerText('systemSettingsWindowLoader'));
		settingsWin.setContentWithURL(this.getActionFile(),
									'showSystemSettings',{},
									new pb_core_Task('loadSystemSettings','Loading system settings'));
	},
	
	showUserProfile: function(windowTitle) {
		var userProfileWin = pb.applications.modalWindowManager.getItem('userProfileWindow');
		if (!userProfileWin) {
			userProfileWin = new pb_applications_ModalWindow('userProfileWindow',windowTitle,700,350,700,450);

			var leftPos = parseInt(pb.core.browserWindow.getWidth()/2)-parseInt(userProfileWin.getWidth()/2);
			userProfileWin.setLeftPos(leftPos + 'px');

			var topPos = parseInt(pb.core.browserWindow.getHeight()/2)-parseInt(userProfileWin.getHeight()/2);
			userProfileWin.setTopPos(topPos + 'px');
			
			userProfileWin.setResizable(true);
			pb.applications.modalWindowManager.addItem(userProfileWin);
		}
		userProfileWin.open();
		userProfileWin.setContent(pb.core.loaderAnimation.getLoaderContainerText('userProfileWindowLoader'));
		userProfileWin.setContentWithURL(this.getActionFile(),
									'showUserProfile',{},
									new pb_core_Task('loadUserProfile','Loading user profile'));
	},

	saveUserPreferences: function(formId) {
		this.saveForm(formId,'saveUserPreferences','saveUserPreferencesTask','userPreferencesWindow')
	},

	saveSystemSettings: function(formId) {
		this.saveForm(formId,'saveSystemSettings','saveSystemSettingsTask','systemSettingsWindow');
	},

	saveUserProfile: function(formId) {
		this.saveForm(formId,'saveUserProfile','saveUserProfileTask','userProfileWindow');
	},
	
	saveForm: function(formId,command,taskId,windowId) {
		var form = $(formId);
		if (form) {
			var params = form.serialize(true);
			pb.core.activityMonitor.addTask(new pb_core_Task(taskId,'Saving changes'));
			pb.core.actions.execute(this.getActionFile(),command,params,
				function(responseText) {
					pb.core.activityMonitor.endTask(taskId);
					var win = pb.applications.modalWindowManager.getItem(windowId);
					if (win) {
						win.close();
					}
				}
			);
		}
	},
	
	closeSystemSettingsWindow: function() {
		this.closeWindow('systemSettingsWindow');
	},
	
	closeUserProfileWindow: function() {
		this.closeWindow('userProfileWindow');
	},
	
	closeUserPreferencesWindow: function() {
		this.closeWindow('userPreferencesWindow');
	},
	
	closeWindow: function(windowId) {
		var win = pb.applications.modalWindowManager.getItem(windowId);
		if (win) {
			win.close();
		}
	},
	
	getActionFile:function() {
		return pb.core.system.getLibraryPath() + 'plasticbriqFramework/applications/_user_bar_actions.php'
	}
}); // pb_applications_UserBar

var pb_applications_LoginWindow = Class.create({
	showLogInWindow: function(loginParams) {
		var container = document.createElement('div');
		Element.extend(container);
		container.id = 'logInWindow';
		document.body.appendChild(container);
		pb.core.activityMonitor.addTask(new pb_core_Task('showLoginTask','Show login window'));
		pb.core.actions.executeAndPutResultIntoContainer(this.actionFile(),
			'printLogIn',loginParams,'logInWindow',true,false,'showLoginTask');
	},
	
	closeLogInWindow: function() {
		if ($('logInWindow')) {
			$('logInWindow').remove();
		}
	},
	
	actionFile: function() {
		return pb.core.system.getLibraryPath() + 'plasticbriqFramework/applications/_session.php';
	}
}); // pb_applications_LoginWindow

var pb_applications_Window = Class.create(pb_core_Container,{
	
	draggable: null,
	startCallback: null,
	dragCallback: null,
	endCallback: null,
	
	resizerDraggable: null,
	startResizeCallback: null,
	resizeCallback: null,
	endResizeCallback: null,
	
	resizable: false,
	
	changeCallback: null, // Esto se podría usar para limitar el movimiento de las ventanas
	
	iconTitleLeft: null,
	iconTitleRight: null,
	iconTitleClose: null,
	
	contentMaxWidth: null,
	contentMaxHeight: null,

	container: null,
	titleNode: null,
	content: null,
	titleLeft: null,
	titleClose: null,
	titleRight: null,
	
	minWidth: 0,
	minHeight: 0,
	
	Window: function(identifier,title,minWidth,minHeight,width,height){
		this.Container(identifier);
		this.initIcons();
		this.createWindow(title,minWidth,minHeight,width,height);		
		this.Node(this.identifier);
		this.type = 'window';
	},
	
	initialize: function(identifier,title,minWidth,minHeight,width,height){
		this.Window(identifier,title,minWidth,minHeight,width,height);
		pb.applications.windowManager.addItem(this);
	},
	
	setResizable: function(resizable){
		this.resizable = resizable;
		this.updateResizer();
	},

	getResizable: function(){
		return this.resizable;
	},
	
	open: function(){
		this.show();
	},
	
	close: function(){
		this.hide();
	},
	
	show: function($super){
		this.moveIntoViewport();
		$super();
	},
	
	hide: function($super){
		this.moveIntoViewport();		
		$super();
	},
	
	moveIntoViewport: function(){
		var viewport = pb.core.system.getViewportSize();
		if ((this.getLeftPos() + this.getWidth())<0){
			this.setLeftPos(0);
		}
		if ((this.getTopPos() + this.getHeight())<0){
			this.setTopPos(0);
		}
		
		if ((this.getLeftPos())>viewport[0]){
			this.setLeftPos(viewport[0]-this.getWidth());
		}

		if ((this.getTopPos())>viewport[1]){
			this.setTopPos(viewport[1]-this.getHeight());
		}
	},
	
	setContentMaxWidth: function(contentMaxWidth){
		this.contentMaxWidth = contentMaxWidth;
		if (this.content) this.content.style.maxWidth = contentMaxWidth;
	},
	
	setContentMaxHeight: function(contentMaxHeight){
		this.contentMaxHeight = contentMaxHeight;
		if (this.content) this.content.style.maxHeight = contentMaxHeight;
	},
	
	initIcons: function(){
		this.iconTitleLeft = pb.core.resourceManager.getWindowIconTitleLeft();
		this.iconTitleRight = pb.core.resourceManager.getWindowIconTitleRight();
		this.iconTitleClose = pb.core.resourceManager.getWindowIconTitleClose();
	},
	
	initContainerStyle: function(container){
		container.style.position = 'relative';
		container.style.color = 'black';
		container.style.zIndex = 1000;
		container.style.fontFamily = 'verdana';
	},
	
	initTitleStyle: function(titleContainer,leftNode,centerNode,rightNode,closeNode){
		
		titleContainer.style.width = '100%';
		titleContainer.style.textAlign = 'center';
		titleContainer.style.fontSize = '12px';
		
		leftNode.style.position = 'absolute';
		leftNode.style.left = 0;
		leftNode.style.background = "url(" + this.iconTitleLeft + ")";
		
		closeNode.style.position = 'absolute';
		closeNode.style.background = "url(" + this.iconTitleClose + ')';
		closeNode.style.cursor = 'default';
				
		rightNode.style.position = 'absolute';
		rightNode.style.right = 0;
		rightNode.style.top = 0;
		rightNode.style.background = "url(" + this.iconTitleRight + ")";		

		centerNode.style.position = 'absolute';
		centerNode.style.top = 0;
		centerNode.style.backgroundColor = '#b2b2b2';
		centerNode.style.cursor = 'default';
		centerNode.style.borderTopStyle = 'solid';
		centerNode.style.borderTopColor = '#b2b2b2';
		centerNode.style.borderTopWidth = '1px';
	},
	
	initContentStyle: function(content){
		content.style.color = 'black';
		content.style.backgroundColor = '#e1e1e1';
		// content.style.width = '100%';
		// content.style.height = '100%';
		content.style.overflow = 'auto';
		content.style.fontSize = '11px';
		content.style.borderLeftStyle = 'solid';
		content.style.borderLeftColor = '#999';
		content.style.borderLeftWidth = '1px';
		content.style.borderBottomStyle = 'solid';
		content.style.borderBottomColor = '#999';
		content.style.borderBottomWidth = '1px';
		content.style.borderRightStyle = 'solid';
		content.style.borderRightColor = '#999';
		content.style.borderRightWidth = '1px';
		content.style.marginRight= '-2px';
	},
	
	setLeftPos: function(pos){
		if (this.htmlContainer) this.htmlContainer.style.left = parseInt(pos) + 'px';
		this.updateDraggable();
	},
	
	setTopPos: function(pos){
		if (this.htmlContainer) this.htmlContainer.style.top = parseInt(pos) + 'px';
		this.updateDraggable();
	},
	
	getLeftPos: function(){
		if (this.htmlContainer){
			if (this.htmlContainer.style.left) {
				return parseInt(this.htmlContainer.style.left);
			}
			else {
				return this.htmlContainer.offsetLeft;
			}
		}
		return 0;
	},
	
	getTopPos: function(){
		if (this.htmlContainer){
			if (this.htmlContainer.style.top){
				return parseInt(this.htmlContainer.style.top);
			}
			else {
				return this.htmlContainer.offsetTop;
			}
		}
		return 0;
	},
		
	getWidth: function(){
		if (this.htmlContainer)	{
			if (this.htmlContainer.style.width){
				return parseInt(this.htmlContainer.style.width);
			}
			else {
				return this.htmlContainer.offsetWidth;				
			}

		}
			
		return 0;
	},
	
	getHeight: function(){
		if (this.htmlContainer)	{
			if (this.htmlContainer.style.height){
				return parseInt(this.htmlContainer.style.height);
			}
			else {
				return this.htmlContainer.offsetHeight;
			}

		}
			
		return 0;
	},
	
	setStartCallback: function(callback){
		this.startCallback = callback;
		this.updateDraggable();
	},
	
	setDragCallback: function(callback){
		this.dragCallback = callback;
		this.updateDraggable();
	},
	
	setEndCallback: function(callback){
		this.endCallback = callback;
		this.updateDraggable();
	},
	
	startDrag: function(draggable,event){
		if (this.startCallback) this.startCallback(draggable,event);
	},
	
	drag: function(draggable,event){
		if (this.dragCallback) this.dragCallback(draggable,event);
	},
	
	endDrag: function(draggable,event){
		if (this.endCallback) this.endCallback(draggable,event);
	},
	
	getDraggableZIndex: function(){
		return pb.applications.windowManager.getFrontZIndex() + 50;
	},
	
	updateResizer: function(){

		if (this.handle) this.handle.remove();
		
		if (this.resizable){

			if (!this.container) return;

			this.handle = pb.core.resizeHandle.getContainer(this.identifier + '_resizer');

			var currentWindow = this;
			
			this.container.appendChild(this.handle);
			
			if (this.minHeight==0) this.minHeight = this.titleNode.offsetHeight + this.handle.offsetWidth + 4 + 50; // 4 es por los bordes, 50 un extra pa que no quede tan pequeñico

			this.updateResizerPos();			
			
			// this.resizerDraggable = new Draggable(this.identifier + '_resizer', { 
			// 				onStart: function(){ if (currentWindow.startResizeCallback) currentWindow.startResizeCallback(); },
			// 				onDrag: function() { currentWindow.updateSize.bind(currentWindow); if (currentWindow.resizeCallback) currentWindow.resizeCallback(); },
			// 				onEnd: function(){ if (currentWindow.endResizeCallback) currentWindow.endResizeCallback(); } });

			this.resizerDraggable = new Draggable(this.identifier + '_resizer', { 
					onDrag: currentWindow.updateSize.bind(currentWindow),
					onEnd: currentWindow.updateSize.bind(currentWindow) });
					
			this.updateSize();
		}
	},
	
	updateResizerPos: function() {
		// var posX = this.container.positionedOffset().left;
		// var posY = this.container.positionedOffset().top;
		
		var posX = 0;
		var posY = 0;
		
		var width = parseInt(this.container.style.width);
		var height = parseInt(this.container.style.height);
	
		var resizerX = width + posX -this.handle.offsetWidth;
		var resizerY = height + posY -this.handle.offsetHeight;
		
		this.handle.style.left = (resizerX) + 'px';
		this.handle.style.top = (resizerY) + 'px';
	},
	
	updateSize: function() {
		
		if (!this.container || !this.handle) return;
		
		var posX = 0;//this.container.positionedOffset().left;
		var posY = 0;//this.container.positionedOffset().top;

		var resizeX = this.handle.positionedOffset().left;
		var resizeY = this.handle.positionedOffset().top;
		
		var width = (resizeX - posX + this.handle.offsetWidth);
		var height = (resizeY - posY + this.handle.offsetHeight);
		
		if (width<this.minWidth) width = this.minWidth;
		if (height<this.minHeight) height = this.minHeight;
		
		this.container.style.width = width + 'px';
		this.container.style.height = height + 'px';
		
		this.content.style.width = (parseInt(this.container.style.width)-2) + 'px';
		this.titleNode.style.width = (parseInt(this.container.style.width) - this.titleLeft.offsetWidth - this.titleRight.offsetWidth + 2) + 'px';
		
		this.content.style.height = (parseInt(this.container.style.height) - this.titleNode.offsetHeight) + 'px';
		
		this.updateResizerPos();
		
		//this.content.style.height = (this.content.offsetHeight - this.handle.offsetHeight) + 'px';
	},

	
	updateDraggable: function(){
		if (this.draggable) this.draggable.destroy();
		this.draggable = new Draggable(this.identifier, { zindex: this.getDraggableZIndex(),
			starteffect: null,endeffect: null,handle: this.identifier + '_title',
			onStart:this.startDrag.bind(this),onDrag: this.drag.bind(this),onEnd: this.endDrag.bind(this)
		});
	},
		
	getContainerAction: function(){
		return "pb.applications.windowManager.sendToFront('" + this.identifier + "');";
	},
	
	getCloseAction: function(){
		return "pb.applications.windowManager.getItem('" + this.identifier + "').close();event.cancelBubble = true;return false;";
	},
	
	updateWindowSize: function(leftIconSize,closeIconSize,rightIconSize){
		if (leftIconSize){
			this.titleLeft.style.width = leftIconSize.width;
			this.titleLeft.style.height = leftIconSize.height;
		}
		
		if (closeIconSize){
			this.titleClose.style.width = closeIconSize.width;
			this.titleClose.style.height = closeIconSize.height;
		}
		
		if (rightIconSize){
			this.titleRight.style.width = rightIconSize.width;
			this.titleRight.style.height = rightIconSize.height;
		}
	},
	
	createWindow: function(titleText,minWidth,minHeight,width,height){

		var container = new Element('div',{ 'id': this.identifier });

		if (width) container.style.width = width;
		if (height) container.style.height = height;
		if (minWidth) {
			container.style.minWidth = minWidth;
			this.minWidth = minWidth;
		}
		if (minHeight) {
			container.style.minHeight = minHeight;
			this.minHeight = minHeight;
		}
		
		this.initContainerStyle(container);
		container.setAttribute("onclick", this.getContainerAction());
		//container.setAttribute("onmousedown", this.getContainerAction());
		container.style.zIndex = 1000;
		if (width) container.style.width = parseInt(width) + 'px';
		if (height) container.style.height = parseInt(height) + 'px';

		var title = new Element('div',{ 'id': this.identifier + '_title' });
		title.style.height = '20px';
				
		var titleLeft = new Element('div',{ 'width': '7px','height': '19px' });
		titleLeft.style.left = 0;
		titleLeft.style.width = '8px';
		titleLeft.style.height = '20px';
		
		var titleClose = new Element('div',{ 'id': this.identifier + '_close' });
		titleClose.style.width = '13px';
		titleClose.style.height = '13px';
		titleClose.style.left = '5px';
		titleClose.style.top = '4px';
		titleClose.style.zIndex = 1010;
		
		titleClose.setAttribute("onclick",this.getCloseAction());
		
		var titleRight = new Element('div');
		titleRight.style.width = '8px';
		titleRight.style.height = '20px';


		var titleCenter = new Element('div',{ 'id': this.identifier + '_title_center' });
		titleCenter.innerHTML = titleText;
		titleCenter.style.left = '7px';
		if (width) titleCenter.style.width = (parseInt(width)-14) + 'px';
		titleCenter.style.height = '19px';
		titleCenter.style.lineHeight = '19px';
		
		this.initTitleStyle(title,titleLeft,titleCenter,titleRight,titleClose);
		
		var content = new Element('div',{ 'id': this.identifier + '_content' });
		
		content.style.position = 'relative';
		content.style.top = '-1px';
		content.style.width = (parseInt(width)-2) + 'px';
		if (height){
			content.style.height = (parseInt(height) - 19) + 'px';			
		}

		this.initContentStyle(content);

		this.titleNode = titleCenter;
		this.content = content;
		this.titleLeft = titleLeft;
		this.titleRight = titleRight;
		this.titleClose = titleClose;
		
		var obj = this;
		pb.media.imageUtils.getImageSize(this.iconTitleLeft,function(size){ obj.updateWindowSize(size); });
		pb.media.imageUtils.getImageSize(this.iconTitleClose,function(size){ obj.updateWindowSize(null,size); });
		pb.media.imageUtils.getImageSize(this.iconTitleRight,function(size){ obj.updateWindowSize(null,null,size); });
				
		title.appendChild(titleLeft);
		title.appendChild(titleClose);
		title.appendChild(titleCenter);
		title.appendChild(titleRight);
		
		container.appendChild(title);
		container.appendChild(content);
		this.container = container;
		
		document.body.appendChild(container);
		
		if (container.offsetLeft) container.style.left = container.offsetLeft + 'px';
		if (container.offsetTop) container.style.top = container.offsetTop + 'px';
		this.updateDraggable();
		
		if (this.minWidth==0) this.minWidth = this.titleLeft.offsetWidth + this.titleClose.offsetWidth + this.titleRight.offsetWidth + 50;
	},
	
	setContent: function(content) {
		if (this.content) this.content.innerHTML = content;
	},

	setContentWithURL: function(url,command,parameters,task) {
		if (!task){
			var task = new pb_core_Task(pb.core.counter.getId(this.identifier),pb.core.localizedString.get('Loading window content'));
		}
		
		pb.core.activityMonitor.addTask(task);
		pb.core.actions.executeAndPutResultIntoContainer(url,command,parameters,this.content.id,true,false,task.identifier);
	}
	
}); // pb_applications_Window

var pb_applications_HUDWindow = Class.create(pb_applications_Window,{
	
	HUDWindow: function(identifier,title,minWidth,minHeight,width,height){
		this.Window(identifier,title,minWidth,minHeight,width,height);
		this.type = 'hudWindow';
	},
	
	initialize: function(identifier,title,minWidth,minHeight,width,height){
		this.HUDWindow(identifier,title,minWidth,minHeight,width,height);
		pb.applications.hudWindowManager.addItem(this);
	},
	
	getContainerAction: function(){
		return "pb.applications.hudWindowManager.sendToFront('" + this.identifier + "');event.cancelBubble = true;return false;";
	},
	
	getCloseAction: function(){
		return "pb.applications.hudWindowManager.getItem('" + this.identifier + "').close();event.cancelBubble = true;return false;";
	},
	
	initIcons: function(){
		this.iconTitleLeft = pb.core.resourceManager.getHUDWindowIconTitleLeft();
		this.iconTitleRight = pb.core.resourceManager.getHUDWindowIconTitleRight();
		this.iconTitleClose = pb.core.resourceManager.getHUDWindowIconTitleClose();
	},
	
	initContainerStyle: function($super,container){
		$super(container);
		container.style.color = 'white';
	},
	
	initTitleStyle: function($super,titleContainer,leftNode,centerNode,rightNode,closeNode){
		$super(titleContainer,leftNode,centerNode,rightNode,closeNode);
		
		titleContainer.style.width = '100%';
		titleContainer.style.textAlign = 'center';
		titleContainer.style.fontSize = '12px';
		
		leftNode.style.position = 'absolute';
		leftNode.style.left = 0;
		leftNode.style.background = "url(" + this.iconTitleLeft + ")";
		
		closeNode.style.position = 'absolute';
		closeNode.style.background = "url(" + this.iconTitleClose + ')';
		closeNode.style.cursor = 'default';
				
		rightNode.style.position = 'absolute';
		rightNode.style.right = 0;
		rightNode.style.top = 0;
		rightNode.style.background = "url(" + this.iconTitleRight + ")";		

		centerNode.style.position = 'absolute';
		centerNode.style.top = 0;
		centerNode.style.backgroundColor = '#363636';
		centerNode.style.cursor = 'default';
		centerNode.style.borderTopStyle = 'solid';
		centerNode.style.borderTopColor = '#999';
		centerNode.style.borderTopWidth = '1px';
	},
		
	initContentStyle: function($super,content){
		$super(content);
		content.style.color = 'white';
		content.style.backgroundColor = '#2a2a2a';
		content.style.borderLeftColor = '#999';
		content.style.borderBottomColor = '#999';
		content.style.borderRightColor = '#999';
	},
	
	getDraggableZIndex: function(){
		return pb.applications.hudWindowManager.getFrontZIndex() + 50;
	}
	
}); // pb_applications_HUDWindow


var pb_applications_ModalWindow = Class.create(pb_applications_Window,{
	ModalWindow: function(identifier,title,minWidth,minHeight,width,height){
		this.Window(identifier,title,minWidth,minHeight,width,height);
		this.type = 'modalWindow';
		if (this.container) this.container.style.position = 'fixed';
	},
	
	getContainerAction: function(){
		return "pb.applications.modalWindowManager.sendToFront('" + this.identifier + "');event.cancelBubble = true;return false;";
	},
	
	getCloseAction: function(){
		return "pb.applications.modalWindowManager.getItem('" + this.identifier + "').close();event.cancelBubble = true;return false;";
	},
	
	initialize: function(identifier,title,minWidth,minHeight,width,height){
		this.ModalWindow(identifier,title,minWidth,minHeight,width,height);
		pb.applications.hudWindowManager.addItem(this);
	},
	
	open: function(){
		this.show();
	},
	
	close: function(){
		this.hide();
	},
	
	show: function($super){
		var container = $(this.identifier + '_background');
		if (container){
			container.show();
		}
		else {
			container = pb.core.modalBackground.getContainer(this.identifier + '_background');
			document.body.appendChild(container);	
		}
		
		$super();
	},
	
	hide: function($super){
		var container = $(this.identifier + '_background');
		if (container){
			container.remove();
		}
		$super();
	},
	
	getDraggableZIndex: function(){
		return pb.applications.modalWindowManager.getFrontZIndex() + 50;
	}
	
}); // pb_applications_ModalWindow

var pb_applications_WindowManager = Class.create(pb_core_DepthManager,{	
	
	WindowManager: function(){
		this.DepthManager(1000,2000,false);
	},
	
	initialize: function(){
		this.WindowManager();
	}
}); // pb_applications_WindowManager

var pb_applications_HUDWindowManager = Class.create(pb_core_DepthManager,{	

	HUDWindowManager: function(){
		this.DepthManager(3000,4000,false);
	},
	
	initialize: function(){
		this.HUDWindowManager();
	}

}); // pb_applications_HUDWindowManager

var pb_applications_ModalWindowManager = Class.create(pb_core_DepthManager,{	

	ModalWindowManager: function(){
		this.DepthManager(5000,6000,false);
	},
	
	initialize: function(){
		this.ModalWindowManager();
	},
	
	addItem: function($super,item) {
		$super(item);
		if (typeof item.open == 'function') item.open();
	}

}); // pb_applications_ModalWindowManager

var pb_applications_Launcher = Class.create({
	launch: function(appname) {
		pb.core.actions.execute(system.getLibraryPath() + 'plasticbriqFramework/applications/_applications.php',
			'getApplicationUrl',{appname:appname},
			function(responseText) {
				if (responseText) {
					window.location.href = responseText;
				}
			});
	}
});

// Este es el botón de notificaciones. Es algo especial porque tiene que actualizarse cada cierto tiempo, con las
// notificaciones que hay pendientes de leer.
var pb_applications_NotificationsItem = Class.create(pb_controls_Button,{
	NotificationsItem: function(identifier,action,disabled,selected) {
		this.Button(identifier,action,disabled,selected);
		this.type = 'button';
		this.initTimer();
	},

	initialize: function(identifier,action,disabled,selected) {
		this.NotificationsItem(identifier,action,disabled,selected);
	},
	
	reload: function(){
		//console.log('reloading notifications item');
		var actionURL = pb.core.system.getLibraryPath() + 'plasticbriqFramework/applications/_user_bar_actions.php';
		pb.core.actions.executeAndPutResultIntoContainer(actionURL,'getUnreadedNotifications',{},'notificationsIcon_unreadedNotifications',true);
		
	},
	
	initTimer: function() {
		var actionURL = pb.core.system.getLibraryPath() + 'plasticbriqFramework/applications/_user_bar_actions.php';
		pb.core.actions.periodicalUpdater(actionURL,'getUnreadedNotifications',{},
					'notificationsIcon_unreadedNotifications',60);
	}
});

var pb_applications = Class.create({
	actions: new pb_applications_Actions(),
	application:null,
	userBar: new pb_applications_UserBar(),
	loginWindow: new pb_applications_LoginWindow(),
	windowManager: new pb_applications_WindowManager(),
	hudWindowManager: new pb_applications_HUDWindowManager(),
	modalWindowManager: new pb_applications_ModalWindowManager(),
	launcher: new pb_applications_Launcher()
});