function refrescar_herramienta()
{
	aulas_seleccionadas.limpiar_entradas();
	aulas_seleccionadas.obtener_array();

	var it;
	for(it=0; it < aulas.length; it++)
	{
		if(aulas[it].desplegado)	//Sólo actualizamos las desplegadas.
		{
			aulas[it].refrescar_calendario();
		}
	}
}

function dummy_call(par_1)
{
	var segmento=par_1.padre.padre.selector.value;
	var id_aula=par_1.padre.padre.id_aula;
	var fecha=par_1.fecha.getFullYear()+'-'+(par_1.fecha.getMonth()+1)+'-'+par_1.fecha.getDate();

	/*
	Aquí hacemos la petición para ver si podemos solicitar el día... Si
	es posible solicitarlo lo metemos en sesión y hacemos el llamamiento al
	método que lo metería en el "carrito" y al que puede dibujar. Si no pues
	sacaríamos un alert de la muerte por:
		-Ya lo tienes, melón.
		-Ya está ocupado.
		-Está reservado, vuelve mañana si eso.  
	*/

	var peticion=new request_http;
	var url='http://www.comsevilla.es/peticiones/reserva_aulas_01.ajax.php?modo=solicitar&id_aula='+id_aula+'&fecha='+fecha+'&segmento='+segmento;
	var aquello=this;

	b.conmutar();

	peticion.cargar
	(	
		url, 	
		function(resultado)
		{	
			if(resultado.substr(0, 2)=='si')
			{
				eval(resultado.substr(2));
			}
			else
			{
				alert('La fecha se encuentra ya reservada');
			}
			b.conmutar();
		}
	);

}

function Aula()
{	
	//Propiedades del mostrado...
	this.capa_padre=null;
	this.contenedor=null;

		this.cabecera=null;
			this.capa_nombre=null;
			this.capa_precio=null;		
			this.boton_mostrar=null;
		this.cuerpo=null;
			this.col1=null;
				this.contenedor_calendario=null;
			this.col2=null;
				this.selector=null;
	this.calendario=null;

	//Propiedades de datos...
	this.id_aula=null;
	this.nombre=null;
	this.asientos=null;
	this.precio=null;
	this.texto=null;

	//Otras propiedades...
	this.clase=null;
	this.desplegado=false;
}

Aula.prototype.cargar=function(v_capa_padre, v_id_aula, v_nombre, v_asientos, v_texto, v_clase)
{
	this.capa_padre=v_capa_padre;
	this.id_aula=v_id_aula;
	this.nombre=v_nombre;
	this.asientos=v_asientos;
	this.texto=v_texto;
	this.clase=v_clase;
}

Aula.prototype.crear=function()
{
	var aquello=this;	
	
	this.contenedor=montar_elemento('div', 'aula', null, this.capa_padre);

	//Cabecera...
	this.cabecera=montar_elemento('div', 'cabecera '+this.clase, null, this.contenedor);
		this.capa_nombre=montar_elemento('div', 'nombre', '<b>'+this.nombre+'</b><br /><span class="aforo">'+this.asientos+' asientos</span>', this.cabecera);
		this.capa_precio=montar_elemento('div', 'precio', null, this.cabecera);

		this.boton_mostrar=montar_elemento('a', 'btnDisponibilidad', 'ver detalles', this.capa_precio);
		this.boton_mostrar.href='#';	
		this.boton_mostrar.onclick=function() {aquello.cambiar_vista(); return false;}

	//Cuerpo
	this.cuerpo=montar_elemento('div', 'cuerpo', null, null);
	this.cuerpo.style.display='none';
	this.contenedor.appendChild(this.cuerpo);

		this.col1=montar_elemento('div', 'col1', null, this.cuerpo);
			this.contenedor_calendario=montar_elemento('div', null, null, this.col1);
			montar_elemento('div', 'leyenda', '<img src="img/reserva_aulas/leyenda_reservado.gif" /> <span class="color_reservado">Reservado</span><br /><img src="img/reserva_aulas/leyenda_pendiente.gif" /> <span class="color_pendiente">Pendiente de confirmar</span><br /><img src="img/reserva_aulas/leyenda_seleccionado.gif" /> <span class="color_seleccionado">Selecci&oacute;n</span>', this.col1);
			montar_elemento('span','texto_selector','Indique tramo&nbsp;',this.col1);
			this.selector=montar_elemento('select', '', null, this.col1);
			insertar_opcion('m', "Ma\xf1ana", this.selector);
			insertar_opcion('t', 'Tarde', this.selector);
			insertar_opcion('a', 'Ambos', this.selector);

		this.col2=montar_elemento('div', 'col2', null, this.cuerpo);
			montar_elemento('p', null, '<b>'+this.nombre+'</b><br/><br/>', this.col2);
			montar_elemento('p', null, '<b>Detalles del aula: </b><br/>'+this.texto+'<br />', this.col2);

		montar_elemento('div', 'clear', null, this.cuerpo);

	//El calendario...
	this.calendario=new Calendario_vf(this.contenedor_calendario, dummy_call);
	this.calendario.asignar_a(this);
        //this.calendario.asignar_metodo_actualizar(this.refrescar_calendario);

         this.calendario.selector.desplegable_mes.onchange=null;
         this.calendario.selector.desplegable_mes.onchange = function()
         {
             aquello.calendario.actualizar(aquello.calendario.selector.desplegable_mes.value, aquello.calendario.selector.desplegable_anho.value);
             aquello.refrescar_calendario();
         }


         this.calendario.selector.desplegable_anho.onchange=null;
        this.calendario.selector.desplegable_anho.onchange=function()
        {
                aquello.calendario.actualizar(aquello.calendario.selector.desplegable_mes.value, aquello.calendario.selector.desplegable_anho.value);
                aquello.refrescar_calendario();
        }


}



