﻿function ContextMenu(latlng, arrContent, arrHandler)
{
    this.latlng = latlng;
    this.arrContent = arrContent;
    this.arrHandler = arrHandler;
    
    this.initialize = initialize;
    this.remove = remove;
    this.copy = copy;
    this.redraw = redraw;
    
    function initialize(map)
    {
        var myDiv = document.createElement("div");
        this.id = map.id + "_ContextMenu";
        myDiv.id = map.id + "_ContextMenu";
        myDiv.style.border = "1px #558800 solid";
        myDiv.style.position = "absolute";
        myDiv.style.width = "150px";
        myDiv.style.backgroundColor = "white";
        
        var strHTML = '';
        var menuHeight = 0;
        
        for (var i = this.arrContent.length - 1; i >= 0; i--)
        {
            if (this.arrContent[i] == '-')
            {
                strHTML += "<tr>";
                strHTML += "<td align='left' valign='middle' style='height:5px; background-image:url(images/bgpopup.gif);'></td>";
                strHTML += "</tr>";
                menuHeight += 5;
            }
            else
            {
                strHTML += "<tr>";
		        strHTML += "<td id='" + this.id + "_" +  i + "' index='" + i + "' align='left' valign='middle' style='height:20px;cursor:pointer;'>&nbsp;" + this.arrContent[i] + "</td>";
                strHTML += "</tr>";
                menuHeight += 20;
            }
        }
        
        strHTML = "<table width='130' border='0' align='center' cellpadding='0' cellspacing='0' style='width:140px; height:" + menuHeight + "px; margin-top:5px;'>" + strHTML;
        strHTML += "</table>";
        
        this.width = 150;
        this.height = menuHeight + 10;
        
        myDiv.style.height = this.height + 'px';
        
        myDiv.innerHTML = strHTML;
        map.getContainer().appendChild(myDiv);
        
        if (this.arrHandler != undefined)
        {
            for (var i = this.arrContent.length - 1; i >= 0 ; i--)
            {
                if (this.arrContent[i] != '-')
                {
                    var mnuItem = document.getElementById(this.id + "_" + i);
                    mnuItem.onmouseover = function(){
                            this.style.backgroundColor = '#e8fdcb';
                            this.style.color = '#000000';
                        };
                    mnuItem.onmouseout = function(){
                            this.style.backgroundColor = '#FFFFFF';
                            this.style.color = '#333333';
                        };
                    var mnuObj = this;
                    if (this.arrHandler[i] != undefined)
                    {
                        mnuItem.onclick = function(){
                                var i = (browserName == 'msie') ? this.index : this.getAttribute('index');
                                mnuObj.arrHandler[i](mnuObj.latlng);
                            };
                    }   
                }
            }
        }
        
        this.mapObj = map;
        this.divObj = myDiv;
        this.redraw();
    }
    
    function remove()
    {
        this.divObj.parentNode.removeChild(this.divObj);
    }
    
    function copy()
    {
        return new ContextMenu(this.latlng);
    }
    
    function redraw()
    {
        var pt = this.mapObj.fromLatLngToContainerPixel(this.latlng);
        var divContainer = this.mapObj.getContainer();
        var mnuLeft = pt.x;
        var mnuTop = pt.y;
        if (mnuLeft + this.width >= parseInt(divContainer.style.width))
            mnuLeft -= this.width;
        if (mnuTop + this.height >= parseInt(divContainer.style.height))
            mnuTop -= this.height;
        
        this.divObj.style.left = mnuLeft + "px";
        this.divObj.style.top = mnuTop + "px";
    }
}
ContextMenu.prototype = new VControl();
