/******************************************************************************
 * tablekit_extend.js
 *****************************************************************************/
/**
 *
 * extends tablekit: changes bgcolor selected columns 
 */
var tableCols = Class.create ( {

	/*
	 * CONSTRUCTOR
	 */
	initialize: function( table ) {
		this.table 		= $( table );		
		this.sortel		= $(this.table.tHead).select( 'th' );
		this.rows		= {};
		this.rows		= this.getBodyRows(this.table);
		this.initEventHandlers();
		this.initTable();
	},
	
	/*
	 * altered function getBodyRows from tablekit ( http://www.millstream.com.au/view/code/tablekit/ )
	 */
	getBodyRows : function(table) {
		table = $(table);
		var id = table.id;
		if(!this.rows[id]) {
			this.rows[id] = (table.tHead && table.tHead.rows.length > 0) ? $A(table.tBodies[0].rows) : $A(table.rows).without(table.rows[0]);
		}
		return this.rows[id];
	},

	/*
	 * CHANGE THE COL
	 */
	changeCol: function( e , pos , rows ) {
		$( rows ).each( function( row ) {
			$( row ).childElements( 'td.td_sort' ).each( function( td ) {
				$( td ).removeClassName( 'td_sort' );
			});
			if( pos < 0)
			{
				$( row ).down( 'td' ).addClassName( 'td_sort' );
			} else {
				$( row ).down( 'td' ).next( pos ).addClassName( 'td_sort' );
			}
		});
	},

	/*
	 * ADD EVENT HANDLERS
	 */
	initEventHandlers: function() {		
		for ( var i = 0 ; i < this.sortel.length ; i++ ) {
			if ( this.sortel[i].hasClassName( 'sortcol' )) {						
				Event.observe( this.sortel[i], 'click', this.changeCol.bindAsEventListener( this, i - 1 , this.rows ));
			}
		};		
	},
	
	/*
	 * INITIALIZE THE TABLES
	 */
	initTable: function() {
		for ( var i = 0; i < this.sortel.length; i++ ) {
			if ( this.sortel[i].hasClassName( 'sortasc' ) || this.sortel[i].hasClassName( 'sortdesc' ) ) {						
				this.changeCol( null , i - 1 , this.rows );
			}
		}
	}
} );


Event.observe(window, 'load', function() {
	    TableKit.Sortable.addSortType(
	        new TableKit.Sortable.Type('percent', {
	            pattern : /^[-+]?[\d]*,?[\d]+(?:[eE][-+]?[\d]+)?/,
	            normal : function(v) {
	                // grabs first thing that looks like number from string, so you can use it to order a column of various srings containing numbers.
	                v = v.replace(/^.*?([-+]?[\d]*,?[\d]*).*$/,"$1");
	                v = v.replace(',','.');
	                v = parseFloat(v);
	                return isNaN(v) ? 0 : v;
	        }   }   )
	    );

            TableKit.Sortable.addSortType(
	        new TableKit.Sortable.Type('eurosort', {
	            pattern : /^[-+]?[\d]*,?[\d]+(?:[eE][-+]?[\d]+)?/,
	            normal : function(v) {
		    v = v.substring(1);
                    v = v.replace(/[.\s]/gi,'');
                    // grabs first thing that looks like number from string, so you can use it to order a column of various srings containing numbers.
                    v = v.replace(/^.*?([-+]?[\d]*,?[\d]*).*$/,"$1");
                    v = v.replace(',','.');
	                v = parseFloat(v);
	                return isNaN(v) ? 0 : v;
	        }   }   )
	    );
		
		TableKit.Sortable.addSortType(
        new TableKit.Sortable.Type('number', {
                pattern : /^[-+]?[\d]*\.?[\d]+(?:[eE][-+]?[\d]+)?/,
                normal : function(v) {
                   // This will grab the first thing that looks like a number from a string, so you can use it to order a column of various srings containing numbers.
                    v = v.replace(/[.\s]/gi,'');
                    // grabs first thing that looks like number from string, so you can use it to order a column of various srings containing numbers.
                    v = v.replace(/^.*?([-+]?[\d]*,?[\d]*).*$/,"$1");
                    v = v.replace(',','.');
                        v = parseFloat(v);
                        return isNaN(v) ? 0 : v;
                }
			})
		);
		
		$$( 'table.sortable' ).each( function( tblsort ) {
			new tableCols( tblsort );
		});
		

});