var FadeSpeed = 100;
var CodeAdded = 0;
var CurrGame = 0;
var CurrPage = 0;
var GameName = ['iMob', 'iVamp', 'iKnights'];

$(document).ready(function(){
	loadCaptcha();
	loadList(CurrGame);
	initEvents();
    });

function loadCaptcha()
{
    Recaptcha.create("6LcrJAUAAAAAADTSjS_LOJVeAiWi7Gdlu9JYwEOq","CaptchaArea");
}

function createCodeBoxes()
{

}

function checkCaptcha()
{
    $.post('actions.php?a=add',
	   {challenge : Recaptcha.get_challenge(), response : Recaptcha.get_response(), code : $("#FamilyCode").attr("value"), game : CurrGame},
	  function(data){
	      data = eval('('+data+')');
	      console.log(data);
	      if (data.result == "fail")
	      {
		      loadCaptcha();
      		  if (data.reason == "invalid_code")
				$("#AddStatus").html("Invalid Friend Code");
		      else if (data.reason == "invalid_captcha")
				$("#AddStatus").html("Invalid Captcha Response");
			  else if (data.reason == "spam")
				$("#AddStatus").html("You have bumped your code too recently");
	      }
	      else
	      {
			  Recaptcha.destroy();
			  $("#AddArea").empty();
			  $("#AddArea").animate({height: "50px"});
			  $("#AddArea").css("color", "#00ff00");
			  if (data.action == "added")
			  {
				$("#AddArea").append("Congragulations! Your code has been added.");
			  }
			  else
			  {
				$("#AddArea").append("Congragulations! Your code has been bumped.");
			  }
			  CodeAdded = 1;
			  CurrPage = 0;

			  emptyList();
			  loadList(CurrGame);
	      }
	  });
    return false;
}

function initEvents()
{
    $("#CodeContainer").hover(function(){
	    //console.log("Mouseover");
	    //$("#Overlay").fadeIn(FadeSpeed);
		$("#AddArea").animate({width : "0px", height : "0px", display : "none"});
		$("#AddArea").slideUp(100);
	}, function(){
	    //console.log("Mouseout");
	    //$("#Overlay").fadeOut(FadeSpeed);
	});

    $("#AddContainer").click(function(){
	    if (CodeAdded == 0)
		$("#AddArea").animate({width : "315px", height : "180px"});
	    else
		$("#AddArea").animate({width : "315px", height : "50px"});
	});

    $("#CodeFooter").click(function(){
		emptyList();
		loadList(CurrGame);
	});

	$(".GameLabel").click(function(){
		$(".GameLabel").removeClass("GameLabelSelected");
		$(this).addClass("GameLabelSelected");

		for (var c = 0;c < $(".GameLabel").length;c++)	
			if ($(this).html() == $(".GameLabel").eq(c).html())
			{
				$("#AddGame").empty();
				$("#AddGame").append("Adding code for " + GameName[c]);

				CurrGame = c;	
				CurrPage = 0;

				emptyList();
				loadList(CurrGame);
				return;
			}
	});
    bindCodeBoxes();
}

function bindCodeBoxes()
{
    $(".CodeBox").hover(function(){
	    $(this).css("color", "#ffffff");
	    $(this).css("backgroundColor", "#a5a5a5");
	}, function(){
	    $(this).css("color", "");
	    $(this).css("backgroundColor", "");
	});
}

function emptyList()
{
	$(".CodeBox").remove();
	$("#CodeContainer").css("height", "462px");
	//$("#CodeContainer").animate({height: "0px"});
}

function loadList(gamenum)
{
	//console.log("Load list:" + gamenum);

    var codes;
    $.get('actions.php?a=codes&page='+(CurrPage++)+'&game='+gamenum, function(data) {populateList(data.split('|'));});
    $("#AjaxLoader").show();
}

function populateList(codes)
{
    var c = 0;
    for (var code in codes)
	{
	    var container = document.createElement('div');
	    container.setAttribute('class', 'CodeBox');
	    container.innerHTML = codes[code];

		if (++c < 21)
		    $("#LeftContainer").append(container);
		else
			$("#RightContainer").append(container);

	}

    $("#CodeContainer").animate({height: "462px"});
    bindCodeBoxes();
    $("#AjaxLoader").hide();
}
