function showForm(widgetType)
{
  showBanner(widgetType);    //Change Banner
  showHeaders(widgetType);

  if(widgetType == 'blogger' || widgetType == 'wordpress')
  {
    showPreview();
    putCheckForSmallSize();
    showWidgetSize('small');    //Show Small Widget
  }
  else if(widgetType == 'api' || widgetType == 'individual_payments')
  {
    hidePreview();
    blankWidgets();    //No Preview
  }
  else
  {
    showWidget();    //Show Widget of Selected Size
  }
}

function putCheckForSmallSize()
{
  $('#site_embed_size_small').attr('checked', true);
}

function showBanner(widgetType)
{
  $('#widgetHeader').css({'display' : 'none'});
  $('#bloggerHeader').css({'display' : 'none'});
  $('#wordpressHeader').css({'display' : 'none'});
  $('#apiHeader').css({'display' : 'none'});
  $('#individual_paymentsHeader').css({'display' : 'none'});

  $('#'+widgetType+"Header").css({'display' : 'block'});
  return;
}

function showHeaders(widgetType)
{
  if(widgetType == 'blogger')
  {
    //Show Necessary Fields
    $("#addSiteInfo").css({'display' : 'block'});
    $("#pickSize").css({'display' : 'none'})
    $("#gameOptions").css({'display' : 'block'});
    $("#submitButton").css({'display' : 'block'});

    //Change Numbering
    $("#gameOptionsHeader").text("Step 3: Game Selection Options");
    $("#submitButtonHeader").text("Step 4: Add Games to Blogger");
    $("#submitButtonInput").val("Add Games to Blogger");
  }
  else if(widgetType == 'wordpress')
  {
    //Show Necessary Fields
    $("#addSiteInfo").css({'display' : 'block'});
    $("#pickSize").css({'display' : 'none'})
    $("#gameOptions").css({'display' : 'block'});
    $("#submitButton").css({'display' : 'block'});

    //Change Numbering
    $("#gameOptionsHeader").text("Step 3: Game Selection Options");
    $("#submitButtonHeader").text("Step 4: Get Wordpress Plugin");
    $("#submitButtonInput").val("Get Wordpress Plugin");
  }
  else if(widgetType == 'api')
  {
    //Show Necessary Fields
    $("#addSiteInfo").css({'display' : 'block'});
    $("#pickSize").css({'display' : 'none'})
    $("#gameOptions").css({'display' : 'none'});
    $("#submitButton").css({'display' : 'block'});

    //Change Numbering
    $("#submitButtonHeader").text("Step 3: Connect to API");
    $("#submitButtonInput").val("Connect to API");
  }
  else if(widgetType == 'individual_payments')
  {
    //Show Necessary Fields
    $("#addSiteInfo").css({'display' : 'block'});
    $("#pickSize").css({'display' : 'none'})
    $("#gameOptions").css({'display' : 'none'});
    $("#submitButton").css({'display' : 'block'});

    //Change Numbering
    $("#submitButtonHeader").text("Step 3: Get Games");
    $("#submitButtonInput").val("Get Games");
  }
  else
  {
    //Show Necessary Fields
    $("#addSiteInfo").css({'display' : 'block'});
    $("#pickSize").css({'display' : 'block'})
    $("#gameOptions").css({'display' : 'block'});
    $("#submitButton").css({'display' : 'block'});

    //Change Numbering
    $("#gameOptionsHeader").text("Step 4: Game Selection Options");
    $("#submitButtonHeader").text("Step 5: Generate Embed Code");
    $("#submitButtonInput").val("Generate Embed Code");
  }
  return;
}

function hidePreview()
{
  $('#formPreview').css({'display' : 'none'});
  return;
}

function showPreview()
{
  $('#formPreview').css({'display' : 'block'});
  return;
}

function showWidget()
{
  blankWidgets();
  var currentValue = $('#site_embed_size_extra_large').is(':checked') ? "extra_large" : 0;
  if(!currentValue)
    currentValue = $('#site_embed_size_large').is(':checked') ? "large" : 0;
  if(!currentValue)
    currentValue = $('#site_embed_size_medium').is(':checked') ? "medium" : 0;
  if(!currentValue)
    currentValue = $('#site_embed_size_ad_300').is(':checked') ? "ad_300" : 0;
  if(!currentValue)
    currentValue = $('#site_embed_size_small').is(':checked') ? "small" : 0;

  $('#heyzap'+currentValue).css({'display' : 'block'});
  return;
}

function showWidgetSize(size)
{
  blankWidgets();
  showPreview();
  $('#heyzap'+size).css({'display' : 'block'});
  return;
}

