function showTippuebersicht(){
  var n = 0;
  $('#content table').each(function(index) {
    n++;
    if(n == 1){
      $(this).attr("class","borderbottom");
      $(this).before('<table class="bordertop"><tr><td><span>'+$(this).children('caption').html()+'</span></td></tr></table>');
      $(this).wrap('<div class="borderbottom-container" />');
    }
    if(n == 2){
       $(this).attr("class","display");
       $(this).attr("id","tippuebersicht-tabelle");
    }
    if(n == 3){
      $(this).attr("class","display");
      $(this).attr("id","tippuebersicht");
      $(this).after('<div id="tippuebersicht-footer"></div>');
      // hole erste Zeile und rechnet Anzahl der TD
      var allTd = "";
      $('#tippuebersicht tbody tr:nth-child(2) td').each(function(index) {
        allTd = allTd+"<td>-</td>";
      });
      $('#tippuebersicht tr').each(function(index) {
        var summe = $(this).children().length;
        if(summe == 1){
          var aValue = $(this).children().html();
          $(this).remove();
          var tfooter = $('#tippuebersicht-footer').html();
          $('#tippuebersicht-footer').html(tfooter+"&nbsp;&nbsp;"+aValue);
        }
      });
    }
    
  });
  // Tippübersicht Spielplan
	if($('#tippuebersicht-tabelle').length > 0){
  	$('#tippuebersicht-tabelle').before('<div class="table-header"><span>'+$('#tippuebersicht-tabelle caption').html()+'</span></div>');
    var oTableRelegation = $('#tippuebersicht-tabelle').dataTable( {
			"bJQueryUI": true,
      "bPaginate": false,
      oLanguage : {
        "sProcessing": "Laden...",
				"sLengthMenu": "_MENU_ Datensätze anzeigen",
				"sZeroRecords": "Es wurde kein Treffer gefunden.",
				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
				"sInfoEmpty": "0 Datensatz",
				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
				"sInfoPostFix": "",
				"sSearch": "Suchen:"
      }					
		} );
  }
  
  /* Tippübersicht */
  if($("#tippuebersicht").length > 0){
     $('#tippuebersicht').before('<div class="table-header"><span>'+$('#tippuebersicht caption').html()+'</span></div>');
     var nhead = 0;
     $('#tippuebersicht thead tr').each(function(index) {
        nhead++;
        if(nhead == 1){
          $(this).children().attr('rowSpan',1);
          /*$(this).children().removeAttr("rowspan");*/          
        }else{
          var oldhtml = $(this).html();
          $(this).html('<th class="empty">&nbsp;</th><th class="empty">&nbsp;</th><th class="empty">&nbsp;</th>'+oldhtml);
        }
     });
     if($('#tippuebersicht tbody tr:nth-child(1) td').length > 8){
         var oTableTippuebersicht = $('#tippuebersicht').dataTable( {
      			"bJQueryUI": true,
            "sScrollX": "100%",
            "sScrollXInner": "200%",
            "bScrollCollapse": true,
            "sPaginationType": "full_numbers",
            "bPaginate": false,
            "bSorting": false,
            oLanguage : {
              "sProcessing": "Laden...",
      				"sLengthMenu": "_MENU_ Datensätze anzeigen",
      				"sZeroRecords": "Es wurde kein Treffer gefunden.",
      				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
      				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
      				"sInfoEmpty": "0 Datensatz",
      				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
      				"sInfoPostFix": "",
      				"sSearch": "Suchen:"
            }					
      		} );
      		/* new FixedHeader( oTable); */  		
          
          new FixedColumns( oTableTippuebersicht, {
      			"iLeftColumns": 3,
      			"iRightColumns": 0
      		} );
      }else{
         $('#tippuebersicht').dataTable( {
      			"bJQueryUI": true,
            "bPaginate": false,
            "bSorting": false,
            oLanguage : {
              "sProcessing": "Laden...",
      				"sLengthMenu": "_MENU_ Datensätze anzeigen",
      				"sZeroRecords": "Es wurde kein Treffer gefunden.",
      				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
      				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
      				"sInfoEmpty": "0 Datensatz",
      				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
      				"sInfoPostFix": "",
      				"sSearch": "Suchen:"
            }					
      		} );
      }
  		
  }
  // input type submit
  if($('#content input[type=submit]').length > 0){
    $('#content input[type=submit]').attr("class","anzeigen");
  }
}

