
generic.popupMessage = Class.create (Widget,
{

    // summary:
    //Pop-up messages
    
    is_open: false,
    displayDuration: null,
    position: {},
    
    initialize: function($super, /* Object */argsObj, useWidget) {
        //console.log("generic.popupMessage.init: "); 
        if (useWidget) {
       	$super(argsObj); //identify this.popup yourself
        } else {
        	this.popupMessageInit(argsObj);
        } 
    }, 
    popupMessageInit: function(args) {
         console.log("generic.popupMessage.popupMessageInit"); 
         if (!args) return false;
         if (!args.popup) return false;
      
        this.popup = args.popup;
        this.displayDuration = args.displayDuration;
        var self = this;
        
        // set open/close handlers
        // allow no btn trigger for cases where popup display is triggered by an external event or callback
        if (args.trigger) {
            args.trigger.observe('click', self.show.bind(self));
        }
        if (args.buttonClose) {
            this.buttonClose = args.buttonClose
            this.buttonClose.observe("click", self.close.bind(self));
        } else {
        	this.buttonClose = null;
        }
        if(args.position) {
            if(args.position.top) {
                this.position.top = args.position.top;
            }
            if(args.position.left) {
                this.position.left = args.position.left;
            }
        }
    },
    open: function() {
        if (this.is_open) { return; } 
        this.popup.style.top = this.position.top ? this.position.top : "0";
        this.popup.style.left = this.position.left ? this.position.left : "0";
        this.is_open = true;
    },

    show: function() {
        //console.log("generic.popupMessage.show");   
        if (this.is_open) { return; }
        this.open();
      
        if (this.displayDuration) {
            var self = this;
            var reset = function() {
                clearTimeout(timer);
                self.close();
            }
            var delay = this.displayDuration;
            var timer = setTimeout(reset, delay);
        }
    },
    
    close: function() {
        this.popup.style.left = "-10000px";
        this.is_open = false;
    }
    
});