function blankWidgets()
{
  $('#heyzapextra_large').css({'display' : 'none'});
  $('#heyzaplarge').css({'display' : 'none'});
  $('#heyzapmedium').css({'display' : 'none'});
  $('#heyzapad_300').css({'display' : 'none'});
  $('#heyzapsmall').css({'display' : 'none'});

  return;
}

function gameOptionsSubBox(option)
{
  hideOptions();
  showGameOptionsSubBox();
  switch(option)
  {
    case 'categories':
      $('#category_selector').css({'display' : 'block'});
      break;
    case 'channels':
      $('#channel_selector').css({'display' : 'block'});
      break;
    case 'games':
      $('#game_selector').css({'display' : 'block'});
      break;
    default:
      hideGameOptionsSubBox();
    break;
  }
  return;
}

function showGameOptionsSubBox()
{
  $('#choice_extensions').css({'display' : 'block'});
}

function hideGameOptionsSubBox()
{
  $('#choice_extensions').css({'display' : 'none'});
}

function hideOptions()
{
  $('#category_selector').css({'display' : 'none'});
  $('#channel_selector').css({'display' : 'none'});
  $('#game_selector').css({'display' : 'none'});
}

var formVerified = false;

function verifyForm()
{
  var user_route = $('#user_route').val() ? $('#user_route').val() : 0;
  if(user_route == 'login')    //If User Not Signed In
  {
    switchToLogin(true);
    if(formVerified)
    {
      $('#addSiteForm').submit();
      $('#submitButtonInput').attr('disabled', true);
    }
    else
    {
      formVerified = true;
      return;
    }
  }
  else if(user_route == 'signup')    //If User Not Signed In
  {
    switchToLogin(false);
    if(formVerified)
    {
      $('#addSiteForm').submit();
      $('#submitButtonInput').attr('disabled', true);
    }
    else
    {
      formVerified = true;
      return;
    }
  }
  else    //User is signed in
  {
    $('#addSiteForm').submit();
    $('#submitButtonInput').attr('disabled', true);
  }
  return;
}

function handleLoginSignupError()    //ALSO HANDLE EDIT PAGE
{
  var siteType = $('#site_site_type_site').is(':checked') ? "site" : 0;
  if(!siteType)
    siteType = $('#site_site_type_blogger').is(':checked') ? "blogger" : 0;
  if(!siteType)
    siteType = $('#site_site_type_wordpress').is(':checked') ? "wordpress" : 0;
  if(!siteType)
    siteType = $('#site_site_type_api').is(':checked') ? "api" : 0;
  if(!siteType)
    siteType = $('#site_site_type_individual_payments').is(':checked') ? "individual_payments" : 0;
  showForm(siteType);

  handleShowSelections();

  $('#submitButtonInput').attr('disabled', false);
  var loginError = ($('#loginError').text().substring(0,30) == "        Couldn't log you in as") ? 1 : 0;
  var signupError = $('#signupError').text() == "" ? 0 : 1;

  if(loginError)
  {
    switchToLogin(true);    //Show Login Form
    handleShowSelections();
    handleGetPreview();
    formVerified = true;
    return;
  }
  else if(signupError)
  {
    switchToLogin(false);    //Show Signup Form
    handleShowSelections();
    handleGetPreview();
    formVerified = true;
    return;
  }
  else
  {
    return;
  }
  return;
}

function handleGetPreview()
{
  if($('#site_site_type_site').is(':checked'))
  {
    showWidget();
    showPreview();
  }
  else if($('#site_site_type_blogger').is(':checked') || $('#site_site_type_wordpress').is(':checked'))
  {
    showWidgetSize('small');
    showPreview();
  }
  else
  {
    hidePreview();
  }
}

function handleShowSelections()
{
  if($('#site_game_selection_games').is(':checked'))
  {
    showGameOptionsSubBox();
    $('#game_selector').css({'display' : 'block'});
  }
  else if($('#site_game_selection_channels').is(':checked'))
  {
    showGameOptionsSubBox();
    $('#channel_selector').css({'display' : 'block'});
  }
  else if($('#site_game_selection_categories').is(':checked'))
  {
    showGameOptionsSubBox();
    $('#category_selector').css({'display' : 'block'});
  }
}

function putCheck(radioType, checkedItem)
{
  $('#'+radioType+'_'+checkedItem).attr('checked', true);
  if(radioType == "site_site_type")    //Show Form
  {
    showForm(checkedItem);
  }
  else if(radioType == "site_game_selection")    //Show Correct Game Options
  {
    gameOptionsSubBox(checkedItem);
  }
  else if(radioType == "site_embed_size")   //Show Correct Widget Size
  {
    showWidgetSize(checkedItem);
  }
}

