/*

	Formatting library using jQuery
	
	@author: Gyorgy Fekete gyorgy.fekete [at] lateral [dot] com


*/

var formatting = {

	/**
	 * Sets an element's font size so that the element will be a specified width
	 * This method only measures width 
	 *
	 * @param {jQueryElement} srcElement the jQuery html element
	 * @param {Number} width the specified width in pixels
	 */
	setFontSizeByWidth : function(srcElement, width, important)
	{
		var el = $('<div>vv</div>');
			el.css({background:'#FFFF00',
					position:'absolute',
					top:'-1000px',
					left:'-1000px',
					width:'auto'
				});

		el.appendTo(document.body);
		el.hide();

		//clone the CSS element that might affect the width
		el.css({ 
			'font-size': srcElement.css('font-size'),
			'font-style': srcElement.css('font-style'),
			'font-weight': srcElement.css('font-weight'),
			'font-family': srcElement.css('font-family')
		});
		
		//finally copy the source element's content
		el.text(srcElement.html());
		
		//recursively set the the font size until the element's width reaches the target width
		setSize();		
		function setSize()
		{
			var elSize = el.width();
			var fontSize = parseInt(el.css('font-size'));
						
			if (elSize > width)
			{	
				el.css('font-size',fontSize-1);
				setSize();
			}
		}
		
		//copy back the font size to the source element
		srcElement.css('font-size',el.css('font-size'));
		
		//work around to set !important so IE and Safari won't crash (works with FF too)
		if (important === true)
		{
			srcElement.css('cssText', 'font-size: ' + el.css('font-size') + ' !important;');
		}
		
		//cleanup
		el.remove();
		
	}
	
}
