// JavaScript Document
var tabName = new Object();
for(var i = 1; i <= 9; i++)
	tabName["\""+i+"\""] = false;
var estado = [];
var tabActivo = 0;

function crearTab(id, label)
{
	if(tabName["\""+id+"\""] == false){
		var tabID = "tab."+ id;
		var tabs = document.getElementById("tabs");
		var tab = document.createElement("div");
		tab.className = "tab";
		tab.setAttribute("class", "tab");
		tab.style.width = "95px";
		tab.style.height = "14px";
		tab.style.overflow = "auto";
		tab.style.paddingLeft = "5px";
		tab.style.paddingTop = "4px";

		tab.setAttribute("id", tabID);
		tab.onmouseover = new Function("tabOver(this)");
		tab.onmouseout = new Function("tabOut(this)");
		tab.onclick = new Function("tabClick('"+label+"."+id+"')");
		tabName["\""+id+"\""] = true;
	
		var tabLabel = document.createElement("div");
		tabLabel.setAttribute("id", "label."+id);
		tabLabel.id = "label."+id;
		tabLabel.style.width = "60px";
		tabLabel.style.overflow = "auto";
		tabLabel.style.cssFloat = "left";
		tabLabel.setAttribute("float", "left");
		tabLabel.style.fontSize = "10px";

		var res = document.createElement("span");
		res.setAttribute("id", id);
		res.id = id;
		tabActivo = id;
		

		var etq = document.createTextNode(label);
		
		tabLabel.appendChild(etq);
		tabLabel.appendChild(res);
	
		var tabButton = document.createElement("div");
		tabButton.style.cssFloat = "left";
		tabButton.setAttribute("float", "left");
		tabButton.style.marginLeft = "2px";
		tabButton.style.fontSize = "8px";
		tabButton.style.color = "#FFFFFF";
		tabButton.onclick = new Function("tabClose('"+id+"', '"+label+"')");
	
		var button = document.createTextNode("[X]");
		tabButton.appendChild(button);
	
		tab.appendChild(tabLabel);
		tab.appendChild(tabButton);
	
		tabs.appendChild(tab);
		tabClick(""+label+"."+id+"");
	}else{
		tabClose(id, label);
	}
}
function tabOver(elemento)
{
	estado.push(elemento.className);
	elemento.className = "tabOn";
}
function tabOut(elemento)
{
	var x = estado.pop();
	elemento.className = x;
}
function tabClose(id, label)
{
	if(tabName["\""+id+"\""] == true)
	{
		var tabs = document.getElementById("tabs");
		var info = document.getElementById("info");
		var n = document.getElementById("tab."+id);
		tabs.removeChild(n);
		tabName["\""+id+"\""] = false;
		var boton = ""+label+"."+id;
		if(id < 8)
			document.getElementById(boton).className = "boton";
		info.innerHTML = "";
	}
	limpiaPantalla();
}
function tabClick(e)
{
	var secID = (e).split(".");
	limpiaPantalla();
	for(var i = 1; i <= 9; i++)
	{
		var tab = document.getElementById("tab."+i);
		if(tab){
		if(i == secID[1])
			tab.className = "tabOn";
		else
			tab.className = "tab";
		}
	}
	estado.push("tabOn");
	
	informacion(secID[1]);
}

function informacion(cat)
{
	if(cat){
	var ajax = 1;
	var zoom = mapa.GetZoomLevel();
	var c1 = mapa.PixelToLatLong(new VEPixel(0,0));
	var c2 = mapa.PixelToLatLong(new VEPixel(ancho,alto));
	var myRand = parseInt(Math.random()*9999999999999);
	if(cat < 9){

    var xml = $.ajax({
	    url: "info.php",
		type: "GET",
		data: "rand="+myRand+"&cat="+cat+"&lat1="+c1.Latitude+"&long1="+c1.Longitude+"&lat2="+c2.Latitude+"&long2="+c2.Longitude+"&ajax="+ajax+"&zoom="+zoom,
		success: function(xml){
		    nodos(xml);
		}
	});

	}else{
		var duda = $("#texto").val();

    var xml = $.ajax({
	    url: "buscar.php",
		type: "GET",
		data: "rand="+myRand+"&duda="+duda+"&ajax="+ajax+"&lat1="+c1.Latitude+"&long1="+c1.Longitude+"&lat2="+c2.Latitude+"&long2="+c2.Longitude+"&zoom="+zoom,
		success: function(xml){
		    resultado(xml);
		}
	});

	}
	}
}

