/*
 * 	exTable 0.1.3 - jQuery plugin
 *	written by Cyokodog	
 *
 *	Copyright (c) 2010 Cyokodog (http://d.hatena.ne.jp/cyokodog/)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
(function($){
	$.ex = $.ex || {};

	$.ex.table = function(idx , targets , option){
		var o = this,
		c = o.config = $.extend({} , $.ex.table.defaults , option);
	
		c.targets = targets;
		c.target = c.targets.eq(idx);
		c.index = idx;
		
		c.container = $('<div class="ex-table-container">' +
			'<div class="ex-table-head"><table/></div>' +
			'<div class="ex-table-body"></div>' +
			'<div class="ex-table-foot"><table/></div>' +
			'</div>');
		c.head = c.container.find('> div.ex-table-head');
		c.foot = c.container.find('> div.ex-table-foot');

		c.headTable = c.head.find('> table');
		c.footTable = c.foot.find('> table');

		c.body = c.container.find('> div.ex-table-body').css({
			'overflow-x' : 'hidden'
		});

		var thead = c.target.find('> thead')
			,tfoot = c.target.find('> tfoot')
			,tbody = c.target.find('> tbody');
		
		o._fixedWidth(thead);
		o._fixedWidth(tfoot);
		o._fixedWidth(tbody);
		
		c.target.after(c.container);
		
		c.target.find('> thead').appendTo(c.head.find('> table'));
		c.target.find('> tfoot').appendTo(c.foot.find('> table'));
		c.target.appendTo(c.body);

		$([c.target[0],c.headTable[0],c.footTable[0]]).css({
			'table-layout':'fixed',
			'margin':0
		});

		o.adjustHeight();

		if( c.onInit ){
			c.onInit.apply( c.targets , [ o ] );
		}
	}
	$.extend($.ex.table.prototype,{
		_fixedWidth : function(stack){
			var cols = stack.find('> tr:eq(0) > *');
			cols.each(function( idx ){
				var col = cols.eq(idx);
				col.width(col.width()).css('overflow','hidden');
			});
		},
		adjustHeight : function(){
			var o = this, c = o.config;
			var over = c.body.height() > c.maxHeight;
			var adjustWidth = c.maxHeight && !over ? 0 : c.scrollbarWidth;
			c.container.width(c.target.width() + adjustWidth + 1);
			c.head.css('padding-right', adjustWidth);
			c.foot.css('padding-right', adjustWidth);
			c.body.css({
				'overflow-y' : c.maxHeight ? 'auto' : 'scroll',
				height : c.maxHeight ? (over ? c.maxHeight : 'auto') : c.height
			});
			return o;			
		},
		getIndex : function(){
			return this.config.index;
		},
		getTargets : function(){
			return this.config.targets;
		},
		getTarget: function(){
			return this.config.target;
		},
		getContainer : function(){
			return this.config.container;		
		},
		getHead : function(){
			return this.config.head;		
		},
		getHeadTable : function(){
			return this.config.headTable;		
		},
		getBody : function(){
			return this.config.body;		
		},
		getBodyTable: function(){
			return this.config.target;
		},
		getFoot : function(){
			return this.config.foot;		
		},
		getFootTable : function(){
			return this.config.footTable;		
		}
	});
	
	$.ex.table.defaults = {
		scrollbarWidth :16,
		height : 200,
		maxHeight : 0,
		onInit : null
	}
	
	$.fn.exTable = function(option){
		var targets = this;
		return targets.each(function(idx){
			targets.eq(idx).data(
				'ex-table',
				new $.ex.table(idx,targets,option)
			);
		});
	}
})(jQuery);


