var js_core_currentTaskId = 0;

function getTaskId() {
	return ++js_core_currentTaskId;
}

var js_core_ReadTask = Class.create(pb_core_Task,{
	initialize:function($super) {
		$super('read_task_' + getTaskId(),'Loading...',false);
	}
});

var js_core_WriteTask = Class.create(pb_core_Task,{
	initialize:function($super) {
		$super('write_task_' + getTaskId(),'Saving changes...',true);
	}
});

var js_core_FileTask = Class.create(pb_core_Task,{
	initialize:function($super) {
		$super('file_task_' + getTaskId(),'Uploading files...',true);
	}
});

var js_core_Actions = Class.create({
	load:function(actionURL,command,parameters,onDone,onComplete) {
		var task = new js_core_ReadTask();
		pb.core.activityMonitor.addTask(task);
		pb.core.actions.execute(actionURL,command,parameters,onDone,task.identifier,onComplete);
	},
	
	write:function(actionURL,command,parameters,onDone,onComplete) {
		var task = new js_core_WriteTask()
		pb.core.activityMonitor.addTask(task);
		pb.core.actions.execute(actionURL,command,parameters,onDone,task.identifier,onComplete);
	},
	
	file:function(actionURL,command,parameters,onDone,onComplete) {
		var task = new js_core_FileTask();
		pb.core.activityMonitor.addTask(task);
		pb.core.actions.execute(actionURL,command,parameters,onDone,task.identifier,onComplete);
	}
});

var js_System = Class.create({
	initialize:function() {
	}
});

var js_Core = Class.create({
	evalAllScripts:function(tag,contextMessage) {
		pb.core.system.evalAllScripts(tag,contextMessage);
	}
});

var js_core_Mouse = Class.create({
	mousePosition:{x:0,y:0},

	initialize:function() {
		document.observe('mousemove',function(event) { js.core.mouse.setPosition(event); });
	},

	getPosition:function() {
		return this.mousePosition;
	},
	
	setPosition:function(event) {	
		this.mousePosition.x = event.clientX;
		this.mousePosition.y = event.clientY;
	}
});

var js_Resources = Class.create({
	loadImage:function(pkg,file,onload) {
		var imagePath = js.resources.getItemPath(pkg,file);
		var image = new Image();
		image.observe('load',function(event) { onload(event,imagePath,image); });
		image.src = imagePath;
	},

	getItemPath:function(pkg,file) {
		var imagePath = js.system.resourcesPath + pkg + '/' + file;
		return imagePath;
	},
	
	getItemWidth:function(image) {
		return image.width / 4;
	},
	
	getItemOffset:function(image,state) {
		if (state) {
			var width = image.width/4;
			var offset = 0;
			switch(state) {
				case 'hover':
				case 'over':
				case 'onmouseover':
				case 'onmousehover':
				case 'mouseover':
				case 'mousehover':
					offset = width;
					break;
				case 'active':
				case 'clic':
				case 'click':
				case 'down':
				case 'mousedown':
				case 'onmousedown':
					offset = width * 2;
					break;
				case 'disabled':
					offset = width * 3
					break;
			}
			return offset;
		}
		else {
			return 0;
		}
	}
});

var js_Dictionary = Class.create({
	dictionary:{},

	setKey:function(key,value) {
		this.dictionary[key] = value;
	},

	// keys es un string separado por la barra vertical |
	initKeys:function(keys,onSuccess,module) {
		var command = 'getStaticText';
		var parameters = {};
		parameters.keys = keys;
		if (module) {
			command = 'getStaticTextFromModule';
			parameters.module = module;
		}
		js.core.actions.load(js.system.libraryPath + 'plasticbriqFramework/actions/_translator.php',command,parameters,
			function(responseText) {
				var results = JSON.parse(responseText);
				for (var item in results) {
					js.dictionary.setKey(item,results[item]);
				}
				if (onSuccess) {
					onSuccess();
				}
			}
		);
	},
	
	

	translate:function(key) {
		if (this.dictionary[key]) {
			return this.dictionary[key];
		}
		else {
			return key;
		}
	}
});

var js = {};
js.dictionary = new js_Dictionary();

