var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);
var classAttr = false;

var iniArray = Array ();//'js_menu');

function js_inicializar()
{if (W3CDOM){
	for (var i=0; i<iniArray.length; i++)
	{
		eval (iniArray [i] +'();');
	}
}}

function js_inicializarAlta (funcion)
{
	iniArray.push (funcion);
}

function js_obj (name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		if (this.obj) this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		if (this.obj) this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		if (this.obj) this.style = document.layers[name];
	}
}

function js_txt_escribir (obj, text)
{
	// Escribe texto en un objeto
	if (obj.innerHTML)
	{
		obj.innerHTML = text;
	}
}

function js_txt_leer (obj)
{
	// Lee texto de un objeto

	if (obj.innerHTML)
	{
		return obj.innerHTML;
	}
	else return false;
}

function js_class_escribir (objeto, clase)
{
	objeto.setAttribute ('class', clase);
	objeto.setAttribute ('className', clase);
}

function js_class_leer (objeto)
{
	if (classAttr)
	{
		res = objeto.getAttribute (classAttr);
	}
	else
	{
		if (objeto.getAttribute ('className'))
		{
			res = objeto.getAttribute ('className');
			classAttr = 'className';
		}
		else if (objeto.getAttribute ('class'))
		{
			res = objeto.getAttribute ('class');
			classAttr = 'class';
		}
		else res = '';
	}

	return res;
}

function js_class_anadir (objeto, clase)
{
	claseActual = js_class_leer (objeto);
	claseRegExp = new RegExp (clase);

	if (claseActual.length > 0)
	{
		if (claseActual.search (claseRegExp))
		{
			js_class_escribir (objeto, claseActual +' '+ clase);
		}
	}
	else
	{
		js_class_escribir (objeto, clase);
	}
}

function js_class_retirar (objeto, clase)
{
	claseActual = js_class_leer (objeto);
	clases = claseActual.split (/\b/);
	if (clases.length > 0)
	{
		clasesNuevas = new Array ();
		for (var i = 0; i<clases.length; i++)
		{
			if ((clases [i] != '') && (clases [i] != clase))
			{
				clasesNuevas.push (clases [i]);
			}
		}
		js_class_escribir (objeto, clasesNuevas.join (' '));
	}
}

function js_menu () {
	$ES('#menu li').forEach (function (obj) { obj.ajustado = false; });
	$ES('#menu li').addEvent ('mouseover', function () {
		if (this.lastChild.className == 'menusub') {
			$(this.lastChild).setStyle ('display', 'block');
			pos = this.offsetLeft;
			if (pos + this.lastChild.offsetWidth > this.parentNode.offsetWidth)
				pos = this.parentNode.offsetWidth - this.lastChild.offsetWidth;
			$(this.lastChild).setStyle ('left', pos +'px');
			if (window.gecko && !this.ajustado) {
				childMaxWidth = 0;
				$ES('#menu #'+ this.id +' A').forEach (function (obj) {
					if (obj.offsetWidth > childMaxWidth) childMaxWidth = obj.offsetWidth;
				});
				$ES('#menu #'+ this.id +' A').forEach (function (obj) {
					obj.setStyle ('width', childMaxWidth +'px');
				});
				this.ajustado = true;
			}
		}
	});
	$ES('#menu li').addEvent ('mouseout', function () {
		if (this.lastChild.className == 'menusub') {
			$(this.lastChild).setStyle ('display', 'none');
		}
	});
}