function switchToLogin(gologin)
{
  $('#signupOrLogin').show();
  $('#noLogin').show();
      if(gologin)
      {
        $('#signup_prompt').hide();
        $('#login_prompt').show();
        $('#signup_fields').hide();
        $('#login_fields').show();
        $('#login_fields').show();
        $('#user_route')[0].value = "login";
        $('#user_session_username')[0].focus();
      }
      else
      {
        $('#signup_prompt').show();
        $('#login_prompt').hide();
        $('#signup_fields').show();
        $('#login_fields').hide();
        $('#user_route')[0].value = "signup";
        $('#user_username')[0].focus();
      }
}

function generateApiUrl()
{
  //Get the Game Type
  var gameType = getGameType();
  modifyHeaders(gameType);

  //Grab the Keys if they exist
  var embed_key = $('#api_wizard_embed_key').val() ? $('#api_wizard_embed_key').val() : "";
  var secret_key = $('#api_wizard_secret_key').val() ? $('#api_wizard_secret_key').val() : "";

  //Define URLs and Flags
  var serviceUrl = "http://www.heyzap.com/heyzap/service";
  var flags = "?embed_key="+embed_key+"&secret_key="+secret_key;
  var xml_flag = "&format=xml";
  var json_flag = "&format=json";

  //Thumbnail Information
  var thumb_width = getThumbWidth();
  var thumb_height = getThumbHeight();
  var thumb_size_flags = '&thumb_height='+thumb_height+'&thumb_width='+thumb_width;

  //Maximum Width and Length
  var max_width_flag = $('#api_wizard_width').val() ? '&max_width='+$('#api_wizard_width').val() : "";
  var max_height_flag = $('#api_wizard_height').val() ? '&max_height='+$('#api_wizard_height').val() : "";

  //Game Options for All GAmes
  var gameOption = getGameOption();
  var game_flags = "";
  var categories = getGameCategory();

  //Length
  var num_games_returned = $('#api_wizard_length').val() ? '&length='+$('#api_wizard_length').val() : "";

  //Embed and Secret Keys

  switch(gameOption)
  {
    case "category":
      $('#channel_selector').hide();
      $('#category_selector').show();
      $('#numGames').show();
      $('#choice_extensions').show();
      $('#game_selector').hide();
      game_flags += ("&category="+getGameCategory());
      game_flags += num_games_returned;
      break;
    case "keyword":
      $('#category_selector').hide();
      $('#channel_selector').show();
      $('#numGames').show();
      $('#choice_extensions').show();
      $('#game_selector').hide();
      game_flags += $('#site_channel_tags').val() ? ("&search_query=")+$('#site_channel_tags').val() : "";
      game_flags += num_games_returned;
      break;
    case "specific":
      $('#category_selector').hide();
      $('#channel_selector').hide();
      $('#numGames').hide();
      $('#choice_extensions').show();
      $('#game_selector').show();
      game_flags += $('#site_games_permalinks').val() ? ("&permalink=")+$('#site_games_permalinks').val() : "";
      break;
    default:
      $('#category_selector').hide();
      $('#channel_selector').hide();
      $('#game_selector').hide();
      $('#choice_extensions').hide();
      $('#numGames').show();
      game_flags += num_games_returned;
      break;
  }

  //Get Game Type
  switch(gameType)
  {
    case "categories":
      $('#gameOptions').hide();
      $('#addSiteInfo').hide();
      $('#pickSize').hide();
      flags += "&method=get_category_list";
      $('#xmlUrl').val(serviceUrl+flags+xml_flag);
      $('#jsonUrl').val(serviceUrl+flags+json_flag);
      break;
    case "random":
      $('#gameOptions').hide();
      $('#addSiteInfo').show();
      $('#pickSize').show();
      flags += "&method=get_random_game";
      $('#xmlUrl').val(serviceUrl+flags+thumb_size_flags+max_width_flag+max_height_flag+xml_flag);
      $('#jsonUrl').val(serviceUrl+flags+thumb_size_flags+max_width_flag+max_height_flag+json_flag);
      break;
    default:
      $('#gameOptions').show();
      $('#addSiteInfo').show();
      $('#pickSize').show();
      $('#xmlUrl').val(serviceUrl+flags+thumb_size_flags+max_width_flag+max_height_flag+game_flags+xml_flag);
      $('#jsonUrl').val(serviceUrl+flags+thumb_size_flags+max_width_flag+max_height_flag+game_flags+json_flag);
      break;
  }
  return;
}

