﻿jQuery.fn.extend({
    showWidget: function() {
        if ($(this).length == 0) return $(this);

        if (!$(this).hasClass('widget')) {
            throw 'element is not classed as a widget';
        }

        if ($('#widgetoverlay').length == 0) {
            $('<div id="widgetoverlay"></div>').appendTo('body').css({ opacity: '0.65' });
        }

        $('#widgetoverlay').show();

        var widget = $(this);

        widget.css({ marginLeft: -parseInt($(this).width() / 2.0), marginTop: -parseInt($(this).height() / 2.0) });

        if (widget.find('.widgetClose').length == 0) {
            $('<div class="widgetClose" title="Close widget [Press Esc]"><a href="#"></a></div>').prependTo(widget);
        }

        widget.css({ marginLeft: -parseInt($(this).width() / 2.0), marginTop: -parseInt($(this).height() / 2.0) });

        widget.find('.widgetClose a').click(function() {
            widget.hideWidget();
            return false;
        });

        function keyDown(e) {
            if (e.keyCode == 27) {
                widget.hideWidget();
            }
        }

        $(window).unbind('keydown', keyDown).keydown(keyDown);
        $(document.body).unbind('keydown', keyDown).keydown(keyDown);

        widget.show(); // .find('.focus').focus();

        return widget;

    },

    hideWidget: function() {
        $('#widgetoverlay').hide();
        return $(this).hide();
    }
});

$(document).ready(function() {

   
});
