/**
 * lh.js
 * @author Lewis Howles (lh)
 *
 * One part default niceties, one part scripting goodness...
 */

var lh = {
	// Initialisation function. Your code here.
	init : function() {
		lh.externalLinks();
		lh.highlighter();
		lh.styleSwitcher();

		if ($('.slider').length) {
			$('.slider').easySlider({
				speed : 1000,
				continuous : true,
				controlsShow : false,
				auto : true
			});
		}
	},
		

	// 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());

/*
 * ...and one part cake
 *
 *             ,:/+/-
 *             /M/             .,-=;//;-
 *        .:/= ;MH/,    ,=/+%$XH@MM#@:
 *       -$##@+$###@H@MMM#######H:.   -/H#
 *  .,H@H@ X######@ -H#####@+-    -+H###@X
 *   .,@##H;      +XM##M/,    =%@##@X;-
 * X%-  :M##########$.    .:%M###@%:
 * M##H,   +H@@@$/-.  ,;$M###@%,         -
 * M####M=,,---,.-%%H####M%:         ,+@##
 * @##################@/.         :%H##@$-
 * M###############H,         ;HM##M$=
 * #################.    .=$M##M$=
 * ################h..;XM##M$=         .:+
 * M###################@%=          =~@MH%
 * @################M/.         =+H#X%=
 * =~M#############M,       -/X#X+;.
 *   .;XM#########M=    ,/X#H+:,
 *      .=+HM######M+/+HM@+=.
 *          ,:/%XM####H/.
 *              ,.:=-.
 */

