//var markers = [];

function customOnload() {  

  onLoad();
  
  if (typeof(map) != "undefined" && map != null) {
    map.setZoom(12);
    if (markers.length > 0) {
      map.panTo(markers[0].getLatLng());
    }
  }
}

// @override
function createMarker(point, title, html, n, tooltip) {
  if(n >=0) { n = -1; }
  marker = setTwMarkers(map,point,title,html);   
  markers.push(marker);      
  return marker;  
}

function setTwMarkers(map,point,title,html) {
	var marker01 = machTwMarker01(point,title,html);
	map.addOverlay(marker01);	
	// (map, bild, bildgr��e, Gr��e innere Area im Bild, links-oben innere Area, ankerpunkt-marker, [true-nicht wegklickbar])
	//twGmapInfofenster01 = new EBubble(map, "infofenster.png", new GSize(400,400), new GSize(340,270), new GPoint(30,30), new GPoint(120,356));  
	//twGmapInfofenster01 = new EBubble(map, "infofenster.png", new GSize(230,100), new GSize(210,60), new GPoint(8,10), new GPoint(26,-50));
	twGmapInfofenster01 = new EBubble(map, "infofenster.png", new GSize(230,100), new GSize(210,60), new GPoint(8,10), new GPoint(12,-20));  
	//twGmapInfofenster01 = new EBubble(map, "infofenster.png", new GSize(230,100), new GSize(210,60), new GPoint(8,10), new GPoint(12,165));  
		
	return marker01;
}

function machTwMarker01(point,title,html) {
	var icon = new GIcon();
	icon.iconSize = new GSize(15, 19);
	icon.iconAnchor = new GPoint(7, 8);
	icon.infoWindowAnchor = new GPoint(7, 8);
	
	var randomIcons = new Array('schelleMarker.png', 'eichelMarker.png', 'herzMarker.png', 'laubMarker.png', 'laubMarker.png');
	icon.image = 'media/jassen/' + randomIcons[Math.floor(Math.random() * 4)];
    
    // Richtet das GMarkerOptions-Objektliteral ein
    markerOptions = { icon:icon,'title':title  };  

	var marker = new GMarker (point, markerOptions);
	
	// hier das eigene Infofenster �ffnen (anstatt das 'normale' von google)
	GEvent.addListener(marker, "click", function() {twGmapInfofenster01.openOnMarker(marker, html);});
	
	return marker;
}

function EBubble(map, image, size, insize, inset, anchor, noCloseOnClick) {
	// 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.noCloseOnClick = true;
	
	// 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";
	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:' +this.size.height+ 'px; width:'+this.size.width+'px; ' +loader+ '" ></div>';
	} else {
		this.div1.innerHTML = '<img src="' + this.image + '" width="' + this.size.width +'" height="' + this.size.height +'">';
	}
	*/
	
	// 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, "click", 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);

} 
      

/** Funktion openOnMap() */
EBubble.prototype.openOnMap = function(point, html, offset) {

	this.offset = offset||new GPoint(0,0);
	this.point = point;	
	
	//div2.style.backgroundColor = "#0000ff"; 
	this.div2.innerHTML = html;
	
	// 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
	this.div1.style.left = x+"px";
	this.div1.style.top = y+"px";
	      
	// und alles sichtbar machen
	this.visible = true;
	this.show();
}
      
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.div2.style.display="";
	this.visible = true;
}
      
EBubble.prototype.hide = function() {
	this.div1.style.display="none";
	//this.div2.style.display="none";
	this.visible = false;
}
      
EBubble.prototype.isHidden = function() {
	return !this.visible;
}
      
EBubble.prototype.supportsHide = function() {
	return true;
}




