function setWindowLoadedFlagFunction() {
	pb.core.system.windowLoaded = true;
}

if (window.addEventListener) {
	window.addEventListener('load',setWindowLoadedFlagFunction,false);
}
else if (window.attachEvent) {
	window.attachEvent('onload',setWindowLoadedFlagFunction);
}

var pb_core_System = Class.create({
	currentStyle: 'undefined',
	libraryPath: '',
	downloaderUrl: '',
	operatingSystem: 'Unknown Operating System',
	windowLoaded:false,
	
	getLibraryPath: function() { return this.libraryPath; },
	getCurrentStyle: function() { return this.currentStyle; },
	getDownloaderUrl: function() { return this.downloaderUrl; },
	
	initialize: function() {
		this.browser.name = this.searchString(this.dataBrowser) || "Unknown browser";
		this.browser.version = this.searchVersion(navigator.userAgent) ||
				this.searchVersion(navigator.appVersion) ||
				"Unknown browser version";
		this.browser.OS = this.searchString(this.dataOS) || "Unknown OS";
	},
	
	isWindowLoaded: function() {
		return this.windowLoaded;
	},
	
	importJavaScript: function(fileURL) {
		var head = document.getElementsByTagName('head').item(0);
		script = document.createElement('script');
		script.src = fileURL;
		script.type = 'text/javascript';
		head.appendChild(script);
	},
	
	dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		],

	browser: {
		IE: Prototype.Browser.IE,
		Opera: Prototype.Browser.Opera,
		Camino: (navigator.vendor=='Camino'),
		WebKit: Prototype.Browser.WebKit,
		Gecko: Prototype.Browser.Gecko,
		MobileSafari: Prototype.Browser.MobileSafari,
		OmniWeb: (navigator.userAgent=='OmniWeb'),
		Safari: Prototype.Browser.WebKit,
		Firefox: (navigator.userAgent.indexOf("Firefox")!=-1)
	},
	
	platform: {
		Win: ((navigator.platform=='Win')||(navigator.platform=='Win32')),
		Windows: ((navigator.platform=='Win')||(navigator.platform=='Win32')),
		Mac: (navigator.platform=='Mac'),
		Linux: (navigator.platform=='Linux'),
		iPhone: (navigator.userAgent=='iPhone')		
	},
	
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
		return "";
	},

	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return -1;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	
	evalAllScripts: function(elementId,contextMessage){
		var scripts = $(elementId).select('script');
		var x;
		for (x in scripts){
			try {
				if (scripts[x].innerHTML){
					var result = eval(scripts[x].innerHTML);
				}
			}
			catch(err){
				var errorMessage = err;
				if (contextMessage){
					errorMessage = 'system.evalAllScripts. ' + contextMessage + ': ' + err;
				}
				alert(errorMessage);
			}
		}	
	},
	
	hideElementsWithTagName: function(tagName)
	{

		//var elements = document.getElementsByTagName(tagName);
		var elements = $$(tagName);
		for (i=0;i<elements.length;i++)
		{
			if (elements[i]!=null && elements[i]!="0"){
				elements[i].style.visibility = 'hidden';
			}
		}
	},

	showElementsWithTagName: function(tagName)
	{
		//var elements = document.getElementsByTagName(tagName);
		var elements = $$(tagName);

		for (i=0;i<elements.length;i++)
		{
			if (elements[i]!=null  && elements[i]!="0"){
				elements[i].style.visibility = 'visible';
			}
		}
	},
	
	getViewportSize: function() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			var size = document.viewport.getDimensions();
			myWidth = size.width;
			myHeight = size.height;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}

		return [myWidth,myHeight];
	},
	
	getScreenSize: function(){
		return [ screen.availWidth, screen.availHeight ];
	},
	
	getScrolls: function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
    		scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			var size = document.viewport.getScrollOffsets();
			scrOfY = size[0];
			scrOfX = size[1];
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		
		return [ scrOfX, scrOfY ];
	}
});  // pb_core_System

