// games is an an array of games objects. this is the thing on the bottom when you say "get swf games"

var SWFS_PER_PAGE = 5;
var currSWFGames = [];

function setSWFGames(games) {
  currSWFGames = games;

  if(currSWFGames.length) {
    setCurrSWFGamesPage(1);
    $("#heyzap-signup-inner-swf-area .pages").show();
  } else {
    $("#heyzap-signup-inner-swf-area .pages").hide();
  }
}

function setCurrSWFGamesPage(page) {
  var numPages = Math.ceil(parseFloat(currSWFGames.length) / SWFS_PER_PAGE);
  if(numPages < page) {
    page = numPages;
  }

  var pageEl = $("#heyzap-signup-inner-swf-area .pages");
  var pageHtml = "Pages: ";
  if(page != 1) {
    pageHtml += "<span onclick='setCurrSWFGamesPage(" + (page - 1) + ")'>&lt; Prev</span>";
  }

  for(var i = 1; i <= numPages; i++) {
      pageHtml += " ";
    if(i == page) {
      pageHtml += i;
    } else {
      pageHtml += "<span onclick='setCurrSWFGamesPage(" + i + ")'>" + i + "</span>";
    }
  }

  if(page != numPages) {
    pageHtml += " <span onclick='setCurrSWFGamesPage(" + (page + 1) + ")'>Next &gt;</span>";
  }

  // actually show the swfs for the page we're on
  showSWFs(page);

  pageEl.show().html(pageHtml);
}

function showSWFs(page) {
  // show the swfs for this page - populates the table

  var tbody = $("#game-swf-downloads tbody");
  var startI = (page - 1) * SWFS_PER_PAGE;

  var html = "";
  for(var i = startI; i < (startI + SWFS_PER_PAGE) && i < currSWFGames.length; i++) {
    var g = currSWFGames[i];
    html += "<tr>";
    html += "<td><strong>" + g.name + "</strong></td>";
    html += "<td><textarea rows='3' cols='28' onclick='this.select();' >" + g.embedCode + "</textarea></td>";
    html += "</tr>";
  }

  tbody.html(html);
}

