// Google Maps

//<![CDATA[

var map = null;
var marker = null;
var markers = [];
var infowindow;
var thresmap = [];

function showAddressByCoordinatesV3(mapNum, tname, address, lat, lon) { 
	  mapString = "map" + mapNum;
	  if(document.getElementById(mapString).style.width == "580px"){
			document.getElementById(mapString).style.width="0px";
			document.getElementById(mapString).style.height="0px";
			document.getElementById(mapString).style.visibility="hidden";
	  }else {
		document.getElementById(mapString).style.visibility="visible";
		document.getElementById(mapString).style.width="580px";
		document.getElementById(mapString).style.height="300px";
		document.getElementById(mapString).style.border="solid 1px #000000";
		document.getElementById(mapString).style.margin = "12px 0 12px 0";
		var location = new google.maps.LatLng(lat, lon);
		var myOptions = {
		    zoom: 15,
		    mapTypeId: google.maps.MapTypeId.ROADMAP,
		    center: location
		}
		map = new google.maps.Map(document.getElementById(mapString), myOptions);
		var bounds = new google.maps.LatLngBounds();
		marker = new google.maps.Marker(
              {
                 position: location,
                 map: map,
                 title: tname
             }); 
		//markers[i].setTitle(message[i].replace(/(<([^>]+)>)/ig, ''));
        attachSecretMessage2(marker, "<span class='error'>" + address + "</span>");
		bounds.extend(location);
		//map.fitBounds(bounds);
	  }
}

function showAddressV3(map_canvas, thname, address, lat, lon) {
    var arrName = thname.split("///");
    var arraddr = address.split("///");
    var arrlat = lat.split("///");
    var arrlon = lon.split("///");
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById( map_canvas ), myOptions);   
    if(typeof arrName != "undefined")
    {                
        var bounds = new google.maps.LatLngBounds();              
        for (var i = 0; i < arrName.length; i++) {
            var location = new google.maps.LatLng(arrlat[i], arrlon[i]);
            markers[i]  = new google.maps.Marker(
                        { 
                         icon: "http://www.tribute.ca/images/markers/marker" + (i+1) + ".png",
                         position: location,
                         map: map,
                         title:arrName[i]
                        });
            var j = i + 1;
            //markers[i].setTitle(message[i].replace(/(<([^>]+)>)/ig, '')); 
            attachSecretMessage(markers[i], "<span class='error'>" + arraddr[i] + "</span>");
            bounds.extend(location);
        }
        setMinZoom(map, 15);         
        map.fitBounds(bounds); 
    }
}
//multiply maps with different mapnumber, map1, map2...
function showAddressByCoordinatesV3ByJson(mapNum, jsonmap) { 
	  mapString = "map" + mapNum;
	  if(document.getElementById(mapString).style.width == "580px"){
			document.getElementById(mapString).style.width="0px";
			document.getElementById(mapString).style.height="0px";
			document.getElementById(mapString).style.visibility="hidden";
	  }else {
		document.getElementById(mapString).style.visibility="visible";
		document.getElementById(mapString).style.width="580px";
		document.getElementById(mapString).style.height="300px";
		document.getElementById(mapString).style.border="solid 1px #000000";
		document.getElementById(mapString).style.margin="12px 0 12px 0";
		var location = new google.maps.LatLng(jsonmap[0].lat, jsonmap[0].lon);
		var myOptions = {
		    zoom: 15,
		    mapTypeId: google.maps.MapTypeId.ROADMAP,
		    center: location
		}
		map = new google.maps.Map(document.getElementById(mapString), myOptions);
		var bounds = new google.maps.LatLngBounds();
		marker = new google.maps.Marker(
              {
                 position: location,
                 map: map,
                 title: jsonmap[0].tname
             }); 
		//markers[i].setTitle(message[i].replace(/(<([^>]+)>)/ig, ''));
        attachSecretMessage2(marker, "<span class='error'>" + jsonmap[0].message + "</span>");
		bounds.extend(location);
		//map.fitBounds(bounds);
	  }		
}

