var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);
var esMSIE = (navigator.userAgent.indexOf('MSIE') != -1);
var classAttr = false;

function js_inicializar()
{if (W3CDOM){var bLlaves = false;
	for (var i=0; i<iniArray.length; i++) {
		if (iniArray [i].indexOf ('(') >= 0) bLlaves = false;
		else bLlaves = true;
		eval (iniArray [i] +(bLlaves ? '()' : '') +';');
	}
}}

function js_h2 () {
	$$('h2').forEach (function (oH2)
	{
		var txtH2 = oH2.innerHTML.trim ();
		if ((pos = txtH2.indexOf (' ')) < 0) oH2.innerHTML = '<em>'+ txtH2 +'</em>';
		else
		{
			oH2.innerHTML = '<em>'+ txtH2.substr (0, pos) +'</em>'+ txtH2.substr (pos);
		}
	});
}

function js_menu ()
{
	if (typeof window.sPath == 'undefined') var sPath = window.location.pathname.substr (window.location.pathname.lastIndexOf ('/')+1);
	else var sPath = window.sPath;
	$('menu').noAbrir = (sPath.indexOf ('portada') != -1) || (sPath.indexOf ('front-page') != -1);
	$('menu').hijos = 0;
	$('menu').hijosSub = 0;
	$('menu').activo = 0;
	$('menu').activoSub = 0;
	$('menu').noAbrirSub = true;
	$$('#menu li').forEach (function (oLi) {
		if (oLi.parentNode == $('menu')) {
			oLi.posicion = $('menu').hijos;
			$('menu').hijos++;
		}
		if ($(oLi.firstChild).hasClass ('mnup')) {
			$(oLi).firstChild.style.color = '#727272';
			oLi.posicionSub = $('menu').hijosSub;
			$('menu').hijosSub++;
		}
	});
	$$('#menu a').forEach (function (aA) {
		if (aA.href.substr (aA.href.lastIndexOf ('/')+1).indexOf (sPath) >= 0) {
			aA.style.color = '#c7c7c7';
			if (aA.parentNode.parentNode.parentNode.parentNode.id == 'menu') {
				$('menu').activo = aA.parentNode.parentNode.parentNode.posicion;
				if (!$('menu').noAbrir) aA.parentNode.parentNode.parentNode.firstChild.style.color = '#5C9DD5';
			}
			else if (aA.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id == 'menu') {
				$('menu').activo = aA.parentNode.parentNode.parentNode.parentNode.parentNode.posicion;
				$('menu').activoSub = aA.parentNode.parentNode.parentNode.posicionSub;
				$('menu').noAbrirSub = false;
				if (!$('menu').noAbrir) {
					aA.parentNode.parentNode.parentNode.firstChild.style.color = '#5C9DD5';
					aA.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.style.color = '#5C9DD5';
				}
			}
		}
		if ($(aA).hasClass ('mnug')) {
			if ($(aA).style.color != '#5C9DD5') {
				aA.esActivo = false;
				$(aA).onmouseover = function () { this.style.color = '#5C9DD5'; }
				$(aA).onmouseout = function () { if (!this.esActivo) this.style.color = '#c7c7c7'; }
			}
			else aA.esActivo = true;
		}
		if ($(aA).hasClass ('mnup')) {
			if ($(aA).style.color != '#5C9DD5') {
				aA.esActivoSub = false;
				$(aA).onmouseover = function () { this.style.color = '#5C9DD5'; }
				$(aA).onmouseout = function () { if (!this.esActivo) this.style.color = '#727272'; }
			}
			else aA.esActivoSub = true;
		}
	});
	
	// Nivel principal
	new Fx.Accordion ($$('#menu a.mnug'), $$('#menu ul.mnus'), {
		'display': $('menu').activo,
		'start': ($('menu').noAbrir ? 'all-closed' : false),
		onActive: function(aA) {
			$('menu').getChildren ().forEach (function (oLi, j) {
				if (oLi.posicion == $('menu').activo) {
					oLi.firstChild.esActivo = false;
					oLi.firstChild.style.color = '#c7c7c7';
				}
			});
			aA.style.color = '#5C9DD5';
			aA.esActivo = true;
			$('menu').activo = aA.parentNode.posicion;
		},
		onComplete: function (el, ops) {
			var el = this.elements [this.previous];
			if (el.offsetHeight > 0) el.setStyle('height', 'auto');
		}
	});
	
	// Nivel secundario
	new Fx.Accordion ($$('#menu a.mnup'), $$('#menu ul.mnui'), {
		'display': $('menu').activoSub,
		'start': ($('menu').noAbrirSub ? 'all-closed' : false),
		onActive: function(aA) {
			$$('.mnup').forEach (function (oA) {
				if (oA.parentNode.posicionSub == $('menu').activoSub) {
					oA.esActivoSub = false;
					oA.style.color = '#727272';
				}
			});
			aA.style.color = '#5C9DD5';
			aA.esActivoSub = true;
			$('menu').activoSub = aA.parentNode.posicionSub;
		}
	});
	
	$$('li.mnu-imgs').forEach (function (aLi) {
		$(aLi).setStyle ('opacity', 0.5);
		aLi.onmouseover = function () { this.setStyle ('opacity', 0.99); }
		aLi.onmouseout = function () { this.setStyle ('opacity', 0.5); }
	});
}

