function loadLibrary() {
	$("#lib-games").empty(); // first clear it
	var libHTML = '<div id="lib-table">';
	libHTML += '<div id="lib-thead">';
	libHTML += '<div class="ltc-1">Gametype</div>';
	libHTML += '<div class="ltc-2">Details</div>';
	libHTML += '<div class="ltc-3">Maps</div>';
	libHTML += '<div class="cbr"></div>';
	libHTML += '</div>';
	libHTML += '<div id="lib-tbody"></div>';
	libHTML += '<div class="cbr"></div>';
	libHTML += '</div>';
	$("#lib-games").html(libHTML);
	// table created, now load the rows in it
	$("#lib-tbody").empty(); // first erase loading
	var newgame;
	for (var game = 0; game < staticLibrary.length; game++) {
		// open row
		newgame = '<div class="libtr">';
		// gametype
		newgame += '<div class="ltc-1">';
		if (staticLibrary[game]['special'] == '') {
			newgame += '<img src="images/icons/' + staticLibrary[game]['gametype'] + '.jpg" alt="' + staticLibrary[game]['gametype'] + '" title="' + gametypeTitles[staticLibrary[game]['gametype']] + '" />';
		} else {
			newgame += '<img src="images/special/' + staticLibrary[game]['special'] + '.jpg" alt="special" title="Special!" />';
		}
		newgame += '</div>';
		// details
		newgame += '<div class="ltc-2"><ul>';
		newgame += '<li><strong>' + staticLibrary[game]['team-a'] + '</strong> <small>vs</small> <strong>' + staticLibrary[game]['team-b'] + '</strong></li>';
		newgame += '<li><em>' + staticLibrary[game]['info-line'] + '</em></li>';
		newgame += '<li>' + staticLibrary[game]['date'] + '</li>';
		newgame += '<li><em>' + gametypeTitles[staticLibrary[game]['gametype']] + '</em></li>';
		if (staticLibrary[game]['casters'].length > 0) {
			newgame += '<li>Casted by: ';
			for (var i = 0; i < staticLibrary[game]['casters'].length; i++) {
				if (i > 0) {
					newgame += ', ';
				}
				newgame += staticLibrary[game]['casters'][i];
			}
			newgame += '</li>';
		}
		newgame += '</ul></div>';
		// maps
		newgame += '<div class="ltc-3">';
		for (var map = 0; map < staticLibrary[game]['maps'].length; map++) {
			newgame += '<div class="libmap"><a href="#play/' + (game+1) + '/' + (map+1) + '"><img src="images/levelshots-80x60/' + staticLibrary[game]['maps'][map]['map'] + '.png" alt="' + staticLibrary[game]['maps'][map]['map'] + '" /></a><br />';
			newgame += '<em>' + staticLibrary[game]['maps'][map]['map'] + '</em>';
			newgame += '<img class="libmapqueueicon" src="images/icons/add_to_queue.png" alt="Queue" title="Add to queue" onclick="addToQueue(' + (game+1) + ',' + (map+1) + ')" />';
			newgame += '<a href="#play/' + (game+1) + '/' + (map+1) + '"><img class="libmapplayicon" src="images/icons/play.png" alt="Play" /></a>';
			newgame += '</div>';
		}
		newgame += '</div>';
		// close row
		newgame += '</div>';
		// prepend it
		$("#lib-tbody").prepend(newgame);
	}
	$("#lib-nav-item-choice-from").val(1);
	$("#lib-nav-item-choice-to").html(staticLibrary.length);//.html(staticLibrary.length>20?20:staticLibrary.length);
	$("#lib-nav-item-total").html(staticLibrary.length);
}

