// 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

function EBubble(map,insize,anchor,noCloseOnClick) {
	// parameters
	var that=this;
	this.map = map;
	this.insize=insize;
	this.anchor=anchor;
	this.noCloseOnClick=noCloseOnClick;
	// internal variables
	this.visible = false;
	// browser - specific variables
	this.ie = false;
	var agent = navigator.userAgent.toLowerCase();
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}

	this.div1 = document.createElement("div");

	this.div1.style.position = "absolute";
	this.div1.style.display="none";
	this.div1.className = "gs_bubble css-round css-shadow";
    $(this.map.getContainer()).up().appendChild(this.div1);

	// round corners and shadows for resultlist
	if(typeof pieAttachElements == "function") {
		
		PIE.attach(this.div1);
	}


	//document.body.appendChild(this.div1);

	// === Close the bubble if the map moves ===
	GEvent.addListener(map, "dragstart", function() {
		that.hide();
	} );
	GEvent.addListener(map, "moveend", function() {
		that.hide();
	} );
	GEvent.addListener(map, "click", function(overlay,  point) {
		if(that.marker != overlay) that.hide();
	} );


	this.div2 = document.createElement("div");
	this.div2.className = "gs_bubbleContent";

	this.closeDiv = document.createElement("div");
	this.closeDiv.className = "gs_bubbleClose";

	// === Listen for clicks and mousedowns ===
	GEvent.addDomListener(this.closeDiv, "click", function() {
		that.hide();
		GEvent.trigger(that,"click");
	});


	this.pointerDiv = document.createElement("div");
	this.pointerDiv.className = "gs_bubblePointer";

	this.div1.appendChild(this.div2);
	this.div1.appendChild(this.closeDiv);
	this.div1.appendChild(this.pointerDiv);

	this.div2.style.width = this.insize.width + "px";
	this.div2.style.height = this.insize.height + "px";
}


EBubble.prototype.setTimeoutFunc = function(func){
	GEvent.addDomListener(this.div1, 'mouseover', func);
	//GEvent.addDomListener(this.div1, 'click', func);
	//GEvent.addDomListener(this.div1, 'mousemove', func);

}
EBubble.prototype.setTimeoutFunc2 = function(func){
	GEvent.addDomListener(this.div1, 'mouseout', func);

}

EBubble.prototype.openOnMap = function(marker, html, offset, point) {
	//this.offset = offset||new GPoint(0,0);
	this.offset = new GPoint(0,0);

	this.marker = marker;

	if(point) {
		this.point = point;
	} else {
		this.point = marker.getPoint();
	}

	// round corners and shadows for resultlist
	if(typeof pieDetachElements == "function") {
		pieDetachElements($(this.div2).select('.css-round'));
	}

	//div2.style.backgroundColor = "#0000ff";
	this.div2.innerHTML = html;

	// round corners and shadows for resultlist
	if(typeof pieAttachElements == "function") {
		pieAttachElements($(this.div2).select('.css-round'));
	}


	// pixel relative to map world
	var p = this.map.fromLatLngToDivPixel(this.point);

	// map world relative to 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 relative to the page
	//y += this.map.getContainer().offsetTop;
	//x += this.map.getContainer().offsetLeft;

	// offset by the requested anchor position
	y -= this.anchor.y;
	x -= this.anchor.x;

	// offset by the specified offset position
	//y -= offset.y;
	//x -= offset.x;

	// Apply those values

	this.div1.style.left = x + "px";
	this.div1.style.top = y + "px";

	// make it visible
	this.visible = true;
	this.show();


}

EBubble.prototype.openOnMarker = function(marker,html,point) {
	var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
	var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
	this.openOnMap(marker, html, new GPoint(vx,vy),point);
}


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;
}