var pb_core_Actions = Class.create({
	execute: function(actionURL,command,parameters,onDone) {
		parameters.command = command;
		parameters.style = pb.core.system.getCurrentStyle();
		new Ajax.Request(actionURL, {
			method:'post',
			parameters: parameters,
			onSuccess: function(transport) {
				if (onDone) {
					onDone(transport.responseText);
				}
			}
		});
	},

	executeAndPutResultIntoContainer: function(actionURL,command,params,containerId,useInnerHTML,append,taskId) {
		var container = $(containerId);
		if( container ) {
			document.fire("ws:reloading_container",{ name: containerId });
			this.execute(actionURL,command,params,
				function(responseText) {
					if (useInnerHTML) {
						if (append) {
							container.innerHTML = container.innerHTML + responseText;
						}
						else {
							container.innerHTML = responseText;
						}
					}
					else {
						if (append) {
							container.value = container.value + responseText;
						}
						else {
							container.value = responseText;
						}
					}
					pb.core.system.evalAllScripts(containerId);
					document.fire('ws:init');
					if (taskId) {
						pb.core.activityMonitor.endTask(taskId);
					}
				});
		}
	},
	
	// Es una envoltura del periodicalUpdater de prototype, solo que incluye el comando y la plantilla
	periodicalUpdater: function(actionURL,command,parameters,containerId,frequency,decay) {
		parameters.command = command;
		parameters.style = pb.core.system.getCurrentStyle();
		if (!frequency) frequency = 60;
		if (!decay) decay = 1;
		new Ajax.PeriodicalUpdater(containerId, actionURL, {
			method:'post',
			parameters: parameters,
			frequency:frequency,
			decay:decay
		});
	}
}); // pb_core_Actions

var pb_core_Page = Class.create({
	
}); // pb_core_Page

var pb_core_Menu = Class.create({
	
}); // pb_core_Menu

var pb_core_Navigation = Class.create({
	
}); // pb_core_Navigation

var pb_core_Hash = Class.create({
	
}); // pb_core_Hash

var pb_core_Session = Class.create({
	logIn: function(loginParams) {
		pb.core.activityMonitor.addTask(new pb_core_Task('logIn','Login'));
		pb.core.actions.execute(this.actionFile(),'logIn',loginParams,
			function(responseText) {
				if (responseText=='OK') {
					$('logInErrorMessage').innerHTML = '';
					window.location.reload();
				}
				else {
					pb.core.activityMonitor.endTask('logIn');
					$('logInErrorMessage').innerHTML = responseText;
				}
			});
	},
	
	logOut: function() {
		pb.core.activityMonitor.addTask(new pb_core_Task('logOut','Logging out..'));
		pb.core.actions.execute(this.actionFile(),'logOut',{},
			function(responseText) {
				window.location.reload();
			});
	},
	
	actionFile: function() {
		return pb.core.system.getLibraryPath() + 'plasticbriqFramework/core/_session.php';
	}
}); // pb_core_Session

var pb_core_LocalizedString = Class.create({
	
	localizations: new Object(),
	
	get: function(string){
		if (this.localizations[string]) {
			return this.localizations[string];
		}
		return string;
	}	
}); // pb_core_LocalizedString

var pb_core_ActivityMonitorView = Class.create({
	containerId: '',

	initialize:function(containerId) {
		this.containerId = containerId;
	},
	
	showView: function() {
		if ($(this.containerId)) {
			$(this.containerId).show();
		}
	},
	
	hideView: function() {
		if ($(this.containerId)) {
			Effect.Fade(this.containerId,{duration:0.4});
		}
	}
}); // pb_core_ActivityMonitorView

var pb_core_Task = Class.create({
	identifier: '',
	title: '',
	type: '',

	initialize: function(identifier,title,type) {
		this.identifier = identifier;
		this.title = title;
		this.type = type;
	}
}); // pb_core_Task

var pb_core_ActivityMonitor = Class.create({
	views:new Array(),
	tasks:new Array(),

	registerView: function(view) {
		this.views[this.views.length] = view;
	},

	unregisterView: function(viewId) {
		var viewIndex = -1;
		for (var i=0; i<this.views.length; i++) {
			if (this.views[i].containerId==viewId) {
				viewIndex = i;
				break;
			}
		}
		this.views.splice(viewIndex,1);
	},

	addTask: function(task) {
		this.tasks[this.tasks.length] = task;
		this.checkTasks();
	},
	
	endTask: function(taskId) {
		var taskIndex = -1;
		for (var i=0; i<this.views.length; i++) {
			if (this.tasks[i].identifier==taskId) {
				taskIndex = i;
				break;
			}
		}
		this.tasks.splice(taskIndex,1);
		this.checkTasks();
	},
	
	checkTasks: function() {
		var key;
		var activeTasks = this.tasks.length;

		for ( key in this.views) {
			if (activeTasks>0) {
				if (this.views[key] && this.views[key].showView) this.views[key].showView();
			}
			else {
				if (this.views[key] && this.views[key].hideView) this.views[key].hideView();
			}
		}
	}
});	// pb_core_ActivityMonitor

