var mcOptions = {gridSize: 100, maxZoom: 12};
var geocoder;
var markersArray = new Array();
var clusterMarkersArray = new Array();
var markersDistinct = new Array();
var markerCluster = null;
var counter = null;
var counter2 = null;
var map = null;
var anzahl = [];
var k = 0;
var scriptLocation = modulePath + '/xhr/search/';
var scriptLocationNum = modulePath + '/xhr/num/';
var reportLocation = modulePath + '/xhr/report/';
var commentLocation = modulePath + '/xhr/comment/';
var imageLocation = '/static/templates/scdbgeneral/images/cameramap/icons';
var mapLinkResult = ''; 
var reportEvent = null;
var reportMarker = null;
var interval = '';
	
function loadCameras() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map'));
        map.setMapType(G_NORMAL_MAP);

        //map.setMapType(G_PHYSICAL_MAP);
        
        var useDefaultLocation = true;
        
        
    	if (startCenterLat) {
    		useDefaultLocation = false;
        	//var zoomFactor = 10;
        	if (startZoomFactor) {
        		zoomFactor = startZoomFactor;
        	}
        	map.setCenter(new GLatLng(startCenterLat, startCenterLng), zoomFactor);            
            document.getElementById('mapConfigLng').value = startCenterLng;            
            document.getElementById('mapConfigLat').value = startCenterLat;
            document.getElementById('mapConfigZoom').value = zoomFactor;
        }
    	
    	if (!startCenterLat && ipLocationLat) {
    		useDefaultLocation = false;
        	map.setCenter(new GLatLng(ipLocationLat, ipLocationLng), 10);            
            document.getElementById('mapConfigLng').value = ipLocationLat;            
            document.getElementById('mapConfigLat').value = ipLocationLng;
            document.getElementById('mapConfigZoom').value = 10;
    		
    	}

    	if (useDefaultLocation){       
        	map.setCenter(new GLatLng(52.106505,10.546875), 4);  
            document.getElementById('mapConfigLng').value = 52.106505;            
            document.getElementById('mapConfigLat').value = 10.546875;
            document.getElementById('mapConfigZoom').value = 4;
        }

        map.setUIToDefault();
        map.enableScrollWheelZoom();


        geocoder = new GClientGeocoder();
        G_PHYSICAL_MAP.getMinimumResolution = function () {
            return 3
        };
        G_NORMAL_MAP.getMinimumResolution = function () {
            return 3
        };
        G_SATELLITE_MAP.getMinimumResolution = function () {
            return 3
        };
        G_HYBRID_MAP.getMinimumResolution = function () {
            return 3
        };


        document.getElementById('messageBox').style.visibility = 'hidden';
        loadData();
        createIncludeCode();
        createMapLink();
        
        GEvent.addListener(map, 'moveend', function () {
            //map.clearOverlays();
            document.getElementById('message').style.visibility = 'hidden';
            document.getElementById('messageA').style.visibility = 'hidden';
            document.getElementById('messageBox').style.visibility = 'hidden';
            var n = markersArray.length;
            for (var i = 0; i < n; i++) {
                //map.removeOverlay(markersArray[i]);
            }
            markersArray.splice(0, n);
            anzahl.splice(0, k);
            if (markerCluster != null) {
                //markerCluster.clearMarkers();
            }
            k = 0;
            
            loadData();            
            createMapLink();
            createIncludeCode();
            
        });
    }
}

// Map link
function createMapLink()
{
    var mapLinkLat = Math.round(map.getCenter().lat()*Math.pow(10,6))/Math.pow(10,6);
    var mapLinkLng = Math.round(map.getCenter().lng()*Math.pow(10,6))/Math.pow(10,6);
    var mapLinkZoom = map.getZoom(); 
    mapLinkResult = mapLink + 'll/' + mapLinkLat + ',' + mapLinkLng + '/z/' + mapLinkZoom;            
    document.getElementById('mapLinkField').value = mapLinkResult;            
    document.getElementById('mapBBLinkField').value = '[url=' + mapLinkResult + ']Speed Camera Map[/url]';            
    document.getElementById('mapHtmlLinkField').value = '<a href="' + mapLinkResult + '">Speed Camera Map</a>';            
    document.getElementById('mapConfigLng').value = mapLinkLng;            
    document.getElementById('mapConfigLat').value = mapLinkLat;
    document.getElementById('mapConfigZoom').value = mapLinkZoom;	
}