function js_inputesruta ()
{
	if (objs = document.getElementsByClassName ('inputesruta'))
	{
		for (i=0; i<objs.length; i++)
		{
			objs [i].onkeypress = function (e)
			{
				if (!e) var e = window.event;
				if (e.keyCode) code = e.keyCode;
				else if (e.which) code = e.which;
			
				if (code > 31
					&& (code < 48 
						|| (code > 57 && code < 97)
						|| (code > 122)
					)
					&& code != 45 && code != 46 && code != 35
					&& code != 36 && code != 37 && code != 39) return false 
				else return true;
			}
		}
	}
}

function js_table () {
	$$('#contenido table').forEach (function (oTable) {
		oTable.cellPadding = 0;
		oTable.cellSpacing = 0;
		$each(oTable.rows, function (oTr, i) {
			if (i % 2 != 0) $(oTr).addClass ('par');
			else $(oTr).addClass ('impar');
		});
	});
}

function js_galeria () {
	$$('.carpeta').forEach (function (aCarp) {
		$(aCarp).setStyle ('opacity', '0.5');
		aCarp.onmouseover = function () { this.setStyle ('opacity', '1'); }
		aCarp.onmouseout = function () { this.setStyle ('opacity', '0.7'); }
	});
	$$('#fotos a').forEach (function (aFoto) {
		$(aFoto).setStyle ('opacity', '0.5');
		aFoto.titTam = $(aFoto.lastChild).getSize ();
		aFoto.efectos = $(aFoto.lastChild).effects ();
		aFoto.onmouseover = function () {
			this.efectos.stop ();
			$(this).setStyle ('opacity', '1');
			this.efectos.start ({'background-color': '#000000', 'height': (this.titTam.scrollSize.y+5) +'px'});
		}
		aFoto.onmouseout = function () {
			this.efectos.stop ();
			$(this).setStyle ('opacity', '0.7');
			this.efectos.start ({'background-color': '#181512', 'height': this.titTam.size.y +'px'});
		}
	});
}

function js_playlist () {
	var pls = { movie:"imagerotator.swf",width:"186",height:"131",majorversion:"7",build:"0",bgcolor:"#000000",flashvars:"file=imagerotator.php&transition=fade&overstretch=false&displayheight=131&rotatetime=3" };
	UFO.create(pls,"imgrotator");
}

var googleMap = false;
var googleMapA = false;
var gmOverlays = Array ();
function js_googleMap (opts)
{
	if (typeof GBrowserIsCompatible != 'undefined' && GBrowserIsCompatible()) {
		oMap = $('mapap');
		oMap.style.width = '500px';
		oMap.style.height = '450px';
		
		if (opts)
		{
			if (opts.top) oMap.style.top = opts.top;
			if (opts.right) oMap.style.right = opts.right;
			if (opts.width) oMap.style.width = opts.width;
			if (opts.height) oMap.style.height = opts.height;
		}
		
		googleMap = new GMap2 ($('mapap'));
		googleMap.addControl(new GSmallMapControl());
		googleMap.addControl(new GMapTypeControl());
		googleMap.setCenter (new GLatLng(37.275146, -2.302322), 9, G_NORMAL_MAP);
		googleMap.id = 'googleMap';
		
		oMapA = $('mapaa');
		oMapA.style.width = '500px';
		oMapA.style.height = '450px';
		
		if (opts)
		{
			if (opts.top) oMapA.style.top = opts.top;
			if (opts.right) oMapA.style.right = opts.right;
			if (opts.width) oMapA.style.width = opts.width;
			if (opts.height) oMapA.style.height = opts.height;
		}
		
		googleMapA = new GMap2 ($('mapaa'));
		googleMapA.addControl(new GSmallMapControl());
		googleMapA.addControl(new GMapTypeControl());
		googleMapA.setCenter (new GLatLng(36.837016, -2.463384), 15, G_NORMAL_MAP);
		googleMapA.id = 'googleMapA';
	}
}

