/**
 * jquery.large-content.js (helps to deal with large content in tight spaces)
 * 
 * @version 0.2
 * Changelog:
 *   * 0.1 Initial implementation
 *   * 0.2 Added support for video objects
 * 
 * @author Andrew Ramsden
 * @see http://irama.org/
 * @license Common Public License Version 1.0 <http://www.opensource.org/licenses/cpl1.0.txt>
 * @requires jQuery (tested with version 1.4.2) <http://jquery.com/>
 */
jQuery.largeContent = {
	initialised : null
};
 
	$.fn.largeContent = function () {
		
		
		// init - add listeners
			if (!$.largeContent.initialised) {
				ResizeEvents.bind(
					'x-text-resize x-window-width-resize',
					resizeZoomable
				);
				
				$.largeContent.initialised = true;
			}
		
		$(this)
			.wrap('<div class="scroll-container" style="overflow:auto;"></div>')
		;
			
		$(this).each(function(){
			container = $(this).parent();
			if ($(this).is('img')) {
				image = $(this);
				container
					.data('orig-width', image.width())
					.data('orig-height', image.height())
					.addClass('content-image')
					.bind('click', toggleZoom)
				;
			} else if ($(this).is('object')) {
				objEl = $(this);
				container
					.data('orig-width', objEl.attr('width'))
					.data('orig-height', objEl.attr('height'))
					.addClass('content-object')
			}
		});
		
		resizeZoomable();
	}
	
	resizeZoomable = function (eventObj, emPixels, textHeight, windowWidth, windowHeight){
		
		$('.scroll-container.content-image').each(function(){
			
			container = $(this);
			image = container.children().first();
			parentWidth = container.width();
			if (parentWidth < container.data('orig-width')) {
				ratio = container.data('orig-width') / parentWidth;
				image
					.width(parentWidth)
					.height(container.data('orig-height') / ratio)
					.addClass('zoomable')
				;
				container
					.data('zoomed-width', parentWidth)
					.data('zoomed-height', image.data('orig-height') / ratio)
				;
				
			} else {
				image
					.width(container.data('orig-width'))
					.height(container.data('orig-height'))
					.removeClass('zoomable')
				;
			}
			
		});
		$('.scroll-container.content-object').each(function(){
			
			container = $(this);
			objEl = container.children().first();
			object = objEl.get(0);
			
			objEl.hide();
			
			parentWidth = container.css('display','inline').css('display','block').width('auto').width();
			
			if (parentWidth < container.data('orig-width')) {
				ratio = container.data('orig-width') / parentWidth;
				object.width = parentWidth;
				object.height = container.data('orig-height') / ratio;
				
			} else {
				object.width = container.data('orig-width');
				object.height = container.data('orig-height');
			}
			
			objEl.show();
			
		});
	};
	
	toggleZoom = function(){
		container = $(this);
		image = container.children().first();
		
		if (!image.is('.zoomable')) {
			return;
		}
		
		if (image.width() != container.data('orig-width')) {
			image
				.width(container.data('orig-width'))
				.height(container.data('orig-height'))
				.addClass('zoomed-in')
			;
		} else {
			image
				.width(container.data('zoomed-width'))
				.height(container.data('zoomed-height'))
				.removeClass('zoomed-in')
			;
		}
	};
	
	
	