/*CREATE_MARKER*/

function createMarker(point, type, data) {
    var marker = new GMarker(point, cI[type]);
    GEvent.addListener(marker, 'click', function () {
        //streetview(point);		
        //getAddress(point);
        //displayMessageA(type);
        //document.getElementById('messageBox').style.visibility = "visible";

        marker.openInfoWindowHtml(infoWindowContent(data));
    });
    return marker;
}

function showInfoWindow(point, type, data)
{
    var marker = new GMarker(point, cI[type]);
    //GEvent.addListener(marker, 'visibilitychanged', function () {
        //streetview(point);		
        //getAddress(point);
        //displayMessageA(type);
        //document.getElementById('messageBox').style.visibility = "visible";

        marker.openInfoWindowHtml(infoWindowContent(data));
    //});
    return marker;
}

function infoWindowContent(data)
{        
	var html = '<div id="mapBubble"><p><b>' + switchType(data.getAttribute('rawType')) + ' ' + data.getAttribute('speed') + ' ' + data.getAttribute('speedUnit') + ' (ID: ' + data.getAttribute('id') + ')</b></p>';
	address = data.getAttribute('street') + ', ' + data.getAttribute('zip') + ' ' + data.getAttribute('city') + ', ' + data.getAttribute('state');
	html = html + '<p>' + address + '</p>';
	html = html + '<p>' + data.getAttribute('description') + '</p>';
	html = html + '<div id="mapCommentMessage" style="display:none;"><div id="mapCommentMessagePlace"></div></div>';
	html = html + '<form id="mapBubbleForm"><input type="hidden" name="id" value="' + data.getAttribute('id') + '" /><textarea id="mapBubbleComment" name="comment"></textarea><br /><input type="submit" name="submit" value="' + textSubmitComment + '"  onclick="commentSpeedCamera(\'mapBubbleForm\');return false;" /></form>';
	html = html + '</div>';
	return html;
}

function streetview(point){
		panoramaOptions = { latlng:point };
		pano = new GStreetviewPanorama(document.getElementById("streetview"), panoramaOptions);
		GEvent.addListener(pano);
}

/*(REVERSE)_GEOCODER*/

function getAddress(point) {
    if (point != null) {
        address = point;
        geocoder.getLocations(point, showAddress);
    }
}

function showAddress(response, point) {
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    } else {
        place = response.Placemark[0];
        displayMessage(place, point);
    }
}

function showAddress2(address) {
    if (geocoder) {
        geocoder.getLatLng(
        address, function (point) {
            if (!point) {
                alert(address + " not found");
            } else {
                map.setCenter(point, 11);
            }
        });
    }
}
	
/*COO_DATA_SERVER*/
	
function loadData() {

    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var minX = southWest.lng();
    var maxX = northEast.lng();
    var minY = southWest.lat();
    var maxY = northEast.lat();
    var location = scriptLocation + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	
    GDownloadUrl(location, function (data) {
        var xml = GXml.parse(data);		
        var markers = xml.documentElement.getElementsByTagName('marker');
        var n = markers.length;
		var nn = parseInt(markers[0].getAttribute('anzahl'));


        if (n == 1 && nn > 500) { 
        	map.clearOverlays();
        	markersDistinct = new Array();
            var count = parseInt(markers[0].getAttribute('anzahl'));
            counter = count;				
			drawRec();
        } else if (n <= 500) {

        	
            for (var i = 0; i < n; i++) 
            {
                //var name = markers[i].getAttribute('name');
                //var address = markers[i].getAttribute('address');
                var id = parseInt(markers[i].getAttribute('id'));
                //var vm = markers[i].getAttribute('vm');
				var type = markers[i].getAttribute('type');
                var point = new GLatLng(parseFloat(markers[i].getAttribute('lng')), parseFloat(markers[i].getAttribute('lat')));
                
                if (cameraID && cameraID == id) {
                	var marker = showInfoWindow(point, type, markers[i]);
                } else {
                	var marker = createMarker(point, type, markers[i]);
                }
                
                
                if (id in oc(markersDistinct)) {
                } else {
	                markersArray.push(marker);
	                markersDistinct.push(id);
                }
                
                
            }
            
            counter = markers.length;
            
            display2();
			
        }
    }
	
	);
    
}

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}
        
