;(function($) {
    /* Numeric HTML sorting */
    $.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
        var x = a.replace(/[^-.0-9]/g, "");
        var y = b.replace(/[^-.0-9]/g, "");
        x = parseFloat( x );
        y = parseFloat( y );

        if (isNaN(x))
            x = Number.MAX_VALUE;
        if (isNaN(y))
            y = Number.MAX_VALUE;

        return ((x < y) ? -1 : ((x > y) ?  1 : 0));
    };

    $.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
        var x = a.replace(/[^-.0-9]/g, "");
        var y = b.replace(/[^-.0-9]/g, "");
        x = parseFloat( x );
        y = parseFloat( y );

        if (isNaN(x))
            x = Number.MIN_VALUE;
        if (isNaN(y))
            y = Number.MIN_VALUE;

        return ((x < y) ?  1 : ((x > y) ? -1 : 0));
    };

    /* Numeric string */
    $.fn.dataTableExt.oSort['num-str-asc']  = function(a,b) {
        x = parseFloat( a );
        y = parseFloat( b );

        if (isNaN(x))
            x = Number.MAX_VALUE;
        if (isNaN(y))
            y = Number.MAX_VALUE;

        return ((x < y) ? -1 : ((x > y) ?  1 : 0));
    };

    $.fn.dataTableExt.oSort['num-str-desc'] = function(a,b) {
        x = parseFloat( a );
        y = parseFloat( b );

        if (isNaN(x))
            x = Number.MIN_VALUE;
        if (isNaN(y))
            y = Number.MIN_VALUE;

        return ((x < y) ?  1 : ((x > y) ? -1 : 0));
    };

    /* EU format: 1.234.456,97 */
    $.fn.dataTableExt.oSort['num-eu-asc']  = function(a,b) {
        a = a.replace(/\./g,"").replace(/,/g,".");
        b = b.replace(/\./g,"").replace(/,/g,".");

        x = parseFloat( a );
        y = parseFloat( b );

        if (isNaN(x))
            x = Number.MAX_VALUE;
        if (isNaN(y))
            y = Number.MAX_VALUE;

        return ((x < y) ? -1 : ((x > y) ?  1 : 0));
    };

    $.fn.dataTableExt.oSort['num-eu-desc'] = function(a,b) {
        a = a.replace(/\./g,"").replace(/,/g,".");
        b = b.replace(/\./g,"").replace(/,/g,".");

        x = parseFloat( a );
        y = parseFloat( b );

        if (isNaN(x))
            x = Number.MIN_VALUE;
        if (isNaN(y))
            y = Number.MIN_VALUE;

        return ((x < y) ?  1 : ((x > y) ? -1 : 0));
    };

    var dateUkRegEx = /[\.\-\/\\]/;

    /* dd/mm/YYYY date sorting */
    $.fn.dataTableExt.oSort['date-uk-asc']  = function(a,b) {
        var ukDatea = a.split(dateUkRegEx),
            ukDateb = b.split(dateUkRegEx),
            x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1,
            y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

        return ((x < y) ? -1 : ((x > y) ?  1 : 0));
    };

    $.fn.dataTableExt.oSort['date-uk-desc'] = function(a,b) {
        var ukDatea = a.split(dateUkRegEx),
            ukDateb = b.split(dateUkRegEx),
            x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1,
            y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

        return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
    };

    smartcontent_DataTable_DrawCallback = function() {
        if (typeof(SexyLightbox) != "undefined" && jQuery("#SLB-Wrapper").length > 0)
            SexyLightbox.refresh();
    };

    DataTable = {
        hideLoadingPane: function(sel) {
            var pane = $(sel);
            pane.removeClass('dt-loading');
            pane.find('.dt-loading-message').remove();
        },

        prepareTable: function(sel) {
            $(sel).each(function() {
                var tbl = $(this);
                if (tbl.find('thead').length == 0) {
                    tbl.children('tbody').children().unwrap();
                    tbl.children('tr:first-child').wrapAll('<thead>');
                    tbl.find('thead tr td').replaceWith(function() {
                        return $("<th />", {html: $(this).html()});
                    });
                    tbl.children('tr:not(first-child)').wrapAll('<tbody>');
                }
            });

            $(sel).addClass("display dataTable");
        }
    };
})(jQuery);