function showLivepunkte(){
  var n = 0;
  $('#content table').each(function(index) {
    n++;
    if(n == 1){
      $(this).attr("class","borderbottom");
      $(this).before('<table class="bordertop"><tr><td><span>'+$(this).children('caption').html()+'</span></td></tr></table>');
      $(this).wrap('<div class="borderbottom-container" />');
      // input type submit
      if($('#content table tr td input[type=submit]').length > 0){
        $('#content table tr td input[type=submit]').attr("class","anzeigen");
      }
    }
    if(n == 2){
       $(this).attr("class","display");
       $(this).attr("id","livepunkte-spieltag");      
    }
    if(n == 3){
      $(this).attr("class","display");
      $(this).attr("id","livepunkte");
      $(this).after('<div id="livepunkte-footer"></div>');
      // hole erste Zeile und rechnet Anzahl der TD
      var allTd = "";
      $('#livepunkte tbody tr:nth-child(2) td').each(function(index) {
        allTd = allTd+"<td>-</td>";
      });
      $('#livepunkte tr').each(function(index) {
        var summe = $(this).children().length;
        if(summe == 1){
          var aValue = $(this).children().html();
          $(this).remove();
          var tfooter = $('#livepunkte-footer').html();
          $('#livepunkte-footer').html(tfooter+"&nbsp;&nbsp;"+aValue);
        }
      });
    }
    
  });
  // Livepunkte Spieltag
	if($('#livepunkte-spieltag').length > 0){
  	if($('#livepunkte-spieltag tr:last td').length == 1){
      var anzeigen = $('#livepunkte-spieltag tr:last td').html();
      $('#livepunkte-spieltag tr:last').remove();      
      $('#livepunkte-spieltag').after('<div>'+anzeigen+'</div>');
    }
    $('#livepunkte-spieltag').before('<div class="table-header"><span>'+$('#livepunkte-spieltag caption').html()+'</span></div>');
    var oTableRelegation = $('#livepunkte-spieltag').dataTable( {
			"bJQueryUI": true,
      "bPaginate": false,
      oLanguage : {
        "sProcessing": "Laden...",
				"sLengthMenu": "_MENU_ Datensätze anzeigen",
				"sZeroRecords": "Es wurde kein Treffer gefunden.",
				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
				"sInfoEmpty": "0 Datensatz",
				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
				"sInfoPostFix": "",
				"sSearch": "Suchen:"
      }					
		} );
  }
  
  /* Livepunkte */
  if($("#livepunkte").length > 0){
    
    var nhead = 0;
    $('#livepunkte thead tr').each(function(index) {
        nhead++;
        if(nhead == 1){
          /*alert($(this).children().removeAttr("rowspan"));*/
          var childElem = $(this).children();
          $(childElem).attr('rowSpan',1);
        }else{
          var oldhtml = $(this).html();
          $(this).html('<th class="empty">&nbsp;</th><th class="empty">&nbsp;</th><th class="empty">&nbsp;</th>'+oldhtml);
        }
    });
    
    $('#livepunkte').before('<div class="table-header"><span>'+$('#livepunkte caption').html()+'</span></div>');
    if($('#livepunkte tbody tr:nth-child(1) td').length > 8){
        var oTableLivepunkte = $('#livepunkte').dataTable( {
      			"bJQueryUI": true,
            "sScrollX": "100%",
            "sScrollXInner": "200%",
            "bScrollCollapse": true,
            "sPaginationType": "full_numbers",
            "bPaginate": false,
            oLanguage : {
              "sProcessing": "Laden...",
      				"sLengthMenu": "_MENU_ Datensätze anzeigen",
      				"sZeroRecords": "Es wurde kein Treffer gefunden.",
      				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
      				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
      				"sInfoEmpty": "0 Datensatz",
      				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
      				"sInfoPostFix": "",
      				"sSearch": "Suchen:"
            }					
      		} );
    		new FixedColumns( oTableLivepunkte, {
    			"iLeftColumns": 3,
    			"iRightColumns": 0
    		} );
    }else{
       $('#livepunkte').dataTable( {
    			"bJQueryUI": true,
          "bPaginate": false,
          oLanguage : {
            "sProcessing": "Laden...",
    				"sLengthMenu": "_MENU_ Datensätze anzeigen",
    				"sZeroRecords": "Es wurde kein Treffer gefunden.",
    				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
    				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
    				"sInfoEmpty": "0 Datensatz",
    				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
    				"sInfoPostFix": "",
    				"sSearch": "Suchen:"
          }					
    		} );
    }
        	  
  }
}

