


  var streetViewRequested = false;
  var streetViewLoaded = false;
  var streetViewReady = false;
  var streetViewLoadingTO = null;
  var client, panorama, lastLat, lastLon, lastYaw, lastPitch, lastZoom, pendingArgs;
  var recargarFlex = false;
  
  //COLOCAR STREETVIEW SOBRE APLICACION FLEX
  function positionStreetView(visible, width, height, left, top, right, bottom) {
    if (window.console)
      console.log(["positionStreetView", visible, width, height, left, top, right, bottom]);
      
    //alert("positionStreetView: " + visible + " / " + width + " / " + height + " / " + left + " / " + top );
    
    var div = document.getElementById("streetView");
    if (visible && width > 0 && height > 0) {
      div.style.width = width + "px";
      div.style.height = height + "px";
 
      if (left != null)
        div.style.left = left + "px";
      else
        div.style.right = right + "px";
 
      if (top != null)
        div.style.top = top + "px";
      else
        div.style.bottom = bottom + "px";
 
      if (!streetViewRequested) {
        loadStreetView();
      } else if (panorama) {
        window.setTimeout(function() {
          panorama.checkResize();
        }, 10);
      }
    } else {
    	//alert('error');
    	div.style.left = (-1 * parseInt(div.style.width) - 100) + "px";
    }
  }
  
  
  
  //APLICAR COORDENADAS A STREETVIEW
  function setStreetViewLocation(lat, lon, yaw, pitch, zoom) {
  	  recargarFlex = false;
  	  var div = document.getElementById("streetView");
  
  	  //alert("setStreetViewLocation: " + lat + " / " + lon + " / " + yaw + " / " + pitch + " / " + zoom );
  
     if (client && (lat != 0 && lon != 0)) {

	     if (window.console)
		        console.log(["setStreetViewLocation", lat, lon, yaw, pitch, zoom]);		    	 
    	 
    	 client.getNearestPanoramaLatLng(new GLatLng(lat, lon), function(latLon) {
           
    		 if (latLon)
           {
             	//alert("latLon");
             	div.style.display = "block";
             	panorama.setLocationAndPOV(latLon, yaw != null ? {yaw:yaw, pitch:pitch, zoom:zoom} : null);
             	
             	//actualizacion de formulario si es posible
             	if(document.getElementById('ServicioCoordenadasLatitud'))
             	{
             		$('#ServicioCoordenadasLatitud').val(lat);
             		$('#ServicioCoordenadasLongitud').val(lon);
             		$('#ServicioCoordenadasAngulo').val(yaw);
             		$('#ServicioCoordenadasInclinacion').val(pitch);
             		$('#ServicioCoordenadasZoom').val(zoom);
             	}
           }
           else if (lastLat != null)
           {
 	
    			div.style.display = "none";
             	//alert("lastLat != null");
             	showNoPositionError();
             	//updateFlexClient();
           }
           else
           {
             	//alert("else");
             	showNoPositionError();
           }
       });
     } else if (lat != null) {
       pendingArgs = [lat, lon, yaw, pitch, zoom];
     }
  }
  
  //NO SE ENCUENTRAN LAS COORDENADAS EN STREETVIEW
  function showNoPositionError() {
    var movie = document.getElementById("GMaps");
    
	  try  {
	    //movie.onStreetViewError();
	    //alert("sin resultados..");			    
	  } catch(e) {
	    if (window.console)
	      console.log(["showNoPositionError"]);
	  }	
  }
  
  //CARGAR STREETVIEW
  function loadStreetView() {
    google.load("maps", "2", {callback: checkStreetViewLoaded});
    streetViewRequested = true;
  }
    
  function checkStreetViewLoaded() {
    streetViewLoaded = true;
    initializeStreetView();
  }
    
  //INICIALIZANDO STREETVIEW
  function initializeStreetView() {
    if (window.console)
      console.log(["initializeStreetView"]);
 
 	var div = document.getElementById("streetView");
    div.innerHTML = "";
      
    client = new google.maps.StreetviewClient();
    panorama = new google.maps.StreetviewPanorama(div);
      
    if (pendingArgs != null)
      setStreetViewLocation(pendingArgs[0], pendingArgs[1], pendingArgs[2], pendingArgs[3]);
      
    google.maps.Event.addListener(panorama, "error", handleNoFlash);
    google.maps.Event.addListener(panorama, "yawchanged", onYawChanged);
    //google.maps.Event.addListener(panorama, "pitchChanged", onPitchChanged);
    google.maps.Event.addListener(panorama, "initialized", onInitialized);
    google.maps.Event.addListener(panorama, "click", onClick);
	google.maps.Event.addListener(panorama, 'zoomchanged', function(newZoom) {
		if (window.console)
	      console.log("zoomChanged " + [newZoom]);
	     
	    lastZoom = newZoom;
	    
     	//actualizacion de formulario si es posible
     	if($('#ServicioCoordenadasZoom'))
     	{
     		$('#ServicioCoordenadasZoom').val(lastZoom);
     	}			    
	});			    
	google.maps.Event.addListener(panorama, 'pitchchanged', function(newPitch) {
		if (window.console)
	      console.log("onPitchChanged " + [newPitch]);
	     
	    lastPitch = newPitch;
	    
     	//actualizacion de formulario si es posible
     	if($('#ServicioCoordenadasInclinacion'))
     	{
     		$('#ServicioCoordenadasInclinacion').val(lastPitch);
     	}			    
	});		    

      
    streetViewReady = true; // now?
  }
  
	function getMyApp(appName) {
	    var result;
	    if (navigator.appName.indexOf ("Microsoft") !=-1) {
	        result = window[appName];
	    } else {
	        result = document[appName];
	    }
	    //alert(result);
	    return result;
	}		  
  
  //ACTUALIZAR INFORMACION DE FLEX
  function updateFlexClient() {
    if (window.console)
      console.log("updateFlexClient " + [lastLat, lastLon, lastYaw, lastPitch]);
 
 	//actualizacion de formulario si es posible
 	if($('#ServicioCoordenadasLatitud'))
 	{
 		$('#ServicioCoordenadasLatitud').val(lastLat);
 		$('#ServicioCoordenadasLongitud').val(lastLon);
 		$('#ServicioCoordenadasAngulo').val(lastYaw);
 		$('#ServicioCoordenadasInclinacion').val(lastPitch);
 		$('#ServicioCoordenadasZoom').val(lastZoom);
 	}		    
    
    //if(recargarFlex)
    //{
	    //var movie = document.getElementById("GMaps");
	    //movie.onStreetViewUpdate(lastLat, lastLon, lastYaw, lastPitch);
	    
    	if (window.console)
    		console.log("onStreetViewUpdate");
    		
	    var object = getMyApp("GMaps");
	    object.onStreetViewUpdate(lastLat, lastLon, lastYaw, lastPitch);
	    
	    recargarFlex = false;
    //}
  }
		  
 function getYaw() {
     return lastYaw;
 }
  
 function getPitch() {
 	return lastPitch;
 }	
  
 function getZoom() {
 	return lastZoom;
 }			  
  
  function onClick(event) {
    if (window.console)
      console.log("onClick ");
      		  
    //recargarFlex = true;
  }
  
  function onYawChanged(newYaw) {
    if (window.console)
      console.log("onYawChanged " + [newYaw]);
      		  
   
    //if(lastYaw != newYaw)
    //	updateFlexClient();
    	
   	//recargarFlex = true;
    lastYaw = newYaw;
    
 	//actualizacion de formulario si es posible
 	if($('#ServicioCoordenadasAngulo'))
 	{
 		$('#ServicioCoordenadasAngulo').val(lastYaw);
 	}			    
  }
    
  function onInitialized(loc) {
    lastLat = loc.latlng.lat();
    lastLon = loc.latlng.lng();
    updateFlexClient();
  }
    
  function handleNoFlash() {
  }
  

  //--------------------------------------------------------------------------------------------------------------
		
	var map;   
	var geocoder; 
	var alertTimerId = 0;
		  
	function buscarDireccion()
	{
	    if (window.console)
		      console.log(["buscarDireccion"]);

	    clearTimeout(alertTimerId);
	    alertTimerId = setTimeout('getLocation("'+ $('#ServicioDireccion').val() + ', ' + $('#ServicioCiudad').val() + ' ' + $('#ServicioCp').val() +'")', 2000);		
	}

	function getLocation(address)
	{
		if(confirm("Proceder con la busqueda?"))
		{
			geocoder = new GClientGeocoder();
			geocoder.getLocations(address, addAddressToMap);
		}
	}

	function addAddressToMap(response)  
	{  
		console.log([response]);
	  	if (!response || response.Status.code != 200) {  
	    	alert("Lo sentimos, no se ha encontrado esta direccion");  
	  	} else {  
		    place = response.Placemark[0];
		    
		    //Almacenando coordenadas en inputs de texto
			$('#ServicioCoordenadasLatitud').val(place.Point.coordinates[1]);
			$('#ServicioCoordenadasLongitud').val(place.Point.coordinates[0]);

			setStreetViewLocation(place.Point.coordinates[1], place.Point.coordinates[0], 0, 0, 0)
		}  
	}		  
		  
