//创建一个正则表达式
function hasClass(target, name) {
    return target.className.match(new RegExp('(\\s|^)' + name + '(\\s|$)'));
}
//删除与正则表达式匹配的节点的样式
function removeClass(target, name) {
    if (hasClass(target, name)) {
        target.className = target.className.replace(new RegExp('(\\s|^)' + name + '(\\s|$)'), ' ');
    }
}
//添加与正则表达式匹配的节点的样式
function addClass(target, name) {
    if (!hasClass(target, name)) {
        target.className += " " + name;
    }
}

function MenuControl(map) {
    this.container = document.createElement("div");//创建菜单div(container 容器)即自定义控件
    this.coordinate = null;//坐标 coordinate
    this.map = map;
    this.sender = null;//sender发送人
    this.isEnable = true;//是否可用
    this.container.className = "marker_ceng";//菜单样式
    this.hide();
    this.items = new Array();//菜单项
    var self = this;
    google.maps.event.addListener(map, "click", function() {//单击地图时隐藏菜单
        self.hide();
    });
    google.maps.event.addListener(map, "movestart", function() {//隐藏菜单
        self.hide();
    });
    google.maps.event.addListener(map, "zoom_changed", function() {//隐藏菜单
        self.hide();
    });
    google.maps.event.addListener(map, "dragstart", function() {//隐藏菜单
        self.hide();
    });
    this.container.index = 1;//设置显示style="index:1"
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(this.container);//定位自定义控件
}
//显示菜单控件
MenuControl.prototype.show = function(groupName) {//显示指定组下的菜单项
    this.container.style.display = "block";
    if (groupName) {
        for (var i = 0; i < this.items.length; i++) {
            if (groupName == this.items[i].groupName) {
                this.items[i].show();
            } else {
                this.items[i].hide();
            }
        }
    }
}
//隐藏菜单控件
MenuControl.prototype.hide = function(groupName) {//隐藏指定组下的菜单项
    this.container.style.display = "none";
    if (groupName) {
        for (var i = 0; i < this.items.length; i++) {
            if (groupName == this.items[i].groupName) {
                this.items[i].hide();
            } else {
                this.items[i].show();
            }
        }
    }
}

MenuControl.prototype.enable = function() {
    this.isEnable = true;
}

MenuControl.prototype.disable = function() {
    this.isEnable = false;
}
//菜单控件是否已经被隐藏
MenuControl.prototype.isHide = function() {
    return (this.container.style.display == "none");
}
//添加菜单项
MenuControl.prototype.addItem = function(item) {
    item.host = this;
    this.items.push(item);
    this.container.appendChild(item.casing);
}

MenuControl.prototype.addSeparator = function(group) {
    var separator = group || new MenuSeparator();
    if (typeof group == "string") {
        separator = new MenuSeparator(group);
    }
    this.items.push(separator);
    this.container.appendChild(separator.casing);
}

//未指定组添加分隔符
function MenuSeparator(groupName) {
    this.groupName = groupName;
    this.casing = document.createElement("div");
    this.casing.className = "menu_separator";
}
//显示分隔符
MenuSeparator.prototype.show = function() {
    this.casing.style.display = "block";
}
//隐藏分隔符
MenuSeparator.prototype.hide = function() {
    this.casing.style.display = "none";
}

function MenuItem(options) {
    options = options || {};
    this.text = options.text || "";
    this.icon = options.icon;
    this.handler = options.handler;
    this.groupName = options.groupName;
    this.host = null; //宿主

    this.casing = document.createElement("div");
    this.casing.className = "marker_ceng_1";

    var menu_text = document.createElement("a");
    menu_text.className = "";
    var text_lable = document.createElement("span");
    text_lable.innerHTML = this.text;
    menu_text.appendChild(text_lable);
    this.casing.appendChild(menu_text);

    var self = this;
    if (this.icon) {
        var item_icon = document.createElement("div");
        item_icon.className = "menu_icon";
        item_icon.style.backgroundImage = "url(" + self.icon + ")";
        self.casing.appendChild(item_icon);
    }

    if (typeof self.handler == "function") {
        google.maps.event.addDomListener(self.casing, "click", function() {
            if (self.host) {
                self.handler(self.host.coordinate);
                self.host.hide();
            }
        });
    }
    google.maps.event.addDomListener(self.casing, "mouseover", function() {
        addClass(self.casing, "item_active");
    });

    google.maps.event.addDomListener(self.casing, "mouseout", function() {
        removeClass(self.casing, "item_active");
    });
}

MenuItem.prototype.show = function() {
    this.casing.style.display = "block";
}

MenuItem.prototype.hide = function() {
    this.casing.style.display = "none";
}