/*******************************************************************************
	EasyWEB	5

	@Copyright 	Synerway Sp. z o. o. http://www.synerway.pl/
			All rights reserved

	@Author		Antoni Jakubiak <a.jakubiak@synerway.pl>


	@Description	Powiekszanie zdjec dla hoteli na liscie

	$Id: placemap.js,v 1.1.4.2 2007/02/19 11:51:50 mgil Exp $
*******************************************************************************/

function PlaceMap( iOpenerElement, iLat, iLng, iZoom, iOpenNow ) {
	this.lat                   = iLat;
	this.lng                   = iLng;
	this.zoom                  = iZoom;
	this.openerElement         = iOpenerElement;
	this.mapElement            = null;
	this.active                = false;
	var oThis                  = this;
	var timeout                = null;
	var tmpFnShow              = function() {
		oThis.show();
	}

	this.openerElement.onmouseover = function() {
		if ( ! oThis.active ) {
			oThis.timeout = setTimeout( tmpFnShow, 500 );
		}
	}
	this.openerElement.onmouseout = function() {
		clearTimeout( oThis.timeout );
		if ( oThis.active ) {
			oThis.hide();
		}
	}
	this.openerElement.onmousemove = function() {
//		clearTimeout( oThis.timeout );
	}
	if ( iOpenNow ) {
		this.openerElement.onmouseover();
	}
}

/**
 * Pokazywanie powiekszonego zdjecia
 */
PlaceMap.prototype.show = function() {
	if (GBrowserIsCompatible()) {
		if ( ! this.mapElement ) {
			this.mapElement = document.createElement( 'div' );
			this.mapElement.className = 'map';
			this.openerElement.parentNode.appendChild( this.mapElement );
		}
		this.position                 = this.getAbsolutePos( this.openerElement );
		this.mapElement.style.left    = ( this.position[0] - 1 + 20 ) + 'px';
		this.mapElement.style.top     = ( this.position[1] - 1 + 40 ) + 'px';
		this.mapElement.style.display = 'block';
		this.active                   = true;
		var map   = new GMap2( this.mapElement );
		var point = new GLatLng( this.lat, this.lng );
		map.setCenter( point, this.zoom );
		var marker = new GMarker(point, {draggable: true});
		GEvent.addListener(marker, "dragstart", function() {
				map.closeInfoWindow();
				});

		GEvent.addListener(marker, "dragend", function() {
				var p = marker.getPoint()
				marker.openInfoWindowHtml( 'lat: ' + p.lat() + '<br>lng: ' + p.lng() );
				});

		map.addOverlay(marker);
	}

}
/**
 * Ukrywanie powiekszonego zdjecia
 */
PlaceMap.prototype.hide = function() {
	this.mapElement.style.display='none';
	GUnload();
	this.active = false;
}

/**
 * Zmiana statusu mapy, ukrywanie pokazywanie
 */
PlaceMap.prototype.showHide = function() {
	if ( this.active ) {
		this.hide();
	} else {
		this.show();
	}
}
/**
 * Absolutna pozycja elementu
 */
PlaceMap.prototype.getAbsolutePos = function( oElem ) {
	var oTemp = typeof oElem == 'string' ? document.getElementById( oElem ) : oElem;
	oElem = oTemp;
	var absLeft = 0, absTop = 0;
	while ( oTemp ) {
		absTop += oTemp.offsetTop;
		absLeft += oTemp.offsetLeft;
		oTemp = oTemp.offsetParent;
	}
	return new Array(absLeft, absTop, absLeft + oElem.offsetWidth, absTop + oElem.offsetHeight );
}
