// --- choose games ---

var currChooseGamesChoice;

function chooseGames(choice, force) {
  if(!currChooseGamesChoice || choice != currChooseGamesChoice || force) {
    if(!force){
      flushChooseGames();
    }

    var selectedA;

    currChooseGamesChoice = choice;

    switch(choice) {
      case 1:
        $('#select-by-featured').addClass("sel").show();
        isSelectedGamesReadOnly = true;
        $('#currently-selected-games').show();
        setCurrSelectedGames([]);
        addToReadOnlyGames("auto_featured");
        selectedA = $('#featured-button');
        $('#select-channel-results').hide();
        break;
      case 2:
        isSelectedGamesReadOnly = false;
        $('#select-by-search-keyword,#loc').show();
        $('#select-by-search-keyword').addClass("sel");
        $('#currently-selected-games').show();
        $('#select-channel-results').hide();
        $("#loc-text").val("").focus();
        selectedA = $('#search-button');
        break;
      case 3:
        resetSelectedCategories();
        $('#select-by-genre').addClass("sel").show();
        isSelectedGamesReadOnly = true;
        $('#currently-selected-games').show();
        $('#select-channel-results').hide();
        selectedA = $('#genre-button');
        // they choose which ones to load
        break;
      case 4:
        $('#select-by-all').addClass("sel").show();
        isSelectedGamesReadOnly = true;
        $('#currently-selected-games').show();
        setCurrSelectedGames([]);
        addToReadOnlyGames("all");
        $('#select-channel-results').hide();
        selectedA = $('#all-button');
        break;
      case 5:
        isSelectedGamesReadOnly = true;
        $('#select-by-search-keyword').show();
        $('#select-by-search-keyword').addClass("sel");
        $('#currently-selected-games').show();
        selectedA = $('#search-button');
        break;
      default:
        alert("1 There's been an error. Please refresh this page and try again. If the problem persists, contact support@heyzap.com.");
        break;
    }
    selectedA.addClass("selected");
    $("#games-pointer").css("left", selectedA.position().left + selectedA.outerWidth() / 2 + $("#games-pointer").width() / 2);
  }
}

function flushChooseGames() {
  $('#select-games-by a').removeClass("selected");

  $('#select-by-featured,#select-by-search-keyword,#loc,#select-by-search-keyword-results,#select-by-genre,#select-by-all').removeClass("sel").hide();
  $('#currently-selected-games').hide();
  clearGameSearch();
  clearGameGenres();
}

function clearGameGenres() {
   $(".choose-by-genre-checkbox").removeAttr('checked');
}


// --- select integration ---

var currIntegrationChoice;

function selectIntegration(choice) {
  flushSelectIntegration();

  currIntegrationChoice = choice;
  if (choice == 7){return false}; //it's ning!

  var selectedA = $('#sel-int-group-'+choice).attr('checked',true);
  setTimeout(function(){selectedA.attr('checked',true)},100); //mega lame

  //$("#integration-pointer").css("left", selectedA.position().left + selectedA.width() / 2 + $("#integration-pointer").width() / 2);

  switch(choice) {
    case 1:
      $("#int-widget").addClass("sel").show();
      setWidgetIntegration("S");
      break;
    case 2:
      $("#int-blogger").addClass("sel").show();
      setBloggerIntegration();
      break;
    case 3:
      $("#int-wordpress").addClass("sel").show();
      setWordpressIntegration();
      break;
    case 4:
      $("#int-api").addClass("sel").show();
      setApiIntegration();
      break;
    case 5:
      $("#int-flash").addClass("sel").show();
      setSwfIntegration();
      break;
    case 6:
      $("#int-bar").addClass("sel").show();
      setBarIntegration();
      break;
    case 8:
      $("#int-joomla").addClass("sel").show();
      setJoomlaIntegration();
      break;
    case 9:
      $("#int-iframe").addClass("sel").show();
      setIframeIntegration();
      break;
    default:
      alert("2 There's been an error. Please refresh this page and try again. If the problem persists, contact support@heyzap.com.");
      break;
  }
  return false; 
}