function js_googleMapPos (lon, lat, texto)
{
	if (typeof GBrowserIsCompatible != 'undefined' && GBrowserIsCompatible()) {
		var posPoint = new GLatLng(lat, lon);
		googleMap.setCenter (posPoint, 13);
		
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(86, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		icon = new GIcon(baseIcon);
		icon.image = "http://www.google.com/mapfiles/marker.png";
		gmOverlays.push (new GMarker (posPoint, icon));
		googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
		if (texto) GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml(texto); });
	}
}

function js_googleMapPosiciones ()
{
	iconoVerde = new GIcon ();
	iconoVerde.shadow = "http://www.google.com/mapfiles/shadow50.png";
	iconoVerde.iconSize = new GSize(86, 34);
	iconoVerde.shadowSize = new GSize(37, 34);
	iconoVerde.iconAnchor = new GPoint(9, 34);
	iconoVerde.infoWindowAnchor = new GPoint(9, 2);
	iconoVerde.infoShadowAnchor = new GPoint(18, 25);
	icon = new GIcon(iconoVerde);
	icon.image = "img/icong.png";
	
	icon.image = "img/icon_palacioprovincial.png";
	gmOverlays.push (new GMarker (new GPoint (-2.462257, 36.839764), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Palacio provincial</strong><br/>Diputación de Almería<br/>C/ Navarro Rodrígo 17<br/>04001 Almería'); });

	icon.image = "img/icon_teatrocervantes.png";
	gmOverlays.push (new GMarker (new GPoint (-2.463319, 36.837068), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Teatro Cervantes</strong><br/>C/ Poeta Villaespesa 1<br/>04001 Almería<br/>&nbsp;<br/><strong>Secretaría del Festival</strong><br/>Edf. Circulo Mercantil, 2ª planta<br/>&nbsp;'); });
	
	icon.image = "img/icon_almeria.png";
	gmOverlays.push (new GMarker (new GPoint (-2.463319, 36.837068), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Almería</strong>'); });

	icon.image = "img/icon_iea.png";
	gmOverlays.push (new GMarker (new GPoint (-2.466002, 36.839266), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Instituto de Estudios Almerienses</strong><br/>C/ Plaza Julio Alfredo Egea 3<br/>04001 Almería'); });

	icon.image = "img/icon_pubelzaguan.png";
	gmOverlays.push (new GMarker (new GPoint (-2.459403, 36.84234), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Pub El Zaguan</strong><br/>C/ San Leonardo 40<br/>04004 Almería'); });

	icon.image = "img/icon_amhoteles.png";
	gmOverlays.push (new GMarker (new GPoint (-2.4651, 36.840889), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>AM Hoteles</strong><br/>Plaza Flores 3<br/>04001 Almería'); });
	
	icon.image = "img/icon_granhotelalmeria.png";
	gmOverlays.push (new GMarker (new GPoint (-2.463545, 36.834337), icon));
	googleMapA.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Gran Hotel Almería</strong><br/>Avda. Reina Regente 8<br/>04001 Almería'); });
	
	icon.image = "img/icon_velezrubio.png";
	gmOverlays.push (new GMarker (new GPoint (-2.074275, 37.647846), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Vélez Rubio</strong>'); });
	
	icon.image = "img/icon_seron.png";
	gmOverlays.push (new GMarker (new GPoint (-2.508077, 37.343614), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Serón</strong>'); });

	icon.image = "img/icon_cantoria.png";
	gmOverlays.push (new GMarker (new GPoint (-2.18968, 37.352617), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Cantoria</strong>'); });
	
	icon.image = "img/icon_sorbas.png";
	gmOverlays.push (new GMarker (new GPoint (-2.126351, 37.098407), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Sorbas</strong>'); });

	icon.image = "img/icon_tabernas.png";
	gmOverlays.push (new GMarker (new GPoint (-2.387112, 37.05279), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Tabernas</strong>'); });
	
	icon.image = "img/icon_carboneras.png";
	gmOverlays.push (new GMarker (new GPoint (-1.89482, 36.996606), icon));
	googleMap.addOverlay (gmOverlays [gmOverlays.length -1]);
	GEvent.addListener(gmOverlays [gmOverlays.length -1], "mouseover", function() { this.openInfoWindowHtml('<strong>Carboneras</strong>'); });
}

var iniArray = Array ('js_h2', 'js_menu', 'js_table');//, 'js_playlist');

window.onload = js_inicializar;