var pb_core_Counter = Class.create({
	
	counter: 0,
	
	getId: function(prefix){
		this.counter++;
		return this.getLastId(prefix);
	},
	
	getLastId: function(prefix){
		return prefix + this.counter;
	}
	
}); // pb_core_Counter

var pb_core_Node = Class.create({
	identifier:'',
	type:'node',
	htmlContainer:null,
	
	Node: function(identifier) {
		this.identifier = identifier;
		if ($(identifier)) {
			this.htmlContainer = $(identifier);
		}
	},
	
	setZIndex: function(zIndex){
		if (this.htmlContainer) this.htmlContainer.style.zIndex = zIndex;
	},
	
	getZIndex: function(){
		if (this.htmlContainer) return this.htmlContainer.style.zIndex;
	},

	initialize:function(identifier) {
		this.Node(identifier);
	}
}); // pb_core_Node

var pb_core_Container = Class.create(pb_core_Node,{
	
	visible: true,
	
	Container: function(identifier){
		this.Node(identifier);
	},
	
	initialize: function(identifier){
		this.Container(identifier);
	},
	
	show: function(){
		if (this.htmlContainer) this.htmlContainer.show();
		this.visible = true;
	},
	
	hide: function(){
		if (this.htmlContainer) this.htmlContainer.hide();
		this.visible = false;
	},
	
	toggle: function(){
		if (this.htmlContainer){
			if (this.visible){
				this.htmlContainer.hide();
			}
			else {
				this.htmlContainer.show();
			}
		}
		this.visible = !this.visible;
	},
	
	setContent: function(content){
		if (this.htmlContainer){
			this.htmlContainer.innerHTML = content;
			return true;
		}
		return false;
	},
	
	setContentWithURL: function(url,command,parameters,task) {
		if (!task){
			task = new pb_core_Task(pb.core.counter.getId(this.identifier),pb.core.localizedString.get('Loading container content'));
		}
		
		pb.core.activityMonitor.addTask(task);
		pb.core.actions.executeAndPutResultIntoContainer(url,command,parameters,this.identifier,true,false,task.identifier);
	}

}); // pb_core_Container

var pb_core_ItemManager = Class.create({
	items:null,
	
	ItemManager: function(){
		this.items = Object();
	},	
	
	initialize: function(){
		this.ItemManager();
	},

	addItem: function(item) {
		if (item && item.identifier) {
			this.items[item.identifier] = item;
		}
	},

	addItemOfType: function(item,type) {
		if (item && item.type==type) this.addItem(item);
	},

	getItem: function(identifier) {
		return this.items[identifier];
	},

	removeItem: function(identifier) {
		this.items[identifier] = undefined;
	}
}); // pb_core_ItemManager


