(function($){

    $.fn.vetTable = function(settings){
    
        var config = {
            'width': '100%',
            'height': 'auto',
            'rowclick': true
        };
        
        $.fn.vetTable.zebraTable = function($o){
            $o.find("tbody tr:odd").addClass("zebraTable");
        };
        
        if (settings) 
            $.extend(config, settings);
        
        this.each(function(){
        
            var $ui = $("<div/>").addClass("ui table").css('width', config.width);
            var $thead = $("<table cellpadding='0' cellspacing='0' />").addClass("style").css('width', config.width);
            var $content = $("<div/>").addClass("content");
            var $td = $("<td/>").css("width", 10);
            var $vetTable = $(this);
            $content.css({
                'width': config.width ,
                'height': config.height,
                'display': 'block'
            });
            
            $.fn.vetTable.zebraTable($vetTable);			

            $vetTable.css("width", config.width);
            $vetTable.find('thead').clone().appendTo($thead);
            $vetTable.find('thead').remove();
            $vetTable.wrap($ui).wrap($content);
            //$td.appendTo($thead.children().children());
            $thead.insertBefore($vetTable.parent());
            
            var $th = $thead.find('th'), inverse = false;
            
            $th.click(function(){
            
                var $header = $(this), index = $header.index();
                
                $vetTable.find('tr:odd').removeClass("zebraTable");
                $vetTable.find('td').filter(function(){
                    console.log($(this).index() === index);
                    return $(this).index() === index;
                }).sortData(function(a, b){
                
                    a = $(a).text();
                    b = $(b).text();
                    
                    return (isNaN(a) || isNaN(b) ? a > b : +a > +b) ? inverse ? -1 : 1 : inverse ? 1 : -1;
                    
                }, function(){
                    return this.parentNode;
                });
                
                $.fn.vetTable.zebraTable($vetTable);
                inverse = !inverse;
                
            });
			
            var tbodyRow = $vetTable.find("tbody tr");
            //$tbodyRow.css({
            //	cursor:pointer
            //});
            tbodyRow.click(function(){
                if(config.rowclick) {
                    if($(this).hasClass("selected")){
                        $(this).removeClass("selected");
                        $(this).find("input[type='checkbox']").removeAttr("checked");
                    }else{
                        $(this).addClass("selected");
                        $(this).find("input[type='checkbox']").attr("checked", "checked");
                    }
                }
            });
            
            var $selectAll = $thead.find("#selectAll");            
            if ($selectAll != null) {
            
                $selectAll.click(function(event){
                    event.stopPropagation();
                    if ($(this).attr('rel') != "false") {
                        $(this).attr('rel', 'false');
                        $vetTable.find("tbody input[type='checkbox']").attr("checked", "checked").parent().parent().addClass("selected");
                    }
                    else {
                        $(this).removeAttr('rel');
                        $vetTable.find("tbody input[type='checkbox']").removeAttr("checked").parent().parent().removeClass("selected");
                    }
                });
            }			
			
        });
        
        return this;
        
    };
    
})(jQuery);

