function Menu(classname, items) {
    this.classname = classname;
    this.items = items;
    return this;
}
var _MAX_WIDTH=100;
function position(e, shadowElement, parentContainer) {

    var offset = parentContainer.getBoundingClientRect();
    var positions = shadowElement.getBoundingClientRect();

/*
    console.log('e', e);
    console.log('shadowElement', shadowElement);
    console.log('parentContainer', parentContainer);
    console.log('offset of parentContainer', offset);
    console.log('positions of shodowElement', positions);
    console.log('window.pageYOffset', window.pageYOffset);
    console.log('document', document.body.clientWidth, document.documentElement.scrollTop);
*/

    /*to compensate for the exceed width of screen*/
    var compensateX = (positions.left + positions.width + _MAX_WIDTH) > document.body.clientWidth ? positions.width + _MAX_WIDTH : 0;
    var compensateY = (positions.top + positions.height/2 + _MAX_WIDTH) > document.body.clientHeight ? positions.height/2 + _MAX_WIDTH/2 : 0;

    /* 20200108, Fix bugs: wrong position display when  page scroll */
    e.style.top = positions.top + (positions.height/2) - offset.top - compensateY + 'px';
    e.style.left = positions.left + positions.width - offset.left - compensateX + 'px';
}

/**
 * e : Input icon element
 *
 */
Menu.prototype.create = function(e,style={position:'absolute', width:_MAX_WIDTH}, parentContainer = document.body) {
    var d = document;

    const ul = d.createElement('ul');
    ul.className = this.classname;

    /* Input style overriding */
    for (var k in style) {
        eval('ul.style.' + k + ' = "' + style[k] + '";');
    }

    for (var k in this.items) {
        var li = d.createElement('li');
        li.className = 'item';
        li.innerHTML = k;
        li.id = e.id;
        callback = this.items[k];

        ['click', 'touchend'].forEach(function (event) {
            li.addEventListener(event, callback, false);
        });

        ul.appendChild(li);
    }

    position(ul, e, parentContainer);

    d.body.appendChild(ul);
};
