var markersPlaces = [];
var markersTraffic = [];
var markersRecomendados = [];
var markersRecomendados = [];
var markersReferencias = [];
var markersCercanos = [];
var markersSearch = [];
var markersUrl = [];


var albergues = [];
var pagoFacil = [];
var restos = [];
var alojamiento = [];
var indumentaria = [];
var bancos = [];
var estacionesServicio = []; 


var tituloComercio;
var contentString;

function placeMarker(markerArray, location, title, img,id, grande) {
	//var clickedLocation = new google.maps.LatLng(location);
if (grande==1)
{
  var image = new google.maps.MarkerImage(img,
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 20),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 20));
}
else
{
  var image = new google.maps.MarkerImage(img,
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 20),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 20));
}

	
	var marker = new google.maps.Marker({
		position: location, 
		map: map,
		icon: image,
		title: title
	});
	
		var latlngStr = String(location);


    google.maps.event.addListener(marker, 'click', function() {	
	
	  tb_show("estas viendo informacion de " + title,'http://usuarios.minnus.com.ar/comercios.php?id='+ id +'&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=380&width=550&inlineId=map_window',true);
    });


    google.maps.event.addListener(marker, "mouseover", function(e) {
      var infoTotal = new InfoTotal({latlng: marker.getPosition(), map: map},title);
    });




	markerArray.push(marker);
}

function maxiPlace(markerArray, location, title, img,id, grande) {
	//var clickedLocation = new google.maps.LatLng(location);
if (grande==1)
{
  var image = new google.maps.MarkerImage(img,
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 20),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 20));
}
else
{
  var image = new google.maps.MarkerImage(img,
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 20),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 20));
}

	
	var marker = new google.maps.Marker({
		position: location, 
		map: map,
		icon: image,
		title: title
	});
	
		var latlngStr = String(location);


    google.maps.event.addListener(marker, 'click', function() {		
	  tb_show("estas viendo informacion de " + title,'http://usuarios.minnus.com.ar/comercios.php?id='+ id +'&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=380&width=550&inlineId=map_window',true);
    });


    google.maps.event.addListener(marker, "mouseover", function(e) {
      //var infoTotal = new InfoTotal({latlng: marker.getPosition(), map: map},title);
    });

    google.maps.event.addListener(marker, "mouseout", function(e) {
		//deleteRecomendados();
    });
	markerArray.push(marker);
}
//////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////



function InfoBox(opts,contenido) {
	google.maps.OverlayView.call(this);
	this.latlng_ = opts.latlng;
	this.map_ = opts.map;
	this.offsetVertical_ = -275;
	this.offsetHorizontal_ = -1;
	this.height_ = 260;
	this.width_ = 308;
	contentString = contenido;
	var me = this;
	this.boundsChangedListener_ =
	google.maps.event.addListener(this.map_, "bounds_changed", function() {

		return me.panMap.apply(me);
	});

	this.setMap(this.map_);
  
}


function InfoTotal(opts,titulo,estilo) {
	google.maps.OverlayView.call(this);
	this.latlng_ = opts.latlng;
	this.map_ = opts.map;
	if(estilo==1)
	{
		this.offsetVertical_ = -44;
		this.offsetHorizontal_ = -1;
		this.height_ = 30;
		this.width_ = 100;		
		}
	else
	{
		this.offsetVertical_ = -22;
		this.offsetHorizontal_ = -1;
		this.height_ = 13;
		this.width_ = 90;
		}
	var me = this;

	tituloComercio=titulo;
	this.boundsChangedListener_ =
	google.maps.event.addListener(this.map_, "bounds_changed", function() {
		return me.panMap.apply(me);
	});
	this.setMap(this.map_);
  
}

InfoTotal.prototype = new google.maps.OverlayView();

/* Creates the DIV representing this InfoTotal
 */
InfoTotal.prototype.remove = function() {
  if (this.div_) {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  }
};

/* Redraw the Bar based on the current projection and zoom level
 */
InfoTotal.prototype.draw = function() {
  // Creates the element if it doesn't exist already.
  this.createElement();
  if (!this.div_) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our Bar
  var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
  if (!pixPosition) return;

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
  this.div_.style.height = this.height_ + "px";
  this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
  this.div_.style.display = 'block';
};

/* Creates the DIV representing this InfoTotal in the floatPane.  If the panes
 * object, retrieved by calling getPanes, is null, remove the element from the
 * DOM.  If the div exists, but its parent is not the floatPane, move the div
 * to the new pane.
 * Called from within draw.  Alternatively, this can be called specifically on
 * a panes_changed event.
 */
