var DF = {

	themePath: '',

	/*
	 * Start Dan's website here!
	 */
	init: function() {
		this.more.init();
		this.clearAndRestore.init();
		this.mailtoHyperlinks.init('.mailto');
		this.tweets.init();
		this.autoHighlight.init();
		this.imageCaptions.init();
		this.clickableThings.init();
		this.mailChimpForms.init();
	},
	
	
	/*
	 * Allow the 'more' part of the Wordpress page/post
	 * to be expanded/collapsed.
	 */
	more: {
		init: function() {	

		}
	},
	
	
	/*
	 * Use alt tags to show a bit more info
	 */	
	imageCaptions: {
		init: function() {
			this.$images = $('article img[alt]');
			this.$images.each($.proxy(this, 'addCaption'));
		},
		
		addCaption: function(i, img) {
			var caption = $(img).attr('alt');

			if (!caption) {
				return;
			}

			$(img).wrap('<span class="img_container"></span>')
				  .before('<span class="img_caption">'+caption+'</span>');
		}
	},
	
	
	/*
	 * Highlight hyperlinks pointing to the same destination
	 */
	autoHighlight: {
		init: function() {
			$('a.auto_highlight').hover( $.proxy(this,'over'), $.proxy(this,'out') );
		},
		
		over: function(e) {
			this.links( $(e.currentTarget) ).addClass('hover');
		},
		
		out: function(e) {
			this.links( $(e.currentTarget) ).removeClass('hover');
		},
		
		links: function($a) {
			return $('a[href="'+$a.attr('href')+'"].auto_highlight');
		}
	},
	
	
	/*
	 * Clear a text field on focus, and restore its value on blur.
	 * (Use the HTML data attribute to store the initial value).
	 */
	clearAndRestore: {
		init: function() {
			this.$fields = $(':input[data-initial_value]');
			
			this.$fields.bind({
				blur: this.restore,
				focus: this.clear
			});
			
			// Add initial values
			this.$fields.trigger('blur');
		},
		
		clear: function() {
			if ($(this).val() == $(this).data('initial_value')) {
				$(this).val('');
			}
		},
		
		restore: function() {
			if (!$(this).val()) {
				$(this).val( $(this).data('initial_value') );
			}		
		}
	},
	
	
	/*
	 * Prevent email harvesters from picking up email addresses
	 * by writing them like so: joe dot blogs at example dot com
	 * This function converts them to mailto-hyperlinks.
	 */
	mailtoHyperlinks: {
		init: function(selector) {
			$(selector).each(this.convert);
		},
		
		convert: function() {
			var $elem = $(this);
			var email = $elem.text()
						.replace(/ at /, '@')
						.replace(/ dot /g, '.');
						
			$elem.text(email).wrap('<a href="mailto:'+email+'" />');
		}
	},
	
	
	/*
	 * Load Dan's latest tweets using AJAX to avoid
	 * blocking the page whilst they are loading.
	 */
	tweets: {	
		init: function() {
			this.$tweets = $('.tweets');
			this.$tweets.addClass('loading')
				.load(DF.URL+'/latest-tweets/', $.proxy(this, 'done'));
		},
		
		done: function() {
			this.$tweets.removeClass('loading')
				.children('ul').fadeIn();
		}
	},

	
	/*
	 * Make things that aren't usually clickable (not a hyperlink)
	 * into something that is.
	 */
	clickableThings: {
		init: function() {
			$('.archive.category-blog article').click(this.goToArticle);
		},
		
		goToArticle: function() {
			window.location = $(this).find('.more-link').attr('href');
		}
	},
	
	
	/*
	 * High all MailChimp forms by default,
	 * show them when links to them are clicked
	 */
	mailChimpForms: {
		init: function() {
			this.$container = $('#mc_embed_signup');
			this.$form = $('#mc-embedded-subscribe-form');
			this.$links = $('a[href="#mc_embed_signup"]');
			this.$button = $('#mc-embedded-subscribe');
			
			this.$container.hide();
			this.$button.attr('value', 'Submit');
			
			this.$links.click($.proxy(this, 'show'));
			this.$form.submit($.proxy(this, 'hide'));
		},
		
		show: function(e) {
			this.$container.fadeIn();
			return false;
		},
		
		hide: function(e) {
			this.$container.hide();
		}
	}
	 
};