/*MARKER_DISPLAY_OPTION*/
	  
function display2() 
{

    if (counter < 100) {
        for (var i = 0; i < 100; i++) {
            map.addOverlay(markersArray[i]);
        }
    } else {
        markerCluster = new MarkerClusterer(map, {
            markers: markersArray
        },
        mcOptions);
        markerCluster.addMarkers(markersArray);
    }
}
	  
/*LOADING_SCREEN*/
	  
function loading() {
	document.getElementById('loading').style.visibility = "visible";
	document.getElementById('loading').innerHTML = '<center><img style="background:#fff;padding:3px 20px 3px 20px;" src="' + imageLocation + '/throbber.gif"></center>';
}

/*DETAILS*/
	  
	function displayMessage(place, point) {
document.getElementById('message').style.visibility = "visible";
document.getElementById("message").innerHTML = "<b>" + textCameraDetails + "</b> " + "<br/>" 
+ "<b>" + textAddress + ": </b> " + place.address + " (" + place.AddressDetails.Country.CountryNameCode + ")" +  "<br/>";
}

/*COUNTER*/

function displayMessage3(counter) {
        document.getElementById("message3").style.visibility = "visible";
		if (counter > 500 || counter < 500 && counter > 100){
        document.getElementById("message3").innerHTML = "<b>" + textCamerasInThisArea + ": </b> " + "<b>" + counter + "</b>" + "<b style='color:#860000;'>" + " " + textZoomIn + "</b>";
		} else {
		document.getElementById("message3").innerHTML = "<b>" + textCamerasInThisArea + ": </b> " + "<b>" + counter + "</b>" + "<b style='color:#860000;'>" + " " + textClickIcon + "</b>";
		}
		//om.Clear();
    }

/*TYPE*/	
	
  function displayMessageA(type) 
  {
	  //type = switchType(type);
  
        //document.getElementById("messageA").style.visibility = "visible";
        //document.getElementById("messageA").innerHTML = "<b>" + textType + ": </b> " + type; // + "<br/>" + "<b>ID: </b> " + id + "<b> Vermessen: </b> " + vm;
    }
  
  function switchType(type)
  {
	  
	  switch(type) {
		  case "A": {type = textTypeA; break;}
		  case "TU": {type = textTypeTU; break;}
		  case "G": {type = textTypeG; break;}
		  case "GA": {type = textTypeGA; break;}
		  case "GV": {type = textTypeGV; break;}	  
		  case "SC": {type = textTypeSC; break;}	  
		  case "SCE": {type = textTypeSCE; break;}	  
	  }
	  
	  return type;

	  
	  switch(type) {
	  case "A": {type = textTypeA; break;}
	  case "TU": {type = textTypeTU; break;}
	  case "G10": {type = textTypeG10; break;}
	  case "G20": {type = textTypeG20; break;}
	  case "G30": {type = textTypeG30; break;}
	  case "G40": {type = textTypeG40; break;}
	  case "G50": {type = textTypeG50; break;}
	  case "G60": {type = textTypeG60; break;}
	  case "G70": {type = textTypeG70; break;}
	  case "G80": {type = textTypeG80; break;}
	  case "G90": {type = textTypeG90; break;}
	  case "G100": {type = textTypeG100; break;}
	  case "G110": {type = textTypeG110; break;}
	  case "G120": {type = textTypeG120; break;}
	  case "G130": {type = textTypeG130; break;}
	  case "GA10": {type = textTypeGA10; break;}
	  case "GA20": {type = textTypeGA20; break;}
	  case "GA30": {type = textTypeGA30; break;}
	  case "GA40": {type = textTypeGA40; break;}
	  case "GA50": {type = textTypeGA50; break;}
	  case "GA60": {type = textTypeGA60; break;}
	  case "GA70": {type = textTypeGA70; break;}
	  case "GA80": {type = textTypeGA80; break;}
	  case "GA90": {type = textTypeGA90; break;}
	  case "GV": {type = textTypeGV; break;}
  
	  }
	  return type;
  }
  

	/*
	function openPanoramaBubble() {
  var contentNode = document.createElement('div');
  contentNode.style.textAlign = 'center';
  contentNode.style.width = '500px';
  contentNode.style.height = '300px';
  contentNode.innerHTML = 'Loading panorama';

  var smallNode = document.createElement('div');
  smallNode.style.width = '200px';
  smallNode.style.height = '200px';
  smallNode.id = 'pano';
  marker.openInfoWindow(smallNode, {maxContent: contentNode, maxTitle: "Full screen"});

  panorama = new GStreetviewPanorama(smallNode);
  panorama.setLocationAndPOV(marker.getLatLng(), null);
  GEvent.addListener(panorama, "newpano", onNewLocation);
  GEvent.addListener(panorama, "yawchanged", onYawChange); 

  var iw = map.getInfoWindow();
  GEvent.addListener(iw, "maximizeend", function() {
    panorama.setContainer(contentNode);  
    window.setTimeout("panorama.checkResize()", 5);
  });
  GEvent.addListener(marker, "infowindowbeforeclose", function() {
    panorama.remove();
  });
}
*/

