// EBubble.js 
//
//   This Javascript is provided by Mike Williams and adjusted by Shafqat Ibrahim
//   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.5  27/10/2008 Multiple baloon instances done by Shafqat Ibrahim

      function EBubble(map,noCloseOnClick,idofdiv) {
        // parameters
		var that=this;
        this.map = map;
        this.noCloseOnClick=noCloseOnClick;
		this.idofdiv=idofdiv;
        // 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";
        document.getElementById(idofdiv).appendChild(this.div1);

        // === Close the bubble if the map moves ===
        GEvent.addListener(map, "dragstart", function() {
          that.hide();
        } );
        GEvent.addListener(map, "moveend", function() {
          that.hide();
        } );
        // === Listen for clicks and mousedowns ===
        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");
        });

        this.div2 = document.createElement("div");
        this.div1.appendChild(this.div2);

      } 
      

      EBubble.prototype.openOnMap = function(point, html, offset, imagebg, size, insize ,inset ,anchor) {
        offset = offset||new GPoint(0,0);
        point = point;
        //div2.style.backgroundColor = "#0000ff"; 
        this.div2.innerHTML = html;

        // pixel relative to map world
        var p = this.map.fromLatLngToDivPixel(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 -= anchor.y;
        x -= 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(imagebg , size, insize, inset, anchor, html);
      }
      
      EBubble.prototype.openOnMarker = function(marker,html,imagebg,size,insize,inset,anchor) {
        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), imagebg, size, insize ,inset ,anchor);
		
      }

      EBubble.prototype.show = function(imagebg, size, insize, inset, anchor, html) {
		  //build the baloon and show the baloon
		this.div1.innerHTML = '<img src="' + imagebg + '"width="' + size.width +'" height="' + size.height +'">';
        this.div1.style.display="";
        this.div2 = document.createElement("div");
        this.div1.appendChild(this.div2);
        this.div2.style.position = "absolute";
        this.div2.style.left = inset.x + "px"; 
        this.div2.style.top = inset.y + "px";
        this.div2.style.width = insize.width + "px";
        this.div2.style.height = insize.height + "px";
		this.div2.innerHTML = html;
		this.div2.style.display="";
		
        this.visible = true;
      }

      EBubble.prototype.hide = function() {
        this.div1.style.display="none";
        this.visible = false;
      }
      
      EBubble.prototype.isHidden = function() {
        return !this.visible;
      }
      
      EBubble.prototype.supportsHide = function() {
        return true;
      }
      
        