InfoTotal.prototype.createElement = function() {
  var panes = this.getPanes();
  var div = this.div_;
  if (!div) {
    // This does not handle changing panes.  You can set the map to be null and
    // then reset the map to move the div.
    div = this.div_ = document.createElement("div");
    div.style.border = "0px none";
    div.style.position = "absolute";
    //div.style.background = "url('http://gmaps-samples.googlecode.com/svn/trunk/images/blueinfowindow.gif')";
	div.style.background = "#000"
	div.style.color="#FFF"
    div.style.width = this.width_ + "px";
    div.style.height = this.height_ + "px";
    div.style.padding = "1px"
    var contentDiv = document.createElement("div");
    contentDiv.innerHTML = tituloComercio;
    var topDiv = document.createElement("div");
    topDiv.style.textAlign = "right";



    div.appendChild(topDiv);
    div.appendChild(contentDiv);
    div.style.display = 'none';
    panes.floatPane.appendChild(div);
    this.panMap();
  } else if (div.parentNode != panes.floatPane) {
    // The panes have changed.  Move the div.
    div.parentNode.removeChild(div);
    panes.floatPane.appendChild(div);
  } else {
    // The panes have not changed, so no need to create or move the div.
  }
}

/* Pan the map to fit the InfoTotal.
 */
InfoTotal.prototype.panMap = function() {
  // if we go beyond map, pan map
  var map = this.map_;
  var bounds = map.getBounds();
  if (!bounds) return;

  // The position of the infowindow
  var position = this.latlng_;

  // The dimension of the infowindow
  var iwWidth = this.width_;
  var iwHeight = this.height_;

  // The offset position of the infowindow
  var iwOffsetX = this.offsetHorizontal_;
  var iwOffsetY = this.offsetVertical_;

  // Padding on the infowindow
  var padX = 40;
  var padY = 40;

  // The degrees per pixel
  var mapDiv = map.getDiv();
  var mapWidth = mapDiv.offsetWidth;
  var mapHeight = mapDiv.offsetHeight;
  var boundsSpan = bounds.toSpan();
  var longSpan = boundsSpan.lng();
  var latSpan = boundsSpan.lat();
  var degPixelX = longSpan / mapWidth;
  var degPixelY = latSpan / mapHeight;

  // The bounds of the map
  var mapWestLng = bounds.getSouthWest().lng();
  var mapEastLng = bounds.getNorthEast().lng();
  var mapNorthLat = bounds.getNorthEast().lat();
  var mapSouthLat = bounds.getSouthWest().lat();

  // The bounds of the infowindow
  var iwWestLng = position.lng() + (iwOffsetX - padX) * degPixelX;
  var iwEastLng = position.lng() + (iwOffsetX + iwWidth + padX) * degPixelX;
  var iwNorthLat = position.lat() - (iwOffsetY - padY) * degPixelY;
  var iwSouthLat = position.lat() - (iwOffsetY + iwHeight + padY) * degPixelY;

  // calculate center shift
  var shiftLng =
      (iwWestLng < mapWestLng ? mapWestLng - iwWestLng : 0) +
      (iwEastLng > mapEastLng ? mapEastLng - iwEastLng : 0);
  var shiftLat =
      (iwNorthLat > mapNorthLat ? mapNorthLat - iwNorthLat : 0) +
      (iwSouthLat < mapSouthLat ? mapSouthLat - iwSouthLat : 0);

  // The center of the map
  var center = map.getCenter();

  // The new map center
  var centerX = center.lng() - shiftLng;
  var centerY = center.lat() - shiftLat;

  // center the map to the new shifted center
  map.setCenter(new google.maps.LatLng(centerY, centerX));

  // Remove the listener after panning is complete.
  google.maps.event.removeListener(this.boundsChangedListener_);
  this.boundsChangedListener_ = null;
};
//////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////
InfoBox.prototype = new google.maps.OverlayView();

/* Creates the DIV representing this InfoBox
 */
InfoBox.prototype.remove = function() {
  if (this.div_) {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  }
};

/* Redraw the Bar based on the current projection and zoom level
 */
InfoBox.prototype.draw = function() {
  // Creates the element if it doesn't exist already.
  this.createElement();
  if (!this.div_) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our Bar
  var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
  if (!pixPosition) return;

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
  this.div_.style.height = this.height_ + "px";
  this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
  this.div_.style.display = 'block';
};

