(function () {
	var mymarkers = [];/*...*/
	var newMarkerArray = [];
    window.onload = function () {
		var initialLocation;
		var berlin = new google.maps.LatLng(52.500556, 13.398889);
		var browserSupportFlag =  new Boolean();
		var latlng = new google.maps.LatLng(52.5, 12.0);
		var myOptions = {
			center: latlng,
			zoom: 6,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI: true,
			navigationControl: true,
			navigationControlOptions: {
				position: google.maps.ControlPosition.LEFT_BOTTOM,
				style: google.maps.NavigationControlStyle.ZOOM_PAN
			},
			mapTypeControl: true,
			mapTypeControlOptions: {
				mapTypeIds: [
					google.maps.MapTypeId.ROADMAP,
					google.maps.MapTypeId.HYBRID
				],
				position: google.maps.ControlPosition.BOTTOM
			},
			streetViewControl: false
		};
		var bounds = new google.maps.LatLngBounds();
		var infoBubble = new InfoBubble({
			ShadowStyle: 0,
			padding: 10,
			borderWidth: 1,
			backgroundColor: '#F7F7EF',
			maxWidth: 300,
			maxHeight: 150,
			borderRadius: 5,
			arrowSize: 20,
			hideCloseButton: true,
			arrowPosition: 50,
			arrowStyle: 0
		});
	    var $clickFilter = 	$("#mapfilter > li > table > tbody > tr > td >  a");
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		var requestUrl = 'node/27176';
		
		loadMarkers(mymarkers, requestUrl, infoBubble, "map");
	
		filterIcons();		
		
		
		//Search map function called from mapGlobalFunctions.js		
		searchMap();

    
    //Preventing linking for all browsers 
	// function ie8SafePreventEvent(e){
		// if(e.preventDefault){
			// e.preventDefault()
		// } else {
    		// e.stop()
    	// };
	    // e.returnValue = false;
    	// e.stopPropagation();        
	// }
	
	//Find Me Function
	// function findMe() {
    // // Try W3C Geolocation (Preferred)
		  // if(navigator.geolocation) {
		    // browserSupportFlag = true;
		    // navigator.geolocation.getCurrentPosition(function(position) {
		      // initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
		      // map.setCenter(initialLocation);
		      // map.setZoom(12);
		      // return false;
		    // }, function() {
		      // handleNoGeolocation(browserSupportFlag);
		    // });
		  // // Try Google Gears Geolocation
		  // } else if (google.gears) {
		    // browserSupportFlag = true;
		    // var geo = google.gears.factory.create('beta.geolocation');
		    // geo.getCurrentPosition(function(position) {
		      // initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
		      // map.setCenter(initialLocation);
		      // map.setZoom(12);
		    // }, function() {
		      // handleNoGeoLocation(browserSupportFlag);
		    // });
		  // // Browser doesn't support Geolocation
		  // } else {
		    // browserSupportFlag = false;
		    // handleNoGeolocation(browserSupportFlag);
		  // }
		  // function handleNoGeolocation(errorFlag) {
		    // if (errorFlag === true) {
		      // alert("Geolocation service failed.");
		      // initialLocation = berlin;
		    // } else {
		      // alert("Your browser doesn't support geolocation. We've placed you in Berlin.");
		      // initialLocation = berlin;
		    // }
		    // map.setCenter(initialLocation);
		    // map.setZoom(12);
		  // }
		// }
	// Find Me button	
	$("#findMe").click(function(e){findMe();});


	function filterIcons(){
   		$clickFilter.click(function() {
   			var filterNo;
       		filterNo = $(this).attr('class').split(" ")[0];
	   		if ($(this).hasClass("selected")){
   				if (mymarkers){
   					for (i in mymarkers) {
   							if (mymarkers[i].category == filterNo) {
           					mymarkers[i].setVisible(true);
           					}
   					}
				}
       		} else {
	   			if (mymarkers){
   					for (i in mymarkers) {
   						if (mymarkers[i].category == filterNo) {
           						mymarkers[i].setVisible(false);
           				}
       				}
				}
			}
    	});
    }	


	//add a place v1
 		$(".menu-428 a").click(function(e){
 			newContent = "<h4>1. Bitte navigieren Sie sich rechts oben den Ort, an dem Sie einen Eintrag vornehmen möchten.<br>2. Die Karte schlägt Ihnen dann einen Standort vor. Um die korrekte Position zu bestimmen, schließen bitte dieses Fenster und setzen Sie den Pin auf die exakte Stelle, an dem sich der Baum oder Strauch befindet.<br>3. Wenn Sie den Pin korrekt platziert haben, dann klicken Sie bitte <a href='node/add/fruits' class='AjaxHere'>hier.</a> und tragen weitere Details ein.</h4>";
 			mapCenter = map.getCenter();
  			clearOverlays();
 			newMarker =  new google.maps.Marker({
    			map:map,
    			title: 'Ziehen zum der Markierung zu verschieben',
    			draggable:true,
    			animation: google.maps.Animation.DROP,
    			position: mapCenter
  			});
			newMarkerArray.push(newMarker);
  			map.setZoom(17);
  			map.setMapTypeId(google.maps.MapTypeId.HYBRID);
			setTimeout(newMarkerSet, 1000);
			google.maps.event.addListener(newMarker, 'click', function() {
				newMarkerSet();		
			});
 			return false;
		});
		
		function newMarkerSet () {  
			var newBubble = new InfoBubble({
				ShadowStyle: 0,
    			padding: 10,
    			borderWidth: 1,
    			backgroundColor: '#F7F7EF',
    			minWidth: 300,
    			maxWidth: 500,
    			minHeight: 150,
    			borderRadius: 5,
    			arrowSize: 20,
				hideCloseButton: true,
    			arrowPosition: 50,
    			arrowStyle: 0			
			});				
  			//Modal with lightbox
			google.maps.event.addListener(newBubble, "domready", function() {
				setTimeout(attachClicklistener, 500);
				function attachClicklistener() {
					$(".AjaxHere").click(function(e){
						ie8SafePreventEvent(e);
						$("#AjaxTarget").empty().load("/node/add/fruits #eintragen");
						$("#AjaxWrapper").show();
						Drupal.attachBehaviors(document);	
						$("#eintragen").ready(function() {
							setTimeout(autoLatLng, 5000);
							Drupal.attachBehaviors('#eintragen');
							var newFruitLat = newBubble.anchor.position.lat();
							var newFruitLng = newBubble.anchor.position.lng();
							function autoLatLng() {
								$("input#edit-field-latitude-und-0-value").val(newFruitLat);
								$("input#edit-field-longitude-und-0-value").val(newFruitLng);
							};
						});
						return false;
					});
				}
			});
			
			newBubble.setContent(newContent);
			newBubble.open(map,newMarker);
  		}  		

 		function clearOverlays() {
  			if (newMarkerArray) {
    			for (i in newMarkerArray) {
      				newMarkerArray[i].setMap(null);
    			}
  			}
		}
		//end addPlace v1

		
   }; //end window.onload
    
    InfoBubble.prototype.onAdd = function (){
    	if (!this.bubble_) {
    		this.buildDOM_();
    	}
    	
    	this.addEvents_();
    	
    	var panes = this.getPanes();
    	if (panes) {
    		panes.floatPane.appendChild(this.bubble_);
    	}
    	google.maps.event.trigger(this, 'domready');
    	console.log('asdhasd');
    }
    InfoBubble.prototype['onAdd'] = InfoBubble.prototype.onAdd;     
    
})();

// function getInfoBubbleContent(response, title, id){										
	// var content = response.find('#AjaxMe p').html();
	// if (content === null){
		// content = " ";
	// }else if(content.length >= 100) {
		// content = content.substring(0,100)+"...";
	// }
	// var external = content.toString();
	// /*--- thumbnail -----*/
	// var img = response.find('#AjaxMe img');
	// var src = $(img).attr("src");
	// var thumbnail = " ";
	// if(typeof src == 'undefined'){
	  	// thumbnail = " ";
	// } else if(src.indexOf('files/fruits') != -1 ) {
		// var parcedParts = src.split('/');
		// var imageTitle = parcedParts[parcedParts.length-1];
		// src = "/sites/default/files/styles/thumbnail/public/fruits/"+imageTitle;
		// thumbnail = "<img src="+src+">";
	// } 
	// var infobubbleHtml = "<div class='infowin'>" + thumbnail + "<h3>" + title + "</h3><h4><span id='description_bubble'>" + content + "   </span><a href='/node/" + id + "' class='AjaxMe'>weiterlesen/kommentieren</a></h4></div><div id='CloseButton' class='close_popup'>"; /* after "nfowin'>" + thumbnail + " */
	// return infobubbleHtml;
// }  




