(function($){
    
    $.signupDialog = function(options){
            
        var opts = $.extend({}, options);
        
        function showDialog() {
            $.get(opts.url, {}, function(data){
                var dialog = $('<div />');
                dialog.append(data);
                title = dialog.children('h1:first').html();
                dialog.children('h1:first').remove();
                
                dialog.dialog({
                    title : title,
                    open : function(){
                        var dialog = $(this);
                        dialog.find('.dialog-close:not(.direct-link)').click(function(){
                            dialog.dialog('close');
                            return false
                        })
                        
                        $('a#sign-up-submit').click(function(){
                            
                            var form = $(this).parents('form');
                            $.post(opts.url, form.serialize(), function(data){
                                if ($(data).find('.errorlist').length) {
                                    var errorlist = $(data).find('.errorlist');
                                    form.find('.errorlist').remove().end().prepend(errorlist)
                                } else {
                                    dialog.dialog('close');
                                    $('<div />').append('<h2>' + gettext('An email has been sent to you. Please click on link in the email to confirm your registration. Thank you!') + '</h2><a href="#" class="button dialog-close">' + gettext('Close') + '<span class="l"></span><span class="r"></span></a>').dialog({
                                        title : gettext('Thanks for registering at PlaceForPeople.')
                                    })  
                                }
                            })
                            
                            return false
                        });
						if (opts.callback) {
							opts.callback();
						}
                    }
                })
            })
        }
        
        $('.sign-up-dialog').live('click', function(){
            $('.ui-dialog-content').dialog('close');
            opts.url = $(this).attr('href');
            
            showDialog();
            return false
        })
            
    
        $(function(){
            if (location.hash == '#sign-up') {
                showDialog();
            }
        })
    }

})(jQuery)