/* Creates the DIV representing this InfoBox in the floatPane.  If the panes
 * object, retrieved by calling getPanes, is null, remove the element from the
 * DOM.  If the div exists, but its parent is not the floatPane, move the div
 * to the new pane.
 * Called from within draw.  Alternatively, this can be called specifically on
 * a panes_changed event.
 */
InfoBox.prototype.createElement = function() {
  var panes = this.getPanes();
  var div = this.div_;
  if (!div) {
    // This does not handle changing panes.  You can set the map to be null and
    // then reset the map to move the div.

    div = this.div_ = document.createElement("div");
    div.style.border = "0px none";
    div.style.position = "absolute";


	div.style.background = "#000"
	div.style.color="#FFF"
    div.style.width = this.width_ + "px";
    div.style.height = this.height_ + "px";
    div.style.padding = "1px"
    var contentDiv = document.createElement("div");
    contentDiv.innerHTML = contentString;

    var topDiv = document.createElement("div");
    topDiv.style.textAlign = "right";
    var closeImg = document.createElement("img");
    closeImg.style.width = "32px";

    closeImg.style.height = "32px";
    closeImg.style.cursor = "pointer";
    closeImg.src = "http://gmaps-samples.googlecode.com/svn/trunk/images/closebigger.gif";
    topDiv.appendChild(closeImg);

    function removeInfoBox(ib) {
      return function() {
        ib.setMap(null);
      };
    }

    google.maps.event.addDomListener(closeImg, 'click', removeInfoBox(this));





    div.appendChild(topDiv);
    div.appendChild(contentDiv);
    div.style.display = 'none';
    panes.floatPane.appendChild(div);
    this.panMap();
  } else if (div.parentNode != panes.floatPane) {
    // The panes have changed.  Move the div.
    div.parentNode.removeChild(div);
    panes.floatPane.appendChild(div);
  } else {
    // The panes have not changed, so no need to create or move the div.
  }
}

/* Pan the map to fit the InfoBox.
 */
InfoBox.prototype.panMap = function() {
  // if we go beyond map, pan map
  var map = this.map_;
  var bounds = map.getBounds();
  if (!bounds) return;

  // The position of the infowindow
  var position = this.latlng_;

  // The dimension of the infowindow
  var iwWidth = this.width_;
  var iwHeight = this.height_;

  // The offset position of the infowindow
  var iwOffsetX = this.offsetHorizontal_;
  var iwOffsetY = this.offsetVertical_;

  // Padding on the infowindow
  var padX = 40;
  var padY = 40;

  // The degrees per pixel
  var mapDiv = map.getDiv();
  var mapWidth = mapDiv.offsetWidth;
  var mapHeight = mapDiv.offsetHeight;
  var boundsSpan = bounds.toSpan();
  var longSpan = boundsSpan.lng();
  var latSpan = boundsSpan.lat();
  var degPixelX = longSpan / mapWidth;
  var degPixelY = latSpan / mapHeight;

  // The bounds of the map
  var mapWestLng = bounds.getSouthWest().lng();
  var mapEastLng = bounds.getNorthEast().lng();
  var mapNorthLat = bounds.getNorthEast().lat();
  var mapSouthLat = bounds.getSouthWest().lat();

  // The bounds of the infowindow
  var iwWestLng = position.lng() + (iwOffsetX - padX) * degPixelX;
  var iwEastLng = position.lng() + (iwOffsetX + iwWidth + padX) * degPixelX;
  var iwNorthLat = position.lat() - (iwOffsetY - padY) * degPixelY;
  var iwSouthLat = position.lat() - (iwOffsetY + iwHeight + padY) * degPixelY;

  // calculate center shift
  var shiftLng =
      (iwWestLng < mapWestLng ? mapWestLng - iwWestLng : 0) +
      (iwEastLng > mapEastLng ? mapEastLng - iwEastLng : 0);
  var shiftLat =
      (iwNorthLat > mapNorthLat ? mapNorthLat - iwNorthLat : 0) +
      (iwSouthLat < mapSouthLat ? mapSouthLat - iwSouthLat : 0);

  // The center of the map
  var center = map.getCenter();

  // The new map center
  var centerX = center.lng() - shiftLng;
  var centerY = center.lat() - shiftLat;

  // center the map to the new shifted center
  map.setCenter(new google.maps.LatLng(centerY, centerX));

  // Remove the listener after panning is complete.
  google.maps.event.removeListener(this.boundsChangedListener_);
  this.boundsChangedListener_ = null;
};

