/*****************************************************************************
SHAJAX - Styling with XML Framework
Copyright (C) 2006  Daniel Dalgo

This library is free software; you can redistribute it and/or modify it under 
the terms of the GNU Lesser General Public License as published by the 
Free Software Foundation; either version 2.1 of the License, or (at your option) 
any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along 
with this library; if not, write to the Free Software Foundation, Inc., 59 
Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*****************************************************************************
*****************************************************************************
*****************************  CONTACT **************************************
							   -------
PLEASE, CONTACT US AT:
www.7bits.com.ec
ddalgo@7bits.com.ec
Quito, Ecuador
*****************************************************************************
***************************  EXTRA FILES ************************************
							 -----------
Also, please refer to the helper files annexed
- "updates.txt" 
- "bugs.txt"
for info about new features or bugs in this library, and also use it 
for post your own information. Contact us using the media referred above, in
the CONTACT section.

All the files referred here, in the EXTRA FILES section, and the new info
posted in those files are also covered for the license here mentioned.
*****************************************************************************/

// Javascript

/** DEPENDENCIAS:
 * 		Estilos.utilidades.js
 */

/** Clase encargada de crear un objeto de estilos leídos desde un archivo XML que cumpla con el 
 * formato especificado en el archivo "estilos.dtd" (junto a este archivo). Allí se depositan los
 * estilos a ser manejados, encargados de controlar es aspecto y funcionamiento de un control en particular
 */
function Estilos(idTipoControl, idControl, rutaArchivoXML){
	if(!idTipoControl || typeof idTipoControl=="undefined"){
		return;
	}

	if(!idControl || typeof idControl=="undefined"){
		return;
	}

	if(!rutaArchivoXML || typeof rutaArchivoXML=="undefined"){
		return;
	}
	
	this._rutaArchivoXML=rutaArchivoXML;
	this._idTipoControl=idTipoControl;
	this._idControl=idControl;
	
	/** Variable donde se almacenan los datos recuperados del archivo XML de 'estilos'
	 */
	this._xmlDoc=null;

	/** Crea el objeto que va a pantalla en la variable 'elemento'
	 */
	this._doImportarXML();
}

/** Función privada encargada de recuperar los datos enviados en el archivo XML de 'estilos'
 */
Estilos.prototype._doImportarXML=function(){
	var XMLHttpRequestObject=null;
	
	if(window.XMLHttpRequest){
		XMLHttpRequestObject=new XMLHttpRequest();
	} else if(window.ActiveXObject){
		XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return;	
	}
	
	XMLHttpRequestObject.open("GET", this._rutaArchivoXML, false);
	XMLHttpRequestObject.send(null);
	this._xmlDoc=XMLHttpRequestObject.responseXML;

	/** Validación necesaria para poder cargar correctamente el XML
	 * en browser ie (POR QUE NO FUNCIONA SOLO CON EL XMLHTTPREQUEST,
	 * LA VERDAD NO TENGO IDEA. EN OTRAS COSAS FUNCIONA SIN ESTE
	 * TRUCO EL XMLHTTPREQUEST, PERO ACA NO Y NO SE POR QUE)
	 */
	if(window.ActiveXObject){
		this._xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		this._xmlDoc.loadXML(XMLHttpRequestObject.responseText);		
	}
}

/** Función encargada de devolver el elemento principal, que es manejado por el objeto
 */
Estilos.prototype.getElemento=function(){
	return this._xmlDoc;
}

/** Función encargada de devolver el ID del tipo de control al que está asociado
 */
Estilos.prototype.getIDTipoControl=function(){
	if(this._xmlDoc){
		return this._idTipoControl;
	}
}

/** Función encargada de devolver el ID del control al que está asociado
 */
Estilos.prototype.getIDControl=function(){
	if(this._xmlDoc){
		return this._idControl;
	}
}

/** Función encargada de devolver el archivo XML de 'estilos'
 */
Estilos.prototype.getRutaArchivo=function(){
	return this._rutaArchivoXML;
}

/** Función encargada de establecer la ruta del archivo XML de 'estilos' y actualizar 
 * inmediatamente con dicha información
 */
Estilos.prototype.setRutaArchivo=function(rutaArchivoXML){
	if(!rutaArchivoXML || typeof rutaArchivoXML=="undefined"){
		return;
	}
	
	this._rutaArchivoXML=rutaArchivoXML;
	this._doImportarXML();
}

/** Función privada encargada de devolver un estilo específico de acuerdo al id de estilo enviado
 */
Estilos.prototype._getEstilo=function(idEstilo){
	var estiloTemp="";
	
	if(this._xmlDoc){
		var oEstilos=this._xmlDoc.getElementsByTagName("ESTILOS");
		//alert(this._xmlDoc.documentElement);
	
		for(i=0; i<oEstilos.length; i++){
			oEstilo=oEstilos[i];
			
			for(j=0; j<oEstilos[i].childNodes.length; j++){
				if(oEstilos[i].childNodes[j].nodeType!= 1){
					/** Validación necesaria para browsers que no sean del tipo Mozilla (no como IE)
					 */
					continue;
				}
				
				if(oEstilos[i].childNodes[j].attributes.getNamedItem("est_id").nodeValue==idEstilo){
					if(oEstilos[i].childNodes[j].firstChild){
						/** Solo escribe el valor del estilo cuando tenga texto de reglas 
						 */
						estiloTemp=oEstilos[i].childNodes[j].firstChild.nodeValue+estiloTemp;
					}
					
					if(oEstilos[i].childNodes[j].attributes.getNamedItem("est_id_refs")){
						var arrIDs=estilosUtilidades.getIDs(oEstilos[i].childNodes[j].attributes.getNamedItem("est_id_refs").nodeValue);
						
						for(var k=0; k<arrIDs.length; k++){
							estiloTemp=this._getEstilo(arrIDs[k])+estiloTemp;
						}
					}
	
					return estiloTemp;
				}
			}
		}
	}
	
	return null;
}
	
/** Función encargada de devolver un estilo específico de acuerdo al nombre del id de estilo enviado
 */
Estilos.prototype.getEstilo=function(idEstilo){
	return this._getEstilo(idEstilo);
}

/** Función privada encargada de devolver toda la existencia de identificadores de estilo en el 
 * XML de 'estilos'
 */
Estilos.prototype._getIDsEstilos=function(){
	var arrSelectores=new Array();

	if(this._xmlDoc){
		var oEstilos=this._xmlDoc.getElementsByTagName("ESTILOS");
	
		for(i=0; i<oEstilos.length; i++){
			var oEstilo=oEstilos[i];
			
			for(j=0; j<oEstilo.childNodes.length; j++){
				if(oEstilo.childNodes[j].nodeType!= 1){
					//Validación necesaria para browsers que no sean del tipo Mozilla (no como IE)
					continue;
				}
				
				arrSelectores[arrSelectores.length]=oEstilo.childNodes[j].attributes.getNamedItem("est_id").nodeValue;
			}
		}
	}
	
	return ((arrSelectores.length>0)?arrSelectores:null);
}

/** Función encargada de devolver toda la existencia de identificadores de estilo en el XML de 'estilos'
 */
Estilos.prototype.getIDsEstilos=function(){
	return this._getIDsEstilos();
}

