if(typeof(Display) == "undefined")
	var Display = {};
	
Display.GoogleMap = Class.create();

Display.GoogleMap.prototype = {
	initialize: function(argumentsCollection){
		// This object pointer
		var thisPointer = this;

		this.id = argumentsCollection.id;
		this.key = argumentsCollection.key;
		this.width = argumentsCollection.width;
		this.height = argumentsCollection.height;
		this.address = argumentsCollection.address;
		this.message = argumentsCollection.message;
		this.zoomlevel = argumentsCollection.zoomlevel;

		// Get attachment element
		this.element = $(this.id);

		// Create Google Map
		this.googlemap = document.createElement('div');
		this.googlemap.className = 'GoogleMap';

		if(this.width)
			this.googlemap.style.width = this.width;

		this.googlemap.style.height = this.height;

		// Create Loader
		this.loader = document.createElement('div');
		this.loader.className = 'Loader';
		this.loader.style.display = 'none';
		this.loader.appendChild(document.createTextNode('Loading'));
		this.googlemap.appendChild(this.loader);

		// Create Error
		this.error = document.createElement('div');
		this.error.className = 'Error';
		this.error.style.display = 'none';
		this.error.appendChild(document.createTextNode('Failed to load google map.'));
		this.googlemap.appendChild(this.error);

		this.getAddress(this.address,this.message); 

		// Attach google map to element
		this.element.appendChild(this.googlemap);
	},

	getAddress: function(address,message){
		// This object pointer
		var thisPointer = this;
		
		// Show Loader
		thisPointer.loader.style.display = '';

		// Test Compatiblity
		if(GBrowserIsCompatible())
			geocoder = new GClientGeocoder();

		// Set Geo Location & Call setPoint
		if(geocoder){
			geocoder.getLatLng(address,
				function(point){
					thisPointer.setPoint(point,message);
				}
			);
		}	
	},

	setPoint: function(point,message){
		// This object pointer
		var thisPointer = this;
		if(!point){
			// Hide Loader
			thisPointer.loader.style.display = 'none';

			// Display Error
			thisPointer.error.style.display = '';
		}
		else {
			// Create Map
			thisPointer.map = new GMap2(thisPointer.googlemap);

			// Set Controls
			thisPointer.map.addControl(new GSmallMapControl());
        	thisPointer.map.addControl(new GMapTypeControl(true));

			// Set Marker
			var marker = new GMarker(point);
			thisPointer.map.setCenter(point,thisPointer.zoomlevel);
			thisPointer.map.addOverlay(marker);
			
			// Set Marker
			GEvent.addListener(marker, "click", function() {
				if(message.length) marker.openInfoWindowHtml(message);
			});
			
			// Auto pop the info box and it should auto centre the map
			GEvent.trigger(marker, "click");
		}
	},

	unload: function(){
		// Unload Map
		GUnload();
	}

}
