var gHauptBereich;
var gHauptBereichWinter;
var gWinterBereich;
var gBereichA;
var gBereichB;
var gBereichC;
var gBereichD;
var gBereichE;
var geocoder;
var map;
var filialLocation;
var markersArray = [];
var polygonsArray = [];



function LiefergebietAZeichnen(bereichCoords){
	 // Construct the polygon
   gBereichA = new google.maps.Polygon({
     paths: bereichCoords,
     strokeColor: "#0000FF",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#0000FF",
     fillOpacity: 0.35
   });
   gBereichA.setMap(map);
   polygonsArray.push(gBereichA);
   return gBereichA;
}


function LiefergebietBZeichnen(bereichCoords){
	 // Construct the polygon
   gBereichB = new google.maps.Polygon({
     paths: bereichCoords,
     strokeColor: "#0000FF",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#0000FF",
     fillOpacity: 0.35
   });
   gBereichB.setMap(map);
   polygonsArray.push(gBereichB);
   return gBereichB;
}


function LiefergebietCZeichnen(bereichCoords){
	 // Construct the polygon
   gBereichC = new google.maps.Polygon({
     paths: bereichCoords,
     strokeColor: "#0000FF",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#0000FF",
     fillOpacity: 0.35
   });
   gBereichC.setMap(map);
   polygonsArray.push(gBereichC);
   return gBereichC;
}

function LiefergebietDZeichnen(bereichCoords){
	 // Construct the polygon
   gBereichD = new google.maps.Polygon({
     paths: bereichCoords,
     strokeColor: "#0000FF",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#0000FF",
     fillOpacity: 0.35
   });
   gBereichD.setMap(map);
   polygonsArray.push(gBereichD);
   return gBereichD;
}


function LiefergebietEZeichnen(bereichCoords){
	 // Construct the polygon
   gBereichE = new google.maps.Polygon({
     paths: bereichCoords,
     strokeColor: "#0000FF",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#0000FF",
     fillOpacity: 0.35
   });
   gBereichE.setMap(map);
   polygonsArray.push(gBereichE);
   return gBereichE;
}


function WinterBereichZeichnen(bereichWinterCoords){
	 // Construct the polygon
    gWinterBereich = new google.maps.Polygon({
      paths: bereichWinterCoords,
      strokeColor: "#0000FF",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: "#0000FF",
      fillOpacity: 0.35
    });
    gWinterBereich.setMap(map);
    polygonsArray.push(gWinterBereich);
    return gWinterBereich;
}
function HauptBereichZeichnen(bereichHauptCoords){
	 // Construct the polygon
   gHauptBereich = new google.maps.Polygon({
	  paths: bereichHauptCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeOpacity: 0,
      strokeWeight: 2,
      fillColor: "#FF0000",
      fillOpacity: 0.35
   });
   gHauptBereich.setMap(map);
   polygonsArray.push(gHauptBereich);
   return gHauptBereich;
}


function HauptBereichZeichnenFrontend(bereichHauptCoords, zeichnen){
	 // Construct the polygon
  gHauptBereich = new google.maps.Polygon({
	  paths: bereichHauptCoords,
     strokeColor: "#FF0000",
     strokeOpacity: 0,
     strokeOpacity: 0,
     strokeWeight: 2,
     fillColor: "#FF0000",
     fillOpacity: 0
  });
  if(zeichnen==true){
	  gHauptBereich.setMap(map);
  }
  polygonsArray.push(gHauptBereich);
  return gHauptBereich;
}



function HauptBereichWinterZeichnen(bereichHauptWinterCoords){
	 // Construct the polygon
  gHauptBereichWinter = new google.maps.Polygon({
	  paths: bereichHauptWinterCoords,
     strokeColor: "#00FF00",
     strokeOpacity: 0.8,
     strokeWeight: 2,
     fillColor: "#00FF00",
     fillOpacity: 0.35
  });
  gHauptBereichWinter.setMap(map);
  polygonsArray.push(gHauptBereichWinter);
  return gHauptBereichWinter;
}

function clearOverlays() {
	  if (markersArray) {
	    for (i in markersArray) {
	      markersArray[i].setMap(null);
	    }
	  }
	  if (polygonsArray){
		  for (i in polygonsArray) {
		      polygonsArray[i].setMap(null);
		    }
	  }
	}



google.maps.Polygon.prototype.Contains = function(latLng)
{
    var j = 0;
    var oddNodes = false;
    var x = latLng.lng();
    var y = latLng.lat();
    for (var i = 0; i < this.getPath().getLength(); i++) {
        j++;
        if (j == this.getPath().getLength()) { j = 0; }
        if (((this.getPath().getAt(i).lat() < y) &&  (this.getPath().getAt(j).lat() >= y)) || ((this.getPath().getAt(j).lat() < y) && (this.getPath().getAt(i).lat() >= y))) {
            if (this.getPath().getAt(i).lng() + (y -  this.getPath().getAt(i).lat()) / (this.getPath().getAt(j).lat() -  this.getPath().getAt(i).lat()) * (this.getPath().getAt(j).lng() - this.getPath().getAt(i).lng()) < x) {
                oddNodes = !oddNodes;
            }
        }
    }
    return oddNodes;
}

function reducePolygonArea(coords, percent, point){
	for(var i=0;i<coords.length;i++){
		var abstandHoehe = Math.abs(coords[i][0]-point.lat()) / 100 * percent;
		var abstandBreite = Math.abs(coords[i][1]-point.lng()) / 100 * percent;
		if(coords[i][0]>point.lat()){
			coords[i][0] = coords[i][0] - abstandHoehe;
		} else {
			coords[i][0] = coords[i][0] + abstandHoehe;
		}
		if(coords[i][1]>point.lng()){
			coords[i][1] = coords[i][1] - abstandBreite;
		} else {
			coords[i][1] = coords[i][1] + abstandBreite;
		}
	}
	return coords;
}




function codeAddress() {
	  geocoder = new google.maps.Geocoder();

	    //var address = document.getElementById("address").value;
	    //var address = "Kleine Theaterstr. 10, Hamburg";
	    var address = "Alsterarkaden 1, Hamburg";
	    geocoder.geocode( { 'address': address }, function(results, status) {
	      if (status == google.maps.GeocoderStatus.OK) {
	        map.setCenter(results[0].geometry.location);
	        var marker = new google.maps.Marker({
	            map: map,
	            position: results[0].geometry.location
	        });




      	var isWithinPolygon = bermudaTriangle.Contains(results[0].geometry.location);
      	if(isWithinPolygon==true){
	        	//alert("In Polygon");
      	} else {
      		//alert("Ausserhalb Polygon");
      	}
	      } else {
	        alert("Geocode was not successful for the following reason: " + status);
	      }
	    });
}




