var MapEngine = new Class({
	
	 
	latlng: "",	
	circle: new google.maps.Circle({}),
	radio: 2,
	xml: "",
	spots: new Array(),
	infowindows: new Array(),
	updatetimeout: 0,
	detailfunc: "",
 	customicon: false,
	
	initialize: function(customicon){
		  
		  this.customicon = customicon;
		  this.latlng = new google.maps.LatLng(0,0);			
		
		
		  $('#overlay #close').live('click',function(){
		  
			  	$('#overlay').slideUp('fast');
		  })	
		  
		  $('#map_canvas').css({'height' :  ($('#pageend').offset().top-300)+'px'})
		  
		  $(window).resize(function(){
		 	 $('#map_canvas').css({'height' :  ($('#pageend').offset().top-300)+'px'})
		  });
		
	},
	
	
	GetXML: function(outofradioobj,callbackfunction)
	{		
			if(!outofradioobj)
			outofradioobj = "";
	
			latlngArrray = new Array();
			
			n=0;
			for(var i in this.latlng){
				if(n<2)
				latlngArrray[n] = this.latlng[i];
				
				n++;	
			}
			
			//console.log(latlngArrray);
	
			//console.log(this.latlng)
			
			
			
			var jsonRequest = new Request({url: document.mapperpath+"/objects.php?outofradioobj="+outofradioobj+"&radio="+this.radio+"&lat="+latlngArrray[0]+"&lng="+latlngArrray[1], onSuccess: function(e){
				
				
			
			this.StoreXML(e);
			
			if(callbackfunction)
				callbackfunction(outofradioobj);
			
			 
			
			}.bind(this)});
				
				
			jsonRequest.send();
			
			
		
	},
	
	
	 FindMarker: function(){
		
		this.found = false;
		this.dragger = false;
		
		jQuery(this.spots).each(function(c,e){
				
				
			 	
			 	if(this.latlng.lat().toFixed(4) == e.position.lat().toFixed(4) && e.spot){
			 		 this.found = true;
					 document.mapengine.openInfoWindow(e.aid,e.oid);
					 
				}  
				
				if(!e.spot){
					this.dragger = e;
				}
				
				
				
			}.bind(this));
		
			if(this.dragger && this.found)
			this.dragger.setMap(null);
	
	},
		
		
	StoreXML: function(e){
			
	
	
			this.xml =  xml2json.parser(e); //$.xml2json(e);
			
			
			var markers = this.xml.objs.marker;		
			
			
				if(jQuery(markers).length == 1)
 				 markers[0] = markers;
			//console.log(this.xml.objs.marker[0]);
			
			jQuery(this.spots).each(function(e){
				
				this.spots[e].setMap(null);
			 
				
			}.bind(this));
			
			
			 
			jQuery(this.infowindows).each(function(e){
				
				if(this.infowindows[e])
				this.infowindows[e].close();
			}.bind(this));
			 
			
			var c = 0;
			jQuery(markers).each(function(e){
 				
 				id = c;
 				if(markers[c].marker_id)
 				id = markers[c].marker_id;
 				
 				var pos = new google.maps.LatLng(markers[e].latlng.x, markers[e].latlng.y);
 					
 					
 					this.spots[c] = new google.maps.Marker({
					map: document.map, 
				  	position: pos,
				  	title: markers[e].name,
				  	draggable:true,
				  	aid: c,
				  	oid: id
				  	
				})
				
				
				
				
				if(markers[e].marker_text){
					
					
					if(!this.customicon)
					this.spots[c].setIcon(document.mapperpath+"/img/marker1.png");
					else 
					this.spots[c].setIcon(this.customicon);
					
					this.spots[c].setDraggable(false);
					this.spots[c].spot = true;
					
					 
				
				
					google.maps.event.addListener(this.spots[c], 'click', function(){
						document.mapengine.openInfoWindow(this.aid,this.oid);
					}); 
    				
    				 
    				
    				
 				} else {
 				
 					google.maps.event.addListener(this.spots[c], 'dragstart', function(){
						document.mapengine.onDragMarker(this.aid);
					});
					
					google.maps.event.addListener(this.spots[c], 'dragend', function(){
						document.mapengine.onDropMarker(this.aid);
					});
 					
 				}
 				c++;				
 			}.bind(this))	
 			
 					//var latlng = new google.maps.LatLng(i.latlng.x, i.latlng.x);
					
   			 		/*var marker = new google.maps.Marker({
						map: i.latlng.x 
				  		position: results[0].geometry.location
				  		http://www.hpvorlagen24.de/wp-content/plugins/fresh-page/files_flutter/12399604735395-hausicons1-450.jpg
					});*/
			 
	},
	
	onDragMarker: function(id){
		
		jQuery(this.infowindows).each(function(e){
			if(this.infowindows[e])
			this.infowindows[e].close();
		}.bind(this));
		
		this.circle.setMap(null);
	},
	
	
	onDropMarker: function(id){
	
		this.latlng = this.spots[id].getPosition();
		this.setRatio(this.radio);
		this.spots[id].setDraggable(false);
 
	},
	
	
	jumpToMarkerAndOpen: function(mid){
		
		document.mapengine.GetXML(mid,this.openMarkerById.bind(this));

	},
	
	openMarkerById:function(mid){
		
		for(var i in this.spots){
			
			if(this.spots[i].oid == mid){
				this.openInfoWindow(this.spots[i].aid,mid);
			}
		}
	
	},
	
	
	openInfoWindow: function(id,oid){
		
		jQuery(this.infowindows).each(function(e){
			if(this.infowindows[e])
			this.infowindows[e].close();
		}.bind(this));
			 
			
			this.infowindows[id] = new google.maps.InfoWindow({
            				content: 'wird geladen',
           					maxWidth: '1000'
         				});
			
					$.ajax({
  						url: document.mapperpath+'/infowindow.php?id='+oid,
  						success: function(data) {
  						//	console.log(data);
  							this.infowindows[id].setContent(data);
  						//	this.infowindows[id].close(); 
  							this.infowindows[id].open(document.map,this.spots[id]);   			
         			}.bind(this)
					});
		
		
		this.infowindows[id].open(document.map,this.spots[id]);
	
	},
	
	openDetailWindow: function(id){
		 
		 
		 if(detailfunc != ""){
		 
		 	detailfunc($('.more').attr('data-ident'));
		 	
		 } else {
		 
		$('#overlay').slideDown(500);
		$('#popup').html('daten werden geladen...')
		$.ajax({
  						url: 'template/wgmap24/pages/detailwindow.php?id='+id,
  						success: function(data) {
  							 
  							 $('#popup').html(data);
  							 
  							   			
         			}.bind(this)
					});
		
		}
		
	
	},
	
	setDetailview: function(func){
		detailfunc = func;
		
	},
	
	setRatio: function(radio){
		this.circle.setMap(null);

		
		this.radio = radio;
		
		this.circle = new google.maps.Circle({
  			map:document.map,
  			center: this.latlng,
  			radius: radio*1000,
  			strokeColor:"#000000",
  			strokeWeight: 1,
  			strokeOpacity: 0.4,
  			fillColor: "#000000",
  			fillOpacity: 0.2,
  			clickable: false
  			
  		});
	
		this.GetXML();
	},
	
	centerMap: function(){
		document.map.setCenter(this.latlng);
		setTimeout(this.FindMarker.bind(this),1000);
	
	},
	
	
	ShowAddress: function(address) {
		var adresse_in_klarschrifft = address;
		
		if (document.geocoder) {
			document.geocoder.geocode( { 'address': adresse_in_klarschrifft}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					document.map.setCenter(results[0].geometry.location);
					
					this.latlng = results[0].geometry.location;
				//	console.log(results[0]);
					this.setRatio(this.radio);
					
					
				} else {
					//alert("Es wurde leider nichts zu ihrer Eingabe gefunden.");
				}
			}.bind(this));
		}
	},
	
	
	GetMarker: function (){
	
	
		
		google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(document.map,marker);
       });
	
	
	}
	

})


