var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);
var DHTML = document.getElementById || document.all;
var esMSIE = navigator.userAgent.toLowerCase().indexOf ('msie') +1;
var iniArray = Array ();

function js_inicializar ()
{ if (W3CDOM) { for (var i=0; i<iniArray.length; i++) eval (iniArray [i]); }}

function js_objImg (imgUrl)
{
	obj = new Image;
	obj.src = imgUrl;
	return obj;
}

function js_posicion (obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;

	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	return Array (curleft, curtop);
}

function js_styleFloat (objeto, valor)
{
	if (esMSIE)
	{
		eval ('objeto.style.float = \''+ valor +'\'');
		objeto.style.styleFloat = valor;
	}
	else objeto.style.setProperty ('float', valor, null);
}

function js_esdecimal ()
{
	if (objs = document.getElementsByClassName ('inputesdecimal'))
	{
		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 != 46 && code != 35 && code !=36
					&& code != 37 && code != 39 && code != 9
					&& code != 45) return false ;
				else
				{
					if (code == 46 && this.value.indexOf ('.') == -1) return true;
					else if (code == 45 && this.value.indexOf ('-') == -1) return true;
					else if (code != 46 && code != 45) return true;
					else return false;
				}
			}
		}
	}
}

function js_esnumero ()
{
	if (objs = document.getElementsByClassName ('inputesnumero'))
	{
		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 != 35 && code !=36 && code !=46
					&& code != 37 && code != 39) return false;
				else return true;
			}
		}
	}
}

function js_conselintro ()
{
	if (objs = document.getElementsByClassName ('conselintro'))
	{
		for (i=0; i<objs.length; i++)
		{
			if (objs [i].tagName.toLowerCase () == 'select')
			{
				objs [i].onchange = function ()
				{
					$(this.id +'txt').value = '';
				}
			}
			if (objs [i].tagName.toLowerCase () == 'input')
			{
				objs [i].onchange = function ()
				{
					$(this.id.substr (0, this.id.length-3)).options [0].selected = true;
				}
			}
		}
	}
}

