var posicion = 1;
var elementos = 0;
var ultimaFecha = '';
var ocultar = 0;
var scrl;
var eventos = new Array;
var noMover = false;
var maxPosicion = -1;

if( cabeceraUrl == undefined )
	var cabeceraUrl = '';

window.addEvent('domready', function()
{
	
		
	var mySlide = new Fx.Slide('cabecera3', {mode: 'vertical'});

	$('botonagenda').addEvent('click', function(e){
		e = new Event(e);
		mySlide.toggle();
		if( estado == 1 ) {estado=0;} else {estado=1;}
		cambiarEstado();
		e.stop();
	});

	$$('a.imgBoton').each( function( enlace ) {
		enlace.addEvent( 'mouseover' , function() {
			var imagen = enlace.getFirst();
			imagen.oSrc = imagen.src;
			imagen.src =  imagen.src.substring( 0 , imagen.src.indexOf( '.gif' ) ) + '_m.gif';
		});

		enlace.addEvent( 'mouseout' , function() {
			var imagen = enlace.getFirst();
			imagen.src = imagen.oSrc;
		});

	});

	//var url = document.location.href;
	//var pagina = String( url.match(/\w+\.asp/) ).toLowerCase()
	
	//if( ( pagina != 'index.asp' ) && (url.substring(url.length - 1 ) != '/' ) )
	//	{
	//		mySlide.hide();
	//		estado = 0;			
	//	}
	
	if( $( 'mostrarAgenda' ) == undefined )
		{
			mySlide.hide();
			estado = 0;
		}
		
	cambiarEstado();

	var lista = $$( '.aviso a, #contenedorrevista a' );	
	lista.each( function( elemento ) {
		var fx = new Fx.Styles( elemento , { duration : 200 , wait : false } );

		elemento.addEvent( 'mouseenter' , function() {
			fx.start( { color : '#ffffff' } );
		});

		elemento.addEvent( 'mouseleave' , function() {
			fx.start( { color : '#ff7f00' } );
		});

	});	
	
	$('izda').style.visibility = 'hidden'

	scrl = new Fx.Scroll('contenedoragenda');	

	$('dcha').addEvent('click', function(e){
		e=new Event(e);
		moverDerecha();
		e.stop();
	});

	$('izda').addEvent('click', function(e){
		e=new Event(e);
		moverIzquierda();
		e.stop();
	});
	
	hacerPeticion( false );	
}); 

		
var leerDatos = function( agenda ) {
	if( agenda.length == 0 )
		{
			$('contenedoragenda').style.cursor = 'default';
			$('izda').style.cursor = 'pointer';
			$('dcha').style.cursor = 'pointer';
			
			posicion -=3;
			maxPosicion = posicion;

			noMover = true;
			return;
		}
	
	agenda.each( function( ag ) {
		var el = new Element( 'td' , { 'id' : 'tc' + ( elementos + 1 ), 'width' : '200 ' } );
		var dia = new Element( 'div' , { 'class' : 'fecha' } ).setHTML( ag.Dia ).injectInside( el );
		var c = new Element( 'div' , { 'class' : 'contenido' } ).injectAfter( dia );
		var cabecera = new Element( 'div' , { 'class' : 'evento' } ).setHTML( ag.TipoEvento ).injectInside( c );
		
		var texto = new Element( 'div' , { 'class' : 'descripcion' } ).setHTML( ag.Texto );
		
		var enlace = new Element( 'a' , { 'href' : ag.urlAgenda , 'class' : 'descripcion' } ).setHTML(ag.Texto).injectAfter( cabecera );
		
		var fx = new Fx.Styles( enlace , { duration : 200 , wait : false } );
		
		enlace.addEvent( 'mouseenter' , function() {
			fx.start( { color : '#b00640' } );
		});
		
		enlace.addEvent( 'mouseleave' , function() {
			fx.start( { color : '#dddddd' } );
		});

		var lugar = new Element( 'div' , { 'class' : 'lugar' } ).setHTML( ag.Lugar ).injectAfter( enlace );
		var municipio = new Element( 'div' , { 'class' : 'lugar' } ).setHTML( ag.Municipio).injectAfter( lugar );
		
		if( ag.Hora != '00:00' )
			var hora = new Element( 'div' , { 'class' : 'hora' } ).setHTML( ag.Hora ).injectAfter( municipio );

		el.injectAfter( 'tc' + elementos );
		elementos++;
		$('tablaagenda').setProperty( 'width' , elementos * 200 );
		ultimaFecha = ag.Fecha;
		eventos.length++;
		eventos[ eventos.length - 1] = ag;
	});
	
	$( 'contenedoragenda' ).style.cursor = 'default';
	$( 'izda' ).style.cursor = 'pointer';
	$( 'dcha' ).style.cursor = 'pointer';
	
	var divPadre = $( 'cabecera3' ).getParent();
	
	if( divPadre.getCoordinates().height != 0 )
		divPadre.setStyle( 'height' , $( 'cabecera3' ).getCoordinates().height );
}


