﻿/*==========================================
  Function creating maps with Google Maps API
  How to install:
  1. generate a key for your site (www.maps.google.com)
  2. add to your in index file in haed:
  
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=     YOUR_KEY       " type="text/javascript"></script>
    <script type="text/javascript" src="PATH_TO_THIS_SCRIPT/google_map.js"></script>
    
  3. add to your in index file in body:
  
    <script type="text/javascript">
    window.onload = function() {
      load_google_map(); 
    }
    window.onunload = function() { 
      GUnload(); 
    }  
    </script>
    
  4. add container in html file:
  
    <div id="CONTAINER_ID"></div> (default id is "google_map")
  
  5. (OPTIONAL) Configure below configuration
==========================================*/

var map;

function load_google_map(id,XStart,YStart,NamePositionStart)
//load map with parameters
{
  /*============== CONFIGURATION ==============*/

  /*---->VALUE ON START<----*/
  //http://maps.google.com/?q=Tczew&sll=37.0625,-95.677068&sspn=23.875,57.630033&ie=UTF8&ll=54.094033,18.757782&spn=0.016938,0.037594&z=15
  var startCenterX = 54.094033;
  var startCenterY = 18.757782;  
  var zoomStart = 15;
  
  /*---->VIEW<----*/
  var height = "300";         //height of map (default=500)
  var width = "300";          //width of map (default=300)
  var type = G_NORMAL_MAP;    //type od map: G_NORMAL_MAP(default), G_SATELLITE_MAP, G_HYBRID_MAP
  
  /*---->DRAGGING<----*/
  var disableDragging = 0;    //Enable(0)(default) or disable(1) dragging map
  
  /*---->GOOGLE BAR<----*/
  var disableGoogleBar = 1;   //Enable(0)(default) or disable(1) Google Bar
  
  /*---->GOOGLE CONTROL<----*/
  var typeOfControl = 1;
  var showScale = 0;
  var showMiniMap = 0;
  var enableShowConrolOnMouseOver = 1;
  
  /*---->ZOOM<----*/
  var enableScrollWheelZoom = 1;  //Enable(1) or disable(0)(default) Scroll Wheel Zoom
  
  /*---->MARKERS<----*/
  //arguments: coordinate x, coordinate y, path to image, message
  var markers = new Array(
    new Array(startCenterX, startCenterY, "/images/template/marker.png", "<div style='color: #000000;'><br/>Dom wydawniczy<br/>ul. Armii Krajowej 86<br/>Tczew</div>")
    );
  
  /*========== END OF CONFIGURATION ===========*/

  if (GBrowserIsCompatible()) {
    
    //check configuration value
    if(!id)
      id = "google_map"; //default id of container
    if(!height)
      height = "300";
    if(!width)
      width = "300";
    if(!type)
      type = G_NORMAL_MAP;
    if(!startCenterX)
      startCenterX = 52.235674;
    if(!startCenterY)
       startCenterY = 21.053744;
    if(!zoomStart)
       startCenterY = 15;
      
    //create map with parameters
    map = new GMap2(document.getElementById(id),{ 
      size:new GSize(width,height)
      });
    
    //center of map
    map.setCenter(new GLatLng(startCenterX+0.002, startCenterY), zoomStart);
    
    //type of map
    map.setMapType(type);
    
    //disable dragging map
    if(disableDragging)
      map.disableDragging();
    
    //disable Google Bar
    if(disableGoogleBar)
      map.disableGoogleBar();
    else
      map.enableGoogleBar();
      
    if(enableScrollWheelZoom)
      map.enableScrollWheelZoom();
    
    //add contorol and type
    if(typeOfControl)
    {
      var mapControl = new GMapTypeControl();
      map.addControl(mapControl);
      
      switch(typeOfControl)
      {
        case 1:
          map.addControl(new GSmallMapControl());
          break;
        case 2:
          map.addControl(new GLargeMapControl());
          break;  
        case 3:
          map.addControl(new GSmallZoomControl());
          break;
      }
    }
    
    if(showScale)
      map.addControl(new GScaleControl());
      
    if(showMiniMap)
      map.addControl(new GOverviewMapControl());
    
    //add marker as Overlay
    for(var i=0; i<markers.length;i++)
      map.addOverlay(createMarker(new GLatLng(markers[i][0], markers[i][1]),markers[i][2],markers[i][3]));
    
    if(enableShowConrolOnMouseOver)
    {
      GEvent.addListener(map,'mouseout',function()   
      {   
        map.hideControls();   
      });   
      GEvent.addListener(map,'mouseover',function()   
      {   
        map.showControls();   
      }); 
      
      GEvent.trigger(map,'mouseout');
    }   
  }
}

function createMarker(point, url, msg) 
{
  var baseIcon = new GIcon();
  baseIcon.iconSize = new GSize(20, 34);
  baseIcon.iconAnchor = new GPoint(32, 40);
  baseIcon.infoWindowAnchor = new GPoint(32, 2);
  
  var Icon = new GIcon(baseIcon);
  Icon.image = url;
  Icon.printImage = url;
  Icon.mozPrintImage = url;
  
  // Set up our GMarkerOptions object
  markerOptions = { icon:Icon };
  var marker = new GMarker(point, markerOptions);
  
  if(msg)
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(msg);
    });
  return marker;
}
