/** Google-map extension **/

function MiMap(lat, lng, zoom, title, sticker, width, height)
{
    this.lat = lat;
    this.lng = lng;
    this.zoom = zoom;
    this.title = title;
    this.sticker = sticker;
    this.isInited = false;
    this.elScreen = null;
    this.elMap = null;
    this.elClose = null;
    this.width = width;
    this.height = height;
    this.map = null;
    this.evtPoint = null;
    this.evtMarkerOpts = null;
    this.evtMarker = null;
}


MiMap.prototype.getDocMetrics = function()
{
	return getDocMetrics();
}


MiMap.prototype.init = function()
{
    this.elScreen = document.createElement( "div" );
    this.elScreen.style.position = "absolute";
    this.elScreen.style.display = "none";
    this.elScreen.style.background = "#ffffff";
    this.elScreen.style.opacity = 0.7;
    this.elScreen.style.filter = "alpha(opacity=70)";
    if( this.elScreen.style.setAttribute )
	this.elScreen.style.setAttribute( "-moz-opacity", "0.7" );
    this.elScreen.style.width = "1px";
    this.elScreen.style.height = "1px";
    this.elScreen.style.zIndex = 1001;
    document.body.appendChild( this.elScreen );

    this.elMap = document.createElement( "div" );
    this.elMap.style.display = "block";
    this.elMap.style.position = "absolute";
    this.elMap.style.width = this.width + "px";
    this.elMap.style.height = this.height + "px";
    this.elMap.style.border = "solid 1px #5a953d";
    this.elMap.style.zIndex = 1010;
    document.body.appendChild( this.elMap );

    this.elClose = document.createElement( "div" );
    this.elClose.style.display = "none";
    this.elClose.style.position = "absolute";
    this.elClose.style.width = (this.width + 2) + "px";
    this.elClose.style.height = "22px";
    this.elClose.style.zIndex = 1010;
    this.elClose.style.textAlign = "center";
    this.elClose.style.background = "url(/layout/mapclosebg.gif)";
    this.elClose.innerHTML = "<A href=\"javascript:mimap.hide();\"><img src=\"/layout/mapclose.gif\" width=\"18\" height=\"18\" align=\"center\" border=\"0\" /></A>";
    document.body.appendChild( this.elClose );

    this.positionElements();

    this.map = new GMap2( this.elMap, this.mapOptions );
    this.map.addControl( new GSmallMapControl() );
    this.evtPoint = new GLatLng( this.lat, this.lng );
    this.map.setCenter( this.evtPoint, this.zoom );
    this.map.setMapType( G_PHYSICAL_MAP );

    this.evtMarkerOpts = {
	icon: null,
	dragCrossMove: false,
	title: this.title,
	clickable: true,
	draggable: false,
	bouncy: false,
	bounceGravity: false
    };

    this.evtMarker = new GMarker( this.evtPoint, this.evtMarkerOpts );
    this.map.addOverlay( this.evtMarker );

    this.evtMarker.openInfoWindowHtml( this.sticker );
    GEvent.addListener( this.evtMarker, "click", function() {
	irmap.evtMarker.openInfoWindowHtml( irmap.sticker );
    } );
}

MiMap.prototype.positionElements = function()
{
    var mtx = this.getDocMetrics();

    // full
    this.elScreen.style.left = mtx.offX + "px";
    this.elScreen.style.top = mtx.offY + "px";
    this.elScreen.style.width = mtx.width + "px";
    this.elScreen.style.height = mtx.height + "px";

    // center
    this.elMap.style.left = (mtx.offX + ((mtx.width - this.width)/2)) + "px";
    this.elMap.style.top = (mtx.offY + ((mtx.height - this.height)/2)) + "px";

    // below
    this.elClose.style.left = (mtx.offX + ((mtx.width - this.width)/2)) + "px";
    this.elClose.style.top = ((mtx.offY + ((mtx.height - this.height)/2)) + this.height) + "px";
}

MiMap.prototype.show = function()
{
    if( !GBrowserIsCompatible() ) {
	alert( "Impossibile visualizzare la mappa" );
	return;
    }

    if( !this.isInited ) {
	this.init();
    }

   if( document.documentElement !== undefined ) {
        document.documentElement.style.overflow = "hidden";
        document.body.style.overflow = "hidden";
    } else {
        document.body.style.overflow = "hidden";
    }

    this.positionElements();
    this.map.setCenter( this.evtPoint, this.zoom );

    this.elScreen.style.display = "block";
    this.elMap.style.display = "block";
    this.elClose.style.display = "block";
 }

MiMap.prototype.hide = function()
{
    this.elScreen.style.display = "none";
    this.elMap.style.display = "none";
    this.elClose.style.display = "none";

    if( document.documentElement !== undefined ) {
	document.documentElement.style.overflow = "";
	document.body.style.overflow = "";
    } else {
	document.body.style.overflow = "";
    }
}
