// Popup "namespace"
Popup = {};


// How long to delay the initial display of the popup. Note: this must be
// longer that the hidden->visible transition period, i.e. longer than the fade
// in speed, or you get flicker still.
Popup.POPUP_DELAY = 350;


// Speed of the fade in animation. Must be less that the popup delay to avoid
// flicker.
Popup.POPUP_FADE_IN_SPEED = 200;


// DOM element singleton used as the popup layer.
Popup.popup = null;


// Timeout used to delay the display of the popup. It's also used to signal
// that there's a popup on the way ... unless the timeout is cancelled.
Popup.popupTimout = null;


// Mouse position tracking to support lazy creation of the popup element.
Popup.pageX = 0;
Popup.pageY = 0;


Popup.init = function() {
    jQuery('.pop_info').each(Popup.initOne);
}


Popup.initOne = function() {

    var popup_info = jQuery(this);
    var productPanel = popup_info.parent();

    popup_info.hide();

    productPanel.hover(
        function() {
            // If a popup is currently scheduled to be shown then cancel it.
            if(Popup.popupTimout) {
                window.clearTimeout(Popup.popupTimout);
            }
            // Schedule this panel to be shown instead.
            Popup.popupTimout = window.setTimeout(
                function() {
                    Popup.popupTimout = null;
                    Popup.preparePopup(popup_info);
                    Popup.popup.fadeIn(Popup.POPUP_FADE_IN_SPEED);
                    },
                Popup.POPUP_DELAY);
            // Track mouse motion over this panel so we can display it in the
            // correct place in the page.
            productPanel.bind('mousemove', Popup.trackMouse);
        },
        function() {
            // Stop tracking the mouse position.
            productPanel.unbind('mousemove', Popup.trackMouse);
            // If a popup is scheduled to be shown then just cancel the
            // timeout. Otherwise it's visible and so needs to be hidden.
            if(Popup.popupTimout) {
                window.clearTimeout(Popup.popupTimout);
                Popup.popupTimout = null;
            }
            else {
                Popup.popup.fadeOut(1);
            }
        }
        );
}


Popup.preparePopup = function(content) {
    if(Popup.popup == null) {
        Popup.popup = jQuery('<div id="popup_info" class="hidden" />');
        jQuery('body').append(Popup.popup);
    }
    Popup.popup.html(content.html());
    Popup.popup.css('height', content.css('height'));
    Popup.popup.css('width', content.css('width'));
    Popup.positionPopup(Popup.pageX, Popup.pageY);
}


Popup.positionPopup = function(pageX, pageY) {
    // If the popup is visible then we can position it now.
    if(Popup.popup && !Popup.popupTimout) {
        Popup.popup.css('left', pageX+15);
        Popup.popup.css('top', pageY+5);
    }
    // Otherwise we need to store the position for when the popup is first
    // displayed.
    else {
        Popup.pageX = pageX;
        Popup.pageY = pageY;
    }
}


Popup.trackMouse = function(e) {
    Popup.positionPopup(e.pageX, e.pageY);
}