var pb_core_DepthManager = Class.create(pb_core_ItemManager,{
	
	minDepth:0,
	maxDepth:100,
	currentDepth:0,
	currentMinDepth:0,
	currentMaxDepth: 100,
	reverse: false,
	fields:null,
	inc: 1,
	
	ignoreItemsZIndex: true,
	
	DepthManager: function(minDepth,maxDepth,reverse){
		
		this.ItemManager();
		
		this.minDepth = minDepth;
		this.maxDepth = maxDepth;
		this.reverse = reverse;
		if (this.reverse){
			this.currentDepth = this.maxDepth;
			this.currentMinDepth = this.maxDepth;
			this.currentMaxDepth = this.maxDepth;
		}
		else {
			this.currentDepth = this.minDepth;

			this.currentMinDepth = this.minDepth;
			this.currentMaxDepth = this.minDepth;
		}
		this.fields = new Object();
	},
	
	setIgnoreItemsZIndex: function(ignoreItemsZIndex){
		this.ignoreItemsZIndex = ignoreItemsZIndex;
	},

	getIgnoreItemsZIndex: function(){
		return this.ignoreItemsZIndex;
	},
	
	initialize: function(minDepth,maxDepth,reverse){
		this.DepthManager(minDepth,maxDepth,reverse);
	},
	
	getFrontZIndex: function(){
		return this.currentMaxDepth;
	},
	
	addItem: function($super,item) {
		$super(item);
		this.addItemZIndex(item);
	},

	addItemOfType: function($super,item,type) {
		$super(item,type);
		this.addItemZIndex(item);
	},
	
	addItemZIndex: function(item){
		if (item && item.identifier && item.htmlContainer){
			if (this.ignoreItemsZIndex){
				this.fields[this.currentDepth] = new Array(item.identifier);
				item.setZIndex(this.currentDepth);
				document.fire('ws:zIndex_changed',{ identifier: item.identifier });
				this.incDepth();				
			}
			else {
				var depth = item.getZIndex();
				if (!depth){
					if (this.fields[depth]){
						this.fields[depth].push(item.identifier);
					}
					else {
						this.fields[this.currentDepth] = new Array(item.identifier);
					}
					this.currentDepth = depth;					
				}
				else {
					this.fields[this.currentDepth] = new Array(item.identifier);
					item.setZIndex(this.currentDepth);
					document.fire('ws:zIndex_changed',{ identifier: item.identifier });
					this.incDepth();					
				}
			}
		}
	},

	removeItem: function($super,identifier) {
		$super(identifier);
		if (this.items[identifier]){
			
			var item = this.items[identifier];
			var depth = item.getZIndex();
			var objects = this.fields[depth];
			
			for (var i=0;i<objects.length;i++){
				if (objects[i]==item){
					objects.splice(i,1);
					break;
				}
			}
		}
	},
		
	incDepth: function(){
		this.currentDepth = this.increaseDepth(this.currentDepth);
		if (this.reverse){
			this.currentMinDepth = this.currentDepth;
		}
		else {
			this.currentMaxDepth = this.currentDepth;
		}
	},
	
	increaseDepth: function(depth){
		depth = parseInt(depth);
		if (this.reverse){
			depth = depth - this.inc;
			if (depth<this.minDepth) depth = this.minDepth;
		}
		else {
			depth = depth + this.inc;
			if (depth>this.maxDepth) depth = this.maxDepth;
		}
		
		return depth;
	},
	
	decDepth: function(){
		this.currentDepth = this.decreaseDepth(this.currentDepth);
		if (this.reverse){
			this.currentMaxDepth = this.currentDepth;
		}
		else {
			this.currentMinDepth = this.currentDepth;
		}
	},
	
	decreaseDepth: function(depth){
		depth = parseInt(depth);
		if (this.reverse){
			depth = depth + this.inc;
			if (depth>this.maxDepth){
				depth = this.maxDepth;
			}
		}
		else {
			depth = depth - this.inc;
			if (depth<this.minDepth){
				depth = this.minDepth;
			}
		}
		return depth;
	},
	
	sendBackward: function(fieldId){
		if ($(fieldId) && $(fieldId).style.zIndex){
			var depth = $(fieldId).style.zIndex;
			var newDepth = this.decreaseDepth(depth);
			this.move(depth,newDepth);
		}
	},
	
	sendForward: function(fieldId){
		if ($(fieldId) && $(fieldId).style.zIndex){
			var depth = $(fieldId).style.zIndex;
			var newDepth = this.increaseDepth(depth);
			this.move(depth,newDepth);
		}
	},
	
	sendToBack: function(fieldId){
		if ($(fieldId) && $(fieldId).style.zIndex){
			var depth = $(fieldId).style.zIndex;
			this.move(depth,this.currentMinDepth);
		}		
	},
	
	sendToFront: function(fieldId){
		if ($(fieldId) && $(fieldId).style.zIndex){
			var depth = $(fieldId).style.zIndex;
			this.move(depth,this.currentMaxDepth);
		}		
		
	},
	
	applyDepth: function(objects,depth){
		if (!objects) return;
		depth = parseInt(depth);
		for (var i=0;i<objects.length;i++){
			$(objects[i]).style.zIndex = depth;
			document.fire('ws:zIndex_changed',{ fieldId: objects[i] });
		}
	},
	
	setDepth: function(fieldId,newDepth){
		if (newDepth>this.currentMaxDepth){
			this.currentMaxDepth = newDepth;
		}
		if (newDepth<this.currentMinDepth){
			this.currentMinDepth = newDepth;
		}
		
		if ($(fieldId) && $(fieldId).style.zIndex){
			var depth = $(fieldId).style.zIndex;
			this.move(depth,newDepth);
		}		
	},
	
	move: function(originDepth,destinationDepth){
		
		if (originDepth==destinationDepth) return;
		
		originDepth = parseInt(originDepth);
		destinationDepth = parseInt(destinationDepth);
		
		var objects = this.fields[originDepth];

		this.applyDepth(objects,destinationDepth);
		
		if (destinationDepth>originDepth){
			for (var j=originDepth+this.inc;j<=destinationDepth;j=j+this.inc){
				this.applyDepth(this.fields[j],j-this.inc);
				this.fields[j-this.inc] = this.fields[j];
			}
			
			this.fields[destinationDepth] = objects;
			
		}
		else {
			for (var j=originDepth-this.inc;j>=destinationDepth;j=j-this.inc){
				this.applyDepth(this.fields[j],j+this.inc);
				this.fields[j+this.inc] = this.fields[j];
			}
			this.fields[destinationDepth] = objects;
		}		
	}
	
}); // pb_core_DepthManager

var pb_core_ResourceManager = Class.create({

	// TODO Poner iconos de ventanas normales
	getWindowIconTitleLeft: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/window_title_left.png';
	},
	
	getWindowIconTitleRight: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/window_title_right.png';
	},
	
	getWindowIconTitleClose: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/window_close.png';
	},
	
	getHUDWindowIconTitleLeft: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/hud_title_left.png';
	},
	
	getHUDWindowIconTitleRight: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/hud_title_right.png';
	},
	
	getHUDWindowIconTitleClose: function(){
		return pb.core.system.getLibraryPath() + '/plasticbriqFramework/interfaceFiles/applications/hud_close.png';
	}
}); // pb_core_ResourceManager

var pb_core_LoaderAnimation = Class.create({
	getLoaderIconURL: function() {
		return pb.core.system.getLibraryPath() + 'plasticbriqFramework/interfaceFiles/core/page_loader.gif';
	},
	
	getLoaderContainer: function(id) {
		var loader = document.createElement('div');
		Element.extend(loader);
		loader.id = id;
		loader.className = 'loaderContainer';
		loader.style.backgroundImage = 'url(' + this.getLoaderIconURL() + ')';
		loader.style.marginLeft = 'auto';
		loader.style.marginRight = 'auto';
		loader.style.marginTop = '90px';
		loader.style.width = '32px';
		loader.style.height = '32px';
		return loader;
	},
	
	getLoaderContainerText: function(id) {
		var loaderText = '<div id="' + id + '" class="loaderContainer" style="margin-left:auto;margin-right:auto;margin-top:90px;width:32px;height:32px;background:url(' + this.getLoaderIconURL() + ') no-repeat;"></div>';
		return loaderText;
	}
});

var pb_core_ModalBackground = Class.create({
	
	url: null,
	color: null,
	
	setURL: function(url){
		this.url = url;
	},
	
	setColor: function(color){
		this.color = color;
	},
	
	getColor: function(){
		return this.color;
	},

	getURL: function(){
		return this.url;
	},
	
	getContainer: function(id){
		var container = document.createElement('div');
		Element.extend(container);
		container.id = 'modalBackground';
		if (id) container.id = id;
		container.className = 'modalBackground';
		container.style.backgroundImage = 'url(' + this.getURL() + ')';
		container.style.backgroundColor = this.getColor();
		container.style.width = '100%';
		container.style.height = '100%';
		container.style.position = 'fixed';
		container.style.zIndex = '4900';
		container.style.left = '0';
		container.style.top = '0';
		return container;
	},
	
	getContainerText: function(id) {
		var containerText = '<div ';
		if (id) containerText += 'id="' + id + ' ';
		containerText += 'class="modalBackground" style="background:url(' + this.getURL() + ');backgroundColor: '+this.getColor()+';width:100%;height:100%;position: fixed;z-index: 4900;left:0;top:0;"></div>';
		return containerText;
	}
		
}); // pb_core_ModalBackground

