rwMiniMap = function(divId){ 
	this.map = "";
	this.init(divId); 
};
jQuery.extend(rwMiniMap.prototype, {
	init : function(sourceDivId){
		this.map;
		this.divId = sourceDivId;
		//this.mapWidth = "240";
		//this.mapHeight = "200";
		this.mapWidth = jQuery("#"+sourceDivId).width();
		this.mapHeight = jQuery("#"+sourceDivId).height();
		
		rwMaps.status.miniMap = "loading";
		
		this.load();
	},
	load : function(){
		var container = document.createElement("div");
		container.id = "rwminimap";
		container.setAttribute("style", "width:"+this.mapWidth+"px");
		container.setAttribute("width", this.mapWidth+"px");
		container.setAttribute("style", "height:"+this.mapHeight+"px");
		container.setAttribute("height", this.mapHeight+"px");
		
		jQuery("#"+this.divId).html("");
		jQuery("#"+this.divId).append(container);
		jQuery('#rwminimap').css({"width" : this.mapWidth, "height" : this.mapHeight, "overflow":"hidden"});

		this.map = new google.maps.Map2(document.getElementById("rwminimap"));
		//this.map.setCenter(new google.maps.LatLng(rwMaps.config.lat,rwMaps.config.lng), rwMaps.config.zoom);
		// turn off options
		this.map.disableDragging();
		this.map.disableScrollWheelZoom();
		
		var controller = new rwMiniMap.controllerMiniProperty();
	}
});

// ..........................................................................................
// File controllerMiniProperty
rwMiniMap.controllerMiniProperty = function() { this.init(); };
jQuery.extend(rwMiniMap.controllerMiniProperty.prototype, rwMaps.controller.prototype);
jQuery.extend(rwMiniMap.controllerMiniProperty.prototype, {
	init : function()
	{	
		this.getData();
	},
	
	getData : function(){
		jQuery.ajax({
			url			: this.getURL(), 
			dataType	: "jsonp",
			jsonp		: "callback",
			start		: this.startDataLoad(),
			error		: function(xmlHttpRequest, textStatus, errorThrown){
				//if(console) console.log(textStatus + " " + errorThrown);
				this.errorDataLoad();
			},
			success		: function(data){
				if(data && data.properties){
					this.maplocation(data.properties[0]);
				}
				this.finishedDataLoad(1);
			}.bind(this),
			complete	: function (XMLHttpRequest, textStatus) {
				rwMaps.status.miniMap = "loaded";
			}.bind(this)
		});
	},
	
	getURL : function(){
		// get ID from the URL
		var propertID = location.href.match(/\d+/g);
		var url = "/wp-content/plugins/rwMapsRaywhiterural/data/property.php?" + "propertyIDs="+propertID;
		return url;
	},
	
	maplocation : function (property){
		var marker = new rwMiniMap.markerIcons(property,this.getPropertyHTML(property), this.getTooltipText(property));
		if(marker.property.geometry.Point.coordinates[1] != 0 && marker.property.geometry.Point.coordinates[1] != 0){
			//this.mapBounds(property.geometry.Point.coordinates[1], property.geometry.Point.coordinates[0]);
		}
	},
	
	addPropertyEvents : function(property){
		// no events for individual property
	},
	
	openProperty : function(property){
		//GEvent.trigger(rwMaps.gmarkers[property.details.id].marker,"click");
	}
});

rwMiniMap.markerIcons = function(property,html,tooltipText) { this.init(property,html,tooltipText) };
jQuery.extend(rwMiniMap.markerIcons.prototype, rwMaps.markerIcons.prototype);
jQuery.extend(rwMiniMap.markerIcons.prototype, {
	init : function (property,html,tooltipText) {
		this.property = property;
		this.html = html;
		this.tooltipText = tooltipText;
		this.zoomLevel = 13;
		this.displayMarker();
	},
	
	displayMarker : function(){
		var point = new GLatLng(this.property.geometry.Point.coordinates[1],
						this.property.geometry.Point.coordinates[0]);

		var marker = new GMarker(point, this.createMarker(this.property.details.type));
		
		gmarker = {'id' : this.property.details.id,
					'type' : this.property.details.type,
					'marker' : marker,
					'tooltipText' : this.tooltipText,
					'html' : this.html,
					'imageOn' : this._imageOn,
					'imageOff' : this._imageOff,
					'imageCluster' : this._imageCluster,
					'imageClusterOn' : this._imageClusterOn,
					'imageLogo' : this._imageLogo,
					'property' : this.property};
												
		miniMap.map.addOverlay(gmarker.marker);
		
		if (this.property.geometry.Point.coordinates[1] == 0 && 
			this.property.geometry.Point.coordinates[0] == 0 ){

			//marker.hide();
			//jQuery("#"+miniMap.divId).hide();
			//var url = location.href.split(/\//);
			/*
			var url = location.href;
			var data = url.match(/([a-zA-Z-\.]+)/g);
			var suburb = data[3];
			var state = data[2]
			var country = (state == "nz" || state == "NZ" ) ? "new+zealand" : "australia";
			*/
			
			var staticImage = "http://maps.google.com/maps/api/staticmap?center=";
			staticImage += this.property.details.suburb+",";
			staticImage += (this.property.details.country.toLowerCase() == "nz" ) ? "" : this.property.details.state+",";
			staticImage += this.property.details.country;
			staticImage += "&zoom="+this.zoomLevel+"&size="+jQuery("#"+miniMap.divId).width()+"x"+jQuery("#"+miniMap.divId).height()+"&sensor=false&key="+googleMapsKey;
			
			staticImage = staticImage.replace(/ /gi, "+");
			jQuery("#"+miniMap.divId).html("<img src="+staticImage+" />");
			jQuery("#mapDetailsHeader").html("General Location");
			
		}else{
			miniMap.map.setCenter(point,this.zoomLevel);
			// turn off the hand icon
			jQuery("#mtgt_unnamed_0").css({'cursor' : ''});
			//this.addMarkerEvents(gmarker);
		}
	},
	
	addMarkerEvents : function(gmarker) {
		// Switch icon on marker mouseover and mouseout
		/*
		var mouseover = GEvent.addListener(gmarker.marker, "mouseover", function() {
				gmarker.marker.setImage(gmarker.imageOn);
				//gmarker.marker.openToolTip(gmarker.tooltipText);
			});
		var mouseout = GEvent.addListener(gmarker.marker, "mouseout", function() {
				gmarker.marker.setImage(gmarker.imageOff);
				//gmarker.marker.closeToolTip();
			});
		
		return {"mouseover" : mouseover, "mouseout" : mouseout };
		*/
		return {};
	}
})
/*
var miniMap;
function loadMiniMap(){
	rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
	miniMap = new rwMiniMap("mapDetails");
}
google.load("maps", "2", {"callback" : loadMiniMap});
*/
