// ******************************************************************************
// Main Classes
// ******************************************************************************

var EBubble = Class.create();
EBubble.prototype = {

	// ******************************************************************************
	// Constants
	// ******************************************************************************
	Version : '0.1',

	// dom ids
        // see options

	// ******************************************************************************
	// vars
	// ******************************************************************************
	map : null,

	visible : false,
	ie : false,

	// dom Elements
	bubble : null,
	content : null,
	close : null,


	// ******************************************************************************
	// Constructor
	// ******************************************************************************
	initialize: function(map, insize, inset, anchor, options) {
       	if((typeof Prototype=='undefined') ||
	    	(typeof Element == 'undefined') ||
	   	    (typeof Element.Methods=='undefined') ||
        	parseFloat(Prototype.Version.split(".")[0] + "." +
	           Prototype.Version.split(".")[1]) < 1.5)
	   		    throw('Das Google Maps EBubble benötigt das Prototype JavaScript framework >= 1.6.0');



		this.options = {
		    noCloseOnClick : false,
		    bubbleTemplate : false,

		    templateMarkers : {
			bubbleId : 'gs_bubble',
			contentId : 'gs_bubbleContent',
			closeId : 'gs_bubbleClose'
		    },

		    dummy: true
		};
		Object.extend(this.options, options);


		this.map = map;
		this.insize = insize;
		this.inset = inset;
		this.anchor = anchor;


		var agent = navigator.userAgent.toLowerCase();
		if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}



		// BubbleTemplate in document body einfügen
		$$('body').first().insert(this.options.bubbleTemplate.evaluate(this.options.templateMarkers));


		this.bubble = $(this.options.templateMarkers.bubbleId);
		this.close = $(this.options.templateMarkers.closeId);
		this.content = $(this.options.templateMarkers.contentId);



		Event.observe(this.close, 'click', this.hideHandler.bindAsEventListener(this), false);

		GEvent.addListener(map, 'dragstart', this.hide.bind(this) );
		GEvent.addListener(map, 'moveend', this.hide.bind(this) );
		GEvent.addListener(map, 'click', function(overlay,  point) {
		    if(this.marker != overlay) this.hide();
		}.bind(this) );

   	},



	setTimeoutFunc : function(func){
	    GEvent.addDomListener(this.bubble, 'mouseover', func);
	    //GEvent.addDomListener(this.div1, 'click', func);
            //GEvent.addDomListener(this.div1, 'mousemove', func);

	},
	setTimeoutFunc2 : function(func){
	    GEvent.addDomListener(this.bubble, 'mouseout', func);
	},



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


            this.content.innerHTML = html;

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

            y += $(this.map.getContainer()).cumulativeOffset().top;
            x += $(this.map.getContainer()).cumulativeOffset().left;



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


	    x -= this.bubble.getWidth()/2-1;
	    y -= this.bubble.getHeight();

	    ua   = navigator.userAgent.toLowerCase();
	    ie6  = (ua.indexOf("msie") && document.all && ua.indexOf("netscape") == -1);

	    if(ie6) {
		x=x-18;
	    }


            // Apply those values
            this.bubble.style.left = x+"px";
            this.bubble.style.top = y+"px";

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

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


	show : function() {
	    this.bubble.show();
//            this.content.style.display="";
            this.visible = true;
	},


	hideHandler : function(ev) {
	    ev.stop();
	    this.hide();
	},

	hide : function() {
	    this.bubble.hide();
            this.visible = false;
	},

	isHidden : function() {
	    return !this.visible;
	},

	supportsHide : function() {
	    return true;
	},





	dummy : function() {

	}
};