function loadSuggestions () {
	sugcounter = 0;
	var sughtml;
	for (var game = staticLibrary.length-1; game > -1; game--) {
		if (staticLibrary[game]['suggestion'] == true) {
			sughtml = '<div class="lib-suggestion">';
			if (staticLibrary[game]['special'] == '') {
				sughtml += '<img class="lib-sug-gametype-img" src="images/icons/' + staticLibrary[game]['gametype'] + '.jpg" alt="' + staticLibrary[game]['gametype'] + '" title="' + gametypeTitles[staticLibrary[game]['gametype']] + '" />';
			} else {
				sughtml += '<img class="lib-sug-gametype-img" src="images/special/' + staticLibrary[game]['special'] + '.jpg" alt="special" title="Special!" />';
			}
			sughtml += '<div class="lib-sug-info-box"><ul>';
			sughtml += '<li><strong>' + staticLibrary[game]['team-a'] + '</strong> <small>vs</small> <strong>' + staticLibrary[game]['team-b'] + '</strong></li>';
			sughtml += '<li><em>' + staticLibrary[game]['info-line'] + '</em></li>';
			sughtml += '<li>' + staticLibrary[game]['date'] + '</li>';
			sughtml += '<li><em>' + gametypeTitles[staticLibrary[game]['gametype']] + '</em></li>';
			if (staticLibrary[game]['casters'].length > 0) {
				sughtml += '<li>Casted by: ';
				for (var i = 0; i < staticLibrary[game]['casters'].length; i++) {
					if (i > 0) {
						sughtml += ', ';
					}
					sughtml += staticLibrary[game]['casters'][i];
				}
				sughtml += '</li>';
			}
			sughtml += '</ul></div>';
			sughtml += '<div class="lib-sug-map-box">';
			for (var map = 0; map < staticLibrary[game]['maps'].length; map++) {
				sughtml += '<div class="lib-sug-map"><a href="#play/' + (game+1) + '/' + (map+1) + '"><img src="images/levelshots-80x60/' + staticLibrary[game]['maps'][map]['map'] + '.png" alt="' + staticLibrary[game]['maps'][map]['map'] + '" /></a><br />';
				sughtml += '<em>' + staticLibrary[game]['maps'][map]['map'] + '</em>';
				sughtml += '<img class="libmapqueueicon" src="images/icons/add_to_queue.png" alt="Queue" title="Add to queue" onclick="addToQueue(' + (game+1) + ',' + (map+1) + ')" />';
				sughtml += '<a href="#play/' + (game+1) + '/' + (map+1) + '"><img class="libmapplayicon" src="images/icons/play.png" alt="Play" /></a>';
				sughtml += '</div>';
			}
			sughtml += '</div>'
			sughtml += '</div>';			
			$("#lib-suggestions").append(sughtml);
			sugcounter++;
		}
		if (sugcounter == 2) { game=-1; } //only need 2 latest suggestions!
	}
	// fill with empty boxes
	while (sugcounter < 2) {
		$("#lib-suggestions").append('<div class="lib-suggestion"></div>');
		sugcounter++;
	}
}

var currentLibMode = '';
var possibleLibModes = ['list','grid'];
function setLibMode(what) {
	for (var mode in possibleLibModes) {
		if (mode == what) {
			$('#lib-mode-' + what).addClass('active');
		} else {
			$('#lib-mode-' + mode).removeClass('active');
		}
	}
}

/* ** PLAY ** */
var libPlayer = new Object();