Aula.prototype.refrescar_calendario=function()
{
	var peticion=new request_http();
	var url='http://www.comsevilla.es/peticiones/reserva_aulas_01.ajax.php?modo=rellenar&id_aula='+this.id_aula+'&mes='+this.calendario.mes+'&anho='+this.calendario.anho;        
	var aquello=this;

	b.conmutar();

	peticion.cargar
	(	
		url, 	
		function(resultado)
		{								
			if(resultado.substr(0, 2)=='si')
			{
				eval(resultado.substr(2));
			}
			b.conmutar();
		}
	);
}

Aula.prototype.cambiar_vista=function()
{
	if(this.desplegado)
	{
		this.desplegado=false;
		this.boton_mostrar.innerHTML='ver detalles';
		this.cuerpo.style.display='none';
	}
	else
	{
		this.desplegado=true;
		this.boton_mostrar.innerHTML='ocultar detalles';
		this.cuerpo.style.display='block';
		this.refrescar_calendario();		
	}
}

/*------------------------------------------------------------------------------
La clase "Aulas seleccionadas". El control sobre los datos de PHP se genera
haciendo llamadas a AJAX desde aquí.

>>crear: Genera el contenedor y los controles.

>>obtener_array: vacia el array de mostrados y lo obtiene desde AJAX.

>>mostrar_entradas: muestra las entradas del array.

>>limpiar_entradas: elimina las entradas del array.

>>eliminar: elimina un elemento en AJAX. Seguido de obtener_array para
actualizarlo.
------------------------------------------------------------------------------*/

function Aulas_seleccionadas(v_contenedor)
{
	this.contenedor=v_contenedor;
	this.cabecera=null;
	this.cuerpo=null;
		this.listado_aulas=null;
		this.boton_continuar=null;
		this.boton_reiniciar=null;

	this.aulas_mostradas=Array();

	this.crear();
}

Aulas_seleccionadas.prototype.crear=function()
{
	var aquello=this;
	
	this.cabecera=montar_elemento('div', 'cabecera', 'Aulas seleccionadas', this.contenedor);


	this.cuerpo=montar_elemento('div', 'cuerpo', null, this.contenedor);
		this.listado_aulas=montar_elemento('div', null, null, this.cuerpo);               
		this.boton_continuar=montar_elemento('div', 'btnContinuar', 'Continuar', this.cuerpo);

                this.boton_continuar.value='Continuar';
		this.boton_continuar.onclick=function() {aquello.avanzar_paso();}
}

Aulas_seleccionadas.prototype.avanzar_paso=function()
{
	if(this.aulas_mostradas.length!=0)
	{
                //Cambiada ruta el 25-01-2010. Antes: reserva_aulas_02.php
		window.location.href='http://www.comsevilla.es/docencia_y_formacion/reserva_aulas_paso_2.html';                
	}
	else
	{
		alert('Debe seleccionar al menos una jornada para continuar');
	}

	return false;
}

