$(document).ready(function() {
	info();
});

var txt_out;

function info(){

	var txt = "";
	txt_out = "";
	var txt_map = "";
	var caracter = "";

	id = document.getElementById("area_labs")[document.getElementById("area_labs").selectedIndex].value;
	
	$.ajax({
		  type: "POST",
		  url: "info.php",
		  cache: false,
		  data: "id="+id,
		  success: function(msg){

			arr = msg.split("||");

			for(i=0;i<arr.length;i++){
				if(arr[i] != ""){

					caracter = arr[i].substr(0,1)
					if( caracter == "*" ){
						txt = txt + '<img src="/incs/plugins/text_to_image.php?text='+escape(arr[i].substr(1))+'"><br />';
						txt_out = txt_out + escape(arr[i].substr(1))+", ";
						
						//Me salto la 1era linea xq es el nombre de la empresa
						if(i > 0)
							txt_map = txt_map + " " + arr[i].substr(1);
					}else{
						txt = txt + arr[i] + "<br />";
					}
				}
			}

			idLab = id;
			nomLab = document.getElementById("area_labs")[document.getElementById("area_labs").selectedIndex].text;
			Load(txt_map);

			$("#info_lab").html(txt)
					
		  }
	});
	
}




var idLab;
var nomLab;

var geocoder;
var map;
var coordenada1;
var coordenada0;


// On page load, call this function

function Load(address)
{
   // Create new map object
   map = new GMap2(document.getElementById("data1"));
   map.addControl(new GSmallMapControl());

   // Create new geocoding object
   geocoder = new GClientGeocoder();

   // Retrieve location information, pass it to addToMap()
   geocoder.getLocations(address, addToMap);
}

// This function adds the point to the map

function addToMap(response)
{
   // Retrieve the object
   place = response.Placemark[0];

   // Retrieve the latitude and longitude
   coordenada0 = place.Point.coordinates[0];
   coordenada1 = place.Point.coordinates[1];
   $("#link_google").attr("href","http://maps.google.com/maps?hl=es&q="+txt_out+"&sll="+coordenada1+","+coordenada0+"&ie=UTF8&hq="+nomLab+"&z=15&iwloc=A");
   
   point = new GLatLng(coordenada1,	coordenada0);

   // Center the map on this point
   map.setCenter(point, 15);

   // Create a marker
   var aoInfoTabs=[new GInfoWindowTab('Info Empresa', '<table border=0 cellspacing=10 cellpadding=0><tr><td valign="top"><img src="/_In/logos_empresas/'+idLab+'.img"></td><td><b>'+nomLab+'</b></td></tr>')];
   marker = new GMarker(point);
   GEvent.addListener(marker, 'click', function()
		{
	   	marker.openInfoWindowTabsHtml(aoInfoTabs);
		});

   // Add the marker to map
   map.addOverlay(marker);

   // Add address information to marker
   marker.openInfoWindowHtml(aoInfoTabs);
}

function Gcentrar()
{
	if (map!=null)
	{
		map.setCenter(new GLatLng(coordenada1,coordenada0), 15);
	}
}
