/**************************************************************
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 
****************************************************************/
/** Configuración general **/
var idContenedor = &quot;miCalendario&quot; //id del contenedor donde se insertará el calendario
var calendarioCursor = 'pointer'
var calendarioPaddingCelda = 3; //Corrige la posición del cuadro que marca el día seleccionado en caso de haberse aplicado un padding a las celdas
var tagTitulos = 'h2' //Tag a usar en los títulos de eventos y efemérides
var textoVerTodos = 'Listar todo el mes'
/** fin configuración general **/


function evento(fecha, titulo, texto, enlace){
	this.fecha = fecha;
	this.titulo = titulo;
	this.texto = texto;
	this.enlace = false;
	if(enlace) this.enlace = enlace
}

/** agregamos los listados de efemérides y eventos por meses:
var nombreDelMes = new Array();
nombreDelMes.push(new evento('fecha en formato aaaammdd o mmdd', 'Título', 'Texto','Link [opcional]'));
**/
var enero = new Array();
enero .push(new evento('0101', 'Año Nuevo', '','http://es.wikipedia.org/wiki/1_de_enero'))

var febrero = new Array();
febrero .push(new evento('0214', 'San Valentín', 'Día de los enamorados','http://es.wikipedia.org/wiki/San_Valent%C3%ADn'))

var abril = new Array();
abril.push(new evento('0401', 'Día de los Inocentes', '','http://es.wikipedia.org/wiki/Santos_inocentes'))
abril.push(new evento('20070419', 'Pruebita', 'tararía tarará'))
abril.push(new evento('20070419', 'Comienzo esta cosa ', 'Mira que bien'))

var mayo = new Array();
mayo .push(new evento('0525', 'El día del orgullo Friki', 'en conmemoración al estreno oficial de Star Wars-A New Hope.','http://es.wikipedia.org/wiki/D%C3%ADa_del_orgullo_friki'))
mayo .push(new evento('0525', 'El día de la toalla', '','http://es.wikipedia.org/wiki/D%C3%ADa_de_la_toalla'))


var junio = new Array();
junio .push(new evento('0605', 'Día mundial del medio ambiente', '','http://es.wikipedia.org/wiki/D%C3%ADa_mundial_del_medio_ambiente'))


var agosto = new Array();
agosto.push(new evento('0818', 'Santa Helena', 'Las leporcias también escriben','http://es.wikipedia.org/wiki/Elena_de_Constantinopla'))

var septiembre = new Array();
septiembre.push(new evento('0920', 'Cumple Alena', 'Japi berdei'))

var diciembre = new Array();
diciembre.push(new evento('1228', 'Día de los Santos Inocentes', '','http://es.wikipedia.org/wiki/Santos_inocentes'))	
	
/**************************************************************
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 mes = hoy.getMonth()
var dia = 1
var anio = hoy.getFullYear()
var diasSemana = new Array ('L','M','M','J','V','S','D')
var meses = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre')
var tunIex=navigator.appName==&quot;Microsoft Internet Explorer&quot;?true:false;
if(tunIex &amp;&amp; navigator.userAgent.indexOf('Opera')&gt;=0){tunIex = false}
tunOp = navigator.userAgent.indexOf('Opera')&gt;=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 = &quot;cuadroTexto&quot;
	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] + &quot;  -  &quot; + anio))
	fi2.appendChild(fi2b)
	tcabeza.appendChild(fi2)
	fi = document.createElement('tr')
	tcabeza.appendChild(fi)
	for(m=0;m&lt;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&lt;0){d = 6}
	tcuerpo = document.createElement('tbody')
	tab.appendChild(tcuerpo)
	while(escribe2){
	fi = document.createElement('tr')
	co = 0
		for(t=0;t&lt;7;t++){
			ce = document.createElement('td')
			if(escribe &amp;&amp; escribe2){
				fecha2 = new Date(anio,mes,dia)
			
				/*****EVENTOS****/
				var _anio = fecha2.getFullYear().toString()
				var _mes = fecha2.getMonth() + 1
				_mes = _mes &lt; 10 ? '0' + _mes : _mes.toString()
				var _dia = fecha2.getDate()
				_dia = _dia &lt; 10 ? '0' + _dia : _dia.toString()
				buscaEv = buscaEvento(_anio + _mes + _dia, _mes + _dia)
				if(buscaEv){
					_titl = '';
					for( var x in buscaEv){
						_titl += buscaEv[x].titulo
						_titl += x &lt; (buscaEv.length - 1) ? '; ' : '';
					}
				}
				ce.title = buscaEv ? _titl : formatoFecha(_anio + _mes + _dia)
				ce.className = buscaEv ? 'hayEvento' : ''
				/****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 &amp;&amp; !escribe){
				ce.appendChild(document.createTextNode(dia))
				/*****EVENTOS****/
				var _anio = anio
				var _mes = mes + 1
				_mes = _mes &lt; 10 ? '0' + _mes : _mes.toString()
				var _dia = dia
				_dia = _dia &lt; 10 ? '0' + _dia : _dia.toString()
				buscaEv = buscaEvento(_anio + _mes + _dia, _mes + _dia)
				if(buscaEv){
					_titl = '';
					for( var x in buscaEv){
						_titl += buscaEv[x].titulo
						_titl += x &lt; (buscaEv.length - 1) ? '; ' : '';
					}
				}
				ce.title = buscaEv ? _titl :  formatoFecha(_anio + _mes + _dia)
				ce.className = buscaEv ? 'hayEvento' : ''
				/****FIN EVENTOS****/
				dia++;co++
				escribe = true
				ce.style.cursor = calendarioCursor
				ce.onclick = marcaCalendario
			}
			else{
				ce.appendChild(document.createTextNode(&quot; &quot;))
			}
			fi.appendChild(ce)
			if(hoy.getDate()+1 == dia &amp;&amp; mes == hoy.getMonth() &amp;&amp; anio == hoy.getFullYear() &amp;&amp; !gHoy){
				ce.className = &quot;Hoy&quot;
				gHoy = 1;
				if(buscaEv){ escribeEvento(buscaEv)}
				else{ escribeFecha(_anio + _mes + _dia); enlaceListarMes()}

			}
		}
			
		if(co&gt;0){tcuerpo.appendChild(fi)}
		
		}
	dia = dia2
}
function marcaCalendario(){
	/*eventos*/
	var _anio = anio.toString();
	var _mes = mes + 1
	_mes = _mes &lt; 10 ? '0' + _mes  : _mes.toString()
	var _dia = this.firstChild.nodeType == 1 ? this.firstChild.nextSibling.nodeValue : this.firstChild.nodeValue;
	_dia = _dia &lt; 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 = &quot;tunSeleccionado&quot;
	with(ceSe.style){
		borderWidth = &quot;1px&quot;
		borderStyle = &quot;solid&quot;
		borderColor = &quot;#ff0000&quot;
		width = this.scrollWidth  + &quot;px&quot;
		height = this.scrollHeight + &quot;px&quot;
		position = &quot;absolute&quot;
		zIndex = &quot;1000&quot;
		}
	if(tunSel){
		tunSel.removeChild(tunSel.firstChild)
	}
	tunSel = this
	this.insertBefore(ceSe,this.firstChild)
	with(ceSe.style){
		width = this.scrollWidth  + &quot;px&quot;
		marginLeft = &quot;-&quot; + (calendarioPaddingCelda + 1) + &quot;px&quot;
		marginTop = &quot;-&quot; + (calendarioPaddingCelda + 1) + &quot;px&quot;
	}
}

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 &gt; 0 ? _eventos : false;
}

function escribeEvento(obj){
	escribeFecha(obj[0].fecha) 
	escribeEvento2(obj, false)
}
function escribeEvento2(obj, fec){
	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 + &quot; - &quot;))
		}
		var o = ti
		if(obj[w].enlace){
			var en = document.createElement('a')
			en.href =obj[w].enlace
			o.appendChild(en)
			o = en
		}

		o.appendChild(document.createTextNode(obj[w].titulo))
		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 &lt; 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 &lt; 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 + &quot; &quot; + me + &quot; &quot; + 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 &lt; 0 &amp;&amp; val == '-' ? 11 : _mes
	_mes = _mes &gt; 11 &amp;&amp; val == '+' ? 0 : _mes
	if(eval(_mes) == 0 &amp;&amp; val == '+') _anio++
	if(eval(_mes) == 11 &amp;&amp; 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 = &quot;cambiaMes&quot; 
	sp.onclick = function() {cambiarMes('-')}
	sp.appendChild(document.createTextNode('&lt;&lt;'))
	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 = &quot;selectores&quot;
	campo.maxlength = &quot;4&quot; 
	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 = &quot;cambiaMes&quot; 
	sp.onclick = function() {cambiarMes('+')}
	sp.appendChild(document.createTextNode('&gt;&gt;'))
	obj.appendChild(sp)
	carga = true
}