Aulas_seleccionadas.prototype.obtener_array=function()
{
	var aquello=this;
	var peticion=new request_http;
	var url='http://www.comsevilla.es/peticiones/reserva_aulas_01.ajax.php?modo=obtener_array';

	//Limpiamos el array...
	this.aulas_mostradas.splice(0, this.aulas_mostradas.length);	
	b.conmutar();

	//Obtenemos el nuevo array...
	peticion.cargar
	(
		url,
		function(resultado)
		{
			if(resultado.substr(0,2)=='si')
			{				
				//implica llamadas a limpiar_entradas y mostrar_entradas.
				eval(resultado.substr(2));
			}
			b.conmutar();
		}
	);
}

Aulas_seleccionadas.prototype.mostrar_entradas=function()
{
	var x;

	for(x=0; x<this.aulas_mostradas.length; x++)
	{
		this.aulas_mostradas[x].crear_representacion();
	}
}

Aulas_seleccionadas.prototype.limpiar_entradas=function()
{
	var x;
	for(x=0; x<this.aulas_mostradas.length; x++)
	{
		this.aulas_mostradas[x].eliminar_representacion();
	}	
}

Aulas_seleccionadas.prototype.eliminar=function(elemento)
{
	/*
	Se realiza una petición para eliminar en PHP. Si la petición tiene éxito
	llamamos al día con el índice de turno para que desaparezca.
	*/	
	var x;
	for(x=0; x<this.aulas_mostradas.length; x++)
	{
		if(this.aulas_mostradas[x]==elemento)	
		{
			var aquello=this;
			var peticion=new request_http;
			var url='http://www.comsevilla.es/peticiones/reserva_aulas_01.ajax.php?modo=eliminar&id_aula='+elemento.id_aula+'&fecha='+elemento.fecha+'&segmento='+elemento.segmento;

			b.conmutar();

			peticion.cargar
			(
				url,
				function(resultado)
				{
					if(resultado.substr(0,2)=='si')
					{				
						eval(resultado.substr(2));
						aquello.mostrar_entradas();
					}
					b.conmutar();
				}

			);			
			break;
		}
	}	
}

/*------------------------------------------------------------------------------
La entrada de las selecciones... No tiene ningún poder sobre los datos de PHP:
todas las llamadas de AJAX se hacen desde el listado.

>>crear_representacion: muestra la entrada individual

>>eliminar_representacion: hace desaparecer la entrada individual.
------------------------------------------------------------------------------*/

Aulas_seleccionadas.prototype.Entrada=function(var_id_aula, var_nombre, var_fecha, var_segmento)
{
	this.contenedor=null;
	this.nombre=null;
	this.momento=null;
	this.boton=null;

	this.nombre=var_nombre;
	this.id_aula=var_id_aula;
	this.fecha=var_fecha; //DD-MM-YYYY
	this.segmento=var_segmento;	
}

Aulas_seleccionadas.prototype.Entrada.prototype.crear_representacion=function()
{
	var v_contenedor=aulas_seleccionadas.listado_aulas;

	//Generamos contenidos propiamente legibles...
	switch(this.segmento)
	{
		case 't': var segmento_mostrar='Tarde'; break;
		case 'm': var segmento_mostrar='Ma&ntilde;ana'; break;
	}

	//Montamos todos los elementos.
	this.contenedor=montar_elemento('p', null, null, v_contenedor);
	this.nombre=montar_elemento('span', 'nombre_aula', '<b>'+this.nombre+'</b>', this.contenedor);
	montar_elemento('br', null, null, this.contenedor);
	this.momento=montar_elemento('span', null, '<span class="fecha">'+this.fecha+'</span> - <span class="tramo">'+segmento_mostrar+'</span>', this.contenedor);
	montar_elemento('br', null, null, this.contenedor);

	this.boton=montar_elemento('a', 'eliminar', 'eliminar', this.contenedor);
	this.boton.title='eliminar';
	this.boton.href='#';

	var aquello=this;
	this.boton.onclick=function() {aulas_seleccionadas.eliminar(aquello); return false;};
}


Aulas_seleccionadas.prototype.Entrada.prototype.eliminar_representacion=function()
{
	eliminar_contenido(this.contenedor);
	this.contenedor.parentNode.removeChild(this.contenedor);
}

