/**
* Namespace "Contactmap"
* @class Overlay
* @desc Subclass of Base
*/
Base.Contactmap = function () {
    var map1 = $('.contactmap .map1');
    var map2 = $('.contactmap .map2');
    var lines = $('.contactmap .lines');
    var linesnn = $('.contactmap .lines-nn');
    var wbs = $('.contactmap .woonbedrijven');
    var label = $('.contactmap .label');
    var adres = $('.contactmap .adres');

    function initContactMap() {
        $(map1).find('.zoom').live('click', function () {
            zoomAni(map1, map2);
            hideAddress();
        });
        $(map2).find('.zoom').live('click', function () {
            zoomAni(map2, map1);
            hideAddress();
        });

        $('.contactmap .marker').bind('mouseover', function () {
            $('.contactmap .marker').css('z-index', 10);
            var pos = $(this).position();
            var margin = $(this).closest('.map1').length == 0 ? 6 : 9;
            $(this).css('z-index', 12).clone().insertBefore($(this)).addClass('glow').removeClass('marker').css({ left: pos.left - margin, top: pos.top - margin });
            if (!$(this).hasClass('active')) {
                showLabel($(this));
            }
            if ($(this).hasClass('rk') || $(this).hasClass('map1_wb05')) {
                $(lines).show();
            }
            if ($(this).hasClass('rknn') || $(this).hasClass('map1_wb14')) {
                $(linesnn).show();
            }

        }).bind('mouseout', function () {
            $('.contactmap .glow').remove();
            $(lines).hide();
            $(linesnn).hide();
            $(label).hide();
        }).bind('click', function () {
            $(label).hide();

            if ($(this).hasClass('active')) {
                hideAddress();
            } else {
                $('.contactmap .marker').removeClass('active');
                $(this).addClass('active');
                showAddress($(this));
            }
        });

        $(adres).find('.close').live('click', function () {
            hideAddress();
        });
    }

    function hideAddress() {
        $('.contactmap .marker').removeClass('active');
        $(adres).hide();
    }

    function showAddress(marker) {
        var marker_pos = $(marker).position();
        var wb_id = $(marker).attr('lang');
        var html = $(wbs).find('.' + wb_id).html();
        $(adres).attr('class', 'adres ' + ($(marker).hasClass('rk') ? 'rk' : wb_id)).html($(html)).show();

        var pos_left = marker_pos.left - $(adres).width() / 2 + $(marker).width() / 2 + 1;
        var max_left = $('.contactmap').outerWidth() - $(adres).width() - 5;
        $(adres).css({
            left: Math.max(5, Math.min(max_left, pos_left)),
            top: marker_pos.top - $(adres).height() - 10
        })
    }

    function showLabel(marker) {
        $(label).width('auto');
        var wb_id = $(marker).attr('lang');
        var marker_pos = $(marker).position();

        $(label).find('.text span').text($(wbs).find('.' + wb_id + ' h4').text());
        $(label).attr('class', 'label ' + ($(marker).hasClass('rk') ? 'rk' : wb_id)).show();

        var label_width = $(label).find('.text span').width() + 34;

        // zet de breedte van het label
        $(label).width(label_width);
        $(label).find('.arrow').css({ left: (label_width / 2) - ($(label).find('.arrow').width() / 2) });
        $(label).css({
            left: marker_pos.left - $(label).width() / 2 + $(marker).width() / 2 + 1,
            top: marker_pos.top - $(label).height() - 1
        })
    }

    function zoomAni(hide, show) {
        $(hide).slideUp(250, function () {
            $(show).slideDown(250);
        })
    }

    function initPopupView() {
        if ($('a.open-contactmap')) {
            $('a.open-contactmap').bind('click', function () {
                var html = $(this).next('.contactmap-content').html();
                Base.Overlay.show({ closeOnClick: false });
                $('body').append($('#overlay-contactmap'));
                $('#overlay-contactmap').show();
                return false;
            });

            $('#overlay-contactmap .closer').bind('click', function () {
                Base.Overlay.hide();
                $('#overlay-contactmap').hide();
            })
        }
    }

    return {

        /**
        * Initialize this Class
        */
        init: function () {
            initContactMap();
            initPopupView();


        }
    };
} ();

/* Initialize this class */
Base.register(Base.Contactmap.init);