function playLibGame(gameid,mapid) {
	if (staticLibrary[gameid-1]!=undefined) {
		openLibPlayer();
		var stream = staticLibrary[gameid-1]['maps'][mapid-1]['stream'];
		var source = staticLibrary[gameid-1]['maps'][mapid-1]['source'];
		switch (source) {
			case 'ustream' :
				libPlayer.ratio = 5/4;
				//$("#lib-replayer").html('<object id="lib-stream-object" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '"><param name="flashvars" value="autoplay=true" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="wmode" value="transparent" /><param name="src" value="http://www.ustream.tv/flash/video/' + stream + '" /><embed flashvars="autoplay=true" width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '" allowfullscreen="true" allowscriptaccess="always" id="lib-stream-obj" src="http://www.ustream.tv/flash/video/' + stream + '" type="application/x-shockwave-flash" wmode="transparent" /></object>');
				$("#lib-replayer").html('<embed flashvars="autoplay=true" width="' + libPlayer.w + '" height="' + libPlayer.h + '" allowfullscreen="true" wmode="transparent" allowscriptaccess="always" id="lib-stream-obj" src="http://www.ustream.tv/flash/video/' + stream + '" type="application/x-shockwave-flash" />');
				break;
			case 'own3d' :
				libPlayer.ratio = 4/3;
				// 24b013d4b35d3d nog steeds correct?
				$("#lib-replayer").html('<object id="lib-stream-object" width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '"><param name="movie" value="http://www.own3d.tv/stream/' + stream + '/holysh1t" /><param name="allowfullscreen" value="true" /><param name="wmode" value="transparent" /><embed id="lib-stream-obj" src="http://www.own3d.tv/stream/' + stream + '/holysh1t" type="application/x-shockwave-flash" allowfullscreen="true" width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '" wmode="transparent" /></object>');
				break;
			case 'watchquake' : 
				libPlayer.ratio = 5/4;
				$("#lib-replayer").html('<object width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '" id="lib-stream-object" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="http://static.livestream.com/grid/LSPlayer.swf" /><param name="flashVars" value="channel=watchquake&clip=' + stream + '&autoPlay=false&mute=false" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed id="lib-stream-obj" src="http://static.livestream.com/grid/LSPlayer.swf" flashVars="channel=watchquake&clip=' + stream + '&autoPlay=false&mute=false" width="' + libPlayer.w + '" height="' + (libPlayer.w/libPlayer.ratio) + '" allowScriptAccess="always" allowFullScreen="true" type="application/x-shockwave-flash" /></object>');
				break;
			default :
				break;
		}
		var libcasterhtml = '<span>CASTERS:</span>';
		if (staticLibrary[gameid-1]['casters'].length == 0) {
			libcasterhtml += '<span class="no-casters">This game was not commented.</span>';
		} else {
			for (var i = 0; i < staticLibrary[gameid-1]['casters'].length; i++) {
				libcasterhtml += '<span class="caster';
				if (i == 0) {
					libcasterhtml += ' first';
				}
				if ( i == staticLibrary[gameid-1]['casters'].length - 1) {
					libcasterhtml += ' last';
				}
				libcasterhtml += '">' + staticLibrary[gameid-1]['casters'][i] + '</span>';
			}
		}
		$("#lib-caster-list").html(libcasterhtml);
		$("#lib-closer").css({ display: 'block' });
	} else {
		alert('Invalid game specified or missing game info');
	}
	resetHash();
}

function closeLibGame() {
	$("#lib-closer").css({ display: 'none' });
	$("#lib-replayer").empty();
	$("#lib-caster-list").empty();
	libPlayer.ratio = 0;
	closeLibPlayer();
}

function openLibPlayer() {
	$("#lib-sliderbox").css({ top: '0px' });
	$("#lib-caster-box").removeClass('closed');
	$("#wire-lib-casters-left").css('left',libPlayer.wire+'px');
	$("#wire-lib-casters-right").css('right',libPlayer.wire+'px');
	$("#lib-playertoggle-img").attr({
		'src' : 'images/icons/window_up.png',
		'title' : 'Click to hide library player'
	});
	libPlayer.open = true;
}
function closeLibPlayer() {
	$("#lib-sliderbox").css({ top: libPlayer.t + 'px' });
	$("#lib-caster-box").addClass('closed');
	$("#wire-lib-casters-left").css('left',libPlayer.lwc+'px');
	$("#wire-lib-casters-right").css('right',libPlayer.rwc+'px');
	$("#lib-playertoggle-img").attr({
		'src' : 'images/icons/window_down.png',
		'title' : 'Click to show library player'
	});
	libPlayer.open = false;
}

function enlargePlayerSize() {
	$("#reduce-player-size-icon").attr({
		'src':'images/icons/minus-icon.png',
		'title':'Click to enlarge player size'
	});
	if (libPlayer.w == 600) {
		// make 800
		libPlayer.w = 800;
		libPlayer.h = libPlayer.ratio?libPlayer.w/libPlayer.ratio:640;
		libPlayer.t = (libPlayer.h * -1) - 30;
		libPlayer.l = 217;
		libPlayer.wire = 149;
		libPlayer.lwc = 355;
		libPlayer.rwc = 333;
		$("#lib-sliderbox").css({
			width : libPlayer.w + 'px',
			height : libPlayer.h + 'px',
			left : libPlayer.l + 'px'
		});
		$("#wire-lib-casters-left").css('left',libPlayer.wire + 'px');
		$("#wire-lib-casters-right").css('right',libPlayer.wire + 'px');
		$("#lib-stream-object").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#lib-stream-obj").attr({ width: libPlayer.w, height: libPlayer.h });
	} else if (libPlayer.w == 800) {
		// make 1000
		libPlayer.w = 1000;
		libPlayer.h = libPlayer.ratio?libPlayer.w/libPlayer.ratio:800;
		libPlayer.t = (libPlayer.h * -1) - 30;
		libPlayer.l = 117;
		libPlayer.wire = 249;
		libPlayer.lwc = 455;
		libPlayer.rwc = 433;
		$("#lib-sliderbox").css({
			width : libPlayer.w + 'px',
			height : libPlayer.h + 'px',
			left : libPlayer.l + 'px'
		});
		$("#wire-lib-casters-left").css('left',libPlayer.wire + 'px');
		$("#wire-lib-casters-right").css('right',libPlayer.wire + 'px');
		$("#lib-stream-object").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#lib-stream-obj").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#enlarge-player-size-icon").attr({
			'src':'images/icons/plus-icon-disabled.png',
			'title':'Cannot enlarge further!'
		});
	}
}
function reducePlayerSize() {
	$("#enlarge-player-size-icon").attr({
		'src':'images/icons/plus-icon.png',
		'title':'Click to enlarge player size.'
	});
	if (libPlayer.w == 1000) {
		// make 800
		libPlayer.w = 800;
		libPlayer.h = libPlayer.ratio?libPlayer.w/libPlayer.ratio:640;
		libPlayer.t = (libPlayer.h * -1) - 30;
		libPlayer.l = 217;
		libPlayer.wire = 149;
		libPlayer.lwc = 355;
		libPlayer.rwc = 333;
		$("#lib-sliderbox").css({
			width : libPlayer.w + 'px',
			height : libPlayer.h + 'px',
			left : libPlayer.l + 'px'
		});
		$("#wire-lib-casters-left").css('left',libPlayer.wire + 'px');
		$("#wire-lib-casters-right").css('right',libPlayer.wire + 'px');
		$("#lib-stream-object").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#lib-stream-obj").attr({ width: libPlayer.w, height: libPlayer.h });
	} else if (libPlayer.w == 800) {
		// make 600
		libPlayer.w = 600;
		libPlayer.h = libPlayer.ratio?libPlayer.w/libPlayer.ratio:480;
		libPlayer.t = (libPlayer.h * -1) - 30;
		libPlayer.l = 317;
		libPlayer.wire = 49;
		libPlayer.lwc = 255;
		libPlayer.rwc = 233;
		$("#lib-sliderbox").css({
			width : libPlayer.w + 'px',
			height : libPlayer.h + 'px',
			left : libPlayer.l + 'px'
		});
		$("#wire-lib-casters-left").css('left',libPlayer.wire + 'px');
		$("#wire-lib-casters-right").css('right',libPlayer.wire + 'px');
		$("#lib-stream-object").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#lib-stream-obj").attr({ width: libPlayer.w, height: libPlayer.h });
		$("#reduce-player-size-icon").attr({
			'src':'images/icons/minus-icon-disabled.png',
			'title':'Cannot reduce further!'
	});
	}
}

