function LZ() {}

LZ.IE = (window.ActiveXObject != null);

LZ.files = {};

/*
* Inicializa o framework.
*
* @param String classPath (opcional): Caminho para a pasta principal do
* 	framework ("lz"). Ex: "js/lz/" >> classPath = "js"
*/
LZ.initialize = function(classPath) {
	if (classPath == null) classPath = "";
	else {
		if (classPath.match(/\/$/) == null) classPath += "/";
		
		classPath += "lz/";
	}
	
	LZ.importFile(classPath + "event/Event.js");
	//LZ.importFile(classPath + "event/MouseEvent.js");
	//LZ.importFile(classPath + "event/KeyboardEvent.js");
	LZ.importFile(classPath + "event/EventDispatcher.js");
	LZ.importFile(classPath + "display/DisplayElement.js");
	//LZ.importFile(classPath + "ui/ContextMenu.js");
	//LZ.importFile(classPath + "ui/ContextMenuItem.js");
	//LZ.importFile(classPath + "entity/Range.js");
	//LZ.importFile(classPath + "net/Ajax.js");
	LZ.importFile(classPath + "util/ScrollBar.js");
};

LZ.importFile = function(path) {
	if (!LZ.files[path]) {
		var script = document.createElement("script");
		
		script.language = "javascript";
		script.type = "text/javascript";
		LZ.files[script.src = path] = true;

		document.getElementsByTagName("head")[0].appendChild(script);
	}
};

LZ.createContainer = function(flt) {
	var container = document.createElement("div");
	
	if (flt != null) {
		if (typeof(flt) == "boolean") flt = "left";
		if (!LZ.IE) container.style.cssFloat = flt;
		else container.style.styleFloat = flt;
	}
	
	container.style.overflow = "hidden"; // (@) no IE o scroll ocupa um espaço
	
	return container;
};

LZ.drawRectangle = function(width, height, color, flt, transparency) {
	var rectangle = LZ.createContainer(flt);
	
	if (width != null) rectangle.style.width = width + "px";
	if (height != null) rectangle.style.height = height + "px";
	if (color != null) rectangle.style.background = color;
	
	if (transparency != null) {
		if (LZ.IE) rectangle.style.filter = "alpha(opacity=" + transparency*100 + ")";
		else rectangle.style.opacity = transparency;
	}
	
	return rectangle;
};

LZ.measureToNumber = function(m) {
	return Number(m.match(/\d+/)[0]);
};

LZ.setSelectionEnabled = function(state, element) {
	if (element == null) element = document;
	
	if (state) element.onselectstart = element.onmousedown = null;
	else if (LZ.IE) element.onselectstart = function() {
		return false;
	};
	else element.onmousedown = function() {
		return false;
	};
};

LZ.globalToLocal = function(coords, target) {
	var osX = target.offsetLeft;
	var osY = target.offsetTop;
	
	while ((target = target.offsetParent) != null) {
		osX += target.offsetLeft;
		osY += target.offsetTop;
	}
	
	return {x: (coords.x != null) ? coords.x - osX : null, y: (coords.y != null) ? coords.y - osY : null};
};

LZ.getMouseCoords = function(mouseEvent, target) {
	var mX, mY;
	
	if (mouseEvent == null) mouseEvent = event;
	
	if (LZ.IE) {
		mX = mouseEvent.clientX - 2; // (@) IE sobra 2px
		mY = mouseEvent.clientY - 2; // (@) IE sobra 2px
	}
	else {
		mX = mouseEvent.pageX;
		mY = mouseEvent.pageY;
	}
	
	return LZ.globalToLocal({x: mX, y: mY}, target);
};

LZ.preloadImages = function(files, loadingFn, callbackFn) {
	//var timeout = 20000, time = 0;
	var total = files.length, timeId, count = 0, timeInterval = 10;
	var loader = function() {
		if (count < total) {
			var file = new Image();
			
			file.src = files[count];
			
			timeId = setInterval(function() {
				if (file.complete) {
					clearInterval(timeId);
					
					if (loadingFn != null) loadingFn(count, total);
					
					++count;
					loader();
				}
				/*else if ((time += timeInterval) >= timeout) {
					clearInterval(timeId);
					alert("Sua conexão deve estar lenta.\nTente novamente!");
				}*/
			}, timeInterval);
		}
		else if (callbackFn != null) callbackFn();
	};
	
	loader();
};