/**
 * MapMarkerOverlay Object
 * Allow to show dynamic HTML
 *
 * @author shulard@gmail.com
 * @author ocarrera@agenceinteractive.com
 * @copy Agence Interactive 2010
 */
MapMarkerOverlay.prototype = new google.maps.OverlayView();

/**
 * MapMarkerOverlay constructor
 * @return MapMarkerOverlay object
 */
function MapMarkerOverlay( oOffset )
{
	this.setOffset( oOffset );

	this.sContent = null;
	return this;
}

MapMarkerOverlay.prototype.setContent = function( sContent )
{
	this.sContent = sContent;
}

MapMarkerOverlay.prototype.setPositionFromMarker = function( oMarker )
{
	this.oPosition = oMarker.position;
}

MapMarkerOverlay.prototype.setPositionFromGPS = function( iLatitude, iLongitude )
{
	oLatLng = null;
	
	if( iLatitude != null && iLongitude != null )
		oLatLng = new google.maps.LatLng( iLatitude, iLongitude );
	else
		throw ('Latitude and Longitude values are required.');
		
	this.oPosition = oLatLng;
}

MapMarkerOverlay.prototype.onAdd = function()
{
	// Create the DIV and set some basic attributes.
	this.element = new Element('div');
	this.element.set('style', 'position:absolute;');
	this.element.set('html', this.sContent);
	
	// We'll add this overlay to the floatPane pane (at the top pane).
	var panes = this.getPanes();
	panes.floatPane.appendChild(this.element);
}

MapMarkerOverlay.prototype.draw = function()
{
	if( !this.getMap() )
		throw('Map object hasn\'t been attached to Overlay');
	
	this.position();
	
	google.maps.event.trigger(this, 'overlayLoaded');
}

MapMarkerOverlay.prototype.position = function()
{
	var overlayProjection = this.getProjection();
	var oPixelPosition = overlayProjection.fromLatLngToDivPixel(this.oPosition);

	// Resize the image's DIV to fit the indicated dimensions.
	if( $chk( oPixelPosition ) )
	{
		this.element.style.left = (oPixelPosition.x + this.oOffset.left) + 'px';
		this.element.style.top = (oPixelPosition.y + this.oOffset.top) + 'px';
	}
	else
		throw('Overlay hasn\'t been poisitioned...')
}

MapMarkerOverlay.prototype.onRemove = function()
{
	if( this.element )
		this.element.parentNode.removeChild(this.element);
	
	google.maps.event.trigger(this, 'overlayRemoved');
}

MapMarkerOverlay.prototype.setOffset = function( oOffset )
{
	if( !$defined( this.oOffset ) )
	{
		this.oOffset = new Object();
		this.oOffset.left = 0;
		this.oOffset.top = 0;
	}
	
	if( $defined( oOffset )  )
	{
		if( $defined( oOffset.left ) )
			this.oOffset.left = oOffset.left;
		if( $defined( oOffset.top ) )
			this.oOffset.top = oOffset.top;
	}
}
