// JavaScript Document
/**************************************************************
Calendario con efemÃ©rides y eventos. Script por Tunait! (21/4/2007)
Script de libre uso con la condiciÃ³n de que permanezcan intactas estas lÃ­neas, osea, los crÃ©ditos.
DistribuciÃ³n no autorizada en sitios de script sin previa autorizaciÃ³n
Ver condiciones de uso en http://javascript.tunait.com/
tunait@yahoo.com 
****************************************************************/
/*No tocar nada de aquÃ­. La configuraciÃ³n en el archivo calendarioefemerides_config.js */
var hoy = new Date();
var hoy2 = new Date();
//var hoy = "Tue May 19 2009 17:50:35 GMT+0200";
var mes = hoy.getMonth();
var dia = 1;
var anio = hoy.getFullYear();
var diasSemana = new Array ('A','A','A','O','O','L','I');
var meses = new Array('Urtarrila','Otsaila','Martxoa','Apirila','Maiatza','Ekaina','Uztaila','Abuztua','Iraila','Urria','Azaroa','Abendua');
var tunIex=navigator.appName=="Microsoft Internet Explorer"?true:false;
if(tunIex && navigator.userAgent.indexOf('Opera')>=0){tunIex = false;}
tunOp = navigator.userAgent.indexOf('Opera')>=0 ? true: false;
var tunSel = false;
var gHoy = 0;
var anCelda, alCelda, carga =false;
function tunCalendario(){
	if(!carga) escribeControles();
	dia2 = dia;
	tab = document.createElement('table');
	tab.id = 'calendario';
	document.getElementById(idContenedor).appendChild(tab);
	tcabeza = document.createElement('thead');
	tab.appendChild(tcabeza);
	fCalendario = document.createElement('div');
	fCalendario.style.position= 'relative';
	fCalendario.className = "cuadroTexto";
	fCalendario.id = 'tunCuadroTexto';
	document.getElementById(idContenedor).appendChild(fCalendario);
	//fi2 = document.createElement('tr');
	//fi2b = document.createElement('th');
	//fi2b.colSpan = 7;
	//fi2.id = 'mesCalendario';
	//fi2b.appendChild(document.createTextNode(meses[mes] + "  -  " + anio));
	//fi2.appendChild(fi2b);
	//tcabeza.appendChild(fi2);
	fi = document.createElement('tr');
	tcabeza.appendChild(fi);
	for(m=0;m<7;m++){
		ce = document.createElement('th');
		ce.appendChild(document.createTextNode(diasSemana[m]));
		fi.appendChild(ce);
		}
		var escribe = false;
		var escribe2 = true;
	fecha = new Date(anio,mes,dia);
	var d = fecha.getDay()-1 ;
	if(d<0){d = 6;}
	tcuerpo = document.createElement('tbody');
	tab.appendChild(tcuerpo);
	while(escribe2){
	fi = document.createElement('tr');
	co = 0;
		for(t=0;t<7;t++){
			ce = document.createElement('td');
			if(escribe && escribe2){
				fecha2 = new Date(anio,mes,dia);
			
				/*****EVENTOS****/
				var _anio = fecha2.getFullYear().toString();
				var _mes = fecha2.getMonth() + 1;
				_mes = _mes < 10 ? '0' + _mes : _mes.toString();
				var _dia = fecha2.getDate();
				_dia = _dia < 10 ? '0' + _dia : _dia.toString();
				buscaEv = buscaEvento(_anio + _mes + _dia, _mes + _dia);
				if(buscaEv){
					_titl = '';
					for( var x in buscaEv){
						if(_titl == ''){
						//_titl += buscaEv[x].titulo;
							_titl += buscaEv[x].titulo;
							//_titl += x < (buscaEv.length - 1) ? '; ' : '';
						}
					}
				}
				ce.title = buscaEv ? _titl : formatoFecha(_anio + _mes + _dia);
				if(t < 5){
					if(_titl != 'Libre (Haz tu reserva)'){
						if(_titl == 'Visita organizada'){
							ce.className = buscaEv ? 'hayEvento' : '';
							ce.title = _titl;
						}else{
							ce.className = buscaEv ? 'hayEvento2' : '';
							ce.title = _titl;
						}
					}
				}else{
					ce.className = buscaEv ? 'finde' : '';
					ce.title = 'No se organizan visitas ';
				}
				/****FIN EVENTOS****/
			
				if(fecha2.getMonth() != mes){escribe2 = false;}
				else{
					ce.appendChild(document.createTextNode(dia));
					dia++;
					co++;
					ce.style.cursor = calendarioCursor;
					ce.onclick = marcaCalendario;
				}
			}
			if(d == t && !escribe){
				ce.appendChild(document.createTextNode(dia));
				/*****EVENTOS****/
				var _anio = anio;
				var _mes = mes + 1;
				_mes = _mes < 10 ? '0' + _mes : _mes.toString();
				var _dia = dia;
				_dia = _dia < 10 ? '0' + _dia : _dia.toString();
				buscaEv = buscaEvento(_anio + _mes + _dia, _mes + _dia);
				if(buscaEv){
					_titl = '';
					for( var x in buscaEv){
						if(_titl == ''){
							_titl += buscaEv[x].titulo;
							//_titl += x < (buscaEv.length - 1) ? '; ' : '';
						}
					}
				}
				ce.title = buscaEv ? _titl :  formatoFecha(_anio + _mes + _dia);
				if(t < 5){
					if(_titl != 'Libre (Haz tu reserva)'){
						if(_titl == 'Visita organizada'){
							ce.className = buscaEv ? 'hayEvento' : '';
							ce.title = _titl;
						}else{
							ce.className = buscaEv ? 'hayEvento2' : '';
							ce.title = _titl;
						}
					}
				}else{
					ce.className = buscaEv ? 'finde' : '';
					ce.title = 'No se organizan visitas ';
				}
				/****FIN EVENTOS****/
				dia++;co++;
				escribe = true;
				ce.style.cursor = calendarioCursor;
				ce.onclick = marcaCalendario;
			}
			else{
				ce.appendChild(document.createTextNode(" "));
			}
			fi.appendChild(ce);
			if(hoy2.getDate()+1 == dia && mes == hoy2.getMonth() && anio == hoy2.getFullYear() && !gHoy){
				ce.className = "Hoy";
				gHoy = 1;
				if(buscaEv){ escribeEvento(buscaEv);}
				else{ escribeFecha(_anio + _mes + _dia); enlaceListarMes();}

			}
		}
			
		if(co>0){tcuerpo.appendChild(fi);}
		
		}
	dia = dia2;
}
function marcaCalendario(){
	/*eventos*/
	var _anio = anio.toString();
	var _mes = mes + 1;
	_mes = _mes < 10 ? '0' + _mes  : _mes.toString();
	var _dia = this.firstChild.nodeType == 1 ? this.firstChild.nextSibling.nodeValue : this.firstChild.nodeValue;
	_dia = _dia < 10 ? '0' + _dia  : _dia.toString();
	hayEvento = buscaEvento(_anio + _mes + _dia, _mes + _dia);
	/*fin eventos */
	if(hayEvento){ escribeEvento(hayEvento);}
	else {escribeFecha(_anio + _mes + _dia); enlaceListarMes();}
	ceSe = document.createElement('div');
	ceSe.id = "tunSeleccionado";
	with(ceSe.style){
		borderWidth = "0.2em";
		borderStyle = "solid";
		borderColor = "#393d08";
		width = 10  + "px";
		height = 25 + "px";
		position = "absolute";
		zIndex = "1000";
		}
	if(tunSel){
		tunSel.removeChild(tunSel.firstChild);
	}
	tunSel = this;
	this.insertBefore(ceSe,this.firstChild);
	with(ceSe.style){
		width = this.scrollWidth  + "px";
		marginLeft = "-" + (calendarioPaddingCelda + 5) + "px";
		marginTop = "-" + (calendarioPaddingCelda ) + "px";
	}
}

