/*
 * Column Fix
 *
 * Author Keith Deverell
 * Date July 2010
 *
 */

/*
 * @textHeight : sets the intial height of the text that will be turned to columns
 * you need to pass this in from a global variable set on the inital load
 * at this time i can't think of any other way of doing this
 *
 */

/*
 * This pluging beleives that the text box is sat in a parent container.
 * This parent container will have its size set to enable decent scrolling
 *
 */

/*
 * @padding : This is the padding of the parent container and is take off its final size
 * @bottomOffset : Adds an offset to the bottom of the text columns so text doesn't clash with the scroll bars
 * @columnWidth : The width of the columns
 * @resize : Set to TRUE when you want to change the layout after load - eg $(window).resize();
 *
 */

(function($){
    $.fn.extend({
        columnFix: function(options) {
	
			var defaults = {
				textHeight : 0,
				padding : 0, 
				bottomOffset : 0,
				columnWidth : 450,
				resize : false
			};

			var options = $.extend(defaults, options);
			var windowHeight = $(window).height();
															
			return this.each(function()
			{
				if(!$.browser.msie)
				{
					if(options.resize)
					{
						resetCol($(this));
					}
					setColumns($(this));
				} else {
					alert('oops, sorry another feature that Microsoft has not allowed. Try installing Safari, Chrome or Firefox');
				}
			});
			
			function setColumns(obj)
			{
				var colNumber = Math.ceil(options.textHeight/windowHeight);
				var textWidth = colNumber*options.columnWidth;
				var parent = obj.parent();
					parent.height(windowHeight-options.padding+'px');
					
				obj.css({
						'height':parent.height()-options.bottomOffset+'px',
						'width':textWidth+options.columnWidth+'px',
						'column-count':colNumber+'',
						'-webkit-column-count':colNumber+'',
						'-moz-column-count':colNumber+''
					});
			}
						
			function resetCol(obj)
			{
				obj.width(options.columnWidth+'px')
				   .css({
						'column-count':'1',
						'-webkit-column-count':'1',
						'-moz-column-count':'1'
				});
			}

			return this;
        }
    });
})(jQuery);
