var Citizen = new Singleton(function($) {
    var base = this;
	
	this.methods({
		ready: function() {
			this.ContactMe.ready();
			this.setTimerForFlash();
			this.setLockedLinks();
		},
		setLockedLinks: function() {
		    $('#navigation ul li.locked a').addClass('requires_login');
		},
		setTimerForFlash: function() {
			window.setTimeout(function() {
				$('div.flash').hide('slow');
			}, 6000);
		},
		createMessage: function(text) {
			$('<div id="site_message"/>').appendTo('body').html(text);
		}
	});
	
	this.namespace('ContactMe', function() {
	    this.dialog = $('<div/>');
	    this.comments;
	    
	    this.methods({
	        ready: function() {
                this.create();
                $('a.contact-me-dlg').bind('click', function() { Citizen.ContactMe.open(); });
	        },
    		create: function() {
    			this.dialog.dialog({
    				autoOpen: false,
    				bgiframe: true,
    				width: 450,
    				height: 'auto',
    				modal: true,
    				resizable: false,
    				title: 'Contact Me',
    				buttons: {
    					Send: function() { $(this).find('form').trigger('submit'); },
    					Close: function() { $(this).dialog('close'); }
    				}
    			});
    		},
    		open: function() {
    		    this.dialog.dialog('open');
    		    if (this.dialog.html() == '') {
    		        this.dialog.loading('show');
            		$.get('/home/contact_me', function(data) {
            		    Citizen.ContactMe.load(data);
            		    Citizen.ContactMe.loadComments();
            		});
            	} else {
            	    Citizen.ContactMe.loadComments();
            	}
    		},
	        load: function(html) {
    			this.dialog.html(html);
    			this.dialog.dialog('option', 'position', 'center');
    			this.dialog.find('form').ajaxify('submit', {
    			    beforeSend: function() {
    			        Citizen.ContactMe.dialog.loading('show');
    			    },
                    success: function() {
                        alert('Your inquiry has been submitted. I will do my best to contact you as soon as possible.');
                    },
                    error: function() {
                        alert('An error occured while submitting your information.');
                    },
                    complete: function() {
                        Citizen.ContactMe.dialog.loading('hide').dialog('close');
                    }
    			});
    		},
    		loadComments: function() {
    		    if (this.comments) {
            	    this.dialog.find('#contact_comments').val(this.comments);
            	    this.comments = null;
        	    }
    		}
	    });
	});
});