function borra(){
	document.getElementById(idContenedor).removeChild(document.getElementById('calendario'));
	document.getElementById(idContenedor).removeChild(document.getElementById('tunCuadroTexto'));
}
function establecerFecha(){
	tunFe = new Date();
	document.getElementById('tunMes').options[tunFe.getMonth()].selected = true;
	document.getElementById('tunAnio').value = tunFe.getFullYear();
}

function buscaEvento(f, fc){
	try{
		eval(meses[mes].toLowerCase());
	}
	catch(error){
		return false;
	}
	var _array = eval(meses[mes].toLowerCase());
	var _eventos = new Array();
	for(var m in _array){
		if(_array[m].fecha == f || _array[m].fecha == fc){
			_eventos.push(_array[m]);
		}
	}
	return _eventos.length > 0 ? _eventos : false;
}

function escribeEvento(obj){
	escribeFecha(obj[0].fecha);
	escribeEvento2(obj, false);
}
function escribeEvento2(obj, fec){
	var idatzita = false;
	for(var w in obj){
		var ti = document.createElement(tagTitulos);
		if(fec){
			_fec = formatoFecha(obj[w].fecha);
			_d = _fec.substr(0,2);
			ti.appendChild(document.createTextNode(_d + " - "));
		}
		var o = ti;
		mees = (obj[w].fecha).substr(0,2);
		diia = (obj[w].fecha).substr(2,2);
		fech = new Date(anio,mees,diia);
		var day = fech.getDay();
		if(obj[w].enlace){
			if(((mees == 02 || mees == 04 || mees == 06 || mees == 09 || mees == 11) && (day == 0 || day == 1 || day == 2)) || ((mees == 01 || mees == 03 || mees == 05 || mees == 07 || mees == 08 || mees == 10 || mees == 12) && (day == 3 || day == 1 || day == 2))){
				var en = document.createElement('div');
			}else{
				var en = document.createElement('a');
			}
			en.href =obj[w].enlace + '&anio=' + anio;
			o.appendChild(en);
			o = en;
		}
		//var d = new Date(anio + ',' + (obj[w].fecha).substr(0,2) + ',' + (obj[w].fecha).substr(2,2));
		//day = d.getDay();
		//window.alert(day);
		if(obj[w].titulo != 'Libre (Haz tu reserva)'){
			if(obj[w].titulo == 'Visita organizada')
				o.appendChild(document.createTextNode(obj[w].titulo));
			else
				o.appendChild(document.createTextNode(obj[w].titulo));
			idatzita = true;
		}else{
			o.appendChild(document.createTextNode(''));
			idatzita = true;
		}
		document.getElementById('tunCuadroTexto').appendChild(ti);
		var te = document.createElement('p');
		te.appendChild(document.createTextNode(obj[w].texto));
		document.getElementById('tunCuadroTexto').appendChild(te);
	}
	enlaceListarMes();
}
function enlaceListarMes(){
	try{
		eval(meses[mes].toLowerCase());
	}
	catch(error){
		return false;
	}
	var verTodos = document.createElement('strong');
	verTodos.style.cursor = 'pointer';
	verTodos.style.display = 'block';
	//verTodos.appendChild(document.createTextNode(textoVerTodos));
	verTodos.onclick = escribirEventosMes;
	document.getElementById('tunCuadroTexto').appendChild(verTodos);
	
}
function escribeFecha(fecha){
	lNodos = document.getElementById('tunCuadroTexto').childNodes.length;
	if(lNodos){
		for(var m = 0; m < lNodos; m++){
			document.getElementById('tunCuadroTexto').removeChild(document.getElementById('tunCuadroTexto').childNodes[0]);
		}
	}
	var fe = document.createElement('strong');
	fe.appendChild(document.createTextNode(formatoFecha(fecha)));
	document.getElementById('tunCuadroTexto').appendChild(fe);
}
function formatoDiaMes(v){
	v = v < 10 ? '0' + v : v ;
	return v;
}
function formatoFecha(fecha){ 
	if(fecha.toString().length == 8){
		var an = fecha.toString().substring(0,4);
		var me = fecha.toString().substring(4,6);
		var di = fecha.toString().substring(6,8);
	}
	else{
		if(fecha.toString().length == 6){
			var an = fecha.toString().substring(0,4);
			var me = fecha.toString().substring(4,6);
			var di = '';
		}
		else{
			var an = '';
			var me = fecha.toString().substring(0,2);
			var di = fecha.toString().substring(2,4);
		}
	}
	me = eval(me);
	me = meses[me-1];
	return di + " " + me + " " + an;
}

