$.tablesorter.addWidget({ 


    // give the widget a id 
    id: "groups", 
    // format is called when the on init and when a sorting has finished 
    format: function(table){ 

// console.info("funkcje");


if(table.config.debug){ var time = new Date(); }


if(!table.config.emptyGroupCaption){table.config.emptyGroupCaption = "(brak)"}
if(!table.config.collapsableGroups){table.config.collapsableGroups = false}

var badajStan = function(){
  if(!table.config.sortList.toString()){
    return ["0",table.config.parsers[[0][0]].type];
  }else{
    return [table.config.sortList[0][0],table.config.parsers[table.config.sortList[0][0]].type];
  }
};


function prawidlowyOutput(input){

var output = false;

switch($.browser.msie){

  case true:
    if(rodzajDanych == "text"){
	  output = (input.innerText.toUpperCase().replace(/^\W+/,'')).replace(/\W+$/,'').substring(0,1);
    }else{
	  output = (input.innerText);
	}
  break;

  case false:
    if(rodzajDanych == "text"){
	  output = (input.textContent.toUpperCase().replace(/^\W+/,'')).replace(/\W+$/,'')[0];
    }else{
	  output = (input.textContent);
	}
break;


	}


// to szalenie zaskakujące ale to co dla ff jest 160 w ie jest 32 

  if(Boolean(output) && output.charCodeAt(0) != 160 && output.charCodeAt(0) != 32){
    return (output);	
  }else{
    return (table.config.emptyGroupCaption);		
  }


};




function collapsableGroups(){


/*
 jQuery.each(collapsed, function(){
//      $("#" + this).text("My id is " + this + ".");
console.info(this);
    });
 */
//console.info(collapsed);

 $("tbody tr",table).show();


 $("tbody tr.tablesorter-group td img",table).attr('alt','minus');

// var collapsed = new Array();

  
$("tbody tr.tablesorter-group td",table).toggle(function (){

numerTejGrupy = /\u2011([0-9]*)\u2011/;
numerGrupy = numerTejGrupy.exec($(this).parent("tr").attr("class"))[1];

$(this).parent().siblings().slice(mapaGrup[numerGrupy][0],mapaGrup[numerGrupy][1]).hide();



// collapsed[numerGrupy] = mapaGrup[numerGrupy][0]+","+mapaGrup[numerGrupy][1];



$("img",this).attr('alt','plus').next().text(mapaGrup[numerGrupy][0]-mapaGrup[numerGrupy][1]);


	},
      function (){

numerTejGrupy = /\u2011([0-9]*)\u2011/;
numerGrupy = numerTejGrupy.exec($(this).parent("tr").attr("class"))[1];



$("img",this).attr('alt','minus').next().text("");		  



$(this).parent().siblings().slice(mapaGrup[numerGrupy][0],mapaGrup[numerGrupy][1]).show();

      }
    );
}

// var typKolumny = table.config.parsers[sortowanaKolumna].type;

$("tr.tablesorter-group").remove();
$("tbody tr.filter\u2011none",table).remove();


var kolumna = badajStan()[0];
var rodzajDanych = badajStan()[1];
var iloscKolumn = table.config.headerList.length;
var numerGrupy = 0;
var mapaGrup = new Array();

for(var i=0; i < table.tBodies[0].rows.length; i++){ 

var prawidlowyOutputTenWiersz = prawidlowyOutput(table.tBodies[0].rows[i].cells[kolumna]);



set = false;
// klasa = null;

	

if(!set){
// $("tbody tr:eq("+[i]+")",table).addClass("group\u2011"+numerGrupy+"\u2011"+kolumna);
// console.info($("tbody tr:eq("+[i]+")",table));
}


if(i == 0){

    $("tbody tr:eq("+[i]+")",table).before("<tr class=\"tablesorter-group group\u2011"+numerGrupy+"\u2011header\"><td colspan=\""+iloscKolumn+"\">"+prawidlowyOutputTenWiersz+"<img src=\"whoa.gif\" alt=\"minus\"><strong></strong></td></tr>");
    i +=1;
	set = true;
    


   }
  if(table.tBodies[0].rows[i+1]){  //jeżeli istnieje następny wiersz....
    if(prawidlowyOutputTenWiersz != prawidlowyOutput(table.tBodies[0].rows[i+1].cells[kolumna])){

  	numerGrupy++;

    $("tbody tr:eq("+(i+1)+")",table).before("<tr class=\"tablesorter-group group\u2011"+numerGrupy+"\u2011header\"><td colspan=\""+iloscKolumn+"\">"+prawidlowyOutput(table.tBodies[0].rows[i+1].cells[kolumna])+"<img src=\"whoa.gif\" alt=\"minus\"><strong></strong></td></tr>"); 

mapaGrup[numerGrupy] = [(i+1)]; //poczatek

//    mapaGrup[numerGrupy] = [i+2,i+2];	
//    mapaGrup[numerGrupy-1] = [55,11];		
      i += 1;
	  	set = true;
    }
  }


mapaGrup[0] = [0,555] // dla 1 grupy  
  
mapaGrup[numerGrupy][1] = (i+1); // dla koncowki
			}

mapaGrup[0][1] = (mapaGrup[1][0])-1; //zerowy

mapaGrup[mapaGrup.length-1][1] = mapaGrup[mapaGrup.length-1][1]-1 // sposób na ostatni element;



if(table.config.collapsableGroups){
collapsableGroups();	
}


if(table.config.debug){$.tablesorter.benchmark("Applying groups widget : ", time);}			

// console.info(mapaGrup);

    } 
	
}); 
