;(function($) {
    // What does the productSummaryNav plugin do?
    $.fn.preferenceMatchPopup = function(options) {
        var opts = $.extend({}, $.fn.preferenceMatchPopup.defaults, options);
        
        return this.each(function() {
            var $container = $(this);
            
            // Support for the Metadata Plugin.
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            
            var productId = $container.metadata().productId;
            
            var preferenceT;
            $container.hover(
                function(event){
                    preferenceT = setTimeout(function(){ showPopup($container, productId) }, 300);
                },
                function(event){
                    clearTimeout(preferenceT);
                    setTimeout(function(){ hidePopup($container); }, 300);
                }
            );
        });

        function showPopup($container, productId) {
            var $popup = createPreferenceMatchPopup(productId).appendTo($container);
            var $positionElement = $container.find(opts.positioningElementSelector);
            var $zIndexContainer = $container.closest(opts.zIndexContainerSelector);
            
            // Position the popup to the right. If it falls outside the viewport,
            // position it to the left.
            var windowRightEdge = $(window).width();
            var popupRightEdge = $container.offset().left + $positionElement.outerWidth() + $popup.outerWidth();
            if (popupRightEdge > windowRightEdge) {
                $popup.find(".bubble_arrow").removeClass("left").addClass("right");
                $popup.css("right", $container.width() + 11);
            } else {
                $popup.css("left", $positionElement.outerWidth() + 11);
            }
            
            // If the popup's bottom edge will extend below the window, move it up.
            var windowBottomEdge = $(document).height();
            var popupBottomEdge = $container.offset().top + $popup.outerHeight();
            if (popupBottomEdge > windowBottomEdge) {
                // Find the overlap (and add 5px for some padding off the bottom edge).
                var bottomOverlap = (popupBottomEdge - windowBottomEdge) + 5;
                $popup.css("top", -bottomOverlap);
                $popup.find(".bubble_arrow").css("top", bottomOverlap + 3);
            } else {
                $popup.css("top", -22);
            }
            
            // Temporarily set the container's z-index for IE.
            $zIndexContainer.css("z-index", 100);
            
            $popup.fadeIn("fast");
        }
        
        function hidePopup($container) {
            var $zIndexContainer = $container.closest(opts.zIndexContainerSelector);
            $zIndexContainer.css("z-index", "");
            
            $("#preference_matches").fadeOut("fast", function(){ $(this).remove() });
        }
        
        function createPreferenceMatchPopup(productId){
            var fitType = opts.preferenceJSON[productId].fitType;
            var matchingIncreases = opts.preferenceJSON[productId].matchingIncreases;
            var otherIncreases = opts.preferenceJSON[productId].otherIncreases;
            var matchingDecreases = opts.preferenceJSON[productId].matchingDecreases;
            var otherDecreases = opts.preferenceJSON[productId].otherDecreases;
            var matchingAvoids = opts.preferenceJSON[productId].matchingAvoids;
            var hasFitPreferences = (matchingIncreases.length + otherIncreases.length +
                                    matchingDecreases.length + otherDecreases.length) > 0;
            
            if (fitType != "NOT_APPLICABLE") {
                var popup = "<div id=\"preference_matches\" class=\"content_bubble\">";
                if (matchingAvoids.length > 0) {
                    popup += "<dl class=\"fit_preferences\">\
                                <dd class=\"avoid_matches\"><em>contains</em> ";
                    popup += matchingAvoids.join(", ");
                    popup += "</dl>";
                } else if (hasFitPreferences) {
                    popup += "<dl class=\"fit_preferences\">";
                    if (matchingIncreases.length > 0 || otherIncreases.length > 0) {
                        popup += "<dt>Good</dt>";
                        if (matchingIncreases.length > 0) {
                            popup += "<dd class=\"good_matches\">";
                            popup += matchingIncreases.join(", ");
                            popup += "</dd>";
                        }
                        if (otherIncreases.length > 0) {
                            popup += "<dd>" + otherIncreases.join(", ") + "</dd>";
                        }
                    }
                    if (matchingDecreases.length > 0 || otherDecreases.length > 0) {
                        popup += "<dt>Bad</dt>";
                        if (matchingDecreases.length > 0) {
                            popup += "<dd class=\"bad_matches\">";
                            popup += matchingDecreases.join(", ");
                            popup += "</dd>";
                        }
                        if (otherDecreases.length > 0) {
                            popup += "<dd>" + otherDecreases.join(", ") + "</dd>";
                        }
                    }
                    popup += "</dl>";
                }
                popup += "\
                    <div class=\"bubble_arrow left\"></div>\
                </div>\
                ";
                return $(popup);
            } else {
                return "";
            }
        }
    };
    
    // default options
    $.fn.preferenceMatchPopup.defaults = {
        preferenceJSON: {},
        positioningElementSelector: ".preference_match_symbols",
        zIndexContainerSelector: ".product_summary",
        truncate: true,
        listMax: 3
    };
    
})(jQuery);