function js_menu_opcevent (objeto, zonaActiva) {
	objId = objeto.getAttribute ('id');
	if (objId && objId != '') objZona = objeto.getAttribute ('id').substr (1);

	objSpans = objeto.getElementsByTagName ('SPAN');
	for (var i=0; i < objSpans.length; i++) {
		if (js_class_leer (objSpans [i]) == 'menusub') {
			if ((objSpans [i].parentNode.getAttribute ('id').substr (1) == zonaActiva) && (location.hash != '#p')) {
				objSpans [i].style.display = 'block';
				objSpans [i].parentNode.firstChild.style.color = '#fff';
				objSpans [i].parentNode.esactivo = true;
			}
			else {
				objSpans [i].style.display = 'none';
				objSpans [i].parentNode.esactivo = false;
			}
		}
	}

	if ((objeto.firstChild.tagName == 'A') && (objeto.tagName == 'LI')) {
		objeto.firstChild.onmouseover = function () {
			var haySubmenu = false;
			objSpans = this.parentNode.getElementsByTagName ('SPAN');
			for (var i=0; i < objSpans.length; i++) {
				if (js_class_leer (objSpans [i]) == 'menusub') {
					haySubmenu = true;
					if (objSpans [i].parentNode == this.parentNode) {
						objSpans [i].style.display = 'block';
						this.parentNode.firstChild.style.color = '#fff';
						this.parentNode.esactivo = true;
					}
					else {
						objSpans [i].style.display = 'none';
						objSpans [i].parentNode.firstChild.style.color = '#999';
						objSpans [i].parentNode.esactivo = false;
					}
				}
			}
			this.style.color = '#fff'; 
		}
		objeto.firstChild.onmouseout = function () {
			var haySubmenu = false;
			objSpans = this.parentNode.getElementsByTagName ('SPAN');
			for (var i=0; i < objSpans.length; i++) {
				if (js_class_leer (objSpans [i]) == 'menusub') {
					haySubmenu = true;
					if (objSpans [i].parentNode == this.parentNode) {
						objSpans [i].style.display = 'block';
						this.parentNode.firstChild.style.color = '#fff';
						this.parentNode.esactivo = true;
					}
					else {
						objSpans [i].style.display = 'none';
						objSpans [i].parentNode.firstChild.style.color = '#999';
						objSpans [i].parentNode.esactivo = false;
					}
				}
			}
			if (!this.parentNode.esactivo) this.style.color = '#ccc';
		}
	}
}

function js_paginaExternaActivar ()
{
	if (objUrl = document.getElementById ('dat_paginaurl'))
	{
		if (objRadio = document.getElementById ('dat_pagina_url'))
		{
			if (objRadio.checked)
			{
				objUrl.disabled = false;
				objUrl.style.background = "#ffffff";
			}
			else
			{
				objUrl.disabled = true;
				objUrl.style.background = "#dddddd";
			}
			
			objRadio.onclick = function () { document.getElementById ('dat_paginaurl').disabled = false; document.getElementById ('dat_paginaurl').style.background = "#ffffff"; }
			document.getElementById ('dat_pagina_si').onclick = function () { document.getElementById ('dat_paginaurl').disabled = true; document.getElementById ('dat_paginaurl').style.background = "#dddddd"; }
			document.getElementById ('dat_pagina_no').onclick = function () { document.getElementById ('dat_paginaurl').disabled = true; document.getElementById ('dat_paginaurl').style.background = "#dddddd"; }
		}
	}	
}

anuncioLast = 0;
anuncioPer = [];
function js_anuncios () {
	$$('.blqanun').each (function (oBlq, i) {
		iSegs = oBlq.getProperty ('class');
		iSegs = iSegs.substr (iSegs.indexOf ('seg')+3, 2).toInt ();
		if (oBlq.oAes = $ES('a', oBlq)) {
			oBlq.oAes [0].setStyle ('visibility', 'visible');
			if (oBlq.oAes.length > 1) {
				oBlq.aAes = 0;
				oBlq.fxEls = new Fx.Elements (oBlq.oAes, { duration: 1000 });
				(function (p) { anuncioPer [p.i] = js_anuncios_rotar.periodical (p.s * 970, false, p.o.id)}).delay (i*1000, false, {'o': oBlq, 's': iSegs, 'i': i});
			}
		}
	});
}
function js_anuncios_rotar (id) {
	d = new Date ()
	snap = d.getMinutes ()+''+d.getSeconds()+''+d.getMilliseconds();
	if (anuncioLast != snap) {
		o = $(id);
		a = o.aAes;
		b = a + 1;
		if (b >= o.oAes.length) b = 0;
		var c = {};
		c[a] = { opacity: [1, 0] };
		c[b] = { opacity: [0, 1] };
		o.fxEls.start (c);
		o.aAes = b;
		anuncioLast = snap;
	}
}

window.addEvent ('domready', js_inicializar);
window.addEvent ('domready', js_menu);
window.addEvent ('domready', js_anuncios);