function stats(unity)
{
	this.unity = unity;
	
	this.reqs = new reqs(this);
	this.data = null;
	
	this.store = function(type, maze, value, hash)
	{
		switch(type)
		{		
			case "SCORE":
				this.reqs.post("?cmd=stats", '{"type":"SCORE","maze":"'+maze+'","value":"'+value+'","hash":"'+hash+'"}', function(){ xunity.stats.callback(type, maze, value); });
			break;
			
			default:
				this.reqs.post("?cmd=stats", '{"type":"'+type+'","maze":"'+maze+'","value":"'+value+'","hash":"'+hash+'"}', null);
			break;
		}
	}
	
	this.secToTime = function(milliseconds)
	{
		var seconds = parseInt(milliseconds / 1000);
		
		var m = parseInt(seconds / 60);
		var s = parseInt(seconds - (m * 60));
		
		s = new String(s);
		if(s.length == 1)
			s = "0"+s;
		
		return m+":"+s;
	}
	
	this.callback = function(type, maze, value)
	{
		switch(type)
		{
			case "SCORE":
				var position = null;
				//find my position
				var fbid = getCookie(xconfig.apiKey+"_user");
				for(var i = 0; i < this.data.scores.length; i++)
				{
					if(this.data.scores[i].fbid == fbid)
					{
						position = i;
						break;
					}
				}
			
        var desc = "";
  			var icon = "http://"+xconfig.hostname+"/stage1/images/fb/";
  			var time;
  			
				if(position != null)
        {
  				var score = this.data.scores[position].score;

  				if(value > score)
  					score = value;

  				time = this.secToTime(score);
  				
  				//are you first?
  				if(position == 0 && this.data.scores.length > 1)
  				{
  					desc = "{*actor*} beat "+this.data.scores[position + 1].name+" playing Mind Crime maze called "+this.data.name+" in "+time+"! Can you beat it?";
  					icon += "won.jpg";
  				}
  				else if(value == 0)
  				{
  					desc = "{*actor*} just created a maze called "+this.data.name+" in Mind Crime. Time to beat is "+time+".";
  					icon += "new.jpg";
  				}
  				else
  				{
  					desc = "{*actor*} just finished a maze called "+this.data.name+" in Mind Crime. Time to beat is "+time+"! Can you beat it?";
  					icon += "played.jpg";
  				}
        }
        else
        {
          time = this.secToTime(value);
          
          desc = "{*actor*} just finished a maze called "+this.data.name+" in Mind Crime. Time to beat is "+time+"! Can you beat it?";
  				icon += "played.jpg";
        }
				
				var link = "http://"+xconfig.hostname+"/stage1/?maze="+maze;
				
				this.data.name = this.data.name.replace("http://", "");
				
				var attachment = {"name":"Mind Crime: "+this.data.name,"caption":desc,"href":link,"media":[{"type":"image","src":icon,"href":link}]};
				
				var links = [{"text":"Click to play!","href":link}];
				
				xfb.wallPost("", attachment, links, null, "");
			break;			
		}
		
		this.unity.jsStatsCallback();
	}
}