// show a group of markers in one map
function showAddressV3ByJson(map_canvas, jsonmap) {
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById(map_canvas), myOptions);
    if (typeof jsonmap != "undefined") {
        var bounds = new google.maps.LatLngBounds();
        var i = 0;
        for (var item in jsonmap) {
            var location = new google.maps.LatLng(jsonmap[i].lat, jsonmap[i].lon);
            markers[i] = new google.maps.Marker(
                        {
                            icon: "http://www.tribute.ca/images/markers/marker" + (i + 1) + ".png",
                            position: location,
                            map: map,
                            title: jsonmap[i].title
                        });
            //markers[i].setTitle(message[i].replace(/(<([^>]+)>)/ig, ''));
                        attachSecretMessage(markers[i], "<div style='text-align:left;width: 240px; height:90px;'>" + jsonmap[i].message + "</div>"); //"<span class='error'>" + jsonmap[i].message + "</span>");
            bounds.extend(location);
            i++;
        }
        setMinZoom(map,15);
        map.fitBounds(bounds);
    }
}
// show one restaurant and one theatre
function showMapOfTheatreAndRestaurant(mapNum, jsonmap) {
    mapString = "map" + mapNum;
    if (document.getElementById(mapString).style.width == "580px") {
        document.getElementById(mapString).style.width = "0px";
        document.getElementById(mapString).style.height = "0px";
        document.getElementById(mapString).style.visibility = "hidden";
    } else {
        document.getElementById(mapString).style.visibility = "visible";
        document.getElementById(mapString).style.width = "580px";
        document.getElementById(mapString).style.height = "300px";
        document.getElementById(mapString).style.border = "solid 1px #000000";
        document.getElementById(mapString).style.margin = "12px 0 12px 0";
        var myOptions = {
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        thresmap[mapNum] = new google.maps.Map(document.getElementById(mapString), myOptions);
        var markerthres = [];
        var bounds = new google.maps.LatLngBounds();
        var location0 = new google.maps.LatLng(jsonmap[0].lat, jsonmap[0].lon);
        markerthres[0] = new google.maps.Marker(
              {
                  icon: "http://www.google.com/mapfiles/markerR.png",
                  position: location0,
                  map: thresmap[mapNum],
                  title: jsonmap[0].tname
              });
       attachSecretMessage3(markerthres[0], "<span >" + jsonmap[0].message + "</span>", thresmap[mapNum]);
       bounds.extend(location0);
       var location1 = new google.maps.LatLng(jsonmap[1].lat, jsonmap[1].lon);
       markerthres[1] = new google.maps.Marker(
       {
          icon: "http://www.google.com/mapfiles/ms/icons/green-dot.png",
          position: location1,
          map: thresmap[mapNum],
          title: jsonmap[1].tname
       });
       attachSecretMessage3(markerthres[1], "<div style='text-align:left;width: 200px; height:100px;'>" + jsonmap[1].message + "</div>", thresmap[mapNum]);
       bounds.extend(location1);
       setMinZoom(thresmap[mapNum], 15);
       thresmap[mapNum].fitBounds(bounds);       
    }		
}

function showAddressByCoordinates(address, lat, lon) {
    
  point = new GLatLng(lat,lon);
  map.setCenter(point, 13);
  var marker = new GMarker(point);
  map.addOverlay(marker);
  marker.openInfoWindowHtml("<span class='error'>" + address + "</span>");

}
// one fixed position map listener
function attachSecretMessage(marker, message) { 
    google.maps.event.addListener(marker, 'click', function() {
        if (infowindow) infowindow.close();
        infowindow = new google.maps.InfoWindow(
        {           
          content: message,
          size: new google.maps.Size(200, 50)
        });
        infowindow.open(map, marker);
        if (map.getZoom() > 15) { map.setZoom(15); }
    });
}
//for one view/hide  map with infowindows show automatically.
function attachSecretMessage2(marker, message) {
    infowindow = new google.maps.InfoWindow(
        {
            content: message,
            size: new google.maps.Size(200, 50)
        });
        infowindow.open(map, marker);
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
        if (map.getZoom() > 15) { map.setZoom(15); }
    });
}
// for multiply maps
function attachSecretMessage3(marker, message, mymap) {
    infowindow = new google.maps.InfoWindow(
        {
            content: message
        });
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(message);
            infowindow.open(mymap, marker);
        });
}

function setMinZoom(mymap,zoomNum) {
    google.maps.event.addListener(mymap, 'zoom_changed', function() {
        zoomChangeBoundsListener =
        google.maps.event.addListener(mymap, 'bounds_changed', function(event) {
        if (this.getZoom() > zoomNum && this.initialZoom == true) {
                // Change max/min zoom here
                this.setZoom(zoomNum);
                this.initialZoom = false;
            }
            google.maps.event.removeListener(zoomChangeBoundsListener);
        });
    });
    mymap.initialZoom = true;
}
//]]>


