/**
 * lh.js
 * @author Lewis Howles (Boilerplate)
 *
 * One part default niceties, one part scripting goodness...
 */

var lh = {
	// Initialisation function.
	init : function() {
		lh.externalLinks();
		lh.highlighter();
		lh.styleSwitcher();

		// Elastic text areas
		var textarea = $('.fancy').find('textarea');
		textarea.length && textarea.elastic();
		
		$("#slider").innerfade({
			speed: 1000,
			timeout: 6000,
			containerheight: '300px'
		});

	},

	// Set target blank on external links
	externalLinks : function() {
		$('a[rel~="external"]').attr('target', '_blank');
	},

	// Triggers toggleFocus when hovering styled form fields
	highlighter : function() {
		$('form.fancy').delegate('input, select, textarea', 'mouseover focusin mouseout focusout', function(e) {
			// Check whether the event is an 'inward' event
			var isIn = (e.type === 'mouseover' || e.type === 'focusin');

			lh.toggleFocus($(this), isIn);
		});
	},

	// Adds a 'focus' class to parent lis only if a field is hovered,
	// not the li itself.
	toggleFocus : function($this, modifier) {
		// Use the result of isIn above to modify toggleClass
		$this.parent('li').not('.submit').toggleClass('focus', modifier);
	},

	// Adds a 'Reader Mode' button (hidden by default.css in IE).
	// Changes the main stylesheet to the 'reader' stylesheet for a
	// nice readable display of the content.
	styleSwitcher : function() {
		var styleSwitcher = $('#style-switcher').find('a');

		styleSwitcher
			.data('sheet','global')
			.bind('click', function() {
				// Check the current stylesheet and set an array of
				// values appropriately. Update values if stylesheets
				// are named differently.
				var params = (styleSwitcher.data('sheet') == 'global') ? ['reader','Visual Mode'] : ['global','Reader Mode'];
				$('link[rel=stylesheet]')
					.eq(1) // 0-indexed position of stylesheet. Change this is you had to move global.
					.attr('href', '/css/'+params[0]+'.css');
				styleSwitcher
					.data('sheet',params[0])
					.text(params[1]);
			});
	}
}

// Initialise
$(lh.init());

