var CheckList = {
	maxHeight: 87, 
	
	// Maximum number of games to be saved. Avoid too heavy load on client computer
	maxSize: 50, 
	
	cookieName: 'ogCheckList', 
	
	games: [], 
	
	init: function() {
		CheckList.display();
		
		EventAttacher.add(window, 'load', CheckList.adjustWidth);
	}, 
	
	display: function() {
		var list = Cookie.read(CheckList.cookieName); //  myGamesList, ogCheckList

		// list is null when no game info has been added into the cookie.
		if (list) {
			// Replacing plusses (+) with empty space " ".
			var lsRegExp = /\+/g;
			list = unescape(list.replace(lsRegExp, " ")).split(':|:');
		}
	
		var tbody = document.getElementById('ogCheckList');

		// Empty tbody if not empty
		while (tbody.firstChild) {
			tbody.removeChild(tbody.firstChild);
		}
		
		var tr, td1, td2;
		if (list && list.length > 0) {
			var myGame;
			var id, title, image, categoryID;
			var tr, td1, td2;
			var bgColor;
			for (var i=0; i<list.length; i++) {
				myGame = list[i].split(":#:");
				id = myGame[0];
				title = myGame[1];
				image = myGame[2];
				categoryID = myGame[3];
				
				// Save the game into internal array.
				// This array is used when saving data into cookie
				CheckList.games.push(
						{'id': id, 'title': title, 'image': image, 'categoryID': categoryID}
					);
	
				tr = document.createElement('tr');
				td1 = document.createElement('td');
				td2 = document.createElement('td');
	
				tbody.appendChild(tr);
				tr.appendChild(td1);
				tr.appendChild(td2);
	
				if (i % 2 == 0) {
					bgColor = "#ffffff";
					textColor = "#006fa7";
				}
				else {
					bgColor = "#6db3dc";
					textColor = "#ffffff";
				}
	
				td1.style.backgroundColor = bgColor;
				td1.style.color = textColor;
				td1.style.textAlign = 'left';
				td1.style.verticalAlign = 'top';
				td2.style.backgroundColor = bgColor;
	
				td1.innerHTML = (i+1) + '.';
				td2.innerHTML = '<a href="' + Common.HTML_ROOT + '/onlineGame/info.php?id=' + id + '" onclick="CheckList.trackVisit(' + id + ');" style="color: ' + textColor + '; text-decoration: none;">' + title + '</a>';
			}
		}
		else {
			tr = document.createElement('tr');
			td1 = document.createElement('td');

			tbody.appendChild(tr);
			tr.appendChild(td1);

			td1.style.padding = '3px';
			td1.innerHTML = 'Alle spil, du prøver i dag, dukker op i denne huskeliste.';
		}
	}, 
	
	adjustWidth: function() {
		var container = document.getElementById('ogCheckListDiv');
		
		// If container height exceeds a maximum height, 
		// its height is set to the maximum and a vertical scroll will appear. 
		// Otherwise, its height will be set to the scroll height + 2px.
		container.style.height = (container.scrollHeight > CheckList.maxHeight ? CheckList.maxHeight : (parseInt(container.scrollHeight) + 2));
	},
	
	add: function(id, title, image, categoryID) {
		CheckList.remove(id);
		
		CheckList.games.push(
				{'id': id, 'title': title, 'image': image, 'categoryID': categoryID}
			);
		
		CheckList.saveAndDisplay(365);
	}, 
	
	remove: function(id) {
		var game;
		for (var i=0; i<CheckList.games.length; i++) {
			game = CheckList.games[i];
			
			if (game.id == id) {
				CheckList.games.splice(i, 1);
				break;
			}
		}
		
		CheckList.saveAndDisplay(365);
	},
	
	empty: function() {
		CheckList.games = [];
		
		CheckList.saveAndDisplay(-1);
	}, 
	
	saveAndDisplay: function(days) {
		var counter = 1;
		var game;
		var games = [];
		while (CheckList.games.length > 0 && counter++ < CheckList.maxSize) {
			game = CheckList.games.pop();
			
			games.push(game.id + ':#:' + game.title + ':#:' + game.image + ':#:' + game.categoryID);
		}	
		
		var joinedGames = games.join(':|:');	

		Cookie.create(CheckList.cookieName, joinedGames, days);
		
		CheckList.display();
		CheckList.adjustWidth();
	}, 
	
	trackVisit: function(id) {
		// Ajax is included from level1.php as prototype.x.x.x.js
		new Ajax.Request(Common.HTML_ROOT + '/onlineGame/checklistTrackVisit.php',
		{
			method: 'post', 
			parameters: {'id': id}
		});	
	},

	trackOverview: function() {
		// Ajax is included from level1.php as prototype.x.x.x.js
		new Ajax.Request(Common.HTML_ROOT + '/onlineGame/checklistTrackOverview.php',
		{
			method: 'post', 
			parameters: {'track': 1}
		});	
	},

	trackEmpty: function(size) {
		// Ajax is included from level1.php as prototype.x.x.x.js
		new Ajax.Request(Common.HTML_ROOT + '/onlineGame/checklistTrackEmpty.php',
		{
			method: 'post', 
			parameters: {'size': size}
		});	
	}
}

CheckList.init();