function drawRec(){
		var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
				
        var lngDelta = (northEast.lng() - southWest.lng()) / 4;
        var latDelta = (northEast.lat() - southWest.lat()) / 2;
		
//0		
        var rectBounds = new GLatLngBounds(
            new GLatLng(southWest.lat() + latDelta,			
                        southWest.lng()),					
            new GLatLng(northEast.lat(),					
                        northEast.lng() - lngDelta * 3 ));	
						
		a = rectBounds.getSouthWest();
		b = rectBounds.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
    var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds;
	load2(location, x);

//1
		var rectBounds1 = new GLatLngBounds(
            new GLatLng(southWest.lat() + latDelta,
                        southWest.lng() + lngDelta),
            new GLatLng(northEast.lat(),
                        northEast.lng() - lngDelta * 2 ));
						
		a = rectBounds1.getSouthWest();
		b = rectBounds1.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
    var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds1;
	load2(location, x);
						
//2		

		var rectBounds2 = new GLatLngBounds(
            new GLatLng(southWest.lat() + latDelta,
                        southWest.lng() + lngDelta * 2),
            new GLatLng(northEast.lat(),
                        northEast.lng() - lngDelta ));
						
		a = rectBounds2.getSouthWest();
		b = rectBounds2.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//&var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds2;
	load2(location, x);

//3		
		var rectBounds3 = new GLatLngBounds(
            new GLatLng(southWest.lat() + latDelta,
                        southWest.lng() + lngDelta * 3),
            new GLatLng(northEast.lat(),
                        northEast.lng()));						

		a = rectBounds3.getSouthWest();
		b = rectBounds3.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds3;
	load2(location, x);				
		
//4		
		var rectBounds4 = new GLatLngBounds(
            new GLatLng(southWest.lat(),		
                        southWest.lng()),			
            new GLatLng(northEast.lat() - latDelta,			
                        northEast.lng() - lngDelta * 3 ));						

		a = rectBounds4.getSouthWest();
		b = rectBounds4.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds4;
	load2(location, x);							
	
//5		
		var rectBounds5 = new GLatLngBounds(
            new GLatLng(southWest.lat(),		
                        southWest.lng() + lngDelta),			
            new GLatLng(northEast.lat() - latDelta,			
                        northEast.lng() - lngDelta * 2 ));						

		a = rectBounds5.getSouthWest();
		b = rectBounds5.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	
	x = rectBounds5;
	load2(location, x);		
						
//6		
		var rectBounds6 = new GLatLngBounds(
            new GLatLng(southWest.lat(),		
                        southWest.lng() + lngDelta * 2),			
            new GLatLng(northEast.lat() - latDelta,			
                        northEast.lng() - lngDelta ));						

		a = rectBounds6.getSouthWest();
		b = rectBounds6.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds6;
	load2(location, x);		

//7		
		var rectBounds7 = new GLatLngBounds(
            new GLatLng(southWest.lat(),		
                        southWest.lng() + lngDelta * 3),			
            new GLatLng(northEast.lat() - latDelta,			
                        northEast.lng() ));						

		a = rectBounds7.getSouthWest();
		b = rectBounds7.getNorthEast();	
		
	var minY = a.lat();
    var maxY = b.lat();
	var minX = a.lng();
    var maxX = b.lng();
		
	//var location = scriptLocation + '/data2.php' + '?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var location = scriptLocationNum + 'lngmin/' + minX + '/lngmax/' + maxX + '/latmin/' + minY + '/latmax/' + maxY;
	x = rectBounds7;
	load2(location, x);	
		
      }