function js_fotografias ()
{
	// Ajustamos las fotogracias de la galería que mostrar
	
	// ¿Cual es el id del contenedor?
	if ($('fotografias')) idFotos = 'fotografias';
	else if ($('inmfotografias')) idFotos = 'inmfotografias';
	else idFotos = false;

	// Si el contenedor tiene fotos iniciamos los ajustes
	if ($(idFotos) && (fotos = $(idFotos).getElementsByTagName ('img')))
	{
		// Creamos una variable para la cola de acciones
		var oQueue;
		
		// Calculamos el id del contenedor de la imagen ico
		idFoto = idFotos.substr (0, idFotos.length -1);
		if (!$(idFoto))
		{
			// Si no existe el contenedor lo creamos
			oFotos = document.createElement ('DIV');
			oFotos.id = idFoto;
			if ($(idFotos).className) oFotos.className = $(idFotos).className;
			esCreado = true;
		}
		else
		{
			oFotos = $(idFoto);
			esCreado = false;
		}
		
		// Contendrá el id de la imagen activa
		oFotos.activa = 'imgico0';
		
		// Array de imágenes, cada enlace contendrá una imagen
		oImgs = Array (i);
		
		// Recorremos las imágenes contenidas por idFotos
		for (i=0; i<fotos.length; i++)
		{
			// Creamos HREF y cambiamos el cursor
			//fotos [i].href = fotos [i].src.replace (/(_100)/, '_img');
			fotos [i].style.cursor = 'pointer';
			
			// Creamos un IMG de tamaño ico por cada thumb existente, le asignamos id y el archivo q le corresponde
			oImgs [i] = document.createElement ('IMG');
			oImgs [i].id = 'imgico' + i;
			oImgs [i].src = fotos [i].src.replace (/(_100)/, '_ico');
			if (i>0) oImgs [i].style.display = 'none';
			
			// Añadimos el A (que contiene el IMG) al DIV contenedor oFotos (que es igual a $(idFoto))
			oFotos.appendChild (oImgs [i]);
			fotos [i].id = 'img100' + i;
			
			// A cada IMG thumb le asignamos un evento al pasar el ratón por el
			fotos [i].onmouseover = function ()
			{
				/*
					Evento asignado a una imagen thumb (100)
					idFotos div contenedor de thumbs
					idFoto	div contenedor de imagen ico
					idIco	imagen ico a mostrar
				*/
				
				// Obtenemos el id del DIV contedor de la imagen thumb (100)
				idFoto = this.parentNode.id.substr (0, this.parentNode.id.length -1);
				
				// Obtenemos el id de la imagen ico a mostrar y la ponemos encima
				idIco = this.id.replace (/(100)/, 'ico');
				$(idIco).style.zIndex = 51;
				
				if ($(idFoto).activa != idIco)
				{
					// Cancelamos todos los efectos en curso
					oQueue = Effect.Queues.get ('global');
					oQueue.each(function(e) { e.cancel() });
					
					// Ocultamos todos los que no sean el ico activo
					for (j = 0; j < $(idFoto).childNodes.length; j++)
					{
						if ($(idFoto).childNodes [j].tagName == 'IMG')
						{
							$(idFoto).childNodes [j].style.zIndex = 50;
							// Si no es la activa la ocultamos
							if ($(idFoto).childNodes [j].id != $(idFoto).activa &&
								$(idFoto).childNodes [j].id != idIco)
							{
								Effect.Fade ($(idFoto).childNodes [j].id);
							}

							// Si no es la que debemos mostrar, la ponemos en 50
							if ($(idFoto).childNodes [j].id != idIco)
							{
								$(idFoto).childNodes [j].style.zIndex = 50;
							}
						}
					}

					
					// Iniciamos un fade de la activa y appear de la que debemos mostrar
					if ($(idFoto).activa) Effect.Fade ($(idFoto).activa, {limit:2});
					Effect.Appear (idIco, {limit:2});
					
					// Marcamos la objetivo como activa
					$(idFoto).activa = idIco;
				}
			}
		}
		if (esCreado) $(idFotos).parentNode.insertBefore (oFotos, $(idFotos));
	}
	else if ($('inmfotografia'))
	{
		$('inmfotografia').parentNode.onclick = js_fotodetalle;
	}
}

function js_fotodetalle ()
{
	if ($('imgDetalle'))
	{
		oImg = $('imgDetalle');
		//Effect.Fade ('imgDetalle');
	}
	else
	{
		oImg = document.createElement ('IMG');
		oImg.id = 'imgDetalle';
		oImg.style.display = 'none';
		oImg.style.zIndex = 100;
		$('contenido').appendChild (oImg);
	}
	oImg.style.position = 'absolute';
	oImg.style.top = '170px';
	oImg.style.right = '10px';
	oImg.style.border = '#000 solid 1px';
	oImg.style.cursor = 'pointer';
	oImg.alt = 'Pulse para ocultar';
	oImg.onclick = function () { Effect.Fade (this.id); }
	if (this.href) oImg.src = this.href.replace (/(_ico|_100)/, '_img');
	else if (this.src) oImg.src = this.src.replace (/(_ico|_100)/, '_img');

	Effect.Appear ('imgDetalle');
	return false;
}