function showGesamtuebersicht(){
  var n = 0;
  $('#content table').each(function(index) {
    n++;
    if(n == 1){
      $(this).attr("class","borderbottom");
      $(this).before('<table class="bordertop"><tr><td><span>Ansicht</span></td></tr></table>');
      $(this).wrap('<div class="borderbottom-container" />');
      // input type submit
      if($('#content table tr td input[type=submit]').length > 0){
        $('#content table tr td input[type=submit]').attr("class","anzeigen");
      }
    }
    if(n == 2){
       var leftColumn = true;
       var vonIndex = get_url_param("tippspieltagVonIndex");
       var bisIndex = get_url_param("tippspieltagBisIndex");
       if(vonIndex!="" || bisIndex!=""){
          var vonIndexInt = parseInt(vonIndex);
          var bisIndexInt = parseInt(bisIndex);
          if((bisIndexInt - vonIndexInt) < 8){
              var leftColumn = false;
          }
       }
       $(this).attr("class","display");
       $(this).attr("id","gesamtuebersicht");
       $(this).before('<div class="table-header"><span>'+$('#gesamtuebersicht caption').html()+'</span></div>');
       $(this).after('<div id="gesamtuebersicht-footer"></div>');
       $('#gesamtuebersicht tr').each(function(index) {
        var summe = $(this).children().length;
        if(summe == 1){
          var aValue = $(this).children().html();
          $(this).remove();
          var tfooter = $('#gesamtuebersicht-footer').html();
          $('#gesamtuebersicht-footer').html(tfooter+"&nbsp;&nbsp;"+aValue);
        }
       });
       /* Gesamtübersicht */
       if($("#gesamtuebersicht").length > 0){
          // 2 first thead th mit &nbsp; füllen
          var firstth = $('#gesamtuebersicht thead tr th')[0];
          $(firstth).html("&nbsp;");
          var secondth = $('#gesamtuebersicht thead tr th')[1];
          $(secondth).html("&nbsp;");
          
          
          if(leftColumn){
            var oTableUebersicht = $('#gesamtuebersicht').dataTable( {
          			"bJQueryUI": true,
                "sScrollX": "100%",
                "sScrollXInner": "300%",
                "bScrollCollapse": true,
                "sPaginationType": "full_numbers",
                "bPaginate": false,
                oLanguage : {
                  "sProcessing": "Laden...",
          				"sLengthMenu": "_MENU_ Datensätze anzeigen",
          				"sZeroRecords": "Es wurde kein Treffer gefunden.",
          				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
          				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
          				"sInfoEmpty": "0 Datensatz",
          				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
          				"sInfoPostFix": "",
          				"sSearch": "Suchen:"
                }					
          		} );
        		new FixedColumns( oTableUebersicht, {
        			"iLeftColumns": 2,
        			"iRightColumns": 0
        		} );
          }else{
            var oTableUebersicht = $('#gesamtuebersicht').dataTable( {
          			"bJQueryUI": true,                            
                "sPaginationType": "full_numbers",
                "bPaginate": false,
                oLanguage : {
                  "sProcessing": "Laden...",
          				"sLengthMenu": "_MENU_ Datensätze anzeigen",
          				"sZeroRecords": "Es wurde kein Treffer gefunden.",
          				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
          				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
          				"sInfoEmpty": "0 Datensatz",
          				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
          				"sInfoPostFix": "",
          				"sSearch": "Suchen:"
                }					
          		} );            
          }
          	
      }
    }
  });
}

function get_url_param( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );

	if ( results == null )
		return "";
	else
		return results[1];
}

function showSpielplan(){
  var n = 0;
  $('#content table').each(function(index) {
    n++;
    $(this).attr("id","spieltag"+n);
    $('#spieltag'+n).before('<div class="table-header"><span>'+$('#spieltag'+n+' caption').html()+'</span></div>');
    if($('#spieltag'+n).length > 0){
      	var oTableRelegation = $('#spieltag'+n).dataTable( {
    			"bJQueryUI": true,
          "bPaginate": false,
          oLanguage : {
            "sProcessing": "Laden...",
    				"sLengthMenu": "_MENU_ Datensätze anzeigen",
    				"sZeroRecords": "Es wurde kein Treffer gefunden.",
    				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
    				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
    				"sInfoEmpty": "0 Datensatz",
    				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
    				"sInfoPostFix": "",
    				"sSearch": "Suchen:"
          }					
    		} );
    	}
  });
}

