﻿
$(function () {
    // hack to fit the module inside the page boundaries
    if ($('#dnn_contentPane .ModDNNHTMLC').length > 0) {
        $('#dnn_contentPane, #dnn_contentPane .containerWrapper').css(
            { 'width' : '300px', 'padding-right' : '5px' });
    }

    // wait 100ms - enough to ensure the hack above has been applied so the 
    // the correct popup offset will be calculated
    setTimeout(function() {
        var popupTimeout;
        var popupOffset = $('img.facility-map').maphilight({
                fillColor: '38A73A',
                fillOpacity: .5,
                stroke: false
            }).offset();
        var popup = $('<div class="popup location"/>').appendTo('body').css({
            'position': 'absolute',
            'top': popupOffset.top + 10,
            'left': popupOffset.left,
            'display': 'none'
        });

        var clearPopupTimeout = function () {
            if (popupTimeout) {
                clearTimeout(popupTimeout);
                popupTimeout = null;
            }
        };

        var setPopupTimeout = function() {
            popupTimeout = window.setTimeout(function () {
                popup.fadeOut('slow');
            }, 5000);
        };
        
        popup.mouseover(function () {
            clearPopupTimeout();
        })
        .mouseout(function() {
            setPopupTimeout();
        });

        $('map.facility-map area').each(function () {
            var that = $(this);
            that.mouseover(function () {
                clearTimeout(popupTimeout);
                popup.html($('.locations li[location="' + that.attr('href').split('/').pop() + '"]').html()).fadeIn('slow');
            })
            .mouseout(function () {
                clearPopupTimeout();
                setPopupTimeout();
            });
        });
    }, 100);
});
