function getParameter(parameterName) {
	parameterName = parameterName + "=";
	if (this.location.search.length > 0) {
		begin = this.location.search.indexOf(parameterName);
		if (begin == -1) return "null";
		begin += parameterName.length;
		end = this.location.search.indexOf ("&" , begin);
		if (end == -1) end = this.location.search.indexOf ("?" , begin);  // if parameters are separated by '?'
		if (end == -1) end = this.location.search.length;
		return unescape (this.location.search.substring(begin, end));
	}

	return "null";
}

var PORTAL = getParameter('portal');
var LANG = getParameter('lang');
if(LANG&&LANG.length>2) LANG=LANG.substring(0,2);
var ROOM = getParameter('room');
var GAME = getParameter('game').toLowerCase();
if(GAME == 'billiard')
	GAME += 's';
if(GAME == 'snookerpro')
	GAME = 'snooker';
if(GAME == 'chessbullet1')
	GAME = 'chessbullet';

var NOW = new Date();
var TOURS = new Array();

var games = "8ball.9ball.snooker.tanx.billiards.darts501.darts.dartscricket.dartsparchess.chess.chessbullet".split('.');
var days = "SunMonTueWedThuFriSat";

function gamendx(game) {
	var i = games.length;
	while(--i >= 0 && games[i] != game)
		;
	return i;
}

if(GAME != 'null')
	TITLE += " - " + GAMES[gamendx(GAME)];

function Tour(name, time, dur, game, pub, place) {
	this.name = name;
	this.time = time;
	this.dur = dur;
	this.game = game;
	this.pub = pub;
	this.place = place;
}

function addTour(t) {
	var i, j = TOURS.length;
	for(i = 0; i < j; i++)
		if(TOURS[i].time > t.time)
			break;
	while(--j >= i)
		TOURS[j + 1] = TOURS[j];
	TOURS[i] = t;
}

function defTour(t, dur, gamegroup, game, pub, place, period) {
	if(GAME != "null" && GAME != game && GAME != gamegroup)
		return;
	game = GAMES[gamendx(game)];
	var day = DAYS[days.indexOf(t.substring(0, 3)) / 3];
	var name = day + t.substring(16, 22) + ' UTC<BR>' + (period == 7 ? EVERY1W : period == 14 ? EVERY2W : EVERY4W);
	period *= 24*3600000;
	var time = new Date(t);
	while(time.getTime() + dur*3600000 < NOW.getTime())
		time.setTime(time.getTime() + period);
	addTour(new Tour(name, time, dur + ' ' + (dur > 1 ? HOURS : HOUR), game, pub, place));
}


if(PORTAL != 'xreal_com') {
	//defTour("Sun, 19 Jan 2003 18:00:00 UTC", 3, "billiards", "snooker", true, ROOM2, 28);
	defTour("Tue, 21 Jan 2003 19:00:00 UTC", 2, "billiards", "snooker", false, ROOM2, 14);
	defTour("Thu, 23 Jan 2003 02:00:00 UTC", 1, "billiards", "8ball", false, ROOM2, 14);
	defTour("Sat, 25 Jan 2003 20:00:00 UTC", 1, "tanx", "tanx", false, ROOM2 + '<br>' + DESERT, 28);
	defTour("Mon, 27 Jan 2003 02:00:00 UTC", 1, "billiards", "9ball", false, ROOM2, 14);
	defTour("Tue, 28 Jan 2003 20:00:00 UTC", 1, "billiards", "9ball", true, ROOM2, 28);
	defTour("Thu, 30 Jan 2003 02:00:00 UTC", 1, "tanx", "tanx", false, ROOM2 + '<br>' + MARS, 28);
	defTour("Thu, 13 Feb 2003 02:00:00 UTC", 1, "tanx", "tanx", false, ROOM2 + '<br>' + SNOW, 28);
	//defTour("Sat,  1 Feb 2003 20:00:00 UTC", 2, "billiards", "8ball", true, ROOM2, 28);
	defTour("Sun,  2 Feb 2003 18:00:00 UTC", 2, "billiards", "snooker", false, ROOM2, 14);
	defTour("Sat,  8 Feb 2003 20:00:00 UTC", 1, "tanx", "tanx", true, ROOM1 + '<br>' + CLASSIC, 28);
	defTour("Tue, 11 Feb 2003 20:00:00 UTC", 1, "billiards", "9ball", false, ROOM2, 28);
	defTour("Sat, 15 Feb 2003 20:00:00 UTC", 2, "billiards", "8ball", false, ROOM2, 14);
	defTour("Fri, 22 Feb 2008 20:00:00 UTC", 1, "darts", "darts501", false, ROOMT, 14);
	defTour("Fri, 29 Feb 2008 20:00:00 UTC", 1, "darts", "dartscricket", false, ROOMT, 28);
	defTour("Fri, 14 Mar 2008 20:00:00 UTC", 1, "darts", "dartsparchess", false, ROOMT, 28);
	defTour("Wed, 27 Feb 2008 20:00:00 UTC", 1, "chess", "chessbullet", false, ROOMT, 7);
}

function td(content) {
	document.write('<TD ALIGN=LEFT><SPAN CLASS="normal">' + content + '</SPAN></TD>');
}

function writeTour(t) {
	document.write('<TR VALIGN=top>');
	td(t.name);
	var s = dd(t.time);
	var i, j;
	if((i = t.time.getTime()) < (j = NOW.getTime()))
		s += "<br><font color=red><b>" + CURRENT + "</b></font>";
	else if(i - 48*3600000 < j)
		if((i = t.time.getDate()) == NOW.getDate())
			s += "<br><font color=red><b>" + TODAY + "</b></font>"
		else if(i == new Date(NOW.getTime() + 24*3600000).getDate())
			s += "<br><font color=red><b>" + TOMORROW + "</b></font>"
	td(s);
	td(t.dur);
	td(t.game);
	td(t.pub ? EVERYONE : SUBS);
	td(t.place);
	document.write('</TR>');
}

function writeTours() {
	var i;
	for(i = 0; i < TOURS.length; i++)
		writeTour(TOURS[i]);
}

function getNextTour() {
	t = TOURS[0];
	if(!t) return '';
	var s = dd(t.time);
	var i, j;
	if((i = t.time.getTime()) < (j = NOW.getTime()))
		s += "<br><font color=red><b>" + CURRENT + "</b></font>";
	else if(i - 48*3600000 < j)
		if((i = t.time.getDate()) == NOW.getDate())
			s += "<br><font color=red><b>" + TODAY + "</b></font>"
		else if(i == new Date(NOW.getTime() + 24*3600000).getDate())
			s += "<br><font color=red><b>" + TOMORROW + "</b></font>"
	return s;
}

document.write('<title>' + TITLE + '</title>');

var TOUR_JS = 101;