var pb_core_ResizeHandle = Class.create({
	url: null,
	
	setURL: function(url){
		this.url = url;
	},
	
	getURL: function(){
		return this.url;
	},
	
	getContainer: function(id){
		var container = document.createElement('div');
		Element.extend(container);
		if (id) container.id = id;
		container.className = 'resizeHandle';
		container.style.backgroundImage = 'url(' + this.getURL() + ')';
		container.style.cursor = 'se-resize';
		container.style.position = 'absolute';
		var image = new Image();
		image.src = this.getURL();
		if (image.width){
			container.style.width = image.width + 'px';
			container.style.height = image.height + 'px';
		}

		return container;
	},
	
	getContainerText: function(id){
		var containerText = '<div ';
		if (id) containerText += 'id="' + id + ' ';
		
		var image = new Image();
		image.src = this.getURL();
		
		containerText += 'class="resizeHandle" style="background:url(' + this.getURL() + ');position: absolute;cursor: se-resize;';

		if (image.width){
			containerText += 'width:' + image.width + 'px;height:' + image.height + 'px';
		}
		
		containerText += "></div>";
		
		return containerText;
	}
	
}); // pb_core_ResizeHandle


var pb_core_BrowserWindow = Class.create({
	
	getWidth: function() {
		var width = 0;
		if( document.documentElement && document.documentElement.clientWidth ) {
			width = document.documentElement.clientWidth;
		}
		else if( typeof( window.innerWidth ) == 'number' ) {
			width = window.innerWidth;
		}
		else if( document.body && document.body.clientWidth ) {
			width = document.body.clientWidth;
		}
		return width;
	},
	
	getHeight: function() {
		var height = 0;
		if( document.documentElement && document.documentElement.clientHeight ) {
			height = document.documentElement.clientHeight;
		}
		else if( typeof( window.innerHeight ) == 'number' ) {
			height = window.innerHeight;
		}
		else if( document.body && document.body.clientHeight ) {
			height = document.body.clientHeight;
		}
		return height;
	},
	
		getScreenSize: function(){
		return [ screen.availWidth, screen.availHeight ];
	},
	
	getScrolls: function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
    		scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			var size = document.viewport.getScrollOffsets();
			scrOfY = size[0];
			scrOfX = size[1];
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		
		return [ scrOfX, scrOfY ];
	}
}); // pb_core_BrowserWindow

var pb_core_UserAccount = Class.create({
	loadFullUserName: function(nickname,destContainer,useInnerHTML) {
		pb.core.actions.executeAndPutResultIntoContainer(pb.core.system.getLibraryPath() + 'plasticbriqFramework/core/_session.php',
					'loadUserName',{nickname:nickname},destContainer,useInnerHTML);
	}
}); // pb_core_UserAccount

var pb_core_Notification = Class.create({
	category:'',
	sourceType:'',
	sourceId:'',
	title:'',
	description:'',
	params:'',

	Notification: function(category,sourceType,sourceId,title,description,params) {
		this.category = category;
		this.sourceType = sourceType;
		this.sourceId = sourceId;
		this.title = title;
		this.description = description;
		this.params = params;
	},

	initialize: function(category,sourceType,sourceId,title,text,params) {
		this.Notification(category,sourceType,sourceId,title,text,params);
	}
});

var pb_core_NotificationManager = Class.create({
	send: function(notification) {
		var params = Object();
		params.category = notification.category;
		params.sourceType = notification.sourceType;
		params.sourceId = notification.sourceId;
		params.title = notification.title;
		params.description = notification.description;
		params.params = notification.params;

		pb.core.actions.execute(pb.core.system.getLibraryPath() + 'plasticbriqFramework/core/_notification_api.php',
				'send',params,
				function(responseText) {
					// En principio no hay que hacer nada
				});
	}
}); // pb_core_NotificationManager