function cambiarMes(val){
	var _anio = document.getElementById('tunAnio').value;
	var _mes = document.getElementById('tunMes').options[document.getElementById('tunMes').selectedIndex].value;
	var _dia = 1;
	eval('_mes' + val + val);
	_mes = _mes < 0 && val == '-' ? 11 : _mes;
	_mes = _mes > 11 && val == '+' ? 0 : _mes;
	if(eval(_mes) == 0 && val == '+') _anio++;
	if(eval(_mes) == 11 && val == '-') _anio--;
	document.getElementById('tunMes').options[_mes].selected = true;
	document.getElementById('tunAnio').value = _anio;
	mes = _mes;
	anio = _anio;
	borra();
	gHoy = 0;
	tunCalendario();
	escribeFecha(_anio.toString() + formatoDiaMes(_mes + 1)); enlaceListarMes();
}
function escribirEventosMes(){
	escribeFecha(anio.toString() + formatoDiaMes((mes + 1).toString()));
	escribeEvento2(eval(meses[mes].toLowerCase()), true);
}

function escribeControles(){
	var obj = document.getElementById(idContenedor);
	var sp = document.createElement('span');
	sp.className = "cambiaMes" ;
	sp.onclick = function() {cambiarMes('-');}
	sp.appendChild(document.createTextNode('<<'));
	obj.appendChild(sp);
	var sel = document.createElement('select');
	sel.className = 'selectores';
	sel.id = 'tunMes';
	sel.onchange = function(){
		mes = this.selectedIndex; borra(); tunCalendario();
	}
	for(var p in meses){
		opt = document.createElement('option');
		opt.value = p;
		opt.appendChild(document.createTextNode(meses[p]));
		sel.appendChild(opt);
	}
	obj.appendChild(sel);
	var campo = document.createElement('input');
	campo.type = 'text';
	campo.id = 'tunAnio';
	campo.className = "selectores";
	campo.maxlength = "4" ;
	campo.size = 4;
	campo.onblur = function(){
		if(!isNaN(this.value)){anio=this.value;borra();tunCalendario();}
	}
	obj.appendChild(campo);
	var sp = document.createElement('span');
	sp.className = "cambiaMes" ;
	sp.onclick = function() {cambiarMes('+');}
	sp.appendChild(document.createTextNode('>>'));
	obj.appendChild(sp);
	carga = true;
}
