var conf = {
    ieInitialSize: '12px', //IE doesn't treat the font as the actual size, so starting off at 15px makes it behave like a normal browser
    maxSize: '68px',
	selectors: {
		'.fontPlus': 1.1,
		'.fontMinus': 0.9
	},
	anchorTag: 'body'
}

// load prototype library, and set the page up when complete
jQuery.getScript('javascripts/prototype.js', function() {
	
	// Inspired by: http://www.shopdev.co.uk/blog/text-resizing-with-jquery/
	// save in case we want to reset font size
    var originalFontSize = getCurrentFontSize();

	// gets the list of classes that can be used as buttons to control font size
	var selector = $H(conf.selectors).keys().join(', ');

	$$(selector).each(function(button, index) {
		button.observe(
			'click', 
			changeFontSizeBy.curry(getMultiplierForElement(button))
		);
	});

	function changeFontSizeBy(multiplier, event) {
		event.stop(); // don't follow any links

	    var currentFontSize = getCurrentFontSize();
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
	    var newFontSize = currentFontSizeNum*multiplier;

	    $$(conf.anchorTag).each(function(tag) {
			tag.setStyle({fontSize: newFontSize + 'px'})
		});

	    return false;
	}

	function getCurrentFontSize() {
	    var size = $$(conf.anchorTag).first().getStyle('font-size');
	    return (parseFloat(size) > parseFloat(conf.maxSize))
	        ? conf.ieInitialSize    // if a huge number is returned (thanks IE!!), return the default from conf
	        : size;
	}

	function getMultiplierForElement(button) {
		var itemClassName, 
			multiplier = 1;	// if no matching multiplier is found, keep things constant

		$H(conf.selectors).each(function(hashItem) {
			itemClassName = hashItem.key.sub('\.','');
			if ($(button).hasClassName(itemClassName))
				multiplier = hashItem.value;
		});

		return multiplier; 
	}
});


(function($) {
	var wrapTables = function(type) {
		$('table.'+ type)
			.wrap('<div/>')
			.parent()
			.addClass('table-wrapper '+ type +'-wrapper')
			.find('td:nth-child(odd)')
				.addClass(type +'-title')
			.end()
			.find('td:nth-child(even)')
				.addClass(type +'-content');
	}
	
	wrapTables('callout');
	wrapTables('causes-of-loss');
	wrapTables('checklist-multiple');
}(jQuery));
