
/*
* Copyright (c) 2005-2007 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.
*/


//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, icon, oOptions, id) {
  if (usePDMarker)
  {
  	var marker = new PdMarker(point, icon, oOptions.label);
  	/* Doesn't work in APIs > 2.64
  	 * marker.setTooltip(oOptions.label);
  	 * marker.setOpacity((oOptions.opasity ? oOptions.opasity : 100));
  	 */
  }
  else
  {
  	var marker = new GMarker(point, icon);
  }

  GEvent.addListener(marker, "click", function() {
	 
	marker.openInfoWindowTabsHtml(
		[
			new GInfoWindowTab("Info", html),
			new GInfoWindowTab("Directions", GetDirectionForm(id))
		]);
	 
  });

  return marker;
}

// Zoom map to. Used from InfoWindow
function ZoomMapTo(num) {
  // Get current zoom level
  var currentZoom = map.getZoom();
  // Re-center map
  newzoom = (currentZoom > 17) ? 17 : ((currentZoom == 0) ? 0 : (currentZoom + 1));
  //V2-V1 compatability 
  map.setCenter(aLocations[num][3], newzoom);
}

// Function is called when sidebar item is clicked or we are dealing with group boxes.
function myInfoWindowHtml(num)
{
  // Map rendering with open info window is very slow. Lets center map first.
  map.setCenter(aLocations[num][3]);
  // Use markeropenInfoWindowHtml(html)

   
  aLocations[num][0].openInfoWindowTabsHtml(
  	[
  		new GInfoWindowTab("Info", aLocations[num][2]),
  		new GInfoWindowTab("Directions", GetDirectionForm(num))
  	]);
   
}

/* Side Bar */

// Creates a locations list and put it into side bar
function createSideBar() {
  var oUL = document.createElement("ul");
  // 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 + ");";
    listElem.appendChild(linkElem);
    oUL.appendChild(listElem);
  }
  try 
  {
  	document.getElementById("LocationList").appendChild(oUL);
  }
  catch(e)
  {
  	// do nothing
  }

}


// Function is called to show/hide location list 
var LocationListVisible = true;
function ProcessLocationList()
{
  if (LocationListVisible)
  {
    // Hide sidebar
    document.getElementById("LocationList").style.display="none";
    document.getElementById("SideBarActionLink").innerHTML = "+";
  }
  else
  {
    // Show sidebar
    document.getElementById("LocationList").style.display="block";
    document.getElementById("SideBarActionLink").innerHTML = "-";
  }
  LocationListVisible = !LocationListVisible;
}

/* EOF Side Bar */

/* Directions */
var MBDirectionFormBox = false; 

function  MBDirectionFormState(mode, force) {

  var MBCurrID = document.getElementById("MBLocID").value;
  document.getElementById("addr1").value = aLocations[MBCurrID][0].getPoint().lat() + ", " + aLocations[MBCurrID][0].getPoint().lng() + " ("+ aLocations[MBCurrID][1] +")";
  if (mode=='to') {
    document.getElementById("MBDirectionAddr").innerHTML = 'Start address';
    document.getElementById("addr1").name = 'daddr';
    document.getElementById("addr2").name = 'saddr';
  }
  else {
    document.getElementById("MBDirectionAddr").innerHTML = 'End address';
    document.getElementById("addr1").name = 'saddr';
    document.getElementById("addr2").name = 'daddr';
  }

  if (force) {
    document.getElementById('MBDirectionForm').style.display = (MBDirectionFormBox ? 'none' : 'block');
  }
  else {
    document.getElementById('MBDirectionForm').style.display = 'block';
  }
  MBDirectionFormBox = (!MBDirectionFormBox);
}

// Print Directions Form
function GetDirectionForm(LocID) {
  return "<div class=\"IWDirections\">[<a onclick=\"MBDirectionFormState('to', true);\" href=\"javascript:void(0)\">+</a>] Directions:&nbsp;<a onclick=\"MBDirectionFormState('to', false);\" href=\"javascript:void(0)\">To here</a>&nbsp;-&nbsp;<a onclick=\"MBDirectionFormState('from', false);\" href=\"javascript:void(0)\">From here</a><div id=\"MBDirectionForm\" style=\"display:none\"><form action=\"http://maps.google.com/maps\" target=\"_blank\" method=\"get\"><input id=\"addr1\" name=\"daddr\" value=\"0,0(NAME)\" type=\"hidden\"><input id=\"MBLocID\" name=\"MBLocID\" value=\""+LocID+"\" type=\"hidden\"><div id=\"MBDirectionAddr\" style=\"padding-top: 0.2em; width: 290px;\">Start address</div><div style=\"width: 290px;\"><input value=\"\" id=\"addr2\" style=\"width: 16em;\" name=\"saddr\" onload=\"this.select()\" type=\"text\"></div><div style=\"width: 290px;\"><input value=\"Get Directions\" type=\"submit\"></div></form></div></div>"
}
GetGoogleMapsAPIVersion = function() {
    var v = 0;
    var scripts = document.getElementsByTagName("SCRIPT")
    for (var i=0; i<scripts.length; i++) {
        var pattern = /\/maps([0-9])?(\.?[0-9]+)(\.api)?\.js/;
        var m = pattern.exec(scripts[i].src);
        if (m != null) {
            if (m[1] == null) { v = parseFloat('1'+m[2]); }
            else { v = parseFloat(m[1]+m[2]); }
            break;
        }
    }
    return v;
}

function MapBuilder(oMap){ 
    /* Insert MapBuilder Logo */
    var info=document.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" title="Powered by MapBuilder.net v2.03, GMap v'+GetGoogleMapsAPIVersion()+'"><img src="assets/mo24.gif" alt="Powered By MapBuilder.net v2.02, GMap v'+GetGoogleMapsAPIVersion()+'" style="border:0; margin: 2px;"/></a>';
    oMap.getContainer().appendChild(info);
}