/**
 * @author Adam McIntyre -- adam.mcintyre@molecular.com
 * @classDescription Utility class for popping up product flyouts throughout the site.
 *
 */
Popper = function() {
    var isOver = false;
    var position = false;
    var el = null;
    var to = 14;
    var ho = 0;
    var isSingleColMode = false;
    var isNewFlyout = false;

    return{
        addOpenListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'mouseover', this.handleMouseover, this);
        },
        addPollListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'mouseout', this.handleMouseout, this);
        },
        addOverListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'mouseover', this.setOver, this);
        },
        addCloseListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'mouseout', this.handleMouseout, this);
        },
        addOpenClickListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'click', this.handleMouseover, this);
            YAHOO.util.Event.addListener(elId, 'blur', this.doCloseWin, this);
        },
        addPollClickListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'click', this.handleMouseout, this);
        },
        addOverClickListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'click', this.setOver, this);
        },
        addCloseClickListener : function(elId) {
            YAHOO.util.Event.addListener(elId, 'click', this.doCloseWin, this);
        },
        setOffsets : function(topOffset, horizOffset) {
            to = topOffset;
            ho = horizOffset;
        },
        doCloseWin : function(e, obj) {
            obj.hideWin();
        },
        handleMouseover : function(e, obj) {
            YAHOO.util.Event.preventDefault(e);
            YAHOO.util.Event.stopPropagation(e);
            //isOver = false; //!!!!
            if(el) {
                isOver = false;
                isNewFlyout = true;
                obj.hideWin();
            }
            obj.setEl(this.id);
            obj.showWin(e, this);  // pass in the className to determine if obj overrides single col. mode
        },
        showWin : function(e, o) {
            var l = document.getElementById(el + '_pop');
            if(document.getElementById(el + '_img')) {
                var im = document.getElementById(el + '_img');
                im.src = o.href;
            }
            var anchor = document.getElementById(el + '_pop_anchor');

            // Are we past about halfway on the screen? If so, let's make sure no overlays are going to wind
            // going off the edge.
            if(e.clientX > (YAHOO.util.Dom.getViewportWidth() / 2)) {
                var tmpLeftCoord = YAHOO.util.Dom.getX(anchor) + ho;
                var oWidth = l.offsetWidth;

                // Make sure that, in all cases, we ensure that the element does not go off the right edge
                // of the page before we see whether or not we're in "single column"/product detail mode.
                if(tmpLeftCoord + oWidth >= YAHOO.util.Dom.getViewportWidth() ||
                   (!isSingleColMode || o.id.indexOf('twoColMode') > 0)) {
                    l.style.left = YAHOO.util.Dom.getX(anchor) - anchor.offsetWidth - (l.offsetWidth - 2 * anchor.offsetWidth) + "px";
                }
                else {
                    l.style.left = tmpLeftCoord + "px";
                }
                l.style.top = this.setTop(l, anchor);
            }
            else {
                l.style.left = YAHOO.util.Dom.getX(anchor) + ho + "px";
                l.style.top = this.setTop(l, anchor);
            }
        },
        setTop : function(l, anchor) {
            var top = YAHOO.util.Dom.getY(anchor) + to;
            if((top + l.offsetHeight) > YAHOO.util.Dom.getClientRegion().bottom) {
                return (top - l.offsetHeight) + 'px';
            }
            else {
                return top + 'px';
            }
        },
        handleMouseout : function(e, obj) {
            isNewFlyout = false;
            obj.checkMouseout(e, obj);
        },
        hideWin : function() {
            var l = document.getElementById(el + '_pop')
            l.style.left = "-4000px";
        },
        setEl : function(id) {
            el = id;
        },
        setOver : function() {
            isOver = true;
        },
        poll : function() {
            var obj = this;

            if(!obj.isPolling) {
                obj.isPolling = true;
                setTimeout(function() {
                    obj.isPolling = false;
                    if(!isOver && !isNewFlyout) {
                        obj.hideWin();
                    }
                    else {
                        return true;
                    }
                }, 2000);
            }
        },
        checkMouseout : function(e, o) {
            if(e.toElement && e.toElement != o) {
                isOver = false;
                this.poll();
            }
            else if(e.target && e.target != o) {
                isOver = false;
                this.poll();
            }
            else if(MOLECULAR.util.CBEvent.isInTarget(e, this)) {
                isOver = true;
                return true;
            }
            else {
                isOver = false;
                this.poll();
            }
        },
        setPosition : function(o) {
            var p = o.parentNode;
            var g = p.parentNode;

            while(g.tagName.toLowerCase() != 'body') {
                p = g;
                g = p.parentNode;
            }

            p.style.position = 'relative';
            p.style.zIndex = '999';
        },
        setSingleColumnMode : function(mode) {
            isSingleColMode = mode;
        }
    }
}();