function showTabellen(){
  var n = 0;
  $('#content table').each(function(index) {
    n++;
    if(n == 1){
      $(this).before('<table class="bordertop"><tr><td><span>'+$(this).children('caption').html()+'</span></td></tr></table>');
      $(this).wrap('<div class="borderbottom-container" />');
    }
    
    if(n == 2){
      // Tabellen
      $(this).attr("class","display");
      $(this).attr("id","tabellen");
      $(this).before('<div class="table-header"><span>'+$('#tabellen caption').html()+'</span></div>');
      if($('#tabellen').length > 0){
          var oTable = $('#tabellen').dataTable( {
      			"bJQueryUI": true,
            "sScrollX": "100%",
            "sScrollXInner": "190%",
            "bScrollCollapse": true,
            "sPaginationType": "full_numbers",
            "bPaginate": false,
            oLanguage : {
              "sProcessing": "Laden...",
      				"sLengthMenu": "_MENU_ Datensätze anzeigen",
      				"sZeroRecords": "Es wurde kein Treffer gefunden.",
      				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
      				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
      				"sInfoEmpty": "0 Datensatz",
      				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
      				"sInfoPostFix": "",
      				"sSearch": "Suchen:"
            }					
      		} );
      		/* new FixedHeader( oTable); */
      		new FixedColumns( oTable, {
      			"iLeftColumns": 2,
      			"iRightColumns": 0
      		} );
    		}
    }
    
    if(n == 3){
        $(this).attr("class","display");
        $(this).attr("id","relegation");
        $(this).before('<div class="table-header"><span>'+$('#relegation caption').html()+'</span></div>');
        // Tabellen Relegation
      	if($('#relegation').length > 0){
        	// thead erstellen
        	$('#relegation tbody').before('<thead><tr><th>Datum</th><th>Heim</th><th>Aufwärts</th><th>Ergebnis</th></tr></thead>');
          
          var oTableRelegation = $('#relegation').dataTable( {
      			"bJQueryUI": true,
            "bPaginate": false,
            oLanguage : {
              "sProcessing": "Laden...",
      				"sLengthMenu": "_MENU_ Datensätze anzeigen",
      				"sZeroRecords": "Es wurde kein Treffer gefunden.",
      				"sEmptyTable": "Keine Daten in Tabelle vorhanden.",
      				"sInfo": "_START_ - _END_ von _TOTAL_ Datensätze",
      				"sInfoEmpty": "0 Datensatz",
      				"sInfoFiltered": "(Von _MAX_ Datensätze gefiltert)",
      				"sInfoPostFix": "",
      				"sSearch": "Suchen:"
            }					
      		} );
        }
    }
  });
  
  // anzeigen button
  if($('input[type=submit]').length > 0){
    $('input[type=submit]').attr("class","anzeigen");
  }
}

function tippabgabe(){
  standardPage();
  anzeigenbutton();
  if($('#content table.transparent').length > 0){
    $('#content table.transparent').each(function(index) {
        // anzahl td von table.transparent
        var transparentTD = $(this).children().children().children();
        if($(transparentTD).length > 1){
          $(transparentTD).attr("class","transparent-two");
          var firstElemt = $('.transparent-two')[0];
          $(firstElemt).attr("class","transparent-two transparent-first"); 
        }
    });
  }
}


function standardPage(){
   $('#content table').each(function(index) {
     if($(this).children("caption").length > 0){
        $(this).before('<table class="bordertop"><tr><td><span>'+$(this).children('caption').html()+'</span></td></tr></table>');
        $(this).attr("class","borderbottom");
        $(this).wrap('<div class="borderbottom-container" />');
     }
   });
}

function anzeigenbutton(){
  var anzeigen = $('.borderbottom p.submit input').length;
  if(anzeigen > 0){
    for(var i=0;i<anzeigen;i++){       
       if($('.borderbottom p.submit input')[i].value == "Anzeigen"){
          var inputSubmit = $('.borderbottom p.submit input')[i];
          $(inputSubmit).attr("class","anzeigen");
       }
    }
  }
}

$(document).ready(function() {
  /* delete first H2 */
  $('#content h2:first').css("display","none");
  if($('#content h2:first').length > 0){
    $('#topheadline h1:first').html($('#content h2:first').html());
  }
  
  switch(window.location.pathname){
    case '/tippuebersicht.html'   :  showTippuebersicht(); break;
    case '/livepunkte.html'       :  showLivepunkte(); break;
    case '/gesamtuebersicht.html' :  showGesamtuebersicht(); break;
    case '/tippspielplan.html'    :  showSpielplan(); break;
    case '/tabellen.html'         :  showTabellen(); break;
    case '/tippabgabe.html'       :  tippabgabe();break;
    default                       :  standardPage(); break;
  }
  
  /* Anzeigen submit class */
  anzeigenbutton();
  
} );
