/**
 * @class ManagerServer
 */
var ManagerServer = function(){
	this.controllers = new Array();
}
ManagerServer.prototype.load = function(controller,parameters){
	controller.executeBeforeCall();
	if(!parameters){parameters = {}}
	parameters['keyView']=controller.url;
	this.controllers[controller.id] = controller;
	$.ajax({
		type: 'POST',
		url: 'application/actions/Server.php',
		dataType: 'html',
		data: parameters,
		success:  server.processResponse,
		cache: false
	});
}
ManagerServer.prototype.processResponse = function(data, textStatus, XMLHttpRequest){
	var xml = $(data);
	var id = xml.attr('title');
	server.controllers[id].processLoad(xml);
}
/**
 * @class Layer
 */
var Layer = function(idLayer){
	this.id = idLayer;
	this.application = null;
	this.tag = $('#'+idLayer);
	this.boundingBox = new BoundingBox(this.tag);
}
Layer.prototype.setBoundingBoxConfig = function(config){
	this.boundingBox.config(config);
}
Layer.prototype.init = function(anApplication){
	this.application = anApplication;
}
Layer.prototype.show = function(){
	this.tag.fadeIn('slow');
}
Layer.prototype.hide = function(){
	this.tag.fadeOut('fast');
}
Layer.prototype.activate = function(){};
	
Layer.prototype.setEventActivate = function(aFunction){
	this.activate = aFunction;
	this.activate();
}
/**
 * @class LayerBackground
 */
 var LayerBackground = function(idLayer){
	this.id = idLayer;
	this.application = null;
	this.tag = $('#'+idLayer);
	this.tag.css('position','absolute');
	this.tag.css('top','0px');
	this.tag.css('left','0px');
	this.tag.css('width',$(window).width()+'px');
	this.tag.css('height',$(document).height()+'px');
}
 LayerBackground.prototype = new Layer();
 /**
 * @class LayerProxy extends Layer
 */
var LayerProxy = function(idLayer, URLLayer){
	this.id = idLayer;
	this.url = URLLayer;
	this.application = null;
	this.tag = $('#'+idLayer);
	this.loaded = false;
	this.boundingBox = new BoundingBox(this.tag);
	this.cache = true;
}
LayerProxy.prototype = new Layer();
LayerProxy.prototype.load = function(parameters){
	server.load(this,parameters);
}
LayerProxy.prototype.processLoad = function(xml){
	this.tag.html(xml.html());
	this.loaded = true;
	this.activate();
	this.show();
}
LayerProxy.prototype.executeBeforeCall = function(){}
LayerProxy.prototype.show = function(parameters){
	if(!this.loaded){
		this.load(parameters);
	}
	this.tag.fadeIn('slow');
	this.loaded = this.cache;
}
LayerProxy.prototype.hide = function(){
	this.tag.fadeOut('fast');
}
LayerProxy.prototype.activate = function(){}

LayerProxy.prototype.setEventActivate = function(aFunction){
	this.activate = aFunction;
}
/**
 * @class LayerEmpty
 */
var LayerEmpty = function(){
	this.hide = function(){}
}
/**
 * @class Application
 */
var Application = function(){
	this.layers = new Array();
	this.currentShowLayer = new LayerEmpty();
	this.backgroundLayer = null;
	this.dimension = new Dimension();
	this.resources = new Array();
}
Application.prototype.setBackground = function(aLayer){
	aLayer.init(this);
	this.backgroundLayer = aLayer;
}
Application.prototype.addLayer = function(aLayer){
	aLayer.init(this);
	this.layers[aLayer.id] = aLayer;
}
Application.prototype.show = function(idLayer,parameters){
	this.currentShowLayer.hide();
	this.currentShowLayer = this.layers[idLayer];
	this.currentShowLayer.show(parameters);
}
Application.prototype.preloadImage = function(url){
	if(!this.resources[url]){
		this.resources[url] = new Image();
		this.resources[url].src = url;
	}
}

