/**
 * "Transformer layout" adds layout-classes to the body tag depending on window width, text size and zoom level.
 * jquery.transformer-layout.js
 * @version 2.2.1
 * Changelog:
 *   *  2.1 Re-implementation in jQuery
 *   *  2.2 Tweak to sort function
 *   *  2.2.1 Small tweak so resize events is not initialised until page is ready
 * 
 * @author Andrew Ramsden
 * @see http://irama.org/web/dhtml/transformer/
 * @license GNU GENERAL PUBLIC LICENSE (GPL) <http://www.gnu.org/licenses/gpl.html>
 * 
 * @requires jQuery (tested with 1.3.1) <http://jquery.com/>
 * @requires jQuery resize events plugin <http://irama.org/web/dhtml/resize-events/>
 * 
 */
jQuery.tLayout = {};
 
jQuery.tLayout.conf = {
	optionsRef : 'transformer-layout-options'
};

jQuery.tLayout.defaultOptions = {
	layouts : [ // layouts options must contain an array of all layout class and the min-width at which they kick in
		['50', 'layout-large'],
		['30', 'layout-medium'],
		['0', 'layout-small']
	],
	callback : null // a function to be called after layout changes
};

(function($) {// start closure
	
	
	/**
	 * Expects to be sent a DOMNode for the body element
	 */
	$.fn.transformer = function (options) {
		
		if (typeof ResizeEvents == 'undefined') {
			$.debug('DEBUG: ResizeEvents plugin could not be found');
			return;
		}
		
		options = options || {};
		
		options.layouts = options.layouts.sort(sortLayouts);
		
		$(this).each(function () {
			initTransformer.apply(this, [options])
		});
		
		return $(this); // facilitate chaining
	};
	
	/**
	 * Sort layouts in order of largest min-width to smallest
	 */
	sortLayouts = function (a, b) {
		return b[0] - a[0];
	};
	
	/**
	 * 
	 */
	initTransformer = function (options) {
		
		// don't initialise here, wait till document is ready.
		//ResizeEvents.initialise();
		
		// Merge runtime options with defaults
		// Note: The first argument sent to extend is an empty object to
		// prevent extend from overriding the default $.AKN.defaultOptions object.
			options = (typeof options == 'undefined')
				? $.tLayout.defaultOptions
				: $.extend({}, $.tLayout.defaultOptions, options)
			;
			$(this).data($.tLayout.conf.optionsRef, options);
		
		
			
			//ResizeEvents.eventElement.bind(
			ResizeEvents.bind (
				'x-initial-sizes x-text-resize x-window-resize',
				function (eventObj, emPixels, textHeight, windowWidth){
					
					
					// set layout-class. pagewidth / textsize = empixels of space to play with (kinda, roughly)
						layouts = options.layouts;
						layoutMatched = false;
						for (id in layouts) {
							//$.debug(parseFloat(emPixels) + ' > ' + parseFloat(layouts[id][0]));
							if (!layoutMatched && (parseFloat(emPixels) > parseFloat(layouts[id][0]))) {
								$('body').addClass(layouts[id][1]);
								layoutMatched = true;
							} else {
								$('body').removeClass(layouts[id][1]);
							}
						}
					
					// activate callback function if set
						if (options.callback != null) {
							options.callback.apply(this);
						}
				}
			);
		
	};

	/**
	 * Dump debug messages to console if available, otherwise to status bar.
	 */
	$.debug = function (message){
		if (typeof window.console != 'undefined' && window.console.log != 'undefined') {
			window.console.log(message);

		} else {
			// un comment next line if testing IE6 issues in dev environment (don't use for production)
				//window.status = message;
		}
	};
	
})(jQuery); /* end closure */