var pb_core_CSSUtils = Class.create({
	addPx: function(value){
		return this.fixCSSUnit(value,true);		
	},
	
	getLeft: function(node){
		if (node.offsetLeft) return node.offsetLeft;
		else return (parseInt(node.style.left)||0);
	},
	
	getTop: function(node){
		if (node.offsetTop) return node.offsetTop;
		else return (parseInt(node.style.top)||0);
	},
	
	getWidth: function(node){
		if (!node) return null;
		
		if (node.style.width=="") return node.offsetWidth;
		
		// var w = node.offsetWidth;
		// if (!w){
			w = node.style.width;
			if (w){
				w = (parseInt(w)||0);
				var lw = this.getLeftExtraWidth(node);
				var rw = this.getRightExtraWidth(node);
				return (w+lw+rw);
			}
			return 0;
		// }
		// return w;
	},
	
	setWidth: function(node,value){
		var w = node.offsetWidth;
		var innerW = node.style.width;
		if (w && innerW){
			var offset = w-(parseInt(innerW)||0);
			node.style.width = (value-offset) + 'px';
		}
		else {
			var lw = this.getLeftExtraWidth(node);
			var rw = this.getRightExtraWidth(node);
			node.style.width = (value-lw-rw) + 'px';
		}
	},
	
	getHeight: function(node){
		if (!node) return null;
		
		if (node.style.height=='') return node.offsetHeight;
		
		// var h = node.offsetHeight;
		// 	if (!h){
			h = node.style.height;
			if (h){
				h = (parseInt(h)||0);
				var th = this.getTopExtraHeight(node);
				var bh = this.getBottomExtraHeight(node);
				return (h+th+bh);
			}
			return 0;
		//}
		//return h;
	},
	
	setHeight: function(node,value){
		var h = node.offsetHeight;
		var innerH = node.style.height;
		if (h && innerH){
			var offset = h-(parseInt(innerH)||0);
			node.style.height = (value-offset) + 'px';
		}
		else {
			var th = this.getTopExtraHeight(node);
			var bh = this.getBottomExtraHeight(node);
			node.style.height = (value-th-bh) + 'px';
		}
	},
	
	getLeftExtraWidth: function(node){
		var w = 0;

		if (node.style.paddingLeft) {
			w += (parseInt(node.style.paddingLeft)||0);
		}
		
		if (node.style.borderLeftWidth && (node.style.borderLeftStyle!='none')) {
			w += (parseInt(node.style.borderLeftWidth)||0);
		}
		
		return w;
	},
	
	getRightExtraWidth: function(node){
		var w = 0;

		if (node.style.paddingRight) {
			w += (parseInt(node.style.paddingRight)||0);
		}
		
		if (node.style.borderRightWidth && (node.style.borderRightStyle!='none')) {
			w += (parseInt(node.style.borderRightWidth)||0);
		}
		
		return w;
	},
	
	getTopExtraHeight: function(node){
		var h = 0;

		if (node.style.paddingTop) {
			h += (parseInt(node.style.paddingTop)||0);
		}
		
		if (node.style.borderTopWidth && (node.style.borderTopStyle!='none')) {
			h += (parseInt(node.style.borderTopWidth)||0);
		}
		
		return h;
	},
	
	getBottomExtraHeight: function(node){
		var h = 0;

		if (node.style.paddingBottom) {
			h += (parseInt(node.style.paddingBottom)||0);
		}
		
		if (node.style.borderBottomWidth && (node.style.borderBottomStyle!='none')) {
			h += (parseInt(node.style.borderBottomWidth)||0);
		}
		
		return h;
	},
	
	getMarginLeft: function(node){
		return (parseInt(node.style.marginLeft)||0);
	},

	getMarginRight: function(node){
		return (parseInt(node.style.marginRight)||0);
	},


	getMarginTop: function(node){
		return (parseInt(node.style.marginTop)||0);
	},

	getMarginBottom: function(node){
		return (parseInt(node.style.marginBottom)||0);
	},
		
	fixCSSUnit: function(value,allowPx,allowPercent,allowPt,allowPc,allowEx,allowIn,allowCm,allowMm,allowEm) {
		value = value.toLowerCase();
		var unit = 'px';

		if (value=='') {
			return value;
		}

		if (allowPx && value.indexOf('px')!=-1) {
			value = value.substr(0,value.length-2);
		}
		else if (allowPercent && value.indexOf('%')!=-1) {
			value = value.substr(0,value.length-1);
			unit = '%';
		}
		else if (allowPt && value.indexOf('pt')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'pt';
		}
		else if (allowPc && value.indexOf('pc')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'pc';
		}
		else if (allowEx && value.indexOf('ex')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'ex';
		}
		else if (allowIn && value.indexOf('in')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'in';
		}
		else if (allowCm && value.indexOf('cm')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'cm';
		}
		else if (allowMm && value.indexOf('mm')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'mm';
		}
		else if (allowEm && value.indexOf('em')!=-1) {
			value = value.substr(0,value.length-2);
			unit = 'em';
		}
		else if (parseInt(value)!=NaN) {
			value = parseInt(value);
			unit = 'px';
		}
		else if (parseInt(value.substr(0,length-1))!=NaN) {	// Por si se ha puesto % pero no se permite usar esa medida
			value=parseInt(value.substr(0,length-1));
			unit = 'px';
		}
		else {	// En el resto de los casos, intentamos substituir cualquier otra medida, que tienen todas dos caracteres de longitud
			value = value.substr(0,value.length-2);
			unit = 'px';
		}

		if (parseInt(value)==NaN) {
			value = '0';
		}
	
		return value + unit;
	},
	
	switchToLeftCoords: function(rightPos){
		return (pb.core.browserWindow.getWidth() - rightPos) + 'px';
	},
	
	switchToRightCoords: function(leftPos){
		return (pb.core.browserWindow.getWidth() - leftPos) + 'px';
	},
	
	switchToTopCoords: function(bottomPos){
		return (pb.core.browserWindow.getHeight() - bottomPos) + 'px';
	},

	switchToBottomCoords: function(topPos){
		return (pb.core.browserWindow.getHeight() - topPos) + 'px';
	},
	
	calculateLeftPosFromRight: function(node){
		var rightPos = node.offsetRight;
		if (!rightPos) rightPos = (parseInt(node.style.right)||0);
		return this.calculateLeftPosFromRightPos(rightPos,node.offsetWidth);
	},
	
	calculateLeftPosFromRightPos: function(rightPos,width){
		//console.log('calculateLeftPosFromRightPos ' + pb.core.browserWindow.getWidth() + ' ' + rightPos + ' ' + width);
		return (pb.core.browserWindow.getWidth() - rightPos - width) + 'px';
	},
	
	calculateRightPosFromLeft: function(node){ // Calcular posición derecha a partir de la izquierda
		//console.log('calculateRightPosFromLeft');
		var leftPos = node.offsetLeft;
		if (!leftPos) leftPos = (parseInt(node.style.left)||0);
		//console.log(pb.core.browserWindow.getWidth() + ' ' + leftPos + ' ' + node.offsetWidth);
		return this.calculateRightPosFromLeftPos(leftPos,node.offsetWidth);
	},
	
	calculateRightPosFromLeftPos: function(leftPos,width){
		return (pb.core.browserWindow.getWidth() - leftPos - width) + 'px';
	},
	
	calculateTopPosFromBottom: function(node){
		var bottomPos = node.offsetBottom;
		if (!bottomPos) bottomPos = (parseInt(node.style.bottom)||0);
		return this.calculateTopPosFromBottomPos(bottomPos,node.offsetHeight);
	},
	
	calculateTopPosFromBottomPos: function(bottomPos,height){
		return (pb.core.browserWindow.getHeight() - bottomPos - height) + 'px';
	},
	
	calculateBottomPosFromTop: function(node){
		var topPos = node.offsetTop;
		if (!topPos) topPos = (parseInt(node.style.top)||0);
		return this.calculateBottomPosFromTopPos(topPos,node.offsetHeight);
	},
	
	calculateBottomPosFromTopPos: function(topPos,height){
		return (pb.core.browserWindow.getHeight() - topPos - height) + 'px';
	},
	
	getOffsetRight: function(node){
		if ((node.getAttribute('anchorLeft')=='both') || (node.getAttribute('anchorLeft')=='right')){
			return (parseInt(node.style.right)||'');
		}
		else {
			return (node.offsetLeft + node.offsetWidth);			
		}
	},
	
	getOffsetBottom: function(node){
		var top = node.offsetTop;
		return (top + node.offsetHeight);
	}
	
}); // pb_core_CSSUtils

var pb_core = Class.create({
	system: new pb_core_System(),
	page: new pb_core_Page(),
	menu: new pb_core_Menu(),
	navigation: new pb_core_Navigation(),
	hash: new pb_core_Hash(),
	actions: new pb_core_Actions(),
	session: new pb_core_Session(),
	activityMonitor: new pb_core_ActivityMonitor(),
	counter: new pb_core_Counter(),
	localizedString: new pb_core_LocalizedString(),
	resourceManager: new pb_core_ResourceManager(),
	loaderAnimation: new pb_core_LoaderAnimation(),
	modalBackground: new pb_core_ModalBackground(),
	resizeHandle: new pb_core_ResizeHandle(),
	browserWindow: new pb_core_BrowserWindow(),
	userAccount: new pb_core_UserAccount(),
	notificationManager: new pb_core_NotificationManager(),
	cssUtils: new pb_core_CSSUtils()
});
