/**
 *
 */

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function DeleteGame(gameName) {
  var allGames = GetCookie('Games');
  var j;
  var found = false;

  var i = allGames.indexOf(gameName);
  if (i == -1) {
    alert("Error: '"+gameName+"' not found.");
    return;
  }
  var head = allGames.substr(0, i);
  for (j=i;j<allGames.length;j++) {
    if (allGames.charAt(j) == '|') {
      var found = true;
      break;
    }
  }
  if (!found) {
    alert("Error finding the game.");
    return;
  }
  var tail = allGames.substr(j+1, allGames.length);
  allGames = head + tail;
  DeleteCookie('Games');
  SetCookie('Games', allGames);
  showMyGames();
}

// If cookies are not enabled, then hide the "Remember Game" button. Otherwise,
// make it visible.
function checkForCookies() {
  if (navigator.cookieEnabled == 0) {
    alert("You must have cookies enabled to save games.");
  }
}

// See if the cookie is already saved
function isDuplicateGame(item) {
  var gameItem = GetCookie('Games');

  // If no cookie found, then it cannot be a duplicate.
  if (gameItem == null) {
   return false;
  }

  // If the the game name is found in the cokkie, then it is a duplicate,
  // so return true; else return false.
  if (gameItem.indexOf(item) != -1) {
    return true;
  }
  else {
    return false;
  }
}

//
function numOfGames() {
  var games = [];
	g = GetCookie('Games');
	if (g == null) return 0;
	else {
		games = g.split('|');
		return games.length;
	}
}

// Add the desired game to the cookie.
//function addGame() {
function addGame(gameDir, gameURL, gameName) {
  //Get the saving parameters: game name and folder path.
  // If no game name value, then there is nothing to save, so tell the
  // visitor and leave.
  if ((gameDir == null) || (gameDir == "")) {
    alert("No game has been selected");
    return;
  }

  // See if the game has already been
  if (isDuplicateGame(gameURL)) {
    alert("You have already saved '" + gameName + "'");
    return;
  }

  if (numOfGames() == 16) {
    alert('Sorry, you can only save 15 games.');
    return;
  }

  var gameItem = GetCookie('Games');
  if (gameItem == null) gameItem = "";
  gameItem = gameItem + gameDir + "~" + gameURL + "|";
  SetCookie('Games', gameItem, exp);
  alert(gameName+' saved');
}

function deleteAllGames() {
  SetCookie('Games', '', exp);
  showMyGames();
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);

  if (endstr == -1) {
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = escape(name) + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;

    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  //alert(name+" cookie not found");
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = "/";
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  document.cookie = escape(name) + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (1); // exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var games = new sack();
var links = new sack();

// This will display the remembered games in the page's iframe.
function showMyGames() {
	games.element = 'gamediv';
  games.requestFile = './scripts/mygames.php';
  games.runAJAX();
}

function loadGames(dir, page) {
  games.setVar('dir', dir);
  games.setVar('pg', page);
	games.method = 'POST';
	games.element = 'gamediv';
	if (dir == 'whatsnew') games.requestFile = './scripts/newgames.php';
  else games.requestFile = './scripts/games.php';
  games.runAJAX();
}

function loadNewGames() {
	games.element = 'gamediv';
	games.requestFile = './scripts/newgames.php';
  games.runAJAX();
}

function showLinks() {
	fb.start({href: "../links2.html", title: "Visit Our Friends",
		        rev: "width: 590 height: 170 scrolling: no theme: yellow"});
}
