/*	
	************************
	Libreria _grLibs: Incluye múltiples funciones, clases, etc. generalmente usadas por los hurones =).
	Autor: Emmanuel Galván Murrieta [GurúRojo]
	Para uso en sitios desarrollados por GurúRojo.
	Versión: 1.16-Crue. Junio 23, 2010
	
	ESTE ARCHIVO DE PROGRAMACION COMPUTACIONAL SE DISTRIBUYE DE ACUERDO A UNA LICENCIA GNU GPL v3
	http://www.gnu.org/licenses/licenses.es.html#GPL. LA FUNCIONALIDAD ESPERADA DEL CODIGO NO ESTA
	GARANTIZADA BAJO NINGUNA CIRCUNSTANCIA. EL AUTOR SE DESLINDA DE CUALQUIER RESPONSABILIDAD POR
	MAL USO O APLICACION DEL PRESENTE CODIGO.
	
	MODIFICADO PARA RADIOALTERNATIVA.COM
	************************
*/



/*
 Librería:		GurúAjax
 Versión:		2.18
 Llamada:		str 			= new __invokeGuruAjax();		//Inicializar el objeto
 				str.url			= URL_OBJETIVO					//Argumento requerido, dirección objetivo
				str.method		= POST | GET					//Argumento opcional, default = GET
				str.postData	= VARIABLES_A_PASAR				//Argumento opcional, default = null
				variable		= str.callRequest();			//Hacer la petición
				str.onComplete	= foo							//Evento Registrado
 Return:		str.fResponse	= Array [0] -> false|true [1] -> error_text|text_response
*/

