gameDivName = 'feature_game';
gameDivMenu = 'feature_game_menu';
currentGame = 1;
previousGame = 1;
cachePreviousGame = 1;
gameTotal = 0;
gameOpacity = 0;
gameZindex = 0;
gameDivs = [];
gameLinks = [];
gameFadeDegree = 0.1;
gameRotateInterval = 6000 + (1 / gameFadeDegree * 50);

function setOpacity (el, value) {
  if (el.filters && el.filters[0]) { //IE syntax
    if (typeof el.filters[0].opacity == "number") { //IE6
      el.filters[0].opacity = value * 100;
    }
    else { //IE 5.5
      el.style.filter = "alpha(opacity=" + value * 100 +")";
    }
  }
  else if (typeof el.style.MozOpacity != "undefined") { // Old Mozilla syntax
    el.style.MozOpacity = value;
  }
  else if (typeof el.style.opacity != "undefined") { // Standard opacity syntax
    el.style.opacity = value;
  }
}

function clearTimer(timervar) {
  clearTimeout(timervar);
  clearInterval(timervar);
  if (cachePreviousGame != currentGame) { // if previous content isn't the same as the current shown div
    gameDivs[cachePreviousGame-1].style.display = "none";
  }
}

function previousGame() {
  var newGame = 1;
  if (currentGame > 1) {
    newGame = currentGame - 1;
  }
  else {
    newGame = gameTotal;
  }
  setGame(newGame, false);
}

function nextGame() {
  var newGame = 1;
  if (currentGame >= gameTotal) {
    newGame = 1;
  }
  else {
    newGame = currentGame + 1;
  }
  setGame(newGame, false);
}

function setGame(newGame, forceVisible) {

  if (newGame == currentGame && forceVisible == false) {
    return;
  }

  currentGame = newGame;
  cachePreviousGame = previousGame;
  gameDivs[newGame - 1].style.zIndex = ++gameZindex;
  clearTimer(window["fgfade"]);
  gameOpacity = 0;
  fadeinGame();
  gameDivs[newGame - 1].style.visibility = "visible";
  gameDivs[newGame - 1].style.display = "block";

  var re = new RegExp("(^|\\s+)selected($|\\s+)", "ig");
  gameLinks[previousGame - 1].className = gameLinks[previousGame - 1].className.replace(re, "");
  gameLinks[newGame - 1].className += " selected";

  previousGame = newGame;

}

function fadeinGame() {
  if (gameOpacity < 1) {
    gameOpacity = gameOpacity + gameFadeDegree;
    setOpacity(gameDivs[currentGame - 1], gameOpacity);
    window["fgfade"] = setTimeout(function() {fadeinGame()}, 50);
  }
  else { // when fade is complete
    if (cachePreviousGame != currentGame) { // if previous content isn't the same as the current shown div (happens the first time the page loads/ script is run)
      gameDivs[cachePreviousGame - 1].style.display = "none"; // collapse last content div shown (it was set to "block")
    }
  }
}

function startGameRotation() {
  window["fgrotate"] = setInterval("nextGame()", gameRotateInterval);
}

function stopGameRotation() {
  clearTimer(window["fgrotate"]);
}

function featuredGame() {

  var featureddiv = document.getElementById(gameDivName);
  var alldivs = featureddiv.getElementsByTagName("div");
  for (var i = 0; i < alldivs.length; i++) {
    if (/feature_game_div/.test(alldivs[i].className)) {
      gameDivs.push(alldivs[i]);
    }
  }

  var menuEl = document.getElementById(gameDivMenu);
  var linksEl = menuEl.getElementsByTagName("a");
  for (var i = 0; i < linksEl.length; i++) {
    gameLinks.push(linksEl[i]);
  }
  
  gameTotal = gameDivs.length;
  setGame(currentGame, true);
  startGameRotation();
}


function ajax_connection() {
  var xmlhttp, complete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(url, method, vars, callback_function) {
    if (!xmlhttp) return false;
    complete = false;
    method = method.toUpperCase();

    try {
      if (method == "GET") {
        xmlhttp.open(method, url+"?"+vars, true);
        vars = "";
      }
      else {
        xmlhttp.open(method, url, true);
        xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && !complete) {
          complete = true;
          callback_function(xmlhttp);
        }
      };
      xmlhttp.send(vars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

var game_connection = new ajax_connection();

var rating_done = function(xmlhttp) {
  var new_rating = xmlhttp.responseText;
  if (new_rating != '') {
    document.getElementById('current-rating').style.width = (new_rating * 16) + 'px';
    document.getElementById('rating-text').innerHTML = new_rating + ' / 5 stars';
  }
};

function rate_game(rating, id)  {
  game_connection.connect("/rating.php", "POST", "a=r&rating=" + rating + "&id=" + id, rating_done);
}

var suggest_done = function(xmlhttp) {
  var suggestions = xmlhttp.responseText;
  el = document.getElementById('search_results');
  if (suggestions != '') {
    el.innerHTML = suggestions;
    el.style.display = "block";
  }
  else {
    el.style.display = "none";
  }
}
function suggest_game() {
  q = document.getElementById('sch').value;
  if (q.length >= 2) {
    game_connection.connect("/ajax.php", "POST", "a=s&q=" + q + "&c=" + Math.random, suggest_done);
  }
}

