// Version 0.0  Initial version 
// Version 0.1  10/10/2006 Added E_STYLE_7

var mapPadding = 5;
/*
var E_STYLE_1 = new EStyle("/images/stem1.png", "/images/stem1M.png", new GSize(81,87),  "estyle1", new GPoint(-30,87-3));
var E_STYLE_2 = new EStyle("/images/stem2.png", "/images/stem2M.png", new GSize(81,87),  "estyle2", new GPoint(-30,87-1));
var E_STYLE_3 = new EStyle("/images/stem3.png", "/images/stem3M.png", new GSize(81,87),  "estyle3", new GPoint(-30,87-10));
var E_STYLE_4 = new EStyle("/images/stem3.png", "/images/stem3M.png", new GSize(81,87),  "estyle4", new GPoint(-30,87-10));
var E_STYLE_5 = new EStyle("/images/stem1.png", "/images/stem1M.png", new GSize(81,87),  "estyle5", new GPoint(-30,87-3));
var E_STYLE_6 = new EStyle("/images/stem6.png", "/images/stem6M.png", new GSize(100,50), "estyle6", new GPoint(100-2,20));
*/
var E_STYLE_7 = new EStyle(
    "/images/stem7NE.png",
    "/images/stem7SE.png",
    "/images/stem7SW.png",
    "/images/stem7NW.png",
    new GSize(24,24),
    "estyle2",
    new GPoint(-10,23)
);


function EStyle(stemImageNE, stemImageSE, stemImageSW, stemImageNW, stemSize, boxClass, boxOffset) {
    this.stemImageNE = stemImageNE;
    this.stemImageSE = stemImageSE;
    this.stemImageSW = stemImageSW;
    this.stemImageNW = stemImageNW;
    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();

    var fudge = 5;  // assume Netscape if no match found

    if (agent.indexOf("opera") > -1)
    {
        fudge = 3;
    }   
    if (agent.indexOf("firefox") > -1)
    {
        fudge = 5;
    }   
    if (agent.indexOf("safari") > -1)
    {
        fudge = 5;
    }   
    if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1))
    {
        fudge = 0;
    }
    this.fudge = fudge;
}

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)
{
    this.offset = offset || new GPoint(0,0);
    this.point = point;
    //this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '">' + html + '</div>';
    this.div1.innerHTML = html;
    this.div1.className = this.estyle.boxClass;

    var z = GOverlay.getZIndex(this.point.lat());
    this.div1.style.zIndex = z;
    this.div2.style.zIndex = z + 1;
    this.visible = true;
    this.show();
    this.redraw(true);
}

EWindow.prototype.loadStemImage = function(stem)
{
    var image = "";
    if (stem.vDirection == "N")
    {
        if (stem.hDirection == "E")
        {
            image = this.estyle.stemImageNE;
        }
        else
        {
            image = this.estyle.stemImageNW;
        }
    }
    else
    {
        if (stem.hDirection == "E")
        {
            image = this.estyle.stemImageSE;
        }
        else
        {
            image = this.estyle.stemImageSW;
        }
    }

    if (this.ie && image.toLowerCase().indexOf(".png") > -1)
    {
        var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image + "', 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="' + image + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'">';
    }
}

EWindow.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));
}


EWindow.prototype.redraw = function(force)
{
    if (!this.visible)
    {
        return;
    }

    var stem = {
        vDirection: "S",
        hDirection: "W"
    }

    var p = this.map.fromLatLngToDivPixel(this.point);
    var stemLeft = (p.x + this.offset.x);
    this.div2.style.bottom = (-p.y + this.offset.y - this.estyle.fudge) + "px";

    // If necessary, reposition the box
    var boxLeft = (p.x + this.offset.x + this.estyle.boxOffset.x);
    var boxWidth = this.div1.offsetWidth;
    if (typeof(boxWidth) != "undefined" && !isNaN(boxWidth))
    {
        // Determine whether it is displayed inside window
        var xOffset = (this.center.x - this.initCenter.x);
        if (boxLeft > (this.map.getSize().width - boxWidth - mapPadding + xOffset))
        {
            boxLeft = (this.map.getSize().width - boxWidth - mapPadding + xOffset);
            if (this.map.fromLatLngToDivPixel(this.point).x - this.estyle.stemSize.width > boxLeft)
            {
                // flip the stem
                stem.hDirection = "E";
                stemLeft -= this.estyle.stemSize.width;
            }
        }
    }
    
    this.div1.style.left = boxLeft + "px";

    var boxBottom = (-p.y + this.offset.y + this.estyle.boxOffset.y - this.estyle.fudge);
    this.div1.style.bottom = boxBottom + "px";

    var boxHeight = this.div1.offsetHeight;
    if (typeof(boxHeight) != "undefined" && !isNaN(boxHeight))
    {
        // Determine whether it is displayed inside window
        var yOffset = (this.center.y - this.initCenter.y);
        if ((boxBottom + boxHeight + yOffset) > 0)
        {
            boxBottom -= (boxHeight + 2 * this.estyle.boxOffset.y);
            // if necessary, reposition
            this.div1.style.bottom = boxBottom + "px";
            // flip the stem
            stem.vDirection = "N";
            this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge - this.estyle.stemSize.height) + "px";
        }
    }

    this.div2.style.left = stemLeft + "px";
    this.loadStemImage(stem);
}

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;
}



