(function($){
    $.fn.popup = function(mixed, data, callback) {
        if(mixed == 'disable') {
            return this.each(function(){
                $(this).unbind('click').unbind('focus').unbind('keypress').unbind('blur');
            });
        }
        return this.each(function(){
            //$(this).click(function(){
            $(this).focus(function(){
                if($(this).val() == ""){
                    if($('#' + $(this).attr('popup')).html() == '' && mixed != undefined){
                        var $this = $(this);
                        $('#' + $this.attr('popup')).load(mixed, data, function(responseText){
                            if(responseText == '') {
                                $this.popup('disable');
                            } else {
                                var pos = $this.position();
                                var popup_height = $('#' + $this.attr('popup')).height();
                                $('#' + $this.attr('popup')).css({"left": (pos.left + $this.width() + 10) + "px", "top": (pos.top - popup_height / 2 + 3) + "px", zIndex: '5000' })
                                    .stop().show().css('opacity', '1.0');
                                if(typeof(callback == 'function')) {
                                    callback($this);
                                }
                            }
                        });
                        /*$.get(mixed, {}, function(data, textStatus){
                            $('#' + $this.attr('popup')).html(data);
                            if(data == '') {
                                $this.popup('disable');
                            } else {
                                var pos = $this.position();
                                var popup_height = $('#' + $this.attr('popup')).height();
                                $('#' + $this.attr('popup')).css({"left": (pos.left + $this.width() + 10) + "px", "top": (pos.top - popup_height / 2 + 3) + "px", zIndex: '5000' })
                                    .stop().show().css('opacity', '1.0');
                                if(typeof(callback == 'function')) {
                                    callback($this);
                                }
                            }
                        }, 'html');*/
                    } else { // TODO: get rid of this duplication
                        var pos = $(this).position();
                        var popup_height = $('#' + $(this).attr('popup')).height();
                        $('#' + $(this).attr('popup')).css({"left": (pos.left + $(this).width() + 10) + "px", "top": (pos.top - popup_height / 2 + 3) + "px", zIndex: '5000' })
                            .stop().show().css('opacity', '1.0');
                    }
                }
            /*}).focus(function(){
                $(this).click();*/
            }).keypress(function(){
                $('#' + $(this).attr('popup')).fadeOut('fast');
            }).blur(function(){
                $(this).keypress();
            });
        });
    };
})(jQuery);