function load2(location, x) {
	
    GDownloadUrl(location, function (data) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName('marker');
        var count = parseInt(markers[0].getAttribute('anzahl'));

        counter2 = count;

        anzahl.push(counter2);
        map.addOverlay(new Rectangle(x));
    }
    )
}
	  
// A Rectangle is a simple overlay that outlines a lat/lng bounds on the
    // map. It has a border of the given weight and color and can optionally
    // have a semi-transparent background color.
    function Rectangle(bounds, opt_weight, opt_color) {
      this.bounds_ = bounds;
      this.weight_ = opt_weight || 0;
      this.color_ = opt_color || "red";
    }
    Rectangle.prototype = new GOverlay();

    // Creates the DIV representing this rectangle.
    Rectangle.prototype.initialize = function(map) {
	
	x = anzahl[k];
	k++;
	rt = x;
      // Create the DIV representing our rectangle
      div = document.createElement("div");
	  if (rt > 0) {
	      div.style.border = this.weight_ + "px solid " + this.color_;
	      div.style.position = "absolute";      
		  div.innerHTML = "<div style ='padding-top:85px;opacity:.8;'><center><b style='color:#fff;background-color:#FF0075;padding:3px 20px 3px 20px;font-size:13px;' class='mapRectangle'> " + rt +"</b></center></div>";

	
	  }
      // Our rectangle is flat against the map, so we add our selves to the
      // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
      // below the marker shadows)
      map.getPane(G_MAP_MAP_PANE).appendChild(div);
      this.map_ = map;
      this.div_ = div;
    }

    // Remove the main DIV from the map pane
    Rectangle.prototype.remove = function() {
      this.div_.parentNode.removeChild(this.div_);
    }

    // Copy our data to a new Rectangle
    Rectangle.prototype.copy = function() {
      return new Rectangle(this.bounds_, this.weight_, this.color_,
                           this.backgroundColor_, this.opacity_);
    }

    // Redraw the rectangle based on the current projection and zoom level
    Rectangle.prototype.redraw = function(force) {
      // We only need to redraw if the coordinate system has changed
      if (!force) return;

      // Calculate the DIV coordinates of two opposite corners of our bounds to
      // get the size and position of our rectangle
      var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
      var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

      // Now position our DIV based on the DIV coordinates of our bounds
      this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
      this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
      this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_) + "px";
      this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_) + "px";
    }
    
function displayLayer(blockid) 
{
	
    if (document.getElementById(blockid).style.display==''){
        document.getElementById(blockid).style.display='none';
    } else {
        document.getElementById(blockid).style.display='';
    }
}

