/*
* Corregido para la API 2 de Google Maps.

* Copyright (c) 2005 Andriy Bidochko.  

* http://www.mapbuilder.net

*

* The script has been generated by MapBuilder.net service and released to the customer under 

* The GNU General Public License (GPL), which can be found at: http://www.opensource.org/licenses/gpl-license.php

* 

* This program is free software; you can redistribute it and/or modify it under the terms of the 

* GNU General Public License as published by the Free Software Foundation; 

* either version 2 of the License, or any later version.

* 

* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 

* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 

* for more details.

*/

MapBuilder.prototype.addMBInfo=function(oMap){

  var info=oMap.ownerDocument.createElement('div');

  info.id='MapBuilderInfo';

  info.style.position='absolute';

  info.style.right='1px';

  info.style.bottom='25px';

  info.style.backgroundColor='transparent';

  info.style.zIndex=25500;

  info.innerHTML='<a id="CreatedByMapBuilder" href="http://www.mapbuilder.net" style="font: 10px verdana; text-decoration: none; padding: 2px; color: #FFFFFF;">FREE Map Code by MapBuilder.net</a>';

  oMap.div.parentNode.appendChild(info);

}



GMap.prototype.addMapBuilder=function(oMap){

  oMap.initialize(this);

}



MapBuilder.prototype.registerMBEvent=function(oMap){

  GEvent.addListener(oMap, "maptypechanged", function() {

  	setCreatedColor(oMap);

  });

}



GMap.prototype.addMapBuilder=function(oMap){

  oMap.initialize(this);

}



//Set "Created By Mapbuilder" color based on map type

function setCreatedColor(oMap) {

    var info=oMap.ownerDocument.getElementById('CreatedByMapBuilder');

  	currmaptype = getCurrentMapTypeNumber(oMap);

	// Regular map

  	if (currmaptype == 0)

      info.style.color='#000000';

    else

      info.style.color='#FFFFFF';

}



//That function will return the element of the array returned by map.getMapTypes() that identifies the current map

function getCurrentMapTypeNumber(oMap){

  var type=-1;

  for(var ix=0;ix<oMap.getMapTypes().length;ix++){

    if(oMap.getMapTypes()[ix]==oMap.getCurrentMapType())

      type=ix;

  }

  return type;

} 

// Creates a marker whose info window displays the given number
function createMarker(point, html, options)
{
  var marker = new GMarker(point, options);

  GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(html);
    //marker.openInfoWindowTabsHtml(html);

  });

  return marker;

}

// Creates a marker whose info window displays the given number
function createMarkerOld(point, html, icon)
{

  var marker = new GMarker(point, icon);

  GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(html);

  });

  return marker;

}

// Zoom map to. Used from InfoWindow
function ZoomMapTo(num) 
{
  // Get current zoom level
  // Google Maps API 1	
  //currentZoom = map.getZoomLevel();
  // Google Maps API 2
  currentZoom = map.getZoom();  

  // Regular el 18 cuando Google permita zoom 20 en las islas Canarias.	
  if (currentZoom == 19)
  {
    alert('Zoom máximo alcanzado.');
    return;
  }

  // Re-center map
  // Google Maps API 1	
  //newzoom = (currentZoom > 7) ? 7 : ((currentZoom == 0) ? 0 : (currentZoom - 1));
  // Google Maps API 2
  newzoom = (currentZoom >= 19) ? 19 : (currentZoom + 1);

  // API 1 Google Maps
  //map.centerAndZoom(aLocations[num][3], newzoom);
  // API 2 Google Maps
  map.setCenter(aLocations[num][3], newzoom);
}

function getInfoWindowContent(num)
{
    urlreq = "./get_map_info_window.php?"
    urlreq = urlreq + "codigo_ficha=" + aLocations[num][4];
    urlreq = urlreq + "&numero=" + num;

    // DEBUG
    //alert(urlreq);
    
    // Se llama al script que devuelve los datos de la ficha
    var request = GXmlHttp.create();
    request.open("GET", urlreq, true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {

      	response = request.responseText;
      	
      	// DEBUG
      	//alert('response: '+ response);
     	//alert('num: '+ num);
      	
  		// Muestra la ventana de información de la ficha
  		aLocations[num][0].openInfoWindowHtml(response);
  
      }
    }
    request.send(null);
}

// Function is called when sidebar item is clicked or we are dealing with group boxes.
function myAJAXInfoWindowHtml(num)
{
  //alert('myAJAXInfoWindowHtml'+num);
  
  getInfoWindowContent(num);

  // DEPRECATED: ahora se hace mediante AJAX
  //aLocations[num][0].openInfoWindowHtml(getInfoWindowContent(num));  
}

// Function is called when sidebar item is clicked or we are dealing with group boxes.
function myInfoWindowHtml(num)
{
  //alert('myInfoWindowHtml'+num);
  
  // Map rendering with open info window is very slow. Lets center map first.

  // API 1 Google Maps
  //map.centerAtLatLng(aLocations[num][3]);
  // API 2 Google Maps
  //map.setCenter(aLocations[num][0].getLatLng());

  aLocations[num][0].openInfoWindowHtml(aLocations[num][2]);  
  
  // Use markeropenInfoWindowHtml(html)

  // Move focus to the map

  // Deprecated 

  //window.scrollTo(10, 10);
}


// Creates a locations list and put it into side bar
function createSideBar() 
{

  // write links into document.

  for (var i=0; i<aLocations.length; i++) {

    var linkElem = document.createElement("a");

    var listElem = document.createElement("li");

    linkElem.innerHTML = aLocations[i][1];



    // must use this approach instead.

    linkElem.href = "javascript:myInfoWindowHtml(" + i + ");";

    var listNode = document.getElementById("LocationList").appendChild(listElem);

    listNode.appendChild(linkElem);

  }

}





// Function is called to hide location list 

function HideLocationList()

{

  document.getElementById("LocationList").innerHTML = '';

  document.getElementById("SideBarAction").innerHTML = 'Map Locations [<a href="#" onclick="ShowLocationList()">+</a>]';

}



// Function is called to show location list 

function ShowLocationList()

{

  document.getElementById("LocationList").innerHTML = listNodeContent;

  document.getElementById("SideBarAction").innerHTML = 'Map Locations [<a href="#" onclick="HideLocationList()">-</a>]';

}



    



function MapBuilder(){}

MapBuilder.prototype.initialize=function(oMap){

  // Show info

  this.addMBInfo(oMap);

  // Event on maptype cahnge

  this.registerMBEvent(oMap);

  // Analize current maptype

  setCreatedColor(oMap);

}