// USE EXAMPLE
/*
layerGroup = new LayerGroup();

layer1 = new Layer("layerId_1", "titleId_1");
layer2 = new Layer("layerId_2", "titleId_2");

layerGroup.setLayerGroup(new Array(layer1, layer2, ...));

layerGroup.showSelectedLayer(layerIndex);

*/

/* LAYER GROUP OBJECT */

function LayerGroup() {
	// Atributtes

	// Methods
	this.setLayerGroup = function setLayerGroup(layersGroup) {
		this.layersGroup = layersGroup;
		this.activeClassName = "active";
		this.inactiveClassName = "";
	}

	this.setClassNames = function setClassNames(activeClassName, inactiveClassName) {
		this.activeClassName = activeClassName;
		this.inactiveClassName = inactiveClassName;
	}

	this.hideAllLayers = function hideAllLayers() {
		this.hideAll();
	}

	this.showSelectedLayer = function showSelectedLayer(index) {
		this.hideAll();
		if (this.layersGroup[index].layerId == null) { return ;} // It usually means the document hasn't been loaded already. We do not want JS error in that case
		if (this.layersGroup[index].titleId == null) { return ;} // It usually means the document hasn't been loaded already. We do not want JS error in that case
		if (document.getElementById(this.layersGroup[index].layerId) != null) { document.getElementById(this.layersGroup[index].layerId).style.display = "block"; }
		if (document.getElementById(this.layersGroup[index].titleId) != null) {
			document.getElementById(this.layersGroup[index].titleId).className = this.activeClassName;
			document.getElementById(this.layersGroup[index].titleId).blur();
		}
	}

	this.hideAll = function hideAll() {
		for (var i = 0; i < this.layersGroup.length; i++) {
			if (this.layersGroup[i].layerId == null) { continue ;} // It usually means the document hasn't been loaded already. We do not want JS error in that case
			if (document.getElementById(this.layersGroup[i].layerId) != null) document.getElementById(this.layersGroup[i].layerId).style.display = "none";
			if (this.layersGroup[i].titleId == null) { continue ;} // It usually means the document hasn't been loaded already. We do not want JS error in that case
			if (document.getElementById(this.layersGroup[i].titleId) != null) document.getElementById(this.layersGroup[i].titleId).className = this.inactiveClassName;
		}
	}
}



/* LAYER OBJECT */

function Layer(layerId, titleId) {

	// Atributtes
	this.layerId = layerId;
	this.titleId = titleId;
	this.activeClassName = "active";
	this.inactiveClassName = "hidden";

	// Methods
	this.setLayerClassNames = function setLayerClassNames(activeClassName, inactiveClassName) {
		this.activeClassName = activeClassName;
		this.inactiveClassName = inactiveClassName;
	}

	this.setTexts = function setTexts(textBlock, textHidden) {
		this.textActive = textBlock;
		this.textInactive = textHidden;
	}

	this.showHide = function showHide() {
		var layerElement = document.getElementById(this.layerId);
		var titleElement = document.getElementById(this.titleId);
		if (layerElement.style.display == "none") { // Layer Oculto, mostrar
			layerElement.style.display = "block";
			if (!isEmpty(this.textActive)) titleElement.innerHTML = this.textActive;
			if (titleElement != null) {
				titleElement.className = this.activeClassName;
				titleElement.blur();
			}
		} else { // ocultar Layer
			layerElement.style.display = "none";
			if (!isEmpty(this.textInactive)) titleElement.innerHTML = this.textInactive;
			if (titleElement != null) {
				titleElement.className = this.inactiveClassName;
				titleElement.blur();
			}
		}
	}
}

// Other Functions

function showHideMe(me, elementToShowHideId, textShown, textHidden, classShown, classHidden) {
	var layerElement = document.getElementById(elementToShowHideId);
	if (layerElement.style.display == "none") { // Layer Oculto, mostrar
		layerElement.style.display = "block";
		if (classShown != null) me.className = classShown;
		if (textShown != null) me.innerHTML = textShown;
		if (me != null)me.blur();
	} else if (layerElement.style.display == "block") { // ocultar Layer
		layerElement.style.display = "none";
		if (classHidden != null) me.className = classHidden;
		if (textHidden != null) me.innerHTML = textHidden;
		if (me != null)me.blur();
	}
}

function hideMe(me, elementToShowHideId, textShown, textHidden, classShown, classHidden) {
	var layerElement = document.getElementById(elementToShowHideId);
	if (layerElement.style.display == "block") { // ocultar Layer
		layerElement.style.display = "none";
		if (classHidden != null) me.className = classHidden;
		if (textHidden != null) me.innerHTML = textHidden;
		if (me != null)me.blur();
	}
}

function showMe(me, elementToShowHideId, textShown, textHidden, classShown, classHidden) {
	var layerElement = document.getElementById(elementToShowHideId);
	if (layerElement.style.display == "none") { // Layer Oculto, mostrar
		layerElement.style.display = "block";
		if (classShown != null) me.className = classShown;
		if (textShown != null) me.innerHTML = textShown;
		if (me != null)me.blur();
	} 
}