function initLibPlayer() {
	$("#lib-playertoggle-img").click(function () {
		if (libPlayer.open) {
			closeLibPlayer();
			$(this).attr({
				'src' : 'images/icons/window_down.png',
				'title' : 'Click to show library player.'
			});
		} else {
			openLibPlayer();
			$(this).attr({
				'src' : 'images/icons/window_up.png',
				'title' : 'Click to hide library player.'
			});
		}
	});
	// initial vars for player object;
	libPlayer.w = 600;
	libPlayer.ratio = 5/4;
	libPlayer.h = libPlayer.ratio?libPlayer.w/libPlayer.ratio:480;
	libPlayer.t = (libPlayer.h * -1) - 30;
	libPlayer.l = 317;
	libPlayer.wire = 49;
	libPlayer.lwc = 255;
	libPlayer.rwc = 233;
	libPlayer.open = false;
	$("#enlarge-player-size-icon").attr({
		'src':'images/icons/plus-icon.png',
		'title':'Click to enlarge player size.'
	});
	$("#reduce-player-size-icon").attr({
		'src':'images/icons/minus-icon-disabled.png',
		'title':'Cannot reduce further!'
	});
	// set initially closed, "play" will load it
	closeLibPlayer();
}

/* QUEUE */
function addToQueue(game,map) {
	alert('Pending!');
}

/* LATEST ADDITIONS */
latestAdditionsTimer = null;
function initLatestAdditions() {
	loadLatestAdditions();
	latestAdditionsTimer = window.setInterval("loadLatestAdditions()",300000); // once every 5 minutes
}
function loadLatestAdditions() {

}

function reloadLibrary() {
	$("#reload-library").css({ display: 'none' });
	loadLibrary();
}


// library startup
$(function () {
	setLibMode('list');
	$("#lib-mode-list").addClass('active');
	loadLibrary();
	loadSuggestions();
	//initLatestAdditions();
	initLibPlayer();
});