function hacerPeticion( mover )
{
	$('contenedoragenda').style.cursor = 'progress';
	$('izda').style.cursor = 'progress';
	$('dcha').style.cursor = 'progress';

	var url = cabeceraUrl + 'ajax/agenda.asp?ocultar=' + ocultar + '&fecha=' + ultimaFecha;
	
	var request = new Json.Remote( url , {
		onComplete : function( jsonObj ) {
			ocultar = jsonObj.ocultar;
			leerDatos( jsonObj.agenda );
			if( mover )
				moverAlElemento( 'c' )
			else
				actualizarMes();
		}
	}).send();

}

function moverDerecha()
{
	posicion += 3;
	if( posicion > elementos )
		hacerPeticion( true )
	else		
		moverAlElemento();
}

function moverIzquierda()
{
	posicion -= 3;
	if( posicion < 1 )
		posicion = 3
	else
		moverAlElemento();
}

function moverAlElemento()
{
	if( noMover )
	{
		$('dcha').style.visibility = 'hidden';	
		noMover = false;
		return;
	}
	
	scrl.toElement( $( 'tc' + posicion ) );	
	actualizarMes();
	
	var mesActual = eventos[ posicion - 1 ].Mes;

	if( eventos[ posicion + 0 ].Mes != mesActual )
		$('tc' + ( posicion + 1 ) ).getFirst().innerHTML = eventos[ posicion + 0 ].Dia + '<br/><img align="center" src="' + cabeceraUrl + 'comun/imagenes/agda_mes_' + eventos[ posicion + 0 ].Mes + '.gif" />'
	else
		$('tc' + ( posicion + 1 ) ).getFirst().innerHTML = eventos[ posicion + 0 ].Dia;
		
	mesActual = eventos[ posicion + 0 ].Mes;

	if( posicion + 1 < eventos.length )
		{
			if( eventos[ posicion + 1 ].Mes != mesActual )
				$('tc' + ( posicion + 2 ) ).getFirst().innerHTML = eventos[ posicion + 1 ].Dia + '<br/><img align="center" src="' + cabeceraUrl + 'comun/imagenes/agda_mes_' + eventos[ posicion + 1 ].Mes + '.gif" />'
			else
				$('tc' + ( posicion + 2 ) ).getFirst().innerHTML = eventos[ posicion + 1 ].Dia;
		};

	if( posicion <= 3 )
		$('izda').style.visibility = 'hidden'
	else
		$('izda').style.visibility = 'visible';
		
		
	if( posicion == maxPosicion )
		$('dcha').style.visibility = 'hidden'
	else
		$('dcha').style.visibility = 'visible';

}

function actualizarMes()
{
	$('agendames').src = cabeceraUrl + 'comun/imagenes/agda_' + eventos[ posicion - 1 ].Mes + '.gif';
}

function moverAlPrincipio()
{
	posicion = 1;
	moverAlElemento();
	posicion = 3;
}

var estado;

estado = 1;
		
function cambiarEstado()
{
	if( estado == 0 )
		{
			$('botonagenda').src = cabeceraUrl + 'comun/imagenes/mostrar1A.gif';
		}
	else
		{
			$('botonagenda').src = cabeceraUrl + 'comun/imagenes/ocultar1A.gif';
		}
}

function cambiarImagen()
{
	if( estado == 0 )
		{
			$('botonagenda').src = cabeceraUrl + 'comun/imagenes/mostrar1B.gif';
		}
	else
		{
			$('botonagenda').src= cabeceraUrl + 'comun/imagenes/ocultar1B.gif';
		}
}
