﻿jQuery(document).ready(function() {
    $('a#quick-contact-btn').qtip({
        content: {
            title: {
                text: '&nbsp;',
                button: '<img src="/Templates/Public/Styles/images/close.png" alt="Stäng" title="Stäng" />'
            },
            text: '<div class="ajax-loading"/>'
        },

        position: {
            target: $(document.body), // Position it via the document body...
            corner: 'topCenter', // ...at the top center of the viewport
            adjust: { y: 100 } // adjust 100px down
        },

        show: {
            when: 'click',
            ready: false,
            solo: true // And hide all other tooltips
        },

        hide: {
            when: {
                event: 'unfocus'
            }
        },
        style: {
            width: 800,

            padding: '14px',
            border: {
                width: 9,
                radius: 5,
                color: '#eee'
            },

            title: {
                'background-color': '#fff'
            },

            classes: {
                content: 'quick-contact-modal'
            }
        },
        api: {
            beforeShow: function() {
                // Fade in the modal "blanket" using the defined show speed
                $('#qtip-blanket').fadeIn(100);
            },
            
            onShow: function() {
                $("a[href^='mailto']").each(function(i) {
                    $(this).mailDefuscator();
                });
            },
            
            beforeHide: function() {
                // Fade out the modal "blanket" using the defined hide speed
                $('#qtip-blanket').fadeOut(100);
            },

            onRender: function() {
                var self = this;

                var url, data, method, options;

                // Setup request configuration
                url = '/WebServices/Public/ContactService.asmx/GetContactListHTML';
                data = '{id :' + ep_settings.getInstance().id + '}';
                method = 'post';
                options = {
                    cache: true,
                    async: false,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8"
                };

                // Load the content using the configuration
                this.loadContent(url, data, method, options);

                //Activate a key listener
                $(document).keydown(function(e) {
                    //Hide the modal dialog if the escape key is pressed
                    if (e.keyCode == 27) {
                        self.hide();
                    }
                });
            },

            onContentLoad: function(json) {
                return json.d;
            }
        }
    }).removeAttr('href'); //Remove default href functionality
});