function generateApiUrlAndSelect(item_id)
{
  $('#'+item_id).attr('checked', true);
  generateApiUrl();
}

var widthEdited = false;
var heightEdited = false;
var lengthEdited = false;

function handleBlankFields(dimension)
{
  if(dimension == "width")
  {
    if(!widthEdited)
    {
      $('#api_wizard_width').val("");
      widthEdited = true;
    }
  }
  else if(dimension == "length")
  {
    if(!lengthEdited)
    {
      $('#api_wizard_length').val("");
      lengthEdited = true;
    }
  }
  else
  {
    if(!heightEdited)
    {
      $('#api_wizard_height').val("");
      heightEdited = true;
    }
  }
  generateApiUrl();
  return;
}

function getThumbWidth()
{
  if($('#api_wizard_size_350x350').is(':checked'))
    return 350;
  else if($('#api_wizard_size_250x200').is(':checked'))
    return 250;
  else if($('#api_wizard_size_100x100').is(':checked'))
    return 100;
  else if($('#api_wizard_size_112x93').is(':checked'))
    return 112;
  else if($('#api_wizard_size_32x32').is(':checked'))
    return 32;
  else
    return 480;
}

function getThumbHeight()
{
  if($('#api_wizard_size_350x350').is(':checked'))
    return 350;
  else if($('#api_wizard_size_250x200').is(':checked'))
    return 200;
  else if($('#api_wizard_size_100x100').is(':checked'))
    return 100;
  else if($('#api_wizard_size_112x93').is(':checked'))
    return 93;
  else if($('#api_wizard_size_32x32').is(':checked'))
    return 32;
  else
    return 360;
}

function getGameType()
{
  if($('#api_wizard_type_categories').is(':checked'))
    return 'categories';
  else if($('#api_wizard_type_random').is(':checked'))
    return 'random';
  else
    return 'games';
}

function getGameCategory()
{
  if($('#api_wizard_category_name_sports').is(':checked'))
    return 'sports';
  else if($('#api_wizard_category_name_shooter').is(':checked'))
    return 'shooter';
  else if($('#api_wizard_category_name_strategy').is(':checked'))
    return 'strategy';
  else if($('#api_wizard_category_name_puzzle').is(':checked'))
    return 'puzzle';
  else if($('#api_wizard_category_name_action').is(':checked'))
    return 'action';
  else if($('#api_wizard_category_name_multiplayer').is(':checked'))
    return 'multiplayer';
  else if($('#api_wizard_category_name_adventure').is(':checked'))
    return 'adventure';
  else if($('#api_wizard_category_name_lifestyle').is(':checked'))
    return 'lifestyle';
  else if($('#api_wizard_category_name_premium').is(':checked'))
    return 'premium';
  else if($('#api_wizard_category_name_social').is(':checked'))
    return 'social';
  else
    return 'popular';
}

function getGameOption()
{
  if($('#api_wizard_get_game_method_category').is(':checked'))
    return 'category';
  else if($('#api_wizard_get_game_method_keyword').is(':checked'))
    return 'keyword';
  else if($('#api_wizard_get_game_method_specific').is(':checked'))
    return 'specific';
  else
    return 'all';
}

function modifyHeaders(feedType)
{
  var gameOptionsHeader = 'Select Game Method';
  var addSiteInfoHeader = 'Select Maximum Game Size';
  var pickSizeHeaderText = 'Select Game Thumbnail Size';
  var enterKeyHeaderText = 'Enter Embed Key and Secret Key';
  var submitHeaderText = 'Connect to the API';
  switch(feedType)
  {
    case "categories":
      $('#enterKeyHeader').text("Step 2: "+enterKeyHeaderText);
      $('#submitHeader').text("Step 3: "+submitHeaderText);
      break;
    case "random":
      $('#addSiteInfoHeader').text("Step 2: "+addSiteInfoHeader);
      $('#pickSizeHeader').text("Step 3: "+pickSizeHeaderText);
      $('#enterKeyHeader').text("Step 4: "+enterKeyHeaderText);
      $('#submitHeader').text("Step 5: "+submitHeaderText);
      break;
    default:
      $('#gameOptionsHeader').text("Step 2: "+gameOptionsHeader);
      $('#addSiteInfoHeader').text("Step 3: "+addSiteInfoHeader);
      $('#pickSizeHeader').text("Step 4: "+pickSizeHeaderText);
      $('#enterKeyHeader').text("Step 5: "+enterKeyHeaderText);
      $('#submitHeader').text("Step 6: "+submitHeaderText);
      break;
  }
  return;
}