/* XHR Report camera*/
function reportSpeedCamera(formID)
{
	if (document.getElementById('mapReportLng').value == '') {
        document.getElementById('mapReportMessage').style.display = 'block';  
		document.getElementById('mapReportMessagePlace').innerHTML = textSelectPoint;
		return false;
	}
	
	if (document.getElementById('mapReportType').value == '') {
        document.getElementById('mapReportMessage').style.display = 'block';  
		document.getElementById('mapReportMessagePlace').innerHTML = textSelectType;
		return false;
	}
	
    var postParams = { 
        url: reportLocation,
        load: function(data) 
        {       
    		console.debug(data);        
            if (data == 'failed') {
            } else {
                //console.debug(data);
	            text = 'mailSent';
	            document.getElementById('mapReportMessage').style.display = 'block';            
                document.getElementById('mapReportMessagePlace').innerHTML = textThankYou;
                document.getElementById('stepTwo').style.display = 'none';
                removeReportMarker();
            }
            
        },

        error: function(data) 
        { 
            console.debug("An error occurred: ", data); 
        },

        form: formID,
        timeout: 4000,
        headers: { "X-Requested-With": "XMLHttpRequest" }
        };

    dojo.xhrPost(postParams); 
}

/* XHR Comment camera*/
function commentSpeedCamera(formID)
{
	if (document.getElementById('mapBubbleComment').value == '') {
		document.getElementById('mapCommentMessage').style.display = 'block';    
		document.getElementById('mapCommentMessagePlace').innerHTML = textEnterComment;
		return false;
	}
	var postParams = { 
			url: commentLocation,
			load: function(data) 
			{       
		console.debug(data);        
		if (data == 'failed') {
		} else {
			console.debug(data);
			text = 'mailSent';
			document.getElementById('mapCommentMessage').style.display = 'block';            
			document.getElementById('mapCommentMessagePlace').innerHTML = textThankYou;
			document.getElementById('mapBubbleForm').style.display = 'none';;
		}
		
			},
			
			error: function(data) 
			{ 
				console.debug("An error occurred: ", data); 
			},
			
			form: formID,
			timeout: 4000,
			headers: { "X-Requested-With": "XMLHttpRequest" }
	};
	
	dojo.xhrPost(postParams); 
}


/* Select position on map */
function selectPosition()
{
    document.getElementById('mapReportMessage').style.display = 'none';            
    document.getElementById('mapReportMessagePlace').innerHTML = '';
    document.getElementById('stepOne').style.display = 'block';
    document.getElementById('stepTwo').style.display = 'none';
    removeReportMarker();

	latlng = map.getCenter();
    var marker = new GMarker(latlng, {draggable: true});
    
    window.reportEvent = GEvent.addListener(map, "click", function(overlay, latlng) {
    	removeReportMarker();
	
        if (latlng) {
            document.getElementById('mapReportLat').value = latlng.lat().toFixed(6);
            document.getElementById('mapReportLng').value = latlng.lng().toFixed(6);
            marker = new GMarker(latlng, {draggable:true});            
            
            GEvent.addListener(marker, "dragstart", function() {
                map.closeInfoWindow();
                });
            GEvent.addListener(marker, "dragend", function() {
                document.getElementById('mapReportLat').value = marker.getPoint().lat().toFixed(6);
                document.getElementById('mapReportLng').value = marker.getPoint().lng().toFixed(6);
                });
            
            map.addOverlay(marker);
            window.reportMarker = marker;
            document.getElementById('stepOne').style.display = 'none';
            document.getElementById('stepTwo').style.display = 'block';
        }
      });
}

function removeReportMarker()
{
	if (window.reportEvent) {
	    GEvent.removeListener(window.reportEvent);		
	}
	if (window.reportMarker) {
		map.removeOverlay(window.reportMarker);
	}
}

function hideAd()
{
	displayLayer('mapAd');
	interval = window.setInterval("displayAd()", 60000 * 5);
}

function displayAd()
{
	displayLayer('mapAd');
	if (interval != '') {
		window.clearInterval(interval);
	}
}

function changeLanguage(language)
{
	setCookie(languageCookieName, language);
	window.location = window.location.href;
}

function setCookie(name, value)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + 30);
	document.cookie=name + "=" +escape(value)+ ";expires="+exdate.toUTCString();
}
