/*
  popupmenu.js - simple JavaScript popup menu library.

  Copyright (C) 2007 Jiro Nishiguchi <jiro@cpan.org> All rights reserved.
  This is free software with ABSOLUTELY NO WARRANTY.

  You can redistribute it and/or modify it under the modified BSD license.

  Usage:
    var popup = new PopupMenu();
    popup.add(menuText, function(target){ ... });
    popup.addSeparator();
    popup.bind('targetElement');
    popup.bind(); // target is document;
*/
var PopupMenu = function(backgroundColor, fontColor) {
    this.init(backgroundColor, fontColor);
}
PopupMenu.SEPARATOR = 'PopupMenu.SEPARATOR';
PopupMenu.current = null;
PopupMenu.addEventListener = function(element, name, observer, capture) {
    if (typeof element == 'string') {
        element = GetElement(element);
    }
    if (element.addEventListener) {
        element.addEventListener(name, observer, capture);
    } else if (element.attachEvent) {
        element.attachEvent('on' + name, observer);
    }
};
PopupMenu.prototype = {
    init: function(backgroundColor, fontColor) {
        this.items  = [];
        this.width  = 0;
        this.height = 0;
        this.background = backgroundColor;
        this.color = fontColor;
    },
    setSize: function(width, height) {
        this.width  = width;
        this.height = height;
        if (this.element) {
            var self = this;
            with (this.element.style) {
                if (self.width)  width  = self.width  + 'px';
                if (self.height) height = self.height + 'px';
            }
        }
    },
    bind: function(element) {
        var self = this;
        if (!element) {
            element = document;
        } else if (typeof element == 'string') {
            element = GetElement(element);
        }
        this.target = element;
        this.target.oncontextmenu = function(e) {
            self.show.call(self, e);
            return false;
        };
        var listener = function() { self.hide.call(self) };
        PopupMenu.addEventListener(document, 'click', listener, true);
    },
    add: function(text, callback) {
        this.items.push({ text: text, callback: callback });
    },
    addSeparator: function() {
        this.items.push(PopupMenu.SEPARATOR);
    },
    SetPosition: function(e) 
    {
        if (!this.element) return;
        if (!e) e = window.event;
        var x, y;
        if (window.opera) 
        {
            x = e.clientX;
            y = e.clientY;
        } 
        else if (document.all) 
        {
            x = e.x; // document.body.scrollLeft + e.x;
            y = e.y; // document.body.scrollTop + e.y;
        } 
        else if (document.layers || document.getElementById) 
        {
            x = e.x;
            y = e.y;
       }
      
        this.element.style.top  = y + 'px';
        this.element.style.left = x + 'px';
    },
    show: function(e) 
    {
        if (PopupMenu.current && PopupMenu.current != this) return;
        PopupMenu.current = this;
        if (this.element) 
        {
            this.SetPosition(e);
            this.element.style.display = '';
        } 
        else 
        {
            this.element = this.createMenu(this.items);
            this.SetPosition(e);
            document.body.appendChild(this.element);
        }
    },
    hide: function() {
        PopupMenu.current = null;
        if (this.element) this.element.style.display = 'none';
    },   
    createMenu: function(items) {
        var self = this;
        var menu = document.createElement('div');
        with (menu.style) {
            if (self.width)  width  = self.width  + 'px';
            if (self.height) height = self.height + 'px';
            border     = "1px solid gray";
            background = self.background;
            color      = self.color;
            position   = 'absolute';
            display    = 'block';
            padding    = '2px';
            cursor     = 'default';
        }
        for (var i = 0; i < items.length; i++) {
            var item;
            if (items[i] == PopupMenu.SEPARATOR) {
                item = this.createSeparator();
            } else {
                item = this.createItem(items[i]);
            }
            menu.appendChild(item);
        }
        return menu;
    },
    createItem: function(item) 
    {
        var self = this;
        var elem = document.createElement('div');
        elem.style.padding = '4px';
        var callback = item.callback;
        PopupMenu.addEventListener(elem, 'click', function(_callback) 
        {
            return function() 
            {
                self.hide();
                _callback(self.target);
            };
        }
        (callback), true);
        PopupMenu.addEventListener(elem, 'mouseover', function(e) {elem.style.background = '#B6BDD2';}, true);
        PopupMenu.addEventListener(elem, 'mouseout', function(e) {elem.style.background = '#FCCD25';}, true);
        elem.appendChild(document.createTextNode(item.text));
        return elem;
    },
    createSeparator: function() 
    {
        var sep = document.createElement('div');
        with (sep.style) 
        {
            borderTop = '1px dotted ' + this.color;
            fontSize  = '0px';
            height    = '0px';
        }
        return sep;
    }
};

