// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var centerLatitude = -26.153227642953;
var centerLongitude = -65.338176727295;
var nlat = -22.098;
var nlng = -64.0263;
var slat = -54.8251;
var slng = -69.7573;

var url_ruta = 'http://www.welcomeargentina.com/rutas';
var titulo_ruta = 'Rutas Argentinas - Buscador de rutas de Welcome Argentina';

var startZoom = 4;

var map;
var tramo=false;
var isNS4 = (navigator.appName=="Netscape")?1:0;
var st;
var offsetAlto = 93; //offset para que se muestre el footer (en IE6 lo cambio porque se ve mal)


var mapOverview;
var ovmap;

function ver_tramo(t){
  map.closeInfoWindow();
  if( tramo ){map.removeOverlay(tramo);}
  tramo = t;
  map.addOverlay(tramo);
}

function sel_lnk(e){$$('li.sel').each(function(l){l.removeClassName('sel')});e.addClassName('sel');}

function init()
{
	$('menu').hide();
	$('resultadoO','resultadoD','resultados').invoke('hide');
/*	generar_tips();*/

	if (GBrowserIsCompatible()) {

		map = new GMap2(document.getElementById("map_div"));


		//map.setBounds(GBounds(-17.224758 -76.464844, -56.072035 -57.304688));


		map.addControl(new GLargeMapControl());
		var location = new GLatLng(centerLatitude, centerLongitude);

		map.setCenter(location, startZoom);

	  	map.addControl(new GScaleControl());

/*		map.addMapType(custommap);*/

      mapOverview = new GOverviewMapControl(new GSize(99,99));

		map.addControl(mapOverview);

		ovmap = mapOverview.getOverviewMap();

/*		map.setMapType(custommap);*/
/*		ovmap.setMapType(custommap2);*/

/*		map.removeMapType(G_NORMAL_MAP);*/
/*		map.removeMapType(G_HYBRID_MAP);*/

      map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();



		// map.addOverlay(new GTileLayerOverlay(tilelayerRuta));
		// ovmap.addOverlay(new GTileLayerOverlay(tilelayerRuta));

		  // ====== Restricting the range of Zoom Levels =====
	      // Get the list of map types
/*	      var mt = map.getMapTypes();*/



	      // Overwrite the getMinimumResolution() and getMaximumResolution() methods
/*	      for (var i=0; i<mt.length; i++) {*/
/*	        mt[i].getMinimumResolution = function() {return 4;}*/
/*	        if ( mt[i]!=G_SATELLITE_MAP ){mt[i].getMaximumResolution = function() {return 11;}}*/
/*	      }*/


/*    if(datosGET) buscaRuta();*/

	}
	handleResize();

/*	    mostrarBanners('','');*/

/*        if( iddestino!='' ){ //si solo esta el parametro destino, busco el nombre y lo pongo en el formulario*/
/*          if( IsNumeric(iddestino) ){*/
/*            request2 = GXmlHttp.create();*/
/*            request2.open('GET','buscaCiudad.php?destino='+iddestino,true);*/
/*            request2.onreadystatechange = function() {*/
/*              if( request2.readyState == 4 ){*/
/*                $('txtDestino').value = request2.responseText;*/
/*              }*/
/*            }*/
/*            request2.send(null);*/
/*          }*/
/*        }*/

      // Add a move listener to restrict the bounds range
      GEvent.addListener(map, "move", function() {
        checkBounds();
      });

      // The allowed region which the whole map must be within
      var allowedBounds = new GLatLngBounds(new GLatLng(-55.078367,-74.091797), new GLatLng(-22.674847,-52.382813));

      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));


      }


/*	if( IsNumeric(ido) && IsNumeric(idd) ) buscaRuta();*/
	$('txtOrigen').activate();
}

function windowHeight() {
	//browsers estandar (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return y = document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Por si acaso
	return 0;
}

function buscando(){
  $('btnBuscar').disable();
  $('resultados').innerHTML='';
  $('resultadoO').innerHTML='';
  $('resultadoD').innerHTML='';
  $('cargando').show();
}

function fin_buscando(){
  $('btnBuscar').enable();
  $('cargando').hide();
}

function handleResize() {
	var height = windowHeight();
	height -= offsetAlto;
	$('map_div').style.height = height + 'px';
	map.checkResize();
	height -= 67;
	$('sidebar').style.height = height + 'px';
}

function mostrando_ruta(e,kms){
  $('sidebar-list').innerHTML = '<img src="/rutas/images/anima_carga.gif" style=\"margin:13px;\" />';
  $('txtkms').innerHTML = kms;
  $$('li.current').each(function(e){e.removeClassName('current')});
  e.ancestors()[0].addClassName('current');
}


window.onresize = handleResize;
//window.onload = init;

function mostrarBanners(origen,destino){
 	var requestB = GXmlHttp.create();
  // GLog.write('mostrarBanner.php?o='+origen+'&d='+destino);
	requestB.open('GET', 'mostrarBanner.php?z=169&o='+origen+'&d='+destino, true);
	requestB.onreadystatechange = function() {
		if (requestB.readyState == 4) {
		  var cbanner169;
			var respuesta = requestB.responseText;
			eval(respuesta);
			$('banner169').update(cbanners.cbanner169.val);
			if( cbanners.cbanner169.val.empty() ){
				$('banner169').hide();
			} else {
				$('banner169').show();
			}
		}
	}
	requestB.send(null);
}

