// Version 0.0  Initial version 
// Version 0.1  10/10/2006 Added E_STYLE_7

      function EStyle(stemImage, stemSize, boxClass, boxOffset) {
        this.stemImage = stemImage;
        this.stemSize = stemSize;
        this.boxClass = boxClass;
        this.boxOffset = boxOffset;
        //this.border = border;
        
        // Known fudge factors are:
        // Firefox (1.0.6 and 1.5)    5, -1
        // IE 6.0                     0, -1
        // Opera 8.54                 3, -1
        // Opera 9 prev               4, -1
        // Netscape (7.2, 8.0)        5, -1
        // Safari                     5, -1        
        
        var agent = navigator.userAgent.toLowerCase();
        
        fudge = 13;  // assume Netscape if no match found
       
        if (agent.indexOf("opera") > -1) {
          fudge = 13;
        }   
        if (agent.indexOf("firefox") > -1) {
          fudge = 13;
        }   
        if (agent.indexOf("safari") > -1) {
          fudge = 13;
        }   
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
          fudge = 13;
        }
        this.fudge = fudge;
      }
      
      var E_STYLE_1 = new EStyle("Images/stem1.png", new GSize(81,87),  "estyle1", new GPoint(-30,87-3));
      var E_STYLE_2 = new EStyle("Images/stem2.png", new GSize(81,87),  "estyle2", new GPoint(-30,87-1));
      var E_STYLE_3 = new EStyle("Images/stem3.png", new GSize(81,87),  "estyle3", new GPoint(-30,87-10));
      var E_STYLE_4 = new EStyle("Images/stem3.png", new GSize(81,87),  "estyle4", new GPoint(-30,87-10));
      var E_STYLE_5 = new EStyle("Images/stem1.png", new GSize(81,87),  "estyle5", new GPoint(-30,87-3));
      var E_STYLE_6 = new EStyle("Images/stem6.png", new GSize(100,50), "estyle6", new GPoint(100-2,20));
      var E_STYLE_10 = new EStyle("Images/stem11.png", new GSize(12,9),  "estyle2", new GPoint(-150,-5));
      var E_STYLE_10R = new EStyle("Images/stem11-R.png", new GSize(12,9),  "estyle2", new GPoint(11,-200)); //change second to move with map
      var E_STYLE_10L = new EStyle("Images/stem11-L.png", new GSize(12,9),  "estyle2", new GPoint(-303,-200));

      function EWindow(map,estyle) {
        // parameters
        this.map=map;
        this.estyle=estyle;
        // 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}
      } 
      
      EWindow.prototype = new GOverlay();

      EWindow.prototype.initialize = function(map) {
        var div1 = document.createElement("div");
        div1.style.position = "absolute";
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
//        var div2 = document.createElement("div");
//        div2.style.position = "absolute";
//        div2.style.width = this.estyle.stemSize.width+"px";
//        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
        this.div1 = div1;
//        this.div2 = div2;
      }

      EWindow.prototype.openOnMap = function(point, html, offset,ewsize) {
        this.offset = offset||new GPoint(0,0);
        this.point = point;
        
        this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';

        if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
          var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
//          this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
        } else {
//          this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'">';
        }
        var z = GOverlay.getZIndex(this.point.lat());
        this.div1.style.zIndex = z;
//        this.div2.style.zIndex = z+1;
        this.div1.style.zIndex = 20;
//        this.div2.style.zIndex = 30;

        this.visible = true;
        this.show();

        this.redraw(true,ewsize);
      }
      
      EWindow.prototype.openOnMarker = function(marker,html,ewsize) {
        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),ewsize);
      }
      

      EWindow.prototype.redraw = function(force,ewsize) {
        if (!this.visible) {return;}
        
        var p = this.map.fromLatLngToDivPixel(this.point);
        var b = getBounds(map);
        var ne = this.map.fromLatLngToDivPixel(b[0]);
        var sw = this.map.fromLatLngToDivPixel(b[1]);
        
        mapNW = map.getCenter();
        centerpoint = map.fromLatLngToDivPixel(mapNW);
        
        //DETERMINE X AXIS
        if (p.x + -320 > sw.x){ //open left
        
            if (ewsize >= 400){ //adjust X for scrollbar
                this.estyle.boxOffset.x = -322;
                //editStem(div2, "Images/stem11-L.png");
            }else
            {
                //editStem(div2, "Images/stem11-L.png");
                this.estyle.boxOffset.x = -303;
            }
        }
        else { //open right
                //editStem(div2, "Images/stem11-R.png");
                this.estyle.boxOffset.x = 11;
        }
        
        //DETERMINE Y AXIS
        hewsize = -ewsize;
        var diff = ((hewsize + p.y) - ne.y) + 10;

        //alert("diff: " + diff + "  ewsize: " + hewsize + " p.y: " + p.y + " ne.y " + ne.y);
        if (hewsize + p.y < ne.y) { //window goes out of the screen on top
            
            finaly = diff - 50;
            
//            if (hewsize == -380){
//                this.estyle.boxOffset.y = parseFloat(finaly) + 78;
//            }
//            else {
                this.estyle.boxOffset.y = parseFloat(finaly);
//            }
            
        }
        else {

            this.estyle.boxOffset.y = -15;

        }
        
        //SET DIV OFFSETS
        this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
        this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
        
//        this.div2.style.left   = (p.x + this.offset.x) + "px";
//        this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";

      }

      EWindow.prototype.remove = function() {
//        this.div1.parentNode.removeChild(this.div1);
//        this.div2.parentNode.removeChild(this.div2);
        this.visible = false;
      }

      EWindow.prototype.copy = function() {
        return new EWindow(this.map, this.estyle);
      }

      EWindow.prototype.show = function() {
       this.div1.style.display="";
//        this.div2.style.display="";
        this.visible = true;
      }
      
      EWindow.prototype.hide = function() {
        this.div1.style.display="none";
//        this.div2.style.display="none";
        this.visible = false;
      }
      
      
    function getBounds(map){
        var ne=map.getBounds().getNorthEast();
        var sw=map.getBounds().getSouthWest();
        var bounds = [];
        bounds[0] = ne;
        bounds[1] = sw;
        return bounds;
    }
    
//    function editStem(div2, URL) {
//        var edit = div2.innerHTML.toString();
//        div2.innerHTML = "";
//        div2.innerHTML = edit.replace("src='Images/stem11-L.png'", "src='"+ URL +"'");
//        edit = null;
//    }

        
