$(document).ready(function()
{

  xeko.widgets.agentcards.getAgent(xeko.agentinfo.agentGUID);

  xeko.widgets.agentcards.renderCards();
	
	$("#prevPage").click(function(e)
	{
		e.preventDefault();
		xeko.widgets.agentcards.prevPage();
	});

	$("#nextPage").click(function(e)
	{
		e.preventDefault();
		xeko.widgets.agentcards.nextPage();
	});

});

xeko.widgets.agentcards =
{
	"currentpage" : 1,
	"pagesize" : 8,
	"totalcards" : 0, 
	"totalpages" : 1,
	"nextPage" : function()
	{
		if (this.currentpage < this.totalpages)
		{
			this.flipPage(this.currentpage+1);
		}
	},
	"prevPage" : function()
	{
		if (this.currentpage > 1)
		{
			this.flipPage(this.currentpage-1);
		}
	},
	"flipPage" : function(page)
	{
		$("div.cardpage").hide();
		$("div.page" + page).show();
		this.currentpage = page;
	},
	"renderCards" : function()
	{
		$("ul#agentcardlist").html("");
		$.ajax
		({
			type: "GET",
			url : "/widgets/widget.AgentInfo.handler.php?request=agentdigitalcards",
			success : function(xml)
			{
				xeko.widgets.agentcards.totalcards = $("agentcard",xml).size();
				xeko.debug.trace("total cards: " + xeko.widgets.agentcards.totalcards);
				xeko.widgets.agentcards.totalpages = Math.ceil(xeko.widgets.agentcards.totalcards / xeko.widgets.agentcards.pagesize);
				for(i=1;i<=xeko.widgets.agentcards.totalpages;i++)
				{
					$("div#agentcardlist").append("<div class='cardpage page" + i + "'><ul></ul></div>");
				}
					
			  
				$("p.loading").hide();
				$("div.page" + xeko.widgets.agentcards.currentpage).show();
					
				var targetpage = 1;
				var count = 1;
				$("agentcard",xml).each(function(node)
				{
					if (count > xeko.widgets.agentcards.pagesize)
					{
						targetpage++;
						count =1;
					}
					var cardid = $(this).attr("id");
					$("div#agentcardlist div.page" + targetpage + " ul").append("<li id='" + cardid + "'>Loading card details...</li>");
					xeko.debug.trace("current: " + targetpage + "," + count);
					count++;

					var cardName = $("name",this).text();
					var cardHotspot = $("hotspot",this).text();
					var cardNumber = $("number",this).text();
					var cardlocation = (cardHotspot.toLowerCase()).replace(" ", "");
					var cardpath = "/images/cardart/" + cardlocation + "/xekocards/" + cardNumber + ".jpg";

					cardHTML = '<img class="card" src="' + cardpath + '" /><h3>' + cardName + '</h3>';

					// TODO - get card type
					cardHTML += '<div class="species">Species Card</div>';

					cardHTML += '<div class="hotspot">Mission ' + cardHotspot + '</div>';

					// TODO - get IRL status

					//cardHTML += '<div class="irl"><img id="agentCardIrlCheckbox' + cardId + '" src="/images/g/g_checkbox_checked.gif"/><label id="agentCardIrlLabel' + cardId + '" for="agentCardIrlCheckbox">I have this card IRL</label></div>';

					$("li#" + cardid).html(cardHTML);
				});
			
			},
			error : function(response)
			{
							
			}
		});

  },

  "getAgent": function(guid) {

    $.ajax(

		  {

		    method: "get",

		    url: "/widgets/widget.AgentInfo.handler.php",

		    success: function(xml) {
			
				var avatarImg = "/images/cardart/" + xeko.utilities.removeSpaces($("card>hotspot", xml).text()) + "/altereco/" + $("card>number", xml).text() + ".png";
			    $(".alterEco img").attr("src", avatarImg);
		    },

		    error: function(xmlHttpRequest, status, err) {

		      // this usually means it can't find the agent

		    }

		  });

  },

  "parseAgentXml": function(xml) {

    // Avatar





  }
};