// UNPLAYED FIXTURES (INITIALLY CREATED FROM database.js)
// fixtures.js

// RETURNS STRING OF FORM...
// <DIV CLASS="sfbanner_title" STYLE="float:left;">[Title]</DIV>
// <DIV CLASS="sfbanner_msg">[Msg]</DIV>
// Width - OPTIONAL WIDTH OF MSG
function f_sfbannerTextStyle(Title,Msg,Width) {
  var t;
  t = '<DIV CLASS="sfbanner_title">' + Title + '</DIV>';
  t += '<DIV CLASS="sfbanner_msg"';
  if (typeof(Width) != 'undefined') t += ' STYLE="position:relative;width:' + Width + 'px;"';
  t += '>' + Msg + '</DIV>';
  return t;
}

// DECIDES WHAT TO PUT IN SF_BANNER
// 1. NEWS/DIARY P1
// 2. FIXTURE TODAY (OR TOMORROW)
// 3. HISTORY (If less than 5 days old)
// 4. RECENT RESULT (Any result posted recently - always Priority 2)
// 5. NEWS/DIARY P2 (and HISTORY > 5 DAYS OLD)
// 6. HISTORY (Default)
// NEEDS history.js, news.js
// <DIV CLASS="sfbanner_title" STYLE="float:left;">What\'s New</DIV>
// <DIV CLASS="sfbanner_msg">' + f_lastHistory() + '</DIV>
function f_sfbannerText() {
  var title, msg, fields, daysago;

  // CHECK NEWS - PRIORITY 1
  msg = f_news(_N,_CURRENT,_BRIEF,1);
  if (isValidData(msg)) {
    title = 'News';
    return f_sfbannerTextStyle(title,msg);
  }

  // CHECK DIARY - PRIORITY 1
  msg = f_news(_D,_CURRENT,_BRIEF,1);
  if (isValidData(msg)) {
    title = 'Diary';
    return f_sfbannerTextStyle(title,msg);
  }

  // FIXTURE TODAY
  msg = f_fixtures(0,true);
  if (isValidData(msg)) {
    title = 'Match Fixture';
		// Presence of 'blankdot' inidicates dividing line, ie. More than one!
		if (msg.indexOf('blankdot') > -1) {title += 's';}
    return f_sfbannerTextStyle(title,msg);
  }

  // FIXTURE TOMORROW
  msg = f_fixtures(1,true);
  if (isValidData(msg)) {
    title = 'Match Fixture';
		if (msg.indexOf('blankdot') > -1) {title += 's';}		
    return f_sfbannerTextStyle(title,msg);
  }

  // HOW OLD IS HISTORY?
  fields = History[History.length - 1].split('|');
  daysago = f_daysAgo(fields[0]);

  // IF HISTORY IS FAIRLY NEW, THEN SHOW IT!
  if (daysago < 5) {
    title = 'What\'s New';
    msg = f_lastHistory();
    return f_sfbannerTextStyle(title,msg);
  }

  // CHECK RECENT RESULT - (Always PRIORITY 2)
  msg = f_news(_R,_CURRENT,_BRIEF,2);
  if (isValidData(msg)) {
    title = 'Recent Result';
    return f_sfbannerTextStyle(title,msg);
  }	
	
  // CHECK NEWS - PRIORITY 2
  msg = f_news(_N,_CURRENT,_BRIEF,2);
  if (isValidData(msg)) {
    title = 'News';
    return f_sfbannerTextStyle(title,msg);
  }

  // CHECK DIARY - PRIORITY 2
  msg = f_news(_D,_CURRENT,_BRIEF,2);
  if (isValidData(msg)) {
    title = 'Diary';
    return f_sfbannerTextStyle(title,msg);
  }

  // DEFAULT - SHOW HISTORY
  title = 'What\'s New';
  msg = f_lastHistory();
  return f_sfbannerTextStyle(title,msg);
}

// RETURNS LIST OF FIXTURES IN NEXT n DAYS
// Days - NUMBER OF DAYS (NB: 0 WILL INDICATE TODAY)
// Split - true/false (DEFAULT: false) SPLIT ONTO MULTIPLE LINES IF IN HEADER
// Raw - true/false (DEFAULT: false) - Displays in Raw Text format for copying to file
// NB: Special case... If more than 1 record found when Days is 0 or 1 and Split is True
//                     then do not display 2nd date, since it will be the same 
function f_fixtures(Days,Split,Raw) {
  var t = '';
  var count;
  var recno, fields, daysago, more;

  // INIT
  count = 0;
  more = false;	// ASSUME NO MORE FIXTURES
  if (typeof(Split) == 'undefined') Split = false;
  if (typeof(Raw) == 'undefined') Raw = false;
	if (Raw) Split == false;
	
	if (Raw) {
		t = _HOME_CLUB + ' Badminton Club<br />';
		t += 'Unplayed Match Fixtures<br />';
		t += 'Last Updated ' + f_formatDateStr(_STATIC_FIXTURES_UPDATED,false) + '<br /><br />';
	}

  // STEP THROUGH FIXTURES
  for (recno=0; recno<CurFixArr.length; recno++) {

    // RECORD
    fields = unescape(CurFixArr[recno]).split(',');

    // ALREADY PLAYED?
    // NB: IF daysago = 0, THEN IT'S TODAY - NOT ALREADY PLAYED
    daysago = f_daysAgo(fields[0]);
    if (daysago > 0) continue;

    // TOO FAR IN FUTURE THEN FINISH
    // -1 IS TOMORROW
    if (Math.abs(daysago) > Days) {
      more = true;
      break;
    }

		// Line Break
		if (count > 0) {
			t += '<br />';
	    if (!Raw) {
  	    t += '<DIV STYLE="margin-top:4px;margin-bottom:3px;border-top:1px dashed #999999;"><IMG SRC="/img/blankdot.gif" WIDTH=1 HEIGHT=1></DIV>';
			}
    }

    // SPLIT?
    if ((count == 0) && (Split)) t += '<br />';

    // DATE (TODAY OR TOMORROW)
		// if ((count > 0) && (Days < 2) && (Split)) {Don't display date}
		if ((count <=0) || (Days >=2) || (!Split)) {
      t += '<B>' + f_formatDateStr(fields[0],false,true);
  		if (!Raw) {
  	    if ((daysago == 0) || (daysago == -1)) t += '<SPAN STYLE="color:#990000;text-decoration:blink;">';
  	    if (daysago == 0) {
  	      t += ' (TODAY!)';
  	    } else if (daysago == -1) {
  	      t += ' (Tomorrow)';
  	    }
  	    if ((daysago == 0) || (daysago == -1)) t += '</SPAN>';
  		}
      t += '</B>';
  
      // SPLIT?
      if (Split) t += '<BR>';
		}

    // TEAM vs CLUB
    t += ' - ' + fields[2] + ' <B>vs</B> ';
		if (Raw) {
			t += fields[3].replace(/<[^<>]*>/g,'');
		} else {
			t += fields[3];		
		}

    // VENUE, TIME and PAIRS
    // unescape(fields[4]) - PREVIOUS
    t += ' at&nbsp;';
		if (Raw) {
			t += fields[4].replace(/<[^<>]*>/g,'');
		} else {
			t += fields[4];		
		}
		if (fields[5] > ' ') {
			t += ' - ' + fields[5] + ', ' + fields[6];
		}
		
		// Additional Info...
		if (fields[7] > ' ') {
			t += ' <b>(' + fields[7] + ')</b>';
		}

    count++;
  }

  // NO FIXTURES?
  if (count == 0) {
    t = _NODATA + '<DIV CLASS="grey">';
    if (!more) {
      t += 'No more fixtures this season.';
    } else {
      t += 'There are no match fixtures<BR>in the next ' + Days + ' days.';
    }
    t += '</DIV>';
  } else if (Raw) {
		t += '<br /><br />' + count + ' fixture(s) listed.';
	}

  return t;
}

/****************************************************
** DATA GENERATED AT Thu, 20 May 2010 11:41:54 GMT
** CURRENT (UNPLAYED) FIXTURES
****************************************************/
var _STATIC_FIXTURES_UPDATED = '20/05/2010';

var CurFixArr = new Array();

CurFixArr[0] = '24/05/2010%2CMon%26nbsp%3B24th%2CCup%2CThree%20of%20Clubs%2C%3Ca%20href%3D%22/clubs/details/crosfields_school.html%22%3ECrosfields%26nbsp%3BSchool%3C/a%3E%2C8%3A15%2C2%20pairs%2C';
