// Copyright 2011 Fenton Web Design Firm
// http://www.fentonwebdesignfirm.com
//
// If you would like to implement this on your site, with
// or without special modifications, please contact us
// and ask about licensing.

function Popups(args) {
    // Get x and y coords for object
    this.objPosition = function(obj) {
        var posX = 0;
        var posY = 0;
        if (obj.offsetParent)
            while (1) {
                posX += obj.offsetLeft;
                posY += obj.offsetTop;
                if (!obj.offsetParent) break;
                obj = obj.offsetParent;
            }
        else if (obj.x) {
            posX += obj.x;
            posY += obj.y;
        }
        return { x: posX, y: posY };
    }

    // Move object to x and y coords
    this.positionObj = function(obj, x, y) {
        obj.style.left = x + 'px';
        obj.style.top = y + 'px';
    }

    // Shortcut to call for hiding popup
    this.hidePopupInner = function(id) {
        document.getElementById(id).style.display = 'none';
    }

    // Start timer for hiding popup
    this.hidePopup = function(id) {
        var t = this;
        this.timer = setTimeout(function() { 
            t.hidePopupInner(id); }, this.delay);
    }

    // Hide others, show selected at given offset
    this.showPopup = function(id) {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        var a_pos;
        for (var i = 0; i < this.popups.length; i++) {
            if (this.popups[i] != id)
                this.hidePopupInner(this.popups[i]);
            else a_pos = this.objPosition(
                document.getElementById(this.anchors[i]));
        }
        var p_div = document.getElementById(id);
        this.positionObj(p_div, a_pos['x'] + this.offset_x,
            a_pos['y'] + this.offset_y);
        p_div.style.display = '';
    }
    
    this.popups = args['popups'];
    this.anchors = args['anchors'];
    this.offset_x = args['offset_x'];
    this.offset_y = args['offset_y'];
    this.delay = args['delay'];
    this.timer = false;
}
