/*if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", callback, false);
}*/

var xhr; 
var mapa;
var noticias;
var not;
var spanGr;

function startAjax () {
	if(window.ActiveXObject){ 
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
		//alert("Usando IE");
	}
	else if (window.XMLHttpRequest) { 
		xhr = new XMLHttpRequest();
		//alert("Usando navegador como la gente");
	}
	xhr.open("GET", "/site_media/js/dataMapaArray.js");
	xhr.onreadystatechange = callback; 
	xhr.send(null);
}

function callback() {
	if (xhr==undefined) {startAjax()};

	//alert(xhr.readyState);
	if(xhr.readyState === 4) { 
		//alert(xhr.status);
		if(xhr.status === 200) {
			noticias = eval("("+xhr.responseText+")");
			inicio();
		}
	}
}

function inicio() {

	mapa = document.getElementById('mapa');

	for (var i = 0; i < noticias.length; i++) {
		//spanGr variables
		spanGr = document.createElement('span');
		spanGr.style.top = noticias[i].puntoTop;
		spanGr.style.left = noticias[i].puntoLeft;
		spanGr.style.position = "absolute";

		//span constantes
		var span = document.createElement('span');
		span.style.textDecoration = "none";
		span.style.Zindex = "1";
		span.style.backgroundColor = "#000000";
		span.style.color = "#FFFFFF";
		span.style.fontSize = "12px";
		span.style.margin = "0 0 0 3px";
		span.style.padding = "3px 4px 1px";
		
		//girador	
		var giradorWrap = document.createElement('a');
		var girador = document.createElement('img');
		giradorWrap.setAttribute('onmouseover', eval('"mostrarNot"+i+"(event)"'));
		//girador.src = "Img/puntoNegro.gif";
		girador.src = "/site_media/imgs/girador.gif";
		girador.style.width = "9px";			
		giradorWrap.appendChild(girador);
		
		//ciudad
		var ciudad = document.createTextNode(noticias[i].ciudad);
		span.appendChild(ciudad);	

		spanGr.appendChild(giradorWrap);
		spanGr.appendChild(span);
		mapa.appendChild(spanGr);

		not = new Array();
		//noticia variables
		not[i] = document.createElement('div');
		not[i].setAttribute('id', eval('"notId"+i'));
		not[i].style.top = noticias[i].divTop;
		not[i].style.left = noticias[i].divLeft;
		not[i].style.width = noticias[i].divAncho;

		//noticia constantes
		not[i].style.textAlign = "center";
		not[i].setAttribute('onmouseout', "fixOnMouseOut(this, event, 'JavaScript Code')");
		not[i].style.display = "none";
		not[i].style.position = "absolute";
		not[i].style.zIndex = "2";
		not[i].style.backgroundColor = "#FFFFFF";
		not[i].style.padding = "2px 13px 7px";

		var img = document.createElement('img');
		if (noticias[i].img != "") {
			img.src = noticias[i].img;
			img.style.padding = "11px 0 0";
			img.style.width = "100%";
			not[i].appendChild(img);
		}

		var h2 = document.createElement('h2');

		h2.style.textAlign = "left";
		h2.style.marginTop = "7px";
		h2.style.fontSize = noticias[i].fontSize;
		h2.style.lineHeight = "1.1em";

		var a = document.createElement('a');
		a.setAttribute('href', noticias[i].href); 
		a.style.fontFamily = "Universe, Lucida Grande";
		a.style.color = "#000000";
		a.style.textDecoration = "none";
		var aTexto = document.createTextNode(noticias[i].titulo); 
		a.appendChild(aTexto);
		h2.appendChild(a);

		not[i].appendChild(h2);

		mapa.appendChild(not[i]);
	}
}

function position(event) {
	var mousePositions = document.getElementById('mapa');
	var x = event.clientX;
	var y = event.clientY;
	mousePositions.innerHTML = 'X coords: '+x+', Y coords: '+y;
}

function is_child_of(parent, child) {
		if( child != null ) {			
			while( child.parentNode ) {
				if( (child = child.parentNode) == parent ) {
					return true;
				}
			}
		}
		return false;
	}

function fixOnMouseOut(element, event, JavaScript_code) {
		var current_mouse_target = null;
		if( event.toElement ) {				
			current_mouse_target 			 = event.toElement;
		} else if( event.relatedTarget ) {				
			current_mouse_target 			 = event.relatedTarget;
		}
		if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
			//not = document.getElementById("noticia");
			not.style.display = "none";
		}
	}
	
function mostrarNot0(e) {
	not = document.getElementById('notId0');
	not.style.display = "block";
	//xhr.abort();
	//alert(e.clientX);
	//not.style.top = e.clientX - mapa.x + "px";
	//not.style.left = e.clientY - mapa.y + "px";
}

function mostrarNot1(e) {
	not = document.getElementById('notId1');
	not.style.display = "block";
}

function mostrarNot2(e) {
	not = document.getElementById('notId2');
	not.style.display = "block";
}
function mostrarNot3(e) {
	not = document.getElementById('notId3');
	not.style.display = "block";
}
function mostrarNot4(e) {
	not = document.getElementById('notId4');
	not.style.display = "block";
}
function mostrarNot5(e) {
	not = document.getElementById('notId5');
	not.style.display = "block";
}

