// JavaScript Document


var nmiMap=new Class({	
	initialize: function(elementId) {
		
		if (GBrowserIsCompatible()) {
			
			this.tid=false;
			this.drawId=false;
			this.markers=[];
			
			this.map = new GMap2(document.getElementById(elementId));
			
			
			this.cluster=new MarkerClusterer(this.map,null,{maxZoom:8,borderPadding:250});
			
			this.geocoder = new GClientGeocoder();
			this.currentPoint=null;
			this.map.setCenter(new GLatLng(36.59788913307022,2.8125),1);
			
			this.map.setMapType(G_NORMAL_MAP);
			this.map.setUIToDefault();			
			this.buildMarkers();
			this.updateMarkers();
			this.currentMark=null;
			GEvent.bind(this.map, "moveend",this,this.updateMarkers);			
		}
	},
	updateMarkers:function(){		
		var bounds = this.map.getBounds();	
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		var latBegin=southWest.lat()-(latSpan / 2);
		var latEnd=(southWest.lat()+(latSpan + (latSpan / 2)));
		var lonBegin=southWest.lng()-(lngSpan / 2);
		var lonEnd=(southWest.lng() +(lngSpan + (lngSpan/2)));
		var center=this.map.getCenter();
		if(this.tid) {
			clearInterval(tid);	
		}		
		if(this.map.getZoom()>=6) {
			var scope=this;
			callRemote('getAddressPoints',[latBegin,latEnd,lonBegin,lonEnd,center.lat(),center.lng(),0],function(points){				
				scope.drawPoints(points);			
				
			});
		} else  {
			this.cluster.clearMarkers();
			this.markers=[];
			
		}
		
		
	},
	drawPoints:function(points) {
		//$('mapDebug').innerHTML=points.length + " - "+this.map.getZoom();			
		this.markers.each(function(Item,Index){			
			var f=false;
			for(var x=0;x<points.length;x++) {				
				if(points[x].id==Index) {					
					f=true;					
					break;
				}
			}
			if(!f) {			
				
				this.cluster.removeMarker(this.markers[Index]);	
				delete this.markers[Index];
				
			}
			
		},this);
		var scope=this;
		if(this.drawId) {
			clearInterval(this.drawId);	
		}	
										 
		this.drawId=setInterval(function(){
			var m=[];
			for(var x=0;x<150;x++) {
				if(!points.length){clearInterval(scope.drawId);break;}
				var point=points.pop();
				var marker=scope.getMarker(point);
				if(!scope.markers[point.id]) {
					scope.markers[point.id]=marker;						
					m.push(marker);
				
				}
			}
			scope.cluster.addMarkers(m);	
		},10);		
		
		
	},
	buildMarkers:function() {
		var purpleIcon = new GIcon();
		purpleIcon.image = "template/img/marker_purple.png";
		purpleIcon.shadow = "template/img/marker_shadow.png";
		purpleIcon.iconSize = new GSize(21, 29);	
		purpleIcon.iconAnchor = new GPoint(0, 29);
		purpleIcon.infoWindowAnchor = new GPoint(19, 5);		
		this.purpleIcon = { icon:purpleIcon };
		
		
		var purpleIconQ = new GIcon();
		purpleIconQ.image = "template/img/marker_purple_q.png";
		purpleIconQ.shadow = "template/img/marker_shadow.png";
		purpleIconQ.iconSize = new GSize(21, 29);	
		purpleIconQ.iconAnchor = new GPoint(0, 29);
		purpleIconQ.infoWindowAnchor = new GPoint(19, 5);		
		this.purpleIconQ = { icon:purpleIconQ };
		
		var greenIcon = new GIcon();
		greenIcon.image = "template/img/marker_green.png";
		greenIcon.shadow = "template/img/marker_shadow.png";
		greenIcon.iconSize = new GSize(21, 29);	
		greenIcon.iconAnchor = new GPoint(0, 29);
		greenIcon.infoWindowAnchor = new GPoint(19, 5);	
		this.greenIcon = { icon:greenIcon };
		
		var greenIconQ = new GIcon();
		greenIconQ.image = "template/img/marker_green_q.png";
		greenIconQ.shadow = "template/img/marker_shadow.png";
		greenIconQ.iconSize = new GSize(21, 29);
		greenIconQ.iconAnchor = new GPoint(0, 29);
		greenIconQ.infoWindowAnchor = new GPoint(19, 5);	
		this.greenIconQ = { icon:greenIconQ };
	},
	getMarker:function(point) {
		if(point.icon==4) {
			var ico=this.greenIconQ;
		} else if(point.icon==3) {
			var ico=this.greenIcon;
		}
		else if(point.icon==2) {
			var ico=this.purpleIconQ;
		}
		else {
			var ico=this.purpleIcon;
		}
		ico.zIndexProcess=function(){ return point.icon; }

		var mark= new GMarker(new GLatLng(point.lat,point.lon),ico)	
		GEvent.bind(mark, "click", this,function(lngLat){					
			scope=this;	
			this.currentMark=mark;
			callRemote('getAddressDetails',[point.id],function(html) {
				mark.openInfoWindow(html);														   
			});			
		});	
		return mark;	
	},
	search:function(address) {
		scope=this;
		this.geocoder.getLatLng(
			address,
			function(point) {
			  if (!point) {
				alert(address + " not found");
			  } else {
				scope.map.setCenter(point, 8);
				
			  }
			}
		  );
	},
	showClientDetails:function(clientId){
		var scope=this;		
		callRemote('getClientDetails',[clientId],function(html) {
			scope.currentMark.openInfoWindow(html);
		});
	}
	
})
var map=null;
window.addEvent('domready',function(){
	map=new nmiMap('map');
	if($('mapSearchField').value.length>0) {
		map.search($('mapSearchField').value);	
	}
	
});
function doClientShow(clientId) {
	map.showClientDetails(clientId);
}
window.addEvent('unload',function(){
	GUnload();
});