﻿/**
 * @author Diana Rodiriguez - www.d-on.net
 * Requires jQuery and GoogleMaps script libraries
 */
 
// Create our "diabetes.dk" marker icon


var redIcon = new GIcon(G_DEFAULT_ICON);
redIcon.image = "/gfx/map-icon-red.png";
redIcon.shadow = "/gfx/map-icon-shadow.png";
redIcon.iconSize = new GSize(14, 14);

redIcon.shadowSize = new GSize(20, 20);
redIcon.iconAnchor = new GPoint(8, 50);
redIcon.infoWindowAnchor = new GPoint(30, 1);

redIcon.iconAnchor = new GPoint(7,7);
redIcon.infoWindowAnchor = new GPoint(7,7);

var yellowIcon = new GIcon();
yellowIcon.image = "/gfx/map-icon-yellow.png";
yellowIcon.shadow = "/gfx/map-icon-shadow.png";
yellowIcon.iconSize = new GSize(14, 14);

yellowIcon.shadowSize = new GSize(20, 20);
yellowIcon.iconAnchor = new GPoint(8, 50);
yellowIcon.infoWindowAnchor = new GPoint(30, 1);

yellowIcon.iconAnchor = new GPoint(7,7);
yellowIcon.infoWindowAnchor = new GPoint(7,7);


var greenIcon = new GIcon();
greenIcon.image = "/gfx/map-icon-green.png";
//greenIcon.shadow = "/gfx/map-icon-shadow.png";
greenIcon.iconSize = new GSize(14, 14);

greenIcon.shadowSize = new GSize(20, 20);
greenIcon.iconAnchor = new GPoint(8, 50);
greenIcon.infoWindowAnchor = new GPoint(30, 1);

greenIcon.iconAnchor = new GPoint(7,7);
greenIcon.infoWindowAnchor = new GPoint(7,7);


var blueIcon = new GIcon();
blueIcon.image = "/gfx/map-icon-blue.png";
blueIcon.shadow = "/gfx/map-icon-shadow.png";
blueIcon.iconSize = new GSize(14, 14);

blueIcon.shadowSize = new GSize(20, 20);
blueIcon.iconAnchor = new GPoint(8, 50);
blueIcon.infoWindowAnchor = new GPoint(30, 1);

blueIcon.iconAnchor = new GPoint(7,7);
blueIcon.infoWindowAnchor = new GPoint(7,7);

// Set up our GMarkerOptions object
markerOptions = { icon:redIcon };
var customIcons = [];
customIcons["Diabetesforeningen"] = redIcon;
customIcons["Børnefamilie/Ungegruppe"] = yellowIcon;
customIcons["Motivationsgrp."] = greenIcon;
customIcons["Lokalforening"] = blueIcon;
var markerGroups = { "Diabetesforeningen": [], "Børnefamilie/Ungegruppe": [], "Motivationsgrp.": [], "Lokalforening": []}
/*
var geocoder = new GClientGeocoder();
function placeAddressOnMap(map,address,id,category,tries) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
    //alert(address + " not found")
    //var newTry = tries + 1;
    //setTimeout(alert("map"),500);
    //setTimeout(placeAddressOnMap(map,address,id,category,2),500);
    if (tries < 15){
        //setTimeout("void(0)",1000);
        placeAddressOnMap(map,address,id,category,tries+1);
     }
     else{
        alert(address + " not found")
     }
} else {

var marker = new GMarker(point,customIcons[category]);
map.addOverlay(marker);

GEvent.addListener(marker, "click", function() {
// append contents of the bubble from html.
var eventInfo = document.getElementById(id).innerHTML;

map.openInfoWindowHtml(point, eventInfo);

});

}
}
);
}


}
var geocoder = new GClientGeocoder();
function showAddress(map, address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 6);
      //  var marker = new GMarker(point);
      //  map.addOverlay(marker);
      //  marker.openInfoWindowHtml(address);
      }
    }
  );
}
*/
function moveToAddress(map, address, depth) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
	  //alert(point+", depth:"+depth);
        map.setCenter(point, depth);
      }
    }
  );
}




function changeURL(path){
	document.forms.mainform.__VIEWSTATE.disabled = true;
	document.forms.mainform.method = "get";
	document.forms.mainform.action = path;
	
}


function submitForm(){
	var path = document.forms.mainform.calendar.options[document.forms.mainform.calendar.selectedIndex].value;
	document.forms.mainform.__VIEWSTATE.disabled = true;
	document.forms.mainform.method = "get";
	document.forms.mainform.action = path;
	document.forms.mainform.submit();
	return false;
    
}



// If the map position is out of range, move it back
function checkBounds() {
	// Perform the check and return if OK
	var currentBounds = map.getBounds();
	var cSpan = currentBounds.toSpan(); // width and height of the bounds
	var offsetX = cSpan.lng() / (2+aberration); // we need a little border
	var offsetY = cSpan.lat() / (2+aberration);
	var C = map.getCenter(); // current center coords
	var X = C.lng();
	var Y = C.lat();

	// now check if the current rectangle in the allowed area
	var checkSW = new GLatLng(C.lat()-offsetY,C.lng()-offsetX);
	var checkNE = new GLatLng(C.lat()+offsetY,C.lng()+offsetX);
	
	if (allowedBounds.containsLatLng(checkSW) &&
		allowedBounds.containsLatLng(checkNE)) {
		return; // nothing to do
	}

	var AmaxX = allowedBounds.getNorthEast().lng();
	var AmaxY = allowedBounds.getNorthEast().lat();
	var AminX = allowedBounds.getSouthWest().lng();
	var AminY = allowedBounds.getSouthWest().lat();

	if (X < (AminX+offsetX)) {X = AminX + offsetX;}
	if (X > (AmaxX-offsetX)) {X = AmaxX - offsetX;}
	if (Y < (AminY+offsetY)) {Y = AminY + offsetY;}
	if (Y > (AmaxY-offsetY)) {Y = AmaxY - offsetY;}

	map.setCenter(new GLatLng(Y,X));
	return;
}