var googleMap = false;
var gmOverlays = Array ();
function js_googleMap (opts)
{
	if (typeof GBrowserIsCompatible != 'undefined' && GBrowserIsCompatible()) {
		oMap = document.createElement ('DIV');
		oMap.id = 'map';
		oMap.innerHTML = 'hahha';
		oMap.style.position = 'absolute';
		oMap.style.top = '230px';
		oMap.style.right = '20px';
		oMap.style.width = '450px';
		oMap.style.height = '450px';
		
		if (opts)
		{
			if (opts ['float'])
			{
				oMap.style.position = 'static';
				oMap.style.top = '0';
				oMap.style.right = '0';
				oMap.style ['float'] = opts ['float'];
			}
			else
			{
				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;
		}
		
		if (opts && opts.parent && opts.parent == 'deinmueble' && $('inmgaleria')) $('inmgaleria').insertBefore (oMap, $('inmgaleria').firstChild);
		else $('contenido').appendChild (oMap);

		googleMap = new GMap2 ($('map'));
		googleMap.addControl(new GSmallMapControl());
		googleMap.addControl(new GMapTypeControl());
		//googleMap.setCenter (new GPoint(-3.8885, 36.9648), 10, G_NORMAL_MAP);
		googleMap.setCenter (new GLatLng(36.9648, -3.8885), 11, G_NORMAL_MAP);
		googleMap.id = 'googleMap';
	}
}
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(20, 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.last ());
		if (texto) GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml(texto); });
	}
}

function js_googleMapPoblaciones ()
{
	iconoVerde = new GIcon ();
	iconoVerde.shadow = "http://www.google.com/mapfiles/shadow50.png";
	iconoVerde.iconSize = new GSize(20, 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";
	
	gmOverlays.push (new GMarker (new GPoint (-3.597991, 37.176452), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Granada'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.988789, 37.002573), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Alhama&nbsp;de&nbsp;Granada'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.89402, 36.957811), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Arenas&nbsp;del&nbsp;Rey'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.857507, 36.954371), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Fornes'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.91265, 36.935721), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Jatar'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.822885, 36.948776), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Jayena'); });
	
	gmOverlays.push (new GMarker (new GPoint (-4.011364, 37.320968), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Montefrio'); });
	
	gmOverlays.push (new GMarker (new GPoint (-3.965147, 37.169774), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Moraleda&nbsp;de&nbsp;Zafayona'); });
	
	gmOverlays.push (new GMarker (new GPoint (-4.067732, 37.148819), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Salar'); });
	
	gmOverlays.push (new GMarker (new GPoint (-4.121439, 36.957265), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Ventas de Zafarraya'); });
	
	gmOverlays.push (new GMarker (new GPoint (-4.144433, 36.975505), icon));
	googleMap.addOverlay (gmOverlays.last ());
	GEvent.addListener(gmOverlays.last (), "mouseover", function() { this.openInfoWindowHtml('Zafarraya'); });
}

function js_googleMapClick ()
{
	if (typeof GBrowserIsCompatible != 'undefined' && GBrowserIsCompatible()) {
		GEvent.addListener(googleMap, 'click', function(marker, point) {
			if (marker) {
			//	googleMap.removeOverlay(overlay);
			} else if (point) {
				gmOverlays.last ().setPoint (point);
				//gmOverlays.last ().point.x = point.x;
				//gmOverlays.last ().point.y = point.y;
				//gmOverlays.last ().setZIndex(Math.round(gmOverlays.last ().getLatitude()*-100000))
				gmOverlays.last ().redraw (true);
				$('dat_longitud').value = point.x;
				$('dat_latitud').value = point.y;
				googleMap.updateOverlays ();
			}
		});
	}
}

function js_ajusteIE ()
{
	// Corregir bug extraño
	if (esMSIE) {
	if ($('banner')) $('banner').style.position = 'absolute';
	if ($('contacto')) $('contacto').style.position = 'absolute';
	if ($('fotografia')) $('fotografia').style.position = 'absolute';
	if ($('menulocal')) $('menulocal').style.position = 'absolute';
	}
}

var arrPreload = Object ();

iniArray.push ('js_conselintro ()');
iniArray.push ('js_esnumero ()');
iniArray.push ('js_esdecimal ()');
iniArray.push ('js_fotografias ()');
iniArray.push ('js_ajusteIE ()');

window.onload = js_inicializar;