var oBody = false;

function js_reservas () {
	if ($('reservadatos')) {
		$('dat_obra').addEvent ('change', function () {
			$('dat_lugar').options.length= 0;
			if (this.options.length> 1) {
				reps = new Hash (obrJs.get(this.options [this.selectedIndex].value).reps);
				reps.each (function (r) {
					$('dat_lugar').options [$('dat_lugar').options.length] = new Option (r.lugar, r.idl);
					$('dat_lugar').options [$('dat_lugar').options.length-1].fecha = r.fecha;
					$('dat_lugar').options [$('dat_lugar').options.length-1].funcs = r.funcs;
				});
				if ($('dat_lugar').options.length) $('dat_lugar').fireEvent ('change');
			}
			if (!$('dat_lugar').options.length) {
				lugJs.each (function (v, k) {
					$('dat_lugar').options [$('dat_lugar').options.length] = new Option (v, k);
					$('dat_lugar').options [$('dat_lugar').options.length-1].fecha = false;
					$('dat_lugar').options [$('dat_lugar').options.length-1].funcs = false;
				});
				if ($('dat_lugar').options.length) $('dat_lugar').fireEvent ('change');
			}
		});
		$('dat_lugar').addEvent ('change', function () {
			if ($('dat_pases').options.length) {
				$('dat_pases').valSel= $('dat_pases').options [$('dat_pases').selectedIndex].value;
				$('dat_pases').options.length= 0;
			}
			if ($('dat_lugar').options [$('dat_lugar').selectedIndex].funcs)
				$('dat_lugar').options [$('dat_lugar').selectedIndex].funcs.each (function (f) {
					$('dat_pases').options [$('dat_pases').options.length] = new Option (f, f);
				})
			;
			if ($('dat_lugar').options [$('dat_lugar').selectedIndex].fecha) $('dat_fecha').set ('value', js_date (new Date ($('dat_lugar').options [$('dat_lugar').selectedIndex].fecha)));
			else $('dat_fecha').set ('value', '');
		});
		sObra= $('dat_obra').selectedIndex;
		sLugar= $('dat_lugar').selectedIndex;
		sPases= $('dat_pases').selectedIndex;
		$('dat_obra').fireEvent ('change');
		$('dat_obra').selectedIndex = sObra;
		$('dat_lugar').selectedIndex = sLugar;
		$('dat_pases').selectedIndex = sPases;

		$$('input.entero').addEvent ('keydown', function (ev) { return !(ev.code> 31 && (ev.code< 48 || ev.code> 57) && (ev.code< 96 || ev.code> 105) && ev.code!= 46); });
		$('frmReserva').addEvent ('submit', function () { $('dat_fecha').set ('readonly', false) });
	}
}
function js_lugar (o, l) {
	o--; l--;
	if (o <= obras.length) {
		if (obras[o].length > 1) $('dat_fecha').set ('value', js_date (new Date (obras[o][l])));
		else $('dat_fecha').set ('value', 'Sin fecha definida: '+ js_mes (new Date (obras[o][0])));
	}
	else $('dat_fecha').set ('value', 'La obra no tiene fecha concreta asignada');
}

function js_pdf () {
	if ($$('a.pdf') && typeof pageTracker != 'undefined' && pageTracker) {
		$$('a.pdf').addEvent ('click', function (e) {
			e.stop ();
			pageTracker._trackPageview (this.get ('href'));
			window.open (this.get ('href'));
		});
	}
	if ($$('div.participantes a')) $$('div.participantes a').addEvent ('click', function (e) {
		e.stop ();
		window.open (this.get ('href'));
	});
}

function js_videos () {
	if ($$('a.ytvideo')) {
		$$('a.ytvideo').each (function (oA) {
			oA.getParent().setStyle ('clear', 'both');
			ytId = oA.href.substr (oA.href.indexOf ('=')+1);
			o = new Swiff ('http://www.youtube.com/v/'+ytId, {
				id: 'ytv',
				width: 445,
				height: 364,
				params: {
					allowFullScreen: true,
					allowscriptaccess: 'never'
				},
				vars: {
					hl: 'es',
					fs: 1,
					rel: 0,
					color1: '0x2C8A99', //5d1719',
					color2: '0x154E57', //cd311b',
					border: 1
				}
			});
			o.replaces (oA);
		});
	}
}
 
function js_date (fecha) {
	oT = (fecha);
	dia = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sábado'];
	return (dia [oT.getDay ()] +' '+ oT.getDate () +' de '+ js_mes (oT));
}
function js_mes (fecha) {
	oT = typeof fecha == 'string' ? (fecha) : fecha;
	mes = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
	return (mes [oT.getMonth ()] +' '+ oT.getFullYear ());
}

window.addEvent ('domready', function () { js_reservas (); js_pdf (); js_videos ();
	if ($('usb')) $('usb').getElement ('a').addEvent ('click', function (ev) {
		ev.stop ();
		window.open (this.href, 'promousb', 'height=230,width=450,toolbar=no,status=no,scrollbars=no');
	});
	else if ($('zprogramacion')) {
		$('cuecont').adopt (new Element ('div', { 'id': 'nBanner', 'styles': { 'opacity': 0 }}));
		(function (){$('nBanner').fade('in');}).delay (1000);
	}
});