function flushSelectIntegration() {
  $('.select-integration-ul a').removeClass('selected');

  $("#int-widget,#int-blogger,#int-wordpress,#int-api,#int-flash,#int-bar,#int-joomla,#int-iframe").hide();

  clearWidgetIntegration();
  clearBloggerIntegration();
  clearWordpressIntegration();
  clearApiIntegration();
  clearSwfIntegration();
  clearBarIntegration();
  clearJoomlaIntegration();
  clearIframeIntegration();
}

// widget integration

var widgetSizeToDims = {"S": "190x190", "MA": "300x250", "M": "425x425", "L": "650x650", "XL": "800x650"};

var currWidgetIntegration;
function setWidgetIntegration(widgetSize) {
  if(currWidgetIntegration && currWidgetIntegration == widgetSize) {
    return;
  }

  currWidgetIntegration = widgetSize;

  $("#widget-preview").show();

  // highlight the left side boxes
  $("#select-int-widget a").removeClass('selected');
  $("#select-int-widget a.widgetSize" + currWidgetIntegration).addClass('selected');

  // set the preview to show the actual widget
  $("#widget-preview div.widgetExample").hide();
  $("#widget-preview div.widgetSize" + currWidgetIntegration).show();

  $("#curr-widget-dims").text(widgetSizeToDims[widgetSize]);

  // 3rd step
  $("#widget-code").show();
}
function clearWidgetIntegration() {
  currWidgetIntegration = null;
  $("#widget-preview").hide();

  // 3rd step
  $("#widget-code").hide();
}

// blogger intrgration
function setBloggerIntegration() {
  $("#blogger-example").show();

  // 3rd step
  $("#blogger-plugin").show();
}
function clearBloggerIntegration() {
  $("#blogger-example").hide();

  // 3rd step
  $("#blogger-plugin").hide();
}

// wordpress integration
function setWordpressIntegration() {
  $("#wordpress-example").show();

  // 3rd step
  $("#wordpress-plugin").show();
}
function clearWordpressIntegration() {
  $("#wordpress-example").hide();

  // 3rd step
  $("#wordpress-plugin").hide();
}

// api integration
function setApiIntegration() {
  $("#api-example").show();

  // 3rd step
  $("#api-code").show();
}
function clearApiIntegration() {
  $("#api-example").hide();

  // 3rd step
  $("#api-code").hide();
}

// swf integration
function setSwfIntegration() {
  $("#game-swf-example").show();

  // 3rd step
  $("#game-swfs").show();
}
function clearSwfIntegration() {
  $("#game-swf-example").hide();

  // 3rd step
  $("#game-swfs").hide();
}

// bar integration
function setBarIntegration() {
  $("#arcade-bar-example").show();

  // 3rd step
  $("#game-bar").show();
}
// iframe
function setIframeIntegration() {
  $("#iframe-example").show();

  // 3rd step
  $("#iframe-code").show();
}
function clearIframeIntegration() {
  $("#iframe-example").hide();

  // 3rd step
  $("#iframe-code").hide();
}
function clearBarIntegration() {
  $("#arcade-bar-example").hide();

  // 3rd step
  $("#game-bar").hide();
}

// joomla integration
function setJoomlaIntegration() {
  $("#joomla-example").show();

  // 3rd step
  $("#joomla-plugin").show();
}
function clearJoomlaIntegration() {
  $("#joomla-example").hide();

  // 3rd step
  $("#joomla-plugin").hide();
}


// 3rd step
var currApiSize = "S";
function setApiSize(size) {
  if(currApiSize && currApiSize == size) {
    return;
  }

  $("#api-size-selector a").removeClass("selected");
  $("#api-size-selector a.apiSize" + size).addClass("selected");

  switch(size) {
    case 'S':
      $("#api-size-dims").text("32x32");
      break;
    case 'M':
      $("#api-size-dims").text("250x200");
      break;
    case 'L':
      $("#api-size-dims").text("480x360");
      break;
  }

  currApiSize = size;
}


// submit
function makeButtonSpinner(buttonID) {
  return;
  var button = $("#" + buttonID);
  button.addClass("spinnerized-button").addClass("spinner-container").children("img").hide();
  button.children(".spinner").remove();
  button.append("<img src='/images/spinner-24.gif' class='spinner' />");
}

function restoreSpinnerButtons() {
  return;
  $(".spinnerized-button .spinner").remove();
  $(".spinnerized-button").removeClass("spinnerized-button").removeClass("spinner-container").children("img").show();
}