function __invokeGuruAjax(){
	
	var __callConnect;
	
	if(window.XMLHttpRequest){
		__callConnect		= new XMLHttpRequest();
	} else if(window.ActiveXObject){
		try{
			__callConnect	= new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			__callConnect	= new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else {
		__callConnect = false;
	}
	if(__callConnect){
		this.method		= 'GET';
		this.url		= null;
		this.postData	= null;
		this.onComplete	= function(){};
		this.fResponse	= null;
		var that		= this;
		
		this.callRequest = function(){
			if(this.url.length >= 1){
				try{
					__callConnect.onreadystatechange = function(){
						if(__callConnect.readyState == 4){
							if(__callConnect.status == 200){
								that.fResponse	= [true,__callConnect.responseText];
								that.onComplete();	

							} else {
								
								that.fResponse	= [false,'bad_response'];
								that.onComplete();
							}
						}
					}
					switch(this.method){
						case 'GET':
							__callConnect.open("GET",this.url,true);
							__callConnect.send(null);							
						break;
						case 'POST':
							__callConnect.open("POST",this.url,true);
							__callConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
							__callConnect.send(this.postData);
						break;
						default:
							alert('Error interno de programa / Internal program error: method_unknown')
							that.fResponse	= [false,'method_unknown'];
							that.onComplete();
						break;
					}

				} catch (e){
					alert('Error interno del explorador / Internal browser error\r\n\r\n' + e)
					that.fResponse	= [false,'browser_error'];
					that.onComplete();
				}
			} else {
				alert('Error interno de programa / Internal program error: empty_url')
				that.fResponse	= [false,'empty_url'];
				that.onComplete();
			}
		}
		
	} else {
		alert('ERROR:\r\n\r\nTu explorador no soporta XmlHttpRequest, que es requerido para este sitio.\nYour browser does not support XmlHttpRequest, which is required for this website.')
		return false;
	}
}



/*
 Librería:		Detectar Resolución de Pantalla
 Versión:		1.1
 Llamada:		__invokeGuruScreenRes();
 Return:		Array: [0] -> Boolean|String (False en error o resolución desconocida, widescreen, square) [1] -> ResX [2] -> ResY
*/

function __invokeGuruScreenRes(){
	return [screen.width,screen.height]
}




/*
 Librería:		Detectar datos del cliente
 Versión:		1.0
 Llamada:		__invokeGuruBrowser();
 Return:		Array [0] -> BROWSER_NAME [1] -> BROWSER_VERSION [2] -> VERSION_IE
*/

function __invokeGuruBrowser(){
	ACN	= navigator.appName
	ACV	= navigator.appVersion;
	if(ACV.indexOf('MSIE') >= 1){
		offset	= ACV.indexOf('MSIE')
		IEVER	= parseFloat(ACV.substr(offset +4,(offset+4 - ACV.indexOf(';'))));
	} else {
		IEVER	= false;
	}
	return [ACN,ACV,IEVER];
}





/*
 Librería:		Convertir lista SELECT
 Versión:		1.43
 Llamada:		str						=	new __convertGuruSelect();	// Inicialización
 				str.orse				=	DOM 						// Selector original
				str.cssClass			=	ClassName					// Estilos del selector
				str.cssClassDisabled	=	ClassName					// Estilos del selector (deshabilitado)
				str.maxOptions			=	int[7]						// Máximo de opciones antes de habilitar scrolling
				str.elementHeight		=	int[18]						// Alto de cada elemento (opción)
				str.elementExtraHeight	=	int[4]						// Alto extra de cada elemento (suma de bordes, margen, etc)
				
				str.enable()											// Deshabilita el selector
				str.enable()											// Habilita el selector
				
				str.onChange			=	function(void)				// Ejecutado cuando el usuario seleccione una opcion distinta
				
				str.construir()											// Construye el nuevo selector

*/


function __convertGuruSelect(){
	
	
	this.orse				=	null;
	this.cssClass			=	null;
	this.cssClassDisabled	=	null;
	this.maxOptions			=	7;
	this.elementHeight		=	18;
	this.elementExtraHeight	=	4;
	this.onChange 			=	function(){};
	this.onCompleto			=	function(){};
	
	var that				=	this;
	
	this.construir			=	function(){
		
		if(this.orse.nodeType == 1 && this.orse.tagName.toLowerCase() == 'select' && !this.orse.disabled && this.cssClass != null){
			
			if(this.orse.options.length > 1){
				
				genId							=	this.orse.id || this.orse.name || 'selectorDesconocido';
				
				divContenedor					=	document.createElement('div');
				divContenedor.id				=	genId + '_gs';
				divContenedor.className			=	this.cssClass;
				divContenedor.style.position	=	'absolute';
				divContenedor.style.height		=	this.elementHeight + 'px';
				divContenedor.style.overflow	=	'scroll';
				divContenedor.style.overflowX	=	'hidden';
				divContenedor.style.overflowY	=	'hidden';

				
				if(this.orse.options.length > this.maxOptions){
					divContenedor.setAttribute('maxHeight',this.maxOptions * this.elementHeight);
				} else {
					divContenedor.setAttribute('maxHeight',this.orse.options.length * this.elementHeight);
				}
				
				divContenedor.setAttribute('elmHeight',this.elementHeight);
				divContenedor.setAttribute('elmExtraHeight',this.elementExtraHeight);
				divContenedor.setAttribute('hovering','false');
				divContenedor.setAttribute('expanded','false');
				divContenedor.setAttribute('deshabilitado','false');
				divContenedor.setAttribute('selindex','0');
				divContenedor.setAttribute('cssDisabled',this.cssClassDisabled);
				divContenedor.setAttribute('cssEnabled',this.cssClass);
				divContenedor.setAttribute('value',this.orse.value);
				divContenedor.setAttribute('oValue',this.orse.value);
				divContenedor.setAttribute('oIndex',this.orse.selectedIndex);
				
				divContenedor.moveIndex			=	function(){
					
					tarScroll		=	parseFloat(this.getAttribute('elmHeight')) + parseFloat(this.getAttribute('elmExtraHeight'));
					this.scrollTop	=	tarScroll * parseFloat(this.getAttribute('selindex')) + 1;
					this.style.overflow	=	'hidden';
					
				}
				
				divContenedor.abrir				=	function(){
					
					if(this.getAttribute('deshabilitado') == 'false'){
						this.setAttribute('hovering','true');
						this.setAttribute('expanded','true');
						
						this.style.height			=	this.getAttribute('maxHeight') + 'px';
						this.style.paddingRight		=	'0px';
						this.style.width 			=	'196px';
						this.style.overflowY		=	'auto';
						
						if(ieViejo){

							//this.style.backgroundPosition	=	'-60px -60px';
						}
					}
					
				}
				
				divContenedor.cerrar			=	function(){
					
					if(this.getAttribute('hovering') == 'false' && this.getAttribute('deshabilitado') == 'false'){
						this.setAttribute('hovering','false');
						this.setAttribute('expanded','false');
						
						this.style.height			=	this.getAttribute('elmHeight') + 'px';
						this.moveIndex();
						
						this.style.paddingRight		=	'16px';
						this.style.width 			=	'180px';
						this.style.overflowY		=	'hidden';
						
						if(ieViejo){
							this.style.backgroundPosition	=	'100% ' + this.scrollTop + 'px';
						}
						
					}						
					
				}
				
				divContenedor.disable			=	function(){
					if(this.getAttribute('cssDisabled') != null){
						this.setAttribute('deshabilitado','true');
						this.className	=	this.getAttribute('cssDisabled');
					}
				}
				
				divContenedor.enable			=	function(){
					this.setAttribute('deshabilitado','false');
					this.className	=	this.getAttribute('cssEnabled');
				}
				
				divContenedor.resetDefault		=	function(){
					this.setAttribute('selindex',this.getAttribute('oIndex'));
					this.setAttribute('value',this.getAttribute('oValue'));
					this.cerrar();
				}
				
				divContenedor.tellValue			=	function(valor){
					
					eles		=	this.childNodes;
					
					for(i=0;i<eles.length;i++){
						if(eles[i].nodeType == 1){
							if(eles[i].getAttribute('value') == valor){
								
								this.setAttribute('selindex',eles[i].getAttribute('index'));
								this.setAttribute('value',valor);
								this.cerrar();
								break;
								
							}
						}
					}
					
				}
				
				divContenedor.onchange			=	this.onChange;
				
				
				
				/* CREA LOS ELEMENTOS */
				
				
				divElemento						=	document.createElement('div');


				for(i=0;i<this.orse.options.length;i++){
					
					__element							=	divElemento.cloneNode(false);
					__element.id						=	'GSOpt_' + genId + '_' + i;
					__element.innerHTML					=	this.orse.options[i].innerHTML;
					__element.style.cssText				=	this.orse.options[i].style.cssText;
					__element.title						=	this.orse.options[i].title;
					
					__element.setAttribute('value',this.orse.options[i].value);
					__element.setAttribute('index',i);

					__element.onmousedown				=	function(){
						if(this.parentNode.getAttribute('expanded') == 'true' && this.parentNode.getAttribute('deshabilitado') == 'false'){
							
							if(this.parentNode.getAttribute('value') != this.getAttribute('value')){
								
								this.parentNode.setAttribute('selindex',this.getAttribute('index'));
								this.parentNode.setAttribute('value',this.getAttribute('value'));
								this.parentNode.setAttribute('hovering','false');
								this.parentNode.setAttribute('expanded','false');
								this.parentNode.cerrar();	
								this.parentNode.onchange();							
							}
							
						} else {
							this.parentNode.abrir();
						}
					}

					divContenedor.appendChild(__element);
					
				}
				
				
				
				/* REGISTRA FUNCIONES E INSTANCIA EL NUEVO SELECTOR */
				
				divContenedor.onclick			=	function(){this.abrir()};
				
				divContenedor.onmouseover		=	function(){
					if(this.getAttribute('deshabilitado') == 'false'){
						this.setAttribute('hovering','true');
					}
				}
				
				
				divContenedor.onmouseout		=	function(){
					if(this.getAttribute('deshabilitado') == 'false'){
						setTimeout("eval(\"document.getElementById('" + this.id + "').cerrar()\")",500);
						this.setAttribute('hovering','false');
					}
				}
				
				divContenedor.setAttribute('selindex',this.orse.selectedIndex);
				
				
				
				
				this.orse.parentNode.insertBefore(divContenedor,this.orse);
				this.orse.style.visibility	=	'hidden';
				this.orse.style.display		=	'none';
				
				divContenedor.enable();
				divContenedor.moveIndex();
				this.onCompleto();

			}
			
		}
		
	}
	

}




/*
 Librería:		GurúMosoc / Abre y cierra un elemento animado. El elemento debe tener estilo display:block
 Versión:		1.17
 Llamada:		str 			=	new __grMosoc(dir[abrir,cerrar], elemento[id], aceleracion[int-pixeles], tiempo[int-milisegundos]);
 
				str.arranca		=	function(void)	// Ejecutado cuando inicia la animación
				str.completado	=	function(void)	// Ejecutado cuando termina la animación
 				str.construir()						// construye el elemento
				str.mover()							// ejecuta la animacion
*/

function __grMosoc(dir,ecu,acel,tiempo){
	
	this.dir	=	dir;
	this.ecu	=	ecu;
	this.acel	=	acel;
	this.tiempo	=	tiempo;
	
	this.arranca	=	function(){};
	this.completado	=	function(){};
	this.iniciado	=	false;
	
	grmsThat		=	this;
	
	if(document.getElementById(ecu)){
		
		this.construir	=	function(){

			ebc							=	document.getElementById(this.ecu);
	
			this._mosocAlturaOriginal	=	ebc.offsetHeight || ebc.clientHeight || ebc.scrollHeight;
			this._mosocAceleracion		=	acel; //segundos
			this._mosocTiempo			=	tiempo; //milisegundos
			this._mosocVelocidad		=	Math.ceil((this._mosocAlturaOriginal * this._mosocTiempo) / (this._mosocAceleracion*1000));
			
		}
		
		
		
		this.mover		=	function(){
			
			alturaActual	=	parseFloat(ebc.style.height);
			if(alturaActual.toString().toLowerCase() == 'nan') alturaActual = this._mosocAlturaOriginal;
			
			switch(dir){
				
				case 'cerrar':
				
					if(ebc.style.position !== 'absolute' && ebc.style.visibility !== 'hidden'){

						if(!this.iniciado){
							grmsThat.arranca();
							this.iniciado			=	true;
						}

						if(alturaActual > this._mosocAceleracion){
							
							diferencia				=	this._mosocAlturaOriginal - alturaActual;
							porcentajeDiferencia	=	Math.ceil(alturaActual * 100 / this._mosocAlturaOriginal);
							nuevaAceleracion		=	Math.round(this._mosocVelocidad * porcentajeDiferencia / 100);
							nuevaAltura				=	Math.floor(alturaActual - nuevaAceleracion) - this._mosocAceleracion;
							
							ebc.style.height		=	nuevaAltura + 'px';
							setTimeout("grmsThat.mover()",this._mosocTiempo);
							
							
						} else {
							
							ebc.style.position		=	'absolute';
							ebc.style.visibility	=	'hidden';
							ebc.style.height		=	this._mosocAlturaOriginal + 'px';
							
							grmsThat.completado();
							
						}
						
						
					}
				
				break;
				
				
				case 'abrir':
	
						
					if(!this.iniciado){
						alturaActual = 0;
						ebc.style.position		=	'static';
						ebc.style.visibility	=	'visible';
						ebc.style.height		=	'0px';
						grmsThat.arranca();
						this.iniciado			=	true;
					}
					
					if(alturaActual < this._mosocAlturaOriginal){
						
						
						porcentajeDiferencia	=	Math.abs(Math.ceil(alturaActual * 100 / this._mosocAlturaOriginal) - 100);
						nuevaAceleracion		=	Math.round(this._mosocVelocidad * porcentajeDiferencia / 100);
						nuevaAltura				=	Math.floor(alturaActual + nuevaAceleracion) + this._mosocAceleracion;
						
						ebc.style.height		=	nuevaAltura + 'px';
						
						
						if(alturaActual !== this._mosocAlturaOriginal){
							setTimeout("grmsThat.mover()",this._mosocTiempo);
						}
						
						if(alturaActual == (this._mosocAlturaOriginal-this._mosocAceleracion)){
							grmsThat.completado();
						}

						
					}
				
				
				break;
				
				
				
			}
			//alert(alturaActual);
			
		
		}
		
		
		
		
	}
	
}




