/**
 * Next dates chessclub Rhy Rheinfelden
 * Author: H. Kortschak
 * Date: 13.12.2009
 * Version: 2.0
 */

var myDoc;
var buffer   = new Array( nTournaments );
var TDList   = new Array( nTournaments );

var date = new Date();
var today = date.getDate() + ". " + ( date.getMonth() + 1 ) + ". " + date.getFullYear();
var line2class = "date"; // default

myDoc = window.document;

// Main sequence
function displayNextDates(){
  setLabels(); 
  searchNextDate();
  sortDates();
  writeParagraphs(); 
  assembleDOMNodes();
}

///////////////////////
// function section ///
///////////////////////

// Set labels for searching and sorting
function setLabels(){
  for( var i = 0; i < nTournaments; ++i ){
      buffer[i] = new Object();
  }

 buffer[0].label = CATEGORY_SGM1;
 buffer[1].label = CATEGORY_SMM;   
 buffer[2].label = CATEGORY_NMM;
 buffer[3].label = CATEGORY_VEREIN;
 buffer[4].label = CATEGORY_TRAINING;
 buffer[5].label = CATEGORY_FREEPLAY;
 buffer[6].label = CATEGORY_TEAMCUP;
 buffer[7].label = CATEGORY_REGION;
 buffer[8].label = CATEGORY_SGM2;
 buffer[9].label = CATEGORY_WP;
 buffer[10].label = CATEGORY_RAPID;
 buffer[11].label = CATEGORY_VM;
}
 
// Write the paragraphs the old-fashioned way before calling assembleDOMNodes()
function writeParagraphs(){
  line2class = ( ! check2ndLine() ) ? "date" : "topDate"; 
  myDoc.writeln( "<div style='float:right;position:relative;' >" );
  myDoc.writeln( "<table class='Famous' width='500px;'>" );
  myDoc.writeln( "<tr><td class='dateHeader' style='border:none;padding-left:5px;text-align:left;' colspan='3'><img src='images/zeigfinger.jpg' width='35px'> Nächste Termine ab " + today + " </td></tr>" );
  myDoc.writeln( "<tr class='topDate'><td class='topDate' id='date0'></td><td class='topDate' id='label0'></td><td class='topDate' id='details0'></td></tr>" );
  myDoc.writeln( "<tr><td class='" + line2class + "' id='date1'></td><td class='" + line2class + "' id='label1'></td><td class='" + line2class + "' id='details1'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date2'></td><td class='date' id='label2'></td><td class='date' id='details2'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date3'></td><td class='date' id='label3'></td><td class='date' id='details3'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date4'></td><td class='date' id='label4'></td><td class='date' id='details4'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date5'></td><td class='date' id='label5'></td><td class='date' id='details5'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date6'></td><td class='date' id='label6'></td><td class='date' id='details6'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date7'></td><td class='date' id='label7'></td><td class='date' id='details7'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date8'></td><td class='date' id='label8'></td><td class='date' id='details8'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date9'></td><td class='date' id='label9'></td><td class='date' id='details9'></td></tr>" );
  myDoc.writeln( "<tr><td class='date' id='date10'></td><td class='date' id='label10'></td><td class='date' id='details10'></td></tr>");
  myDoc.writeln( "<tr><td class='date' id='date11'></td><td class='date' id='label11'></td><td class='date' id='details11'></td></tr></table>" );
  myDoc.writeln( "<table class='Famous' style='font-size:12px;' width='500px;'>" );
  //myDoc.writeln( "<tr><td class='dateHeader' style='font-size:12px;text-align:left; padding-left:5px; border:none;'>Schachkurs bei <a href='http://www.feparheinfelden.ch/KursDetailAction.do?id=402881a1281f4bcc01281f51696e0007' target='_blank'>FerienSpass</a> im Sommer 2010!</td><td class='dateHeader' style='font-size:12px;text-align:right; padding-right:5px; border:none;'>Angebot f&uuml;r <a href='newmember.html'>neue</a> Mitglieder</td></tr>" );
  myDoc.writeln( "<tr><td class='dateHeader' style='font-size:12px;text-align:left; padding-left:5px; border:none;'><a href='jugend.html'>Jugendschach</a></td></tr>" );
  myDoc.writeln( "</table></div>" );
}

// modification: the rouine ignores now empty entries in 'buffer'
function assembleDOMNodes() {
  var node;
  for( var i = 0; i < nTournaments; ++i ){
      TDList[i] = myDoc.getElementById( "date" + i );
  }
  for( var i = 0; i < nTournaments; ++i ){
    if( buffer[i].date == "" ){
      continue;
    }
    TDList[i].appendChild( myDoc.createTextNode( buffer[i].date ));
    node = TDList[i].nextSibling;
    if( node != null ){
      node.appendChild( myDoc.createTextNode( buffer[i].label ));
      node = node.nextSibling;
      if( node != null ){
	node.appendChild( myDoc.createTextNode( buffer[i].text ));
      }
    }
  }
}  

function searchNextDate() {
  var index, lut, maxRound;

  for( var i = 0; i < nTournaments; ++i )
    {
      index = -1;
      switch( buffer[i].label ){
      case CATEGORY_SGM1 : lut = SGM_Rhy1; maxRound = SGM_Rhy1.length; break;
      case CATEGORY_SGM2 : lut = SGM_Rhy2; maxRound = SGM_Rhy2.length; break;
      case CATEGORY_SMM : lut = SMM_Rhy;  maxRound = SMM_Rhy.length; break;
      case CATEGORY_NMM : lut = NMM_Rhy;  maxRound = NMM_Rhy.length; break;
      case CATEGORY_VEREIN : lut = Verein_Rhy;   maxRound = Verein_Rhy.length; break;
      case CATEGORY_TRAINING : lut = Training_Rhy; maxRound = Training_Rhy.length; break;
      case CATEGORY_FREEPLAY : lut = FreePlay_Rhy; maxRound = FreePlay_Rhy.length; break;
      case CATEGORY_TEAMCUP : lut = Teamcup_Rhy; maxRound = Teamcup_Rhy.length; break;
      case CATEGORY_REGION : lut = Region_Rhy; maxRound = Region_Rhy.length; break;
      case CATEGORY_RAPID: lut = Rapid_Rhy; maxRound = Rapid_Rhy.length; break;
      case CATEGORY_WP: lut = WP_Rhy; maxRound = WP_Rhy.length; break;
      case CATEGORY_VM: lut = VM_Rhy; maxRound = VM_Rhy.length;break;
      default: alert( "Wrong switch value found " + buffer[i].label );
	break;
      }
        
      for( var loop = 0; loop < maxRound; ++loop ){
	  if( date.getTime() < lut[loop][DATECTRL] ){
	      index = loop;
	      break;
	  }               
      } 
       
      if( index >= 0 ) {
	buffer[i].key = lut[index][DATECTRL];
	buffer[i].date = lut[index][DATEDISP];
	buffer[i].text = lut[index][DATEINFO];         
      }
      else {
	buffer[i].key = 99999999999999;
	buffer[i].date = "";
	buffer[i].text = "";
      }
    }
}
      
// Sorting algorithm based on straightselection
function sortDates() {
  var i, j, k;
  var x; 
  for( i = 0; i < nTournaments - 1; ++i )
    {
      k = i; 
      x = buffer[i];
      for( j = i+1; j < nTournaments; ++j )
	{
	  if( buffer[j].key < x.key ) 
	    {
	      k = j;
	      x = buffer[j];
	    }
	}
      buffer[k] = buffer[i];              
      buffer[i] = x;                   
    }
}     

// compare the date of the 2nd line with the 1st in order to assign the class 'topDate' instead of 'date'
function check2ndLine() {
  var topLine = null;
  var Line2   = null;
  if( buffer[0].key != buffer[1].key )
    return( false );
  else return( true );
}

	
myDoc.close();

