var MapClass = new Class({
	initialize: function(options) {
		this.mapcont=options.mapcont;
		this.centerLat=options.centerLat;
		if(!$defined(this.centerLat)||this.centerLat==0)
			this.centerLat=51.500152; // London
		this.centerLng=options.centerLng;
		if(!$defined(this.centerLng)||this.centerLng==0)
			this.centerLng=-0.126236; // London
		this.zoom=options.zoom;
		if(!$defined(this.zoom))
			this.zoom = 14;

		if(!$defined(this.mapcont))
		 alert("MapClass: mapcont must defined");
		this.map=null;
		this.MCPD="MCPD";
	},

	initializeMap: function() {
	  if(!$defined(this.map)){
	  	  if(!$defined($(this.mapcont))){
	  	  	alert("MapClass: mapcont:"+this.mapcont+" can be found");
	  	  }

		  if (GBrowserIsCompatible()) {
		    this.map = new GMap2($(this.mapcont));
		    this.setDefaultCenter();
		    this.map.addControl(new GLargeMapControl());
		    var mapControl = new GMapTypeControl();
		    this.map.addControl(mapControl);
		  }
	  }
	},

	setDefaultCenter: function(){
	    this.map.setCenter(new GLatLng(this.centerLat,this.centerLng), this.zoom);
	},


	createMarker: function(id,type,point,details,shortDetails,dragEnd,pricePer) {
		this.pricePer = pricePer;
		markerOptions = {};
		if (shortDetails){
			markerOptions = { title : shortDetails };
		}
		if ($defined(dragEnd)){
			markerOptions.draggable=true;
		}
		var marker = new GMarker(point, markerOptions);
		if (details === undefined){
			GEvent.addListener(marker, "click", function() {
			  marker.openInfoWindowHtml("<div id='"+this.MCPD+id+"' style='text-align: center'><img src='/img/light_loader.gif'/></div>");
/*			  if(type==0)	{
				  this.loadPropertySales(id);
			  }else{
			  	  this.loadPropertyLettings(id);
			  }*/
			}.bind(this));
			GEvent.addListener(marker, "infowindowopen", function() {
			  if(type==0)	{
				  this.loadPropertySales(id);
			  }else{
			  	  this.loadPropertyLettings(id);
			  }
			}.bind(this));

		}else if(details!=null){
			GEvent.addListener(marker, "click", function() {
			  marker.openInfoWindowHtml(details);
			});
		}

		if ($defined(dragEnd)){
			 GEvent.addListener(marker, "dragend", dragEnd.bind(marker));
		}
		this.map.addOverlay(marker);
	},

	loadPropertySales: function(id){
		RequestProcessor.getPropertyById(id,this.loadPropertySalesCallBack.bind(this));
	},

	loadPropertySalesCallBack: function(property){
		this.loadPropertyCallBack(property,0);
	},

	loadPropertyLettings: function(id){
		RequestProcessor.getPropertyById(id,this.loadPropertyLettingsCallBack.bind(this));
	},

	loadPropertyLettingsCallBack: function(property){
		this.loadPropertyCallBack(property,1);
	},


	loadPropertyCallBack: function(property,type){
        var googleDetails;
        if(type==0){
	        googleDetails = property.googleDetails;
        }else{
        	if($defined(this.pricePer)){
        		if(this.pricePer == 0 ){
	        		googleDetails = property.googleLettingDetailsPW;
        		}else{
	        		googleDetails = property.googleLettingDetailsPCM;
        		}
        	}else{
        		googleDetails = property.googleLettingDetails;
        	}
        }
        if($(this.MCPD+property.id))
	        $(this.MCPD+property.id).setHTML(googleDetails);
	},

	deleteProperties: function(){
		this.map.clearOverlays();
	},

	placeProperty: function(property, dragEnd) {
		this.deleteProperties();
		var gproperty = new GLatLng(property.latitude, property.longitude);
		this.map.setCenter(gproperty);
		this.createMarker(undefined,undefined,gproperty,null,undefined, dragEnd);
	},


	placeProperties: function(properties, type,pricePer) {
	    this.deleteProperties();
            if (properties && properties.length>0){
                var gproperty;
                var bounds = new GLatLngBounds();
                properties.each(function(item){
                   gproperty = new GLatLng(item.latitude, item.longitude);
                   var googleDetails=type==0?item.googleDetails:item.googleLettingDetails;
                   var googleShortDetails;
                           if(type==0){
						        googleShortDetails = item.googleShortDetails;
					        }else{
					        	if($defined(pricePer)){
					        		if(pricePer == 0 ){
						        		googleShortDetails = item.googleLettingShortDetailsPW;
					        		}else{
						        		googleShortDetails = item.googleLettingShortDetailsPCM;
					        		}
					        	}else{
					        		googleShortDetails = item.googleShortDetails;
					        	}
					        }
                   this.createMarker(item.id,type,gproperty, undefined, googleShortDetails,undefined,pricePer);
                   bounds.extend(gproperty);
                }.bind(this));
                this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
                this.map.setCenter(bounds.getCenter());
            } else {
                this.setDefaultCenter();
            }
	}
});




function Property(latitude,longitude,googleDetails,googleShortDetails){
	this.latitude = latitude;
	this.longitude = longitude;
	this.googleDetails = googleDetails?googleDetails:'';
	this.googleShortDetails = googleShortDetails?googleShortDetails:googleShortDetails;
}