function nodos(xml)
{
	var z = 0;
    var lugares = "";
    var j = 0;
    var nodoID = xml.getElementsByTagName("id")[0].childNodes[0].nodeValue;
    var nodoSUB = xml.getElementsByTagName("cat")[0];

    if(xml.getElementsByTagName("titulo")[z]){
        var lugares = "<table width=\"100%\" id=\"resultados\"><tr bgcolor=\"#FFAC65\"><th>No.</th><th>Titulo</th><th>Descripcion</th><th>Categoria</th><th>Subcategoria</th></tr>";
        while(xml.getElementsByTagName("chinchota")[z])
        {
            pushpin[elemento] = new Chinche();
    		pushpin[elemento].setID(z);

    		var nodoA = xml.getElementsByTagName("titulo")[z];
    		pushpin[elemento].setTitulo(nodoA.childNodes[0].nodeValue);

            var nodoB = xml.getElementsByTagName("descripcion")[z];
            pushpin[elemento].setDescripcion(nodoB.childNodes[0].nodeValue);

            var nodoC = xml.getElementsByTagName("latitud")[z];
    		pushpin[elemento].setLatitud(nodoC.childNodes[0].nodeValue);

            var nodoD = xml.getElementsByTagName("longitud")[z];
		pushpin[elemento].setLongitud(nodoD.childNodes[0].nodeValue);

    		var nodoE = xml.getElementsByTagName("categoria")[z];
    		pushpin[elemento].setCategoria(nodoE.childNodes[0].nodeValue);

    		var aData = pushpin[elemento].getTitulo().split("<br />");
    		var l = 0;

            while(l < aData.length)
    		{
                if(pushpin[elemento].getDescripcion() == " - ")
                    var d = aData[l];
                else
                    var d = pushpin[elemento].getDescripcion();

                lugares += "<tr><td align='center'>"+(j+1)+"</td><td><span onmouseover=\"ShowInfoBox(shape["+pushpin[elemento].getID()+"]);\" style=\"cursor:pointer;\">"+aData[l]+"</span></td><td>"+d+"</td><td>"+pushpin[elemento].getCategoria()+"</td><td>"+pushpin[elemento].getSubcategoria()+"</td><tr>";
                l++;
                j++;
            }

            chinche_dos(pushpin[elemento]);

            z++;
        }
        lugares += "</table>";
        document.getElementById('info').innerHTML = lugares;
    	document.getElementById(nodoID).innerHTML = "("+j+")";
    }else{
        document.getElementById('info').innerHTML = "<h1>Lo sentimos, no hay referencias en esa zona</h1>";
        if(document.getElementById(nodoID))
            document.getElementById(nodoID).innerHTML = "";
    }
}

function chinche_dos(ex)
{
	try{
		shape[ex.getID()] = new VEShape(VEShapeType.Pushpin, ex.posicion());
		
		if(ex.getTitulo() == " - ")
			shape[ex.getID()].SetTitle("");
		else
        	shape[ex.getID()].SetTitle(ex.getTitulo());

		if(ex.getDescripcion() == " - ")
			shape[ex.getID()].SetDescription("");
		else
        	shape[ex.getID()].SetDescription(ex.getDescripcion());
			
		mapa.ClearInfoBoxStyles();
		
		shape[ex.getID()].SetCustomIcon("<img src='edificio.gif'/>");

		mapa.AddShape(shape[ex.getID()]);
		
		elemento++;
	}catch(e){
		alert("No se puede: "+e);
	}
}
function chinche(ex)
{
	try{
		mapa.ClearInfoBoxStyles();
		shape[ex.getID()] = new VEShape(VEShapeType.Pushpin, ex.posicion());
        shape[ex.getID()].SetTitle(ex.getTitulo());
		if(ex.getDescripcion() == " - ")
			shape[ex.getID()].SetDescription(ex.getTitulo());
		else
	        shape[ex.getID()].SetDescription(ex.getDescripcion());
			
		if(ex.getImagen() == "default"){
			shape[ex.getID()].SetCustomIcon("<img src='edificio.gif'/>");
		}else{
			shape[ex.getID()].SetCustomIcon("<img src='"+ex.getImagen()+"' />");
		}
		
		mapa.AddShape(shape[ex.getID()]);
		elemento++;
	}catch(e){
		alert("No se puede: "+e);
	}
	return true;
}