// Availability "namespace"
Availability = {};


// 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.
Availability.POPUP_DELAY = 350;


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


// DOM element singleton used as the popup layer.
Availability.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.
Availability.popupTimout = null;


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


Availability.init = function() {
    jQuery('div.availability').each(Availability.initOne);
}


Availability.initOne = function() {

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

    availability.hide();

    productPanel.hover(
        function() {
            // If a popup is currently scheduled to be shown then cancel it.
            if(Availability.popupTimout) {
                window.clearTimeout(Availability.popupTimout);
            }
            // Schedule this panel to be shown instead.
            Availability.popupTimout = window.setTimeout(
                function() {
                    Availability.popupTimout = null;
                    Availability.preparePopup(availability);
                    Availability.popup.fadeIn(Availability.POPUP_FADE_IN_SPEED);
                    },
                Availability.POPUP_DELAY);
            // Track mouse motion over this panel so we can display it in the
            // correct place in the page.
            productPanel.bind('mousemove', Availability.trackMouse);
        },
        function() {
            // Stop tracking the mouse position.
            productPanel.unbind('mousemove', Availability.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(Availability.popupTimout) {
                window.clearTimeout(Availability.popupTimout);
                Availability.popupTimout = null;
            }
            else {
                Availability.popup.fadeOut(1);
            }
        }
        );
}


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


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


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