//////////////////////////////////
//////////////////////////////////
///////////////////////////////////
////////////////////////////////////



















/////////////////////////////////
/////////////////////////////////
/////////////////////////////////
////////////////////////////////
function getPoints(reference, recommended) {
	deletePoints();
	
	if (reference || recommended)
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.open("GET","http://www.minnus.com.ar/xml.php" ,false);
		xmlhttp.send();
		xml=xmlhttp.responseXML;
		
		for (i = 0; i < xml.getElementsByTagName("lat").length ; i++)
		{
			var latlng = new google.maps.LatLng(xml.getElementsByTagName("lat")[i].childNodes[0].nodeValue, xml.getElementsByTagName("lng")[i].childNodes[0].nodeValue);
			var title = xml.getElementsByTagName("des")[i].childNodes[0].nodeValue;
			var cat = xml.getElementsByTagName("cat")[i].childNodes[0].nodeValue;
			var des = xml.getElementsByTagName("des")[i].childNodes[0].nodeValue;
			var id = xml.getElementsByTagName("id")[i].childNodes[0].nodeValue;

			var img = "http://usuarios.minnus.com.ar/image/icono.php?categoria=" + cat ;
			//img = "http://usuarios.minnus.com.ar/image/icono.php?categoria=" + cat;
			//img = "http://www.minnus.com.ar/logos/m_texto.png";

			placeMarker(markersPlaces, latlng, title, img,id);
		}
	}
}

function getSearch(lat, lng, cat, altura,nombre)
{
	 document.getElementById("puntosVisualizados").value=document.getElementById("puntosVisualizados").value+","+cat
	if (altura=="")
	{
		altura=9;
	}

	deleteSearch();
	var latlng = new google.maps.LatLng(lat, lng);
	var img = "http://usuarios.minnus.com.ar/image/icono.php?categoria=" + cat;
	placeMarker(markersSearch, latlng, nombre, img,cat);
	map.setCenter(latlng);
	map.setZoom(17);

	document.getElementById("urlWeb").value="http://www.minnus.com.ar/index_7.php?long="+lng+"&lat="+lat+"&altura="+ map.getZoom() +"&mostrar=" + document.getElementById("puntosVisualizados").value
}

function getTraffic() {
	deleteTraffic();
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.open("GET","http://links.minnus.com.ar/protejeLinkKML.php?aleato=mod_3d_M" ,false);
	xmlhttp.send();
	xml=xmlhttp.responseXML;
	
	for (i = 0; i < xml.getElementsByTagName("lat").length ; i++)
	{
		var latlng = new google.maps.LatLng(xml.getElementsByTagName("coordinates")[i].childNodes[0].nodeValue.split(",")[0], xml.getElementsByTagName("coordinates")[i].childNodes[0].nodeValue.split(",")[1]);
		var title = xml.getElementsByTagName("description")[i].childNodes[0].nodeValue;
		placeMarker(markersTraffic, latlng, title, "http://www.minnus.com.ar/imagenes/btn_ver_trafico_01.png");
	}
}


///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// BORRAR PUNTOS
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function deletePoints() {
	if (markersPlaces) {
		for (i in markersPlaces) {
			markersPlaces[i].setMap(null);
		}
		markersPlaces.length = 0;
	}
}
function deleteCercanos() {
	if (markersCercanos) {
		for (i in markersCercanos) {
			markersCercanos[i].setMap(null);
		}
		markersCercanos.length = 0;
	}
}

function deleteCategorias(categoria)
{
	if (categoria) {
		for (i in categoria) {
			categoria[i].setMap(null);
		}
		categoria.length = 0;
	}
}


function deleteRecomendados() {
	if (markersRecomendados) {
		for (i in markersRecomendados) {
			markersRecomendados[i].setMap(null);
		}
		markersRecomendados.length = 0;
	}
}
function deleteReferencias() {
	if (markersReferencias) {
		for (i in markersReferencias) {
			markersReferencias[i].setMap(null);
		}
		markersReferencias.length = 0;
	}
}
function deleteTraffic() {
	if (markersTraffic) {
		for (i in markersTraffic) {
			markersTraffic[i].setMap(null);
		}
		markersTraffic.length = 0;
	}
}

function deleteSearch() {
	if (markersSearch) {
		for (i in markersSearch) {
			markersSearch[i].setMap(null);
		}
		markersSearch.length = 0;
	}
}
