// twGmapInfofenster.js
//
// EBubble.js 
//
//   This Javascript is provided by Mike Williams
//   Blackpool Community Church Javascript Team
//   http://www.commchurch.freeserve.co.uk/   
//   http://econym.googlepages.com/index.htm
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.0  13/07/2007 Initial version
// Version 0.1  14/07/2007 Bugfix: Was failing to apply the position offset.
// Version 0.2  30/07/2007 Bugfix: Wasn't clearingout the old contents.
// Version 0.3  21/09/2007 Added noCloseOnClick parameter
// version 0.4  25/12/2007 Bugfix: Problem with offset
//
// version 0.4.1 03/11/2008 Übersetzung und Anpassung von Thomas Weise 
//                          (http://progtw.de/website/twGmap)


/** Konstruktor */
function EBubble(map, image, size, insize, inset, anchor, noCloseOnClick, uid, i) {
	// Parameter zum Erstellen des Info-Fensters
	var that = this;
	this.map            = map;             // unsere Google Map
	this.image          = image;           // die URL für das Hintergrundbild
	this.size           = size;            // die Größe dieses Hintergrundbildes
	this.insize         = insize;          // Größe der inneren Area (für den Fensterinhalt)
	this.inset          = inset;           // lnke obere Ecke dieser inneren Area
	this.anchor         = anchor;          // Ankerpunkt (die Spitze die auf den Marker zeigt)
	this.noCloseOnClick = noCloseOnClick;  // true, wenn dieses Info-Fenster nicht wegklickbar sein soll (optional)

	this.loaded         = false;
	this.t3uid          = uid;
	this.index			= i;

	// interne Variablen (unter Beachtung des IE ;-) )
	this.visible = false;
	this.ie      = false;
	var agent    = navigator.userAgent.toLowerCase();	
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)) { 
		this.ie = true; } 
	else {
		this.ie = false;
	}
	
	// erstes div
	this.div1                = document.createElement("div");
	this.div1.style.position = "absolute";
	//this.div1.style.display  = "none";
	this.div1.style.visibility="hidden";
	this.div1.className='gs_bubble';
	document.body.appendChild(this.div1);

/*
	// wieder mal IE (kann keine Transparenz)
	if (this.ie && this.image.indexOf(".png")>-1) {
		var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.image+"', sizingMethod='scale');";
		this.div1.innerHTML = '<div style="height:1px; width:'+this.size.width+'px; ' +loader+ '" ></div>';
	} else {
		this.div1.innerHTML = '<img src="' + this.image + '" width="' + this.size.width +'" />';
	}
*/
	// Infofenster schließen, wenn sich die Map verschoben wird
	GEvent.addListener(map, "dragstart", function() {
		that.hide();
	} );
	GEvent.addListener(map, "moveend", function() {
		that.hide();
	} );
	
	// Klicks und 'mousedown's abfangen
	GEvent.addDomListener(this.div1, "dblclick", function() { if (!that.noCloseOnClick) { that.hide(); } GEvent.trigger(that, "click"); });

	//GEvent.addDomListener(this.div1, "mousedown", function() { if (!that.noCloseOnClick) { that.hide(); } GEvent.trigger(that, "click"); });

/*
	// zweites div
	this.div2 = document.createElement("div");
	this.div1.appendChild(this.div2);
	//this.div2.style.position = "absolute";
	this.div2.style.left     = this.inset.x + "px"; 
	this.div2.style.top      = this.inset.y + "px";
	this.div2.style.width    = this.insize.width + "px";
	//this.div2.style.height   = 'auto';
	this.div2.className = 'gs_bubble_i';
*/	
} 
      
var bubbleActive=false;
/** Funktion openOnMap() */
EBubble.prototype.openOnMap = function(point, html, offset) {
	if(bubbleActive) {
		bubbleActive.hide();
	}
	this.offset = offset||new GPoint(0,0);
	this.point = point;	
	
	//div2.style.backgroundColor = "#0000ff"; 
	this.div1.innerHTML = html;
	if(!this.loaded) {
		/*
        var url = "index.php";
        var pars = "eID=tx_badlocations&showUid="+this.t3uid+"&i="+this.index;       
        var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: this.getData});
		*/
	}

	// Pixel relativ zur Map
	var p = this.map.fromLatLngToDivPixel(point);
	
	// Abstand Map relativ zum Map-Container
	var dragObject = this.map.getPane(G_MAP_MAP_PANE).parentNode;
	var x = p.x + parseInt(dragObject.style.left);
	var y = p.y + parseInt(dragObject.style.top);
	
	// Map-Container relativ zur Seite
	y += this.map.getContainer().offsetTop;
	x += this.map.getContainer().offsetLeft;
	        
	// Offset der gefragten Anker-Position

	y -= this.anchor.y;
	x -= this.anchor.x;
	
	// Offset der speziellen Offset-Position
	y -= this.offset.y;
	x -= this.offset.x;


	// Abstände setzen
	
	y-=this.div1.offsetHeight;
	//y+=9;
	x-=this.div1.offsetWidth/2;;
//alert(this.div1.offsetHeight);	
	this.div1.style.left = x+"px";
	this.div1.style.top = y+"px";
	      
	// und alles sichtbar machen
	this.visible = true;
	this.show();
	bubbleActive=this;
}
EBubble.prototype.getData = function(orgRequest) {

    var xmldoc = orgRequest.responseXML;
	var result = xmldoc.getElementsByTagName("data")[0].firstChild.data;

    if(result) {
		result=result.replace(/###URL1###/,gmapMarkers[this.index][10]);
		result=result.replace(/###URL1TXT###/,gmapTxtLang[0]["location"]);
		result=result.replace(/###URL2###/,gmapMarkers[this.index][11]);
		result=result.replace(/###URL2TXT###/,gmapTxtLang[0]["request"]);
		result=result.replace(/###LANGPHONE###/,gmapTxtLang[0]["phone"]);
		result=result.replace(/###LANGFAX###/,gmapTxtLang[0]["fax"]);
        //this.div1.innerHTML = result;
		this.loaded=true;
    }	
}
EBubble.prototype.openOnMarker = function(marker, html) {
	var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
	var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y; 
	this.openOnMap(marker.getPoint(), html, new GPoint(vx, vy));
}
      

EBubble.prototype.show = function() {
	//this.div1.style.display="";
	this.div1.style.visibility="visible";
	//this.div2.style.display="";
	this.visible = true;
}
      
EBubble.prototype.hide = function() {
	//this.div1.style.display="none";
	this.div1.style.visibility="hidden";
	//this.div2.style.display="none";
	this.visible = false;
}
      
EBubble.prototype.isHidden = function() {
	return !this.visible;
}
      
EBubble.prototype.supportsHide = function() {
	return true;
}
