var map = null;
var geocoder = null;
var direc = addres+", "+state;
var dos = otras;
var photos;
function getPhotos(panoramio) {
photos = panoramio.photos;

for (id in photos) {
		photosPanoramio(photos[id]);
	}
}
google.load("maps", "2");
function init(){
	if(google.maps.BrowserIsCompatible()){
	map = new google.maps.Map2(document.getElementById("map"));
	geocoder =new google.maps.ClientGeocoder;
	map.addControl(new google.maps.SmallMapControl());
	map.addControl(new google.maps.OverviewMapControl());
	map.addControl(new google.maps.MapTypeControl());
	showAddress(direc);
	for (id in dos){
	moresShowAddress(dos[id].address+', '+ dos[id].state, dos[id].empresa, dos[id].address, dos[id].id_emp);
	}
	//gdir = new GDirections(map);
}
}
window.onload = init;

function moresShowAddress(otra, nombre, direccion, id) {
      if (geocoder) {
        geocoder.getLatLng(
          otra,
          function(point) {
              var marker = new google.maps.Marker(point);
              if(nombre.length > 30){
	              nombre = nombre.substring(0, 30);
	          }
              google.maps.Event.addListener(marker, 'click', function(){
              marker.openInfoWindowHtml("<h4>"+nombre+"</h4><p>"+direccion+"</p><br/><a target='_black' href='ver_anun_emp.php?ID=" + id + "'>See details</a>");});
              map.addOverlay(marker);

          }
        );
      }
}

 function setDirections(toAddress, marca) {
      var fromAddress = document.getElementById("found").value;
      gdir = new  google.maps.Directions(map);
      gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": 'es_ES' });
      marca.closeInfoWindow();
    }

function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 16);
              var marker = new google.maps.Marker(point);
              map.addOverlay(marker);
              var inputForm = document.createElement("form");
			  inputForm.setAttribute("action", "");
			  inputForm.onsubmit = function(){setDirections(address, marker);return false;};
			  inputForm.innerHTML = "<label for='found'>From:<label>"+
			  "<br><input type='text' id='found' size='30'/><br/>"+
			  "<input type='submit' value='Show'/>";
			  var tab1 = new google.maps.InfoWindowTab("Address", address);
              var tab2 = new google.maps.InfoWindowTab("How to get here", inputForm);
              var tabs = [tab1, tab2];
              marker.openInfoWindowTabsHtml(tabs);

              google.maps.Event.addListener(marker, 'click', function(){
              marker.openInfoWindowTabsHtml(tabs);});

              var bounds = map.getBounds();
              var sw = bounds.getSouthWest();
              var ne = bounds.getNorthEast();
              var URL = "http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=20&minx="+sw.lng()+"&miny="+sw.lat()+"&maxx="+ne.lng()+"&maxy="+ne.lat()+"&callback=getPhotos&size=small";
              var jstag=document.getElementById("panoramio");
              jstag.setAttribute("src", URL);

            }
          }
        );
      }
}

function photosPanoramio(fotos){
	    var icon = new GIcon();
		icon.iconSize = new GSize(16, 16);
		icon.iconAnchor = new GPoint(8, 8);
		icon.infoWindowAnchor = new GPoint(8, 0);

	    icon.image = "../images/camera.gif";
	    if(fotos.photo_title.length > 35){
	     fotos.photo_title = fotos.photo_title.substring(0, 35);
	    }
		var marker = new google.maps.Marker(new google.maps.LatLng(fotos.latitude, fotos.longitude), icon);
		map.addOverlay(marker, icon);
		google.maps.Event.addListener(marker, 'click', function(){
			marker.openInfoWindowHtml('<div id ="foto" style="height:'+(fotos.height + 70)+'px;width:'+(fotos.width)+'px"><h4><a target="_black" href="'+fotos.photo_url+'">'+fotos.photo_title+'</a></h4><img src="'+ fotos.photo_file_url+'" width="'+fotos.width+'" height="'+fotos.height+'" /><p><span class="verde">Autor: </span><a target="_black" href="'+fotos.owner_url+'">'+fotos.owner_name+'</a></p><p><span class="aviso"> Las fotos de <a target="_black" href="http://www.panoramio.com">Panoramio</a> est&aacute;n bajo el copyright de sus autores.</span><p></div>');
		})
}
window.onunload = GUnload;
