function CFBPhoto(parent)
{
	this.parent = parent;
	
	this.reqs = new CReqs(this);
	this.link = null;
	this.photoLink = null;
	
	this.selected = null;
	
	this.canvas = new CPhotoCanvas(this);
	
	this.show = function()
	{
    if(isIE())
    {
      var g = gE("game");
      if(g)
        g.style.display = "none";
    }
      
		var content = "";
		
		//content += "<div id=\"target-tagged-container\" style=\"display: none;\"><h3>From photos you've been tagged on</h3><div class=\"scroller\"><div id=\"target-tagged\" class=\"photos\"></div></div></div>";
		content += "<div id=\"target-albums-container\" style=\"display: none;\"><h2>Select your photo</h2><p>Select a photo to represent you in the game.</p><h3>From your albums</h3><div class=\"scroller\"><div id=\"target-albums\" class=\"photos\"></div></div></div>";
		content += "<div id=\"target-photos-container\" style=\"display: none;\"><h3>Album photos</h3><div class=\"scroller\"><div id=\"target-photos\" class=\"photos\"></div></div></div>";
		
		//photo manipulation
		content += "<div id=\"target-manipulator-container\" style=\"display: none;\"><h2>Modify your photo</h2><p>Modify your photo to create the asset you want to hide from other players. Use the mouse to click and stamp (you can stamp up to eight times), then click to continue.</p><div id=\"target-manipulator\" class=\"photos\"></div></div>";
		
		//photo confirmation
		content += "<div id=\"target-disclaimer-container\" style=\"display: none;\"><h2>Confirm your selection</h2><div id=\"target-disclaimer\"></div>";
		
		game.messages.show("photo", content);
		
		this.selectUid(FB.Connect.get_loggedInUser());
	}
	
	this.setPhoto = function(link)
	{
		if(link == null)
			game.fb.photo.show();
		else
			game.fb.photo.link = link;
	}

	this.selectUid = function(uid)
	{		
		//visual
		mE("target-tagged-container,target-albums-container,target-photos-container,target-manipulator-container,target-disclaimer-container").hide();
		this.canvas.cleanup();
		
		//load photos where he's tagged
		/*
		var q = "SELECT pid, src_big, caption FROM photo WHERE pid IN (SELECT pid FROM photo_tag WHERE subject="+uid+")";
		FB.Facebook.apiClient.fql_query(q, this.taggedCallback);
		*/
		
		//load his albums
		var q = "SELECT aid, cover_pid, name FROM album WHERE owner="+uid;
		FB.Facebook.apiClient.fql_query(q, this.albumsCallback);
		
		//visual
		mE("target-tagged-container,target-albums-container").show();

		var e = mE("target-tagged,target-albums");
		e.style("width: 100px;");
		e.content("<div class=\"photo-loading\">LOADING</div>");
	}
	
	this.taggedCallback = function(data)
	{
		var e = gE('target-tagged');
		if(!e)
			return;
			
		if(data == null)
		{
			e.innerHTML = "";
			return;
		}
			
		var content = "";
		for(var i = 0; i < data.length; i++)
		{
			content += "<a href=\"javascript:game.fb.photo.selectPhoto('"+data[i].pid+"', '"+data[i].src_big+"')\"><img src=\""+data[i].src_big+"\" alt=\""+data[i].caption+"\" onload=\"game.messages.updatePosition()\" /><br />"+data[i].caption+"</a>";
		}
		
		e.style.width = data.length * 170 + "px";
		
		e.innerHTML = content + "<div style=\"clear: both;\"></div>";
		
		//TODO: Update controls
	}
	
	this.albumsCallback = function(data)
	{
		var e = gE('target-albums');
		if(!e)
			return;
		
		if(data == null)
		{
			e.innerHTML = "";
			return;
		}
			
		var covers = new Array();
		var content = "";
		for(var i = 0; i < data.length; i++)
		{
			covers.push("pid=\""+data[i].cover_pid+"\"");
			
			var name = data[i].name;
			if(!name.length)
				name = "Album";
			
			content += "<a href=\"javascript:game.fb.photo.selectAlbum('"+data[i].aid+"')\"><img id=\"album-"+data[i].cover_pid+"\" src=\"\" alt=\""+name+"\" onload=\"game.messages.updatePosition()\" /><br />"+name+"</a>";
		}
		
		e.style.width = data.length * 170 + "px";
		
		e.innerHTML = content + "<div style=\"clear: both;\"></div>";
		
		if(covers.length > 0)
		{
			covers = covers.join(" OR ");
		
			//get album covers
			var q = "SELECT pid, src_big, caption FROM photo WHERE "+covers;
			FB.Facebook.apiClient.fql_query(q, game.fb.photo.albumsCoversCallback);
		}
		
		//TODO: Update controls
	}
	
	this.albumsCoversCallback = function(data)
	{
		if(data == null)
			return;
			
		for(var i = 0; i < data.length; i++)
		{
			var e = gE('album-'+data[i].pid);
			if(!e)
				continue;
			
			if(data[i].src_big == null)
				e.src = "images/facebook_icon.jpg";
			else
				e.src = data[i].src_big;
		}
	}
	
	this.selectAlbum = function(aid)
	{
		//load pictures from this album
		var q = "SELECT pid, src_big, caption FROM photo WHERE aid=\""+aid+"\"";
		FB.Facebook.apiClient.fql_query(q, this.albumContentCallback);
		
		var e = gE('target-photos-container');
		if(!e)
			return;
			
		e.style.display = "none";
		
		game.messages.updatePosition();
	}
	
	this.albumContentCallback = function(data)
	{
		var e = gE('target-photos');
		if(!e)
			return;
		
		if(data == null)
		{
			mE("target-photos-container").hide();
			e.innerHTML = "";
			return;
		}
		
		mE("target-photos-container").show();
		
		var covers = new Array();
		var content = "";
		for(var i = 0; i < data.length; i++)
		{
			covers.push("pid="+data[i].cover_pid);
			content += "<a href=\"javascript:game.fb.photo.manipulatePhoto('"+data[i].pid+"', '"+data[i].src_big+"')\"><img src=\""+data[i].src_big+"\" alt=\""+data[i].caption+"\" onload=\"game.messages.updatePosition()\" /><br />"+data[i].caption+"</a>";
		}
		
		e.style.width = data.length * 170 + "px";
		e.innerHTML = content + "<div style=\"clear: both;\"></div>";
		
		game.messages.updatePosition();
		
		game.fb.photo.canvas.hide();
	}
	
	this.manipulatePhoto = function(pid, url)
	{
		//hide elements
		mE("target-tagged-container,target-albums-container,target-photos-container").hide();
		
		var e = gE('target-manipulator');
		if(!e)
			return;
		
		var content = "";
		
		content += "<div><img id=\"target-manipulator-photo\" src=\""+url+"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		content += "<div><input type=\"button\" value=\"<< Back\" onclick=\"game.fb.photo.selection()\" /><input type=\"button\" value=\"Clear\" onclick=\"game.fb.photo.canvas.cleanup()\" /><input type=\"button\" value=\"Continue >>\" onclick=\"game.fb.photo.approvePhoto()\" /></div>";
		
		e.innerHTML = content;
		
		mE("target-manipulator-container").show();
		
		game.messages.updatePosition();
		
		game.fb.photo.canvas.show(pid);
	}
	
	this.approvePhoto = function()
	{
		game.fb.photo.canvas.save();
		
		//hide elements
		mE("target-manipulator-container").hide();
					
		var e = gE('target-disclaimer');
		if(!e)
			return;
		
		var content = "";
		
		content += "<div id=\"target-photo-approval-container\"><img id=\"target-photo-approval\" src=\"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		content += "<div><input type=\"button\" value=\"Back to photo selection\" onclick=\"game.fb.photo.selection()\" /></div>";
		
		content += "<h3>Disclaimer</h3><p>By clicking \"I Agree\" I certify that I have the right to grant the following license to this photo and that any person depicted in the photo that is not me has approved the use proposed by the license.</p><p>By clicking \"I Agree\" I hereby grant to Warner Bros. Entertainment Inc. a perpetual, sublicenseable, transferable, non-exclusive, royalty-free license to copy, modify, distribute, publicly perform and publicly display this photo and I understand and agree that these rights will be sublicensed to players who gain access to this photo within the game.</p>";
		content += "<div><input type=\"button\" value=\"I Disagree\" onclick=\"game.fb.photo.exit()\" /><input type=\"button\" value=\"I Agree\" onclick=\"game.fb.photo.save()\" /></div>";
		
		e.innerHTML = content;
		
		mE("target-disclaimer-container").show();

		game.messages.updatePosition();
		
		game.fb.photo.canvas.hide();
		
    if(isIE())
    {
      var g = gE("game");
      if(g)
        g.style.display = "block";
    }
	}
	
	this.selection = function()
	{
		mE('target-manipulator-container,target-disclaimer-container').hide();
		mE('target-tagged-container,target-albums-container,target-photos-container').show();
		
		this.canvas.hide();
		this.canvas.cleanup();
		
		game.messages.updatePosition();
	}
	
	this.save = function(id)
	{
    this.selected = 1;
    
		this.reqs.get("fb.savePhoto");
		
		this.close();
	}
	
	this.exit = function()
	{
    this.selected = 1;
    
		this.reqs.get("fb.deletePhoto");
		
		this.close();
	}
	
	//display functions
	this.report = function(target, link)
	{
		this.reqs.post("fb.reportPhoto", '{"target":'+target+', "photoLink":"'+link+'"}');
		
		this.close();
	}
	
	this.publish = function(target, link)
	{
		this.reqs.post("fb.publishPhoto", '{"target":'+target+', "photoLink":"'+link+'"}');
		
		var e = gE("target-photo-publish");
		if(e)
      e.disabled = true;
	}
	
	this.close = function()
	{
    if(isIE())
    {
      var g = gE("game");
      if(g)
        g.style.display = "block";
    }

		game.messages.hide();
	}
	
	this.display = function(target, link)
	{
		var content = "";
		
		content += "<div id=\"target-photo-display-container\"><h2 style=\"text-align: center;\">Your Target's Asset</h2>";
		
		content += "<div id=\"target-photo-display\"><img src=\"api?cmd=fb.getPhoto&link="+link+"&target="+target+"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		
		content += "<div class=\"abuse\"><a href=\"javascript:game.fb.photo.report('"+game.fb.target.uid+"', '"+link+"')\">Report Abuse</a></div>";
		
		var friend = "";
		for(var i in game.fb.target.friends)
		{
			if(game.fb.target.uid == game.fb.target.friends[i])
			{
				friend = "<input type=\"button\" value=\"Notify your target\" onclick=\"game.fb.target.shareFriend("+game.fb.target.uid+", '"+link+"')\" />";
				break;
			}
		}
		
		content += "<div><input id=\"target-photo-publish\" type=\"button\" value=\"Save to Facebook album\" onclick=\"game.fb.photo.publish('"+game.fb.target.uid+"', '"+link+"')\" />"+friend+"<input type=\"button\" value=\"Publish on Wall\" onclick=\"game.fb.target.share(null, '"+link+"')\" /><input type=\"button\" value=\"Next\" onclick=\"game.messages.hide()\" /></div>";
		content += "</div>";
		
		game.messages.show("photo", content);
		game.messages.updatePosition();
	}
	
  this.current = function()
	{
		var content = "";
		content += "<h2 style=\"text-align: center;\">Your Asset</h2>";
		content += "<div style=\"text-align: center;\"><img src=\"api?cmd=fb.getPhoto\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		content += "<div style=\"text-align: center;\"><input type=\"button\" value=\"Change\" onclick=\"game.fb.photo.change()\" /><input type=\"button\" value=\"Close\" onclick=\"game.messages.hide()\" /></div>";

		game.messages.show("current-photo", content);
		game.messages.updatePosition();
	}
	
	this.change = function()
	{
    game.messages.hide();
    this.show();
  }
}

function CPhotoCanvas(parent)
{
	this.parent = parent;
	
	this.pid = null;
	
	this.brush = new CBrush(this);
	
	this.stamps = new Array();
	
	this.reqs = new CReqs(this);
	
	this.save = function()
	{
		//get the photo position and relativize the coords for overlays
		var e = gE('target-manipulator-photo');
		if(!e)
			return;
		
		var x = 0;
		var y = 0;
		for(var obj = e; obj != null; obj = obj.offsetParent)
		{      
			x += obj.offsetLeft;
			y += obj.offsetTop;
		}
		
		//assemble the json string
		var json = new Array();
		json.push('{"pid":"'+this.pid+'"}');
		
		for(var i in this.stamps)
			json.push('{"x":"'+(this.stamps[i].x - x)+'", "y":"'+(this.stamps[i].y - y)+'"}');
		
		this.reqs.post("fb.generatePhoto", "["+json.join(",")+"]", "link", this.saveCallback);
	}
	
	this.saveCallback = function(type, data)
	{		
		if(data == null)
			return; //TODO: return default image link
		
		var p = gE("target-photo-approval");
		if(!p)
			return;
			
		p.src = "api/?cmd=fb.getPhoto&link="+data.link;
		p.style.display = "inline-block";
	}
	
	this.show = function(pid)
	{
		this.pid = pid;
		
		this.brush.enable();
		mE("manipulator-canvas,manipulator-brush").show();
	}
	
	this.hide = function()
	{
		this.brush.disable();
		mE("manipulator-canvas,manipulator-brush").hide();
	}
	
	this.cleanup = function()
	{
		this.stamps = new Array();
		this.render();
	}
	
	//based on current cursor position, add a stamp to the screen
	this.paint = function(mx, my)
	{
		var c = game.fb.photo.canvas;
		
		//if the position is outside the photo, disable the manipulation
		var e = gE('target-manipulator-photo');
		if(!e)
			return;
		
		var x = 0;
		var y = 0;
		for(var obj = e; obj != null; obj = obj.offsetParent)
		{      
			x += obj.offsetLeft;
			y += obj.offsetTop;
		}
		
		/*
		if( mx - Math.round(c.brush.stamp.image.sizex / 1.9) >= x &&
			my - Math.round(c.brush.stamp.image.sizey / 1.9) >= y && 
			mx + Math.round(c.brush.stamp.image.sizex / 1.9) <= x + e.offsetWidth &&
			my + Math.round(c.brush.stamp.image.sizey / 1.9) <= y + e.offsetHeight )
		{
			if(c.stamps.length < 8)
			{
				c.stamps.push(new CStamp(this, "default", c.brush.stamp.x, c.brush.stamp.y));
				c.render();
			}
		}
		*/
		
		if( mx >= x &&
			my >= y && 
			mx <= x + e.offsetWidth &&
			my <= y + e.offsetHeight &&
			c.stamps.length < 8
		)
		{
			c.stamps.push(new CStamp(this, "default", c.brush.stamp.x, c.brush.stamp.y));
			c.render();
		}
		
		//c.stamps.push(new CStamp(this, "default", mx - (c.brush.stamp.image.sizex / 2), my - (c.brush.stamp.image.sizey / 2)));
		//c.render();
	}
	
	this.render = function()
	{
		//flush the canvas first
		var e = gE('manipulator-canvas');
		if(!e)
			return;
		
		var content = "";
		
		for(var i in this.stamps)
			content += this.stamps[i].render(128 + parseInt(i));
		
		e.innerHTML = content;
	}
}

function CBrush(parent)
{
	this.parent = parent;
	
	this.stamp = new CStamp(this, "default", 0, 0);
	
	//on mouse move, update stamp position
	this.updatePosition = function()
	{
		var c = game.fb.photo.canvas.brush;
		
		//if the position is outside the photo, disable the manipulation
		var e = gE('target-manipulator-photo');
		if(!e)
			return;
		
		var x = 0;
		var y = 0;
		for(var obj = e; obj != null; obj = obj.offsetParent)
		{      
			x += obj.offsetLeft;
			y += obj.offsetTop;
		}
		
		if(
		mousex - (c.stamp.image.sizex / 2) >= x && 
		mousey - (c.stamp.image.sizey / 2) >= y &&
		mousex + (c.stamp.image.sizex / 2) <= x + e.offsetWidth &&
		mousey + (c.stamp.image.sizey / 2) <= y + e.offsetHeight
		)
		{
			c.stamp.updatePosition(mousex - (c.stamp.image.sizex / 2), mousey - (c.stamp.image.sizey / 2));
			c.render();
		}
		
		//c.stamp.updatePosition(mousex - (c.stamp.image.sizex / 2), mousey - (c.stamp.image.sizey / 2));
		//c.render();
	}

	this.paint = function(e)
	{
		var c = game.fb.photo.canvas;
		
		c.paint(e.positionX, e.positionY);
		c.render();
	}
	
	this.paintIE = function()
	{
    var c = game.fb.photo.canvas;
    
    c.paint(mousex, mousey);
		c.render();
  }
	
	this.enable = function()
	{
    if(!isIE())
    {
		  mouseEvents.addEvent("brush-move", "onmousemove", this.updatePosition);
		  mouseEvents.addEvent("brush-paint", "onclick", this.paint);
    }
    else
    {
      document.onmousemove = function(e){ getMouse(e); try{ game.fb.photo.canvas.brush.updatePosition(); }catch(e){} };
      //document.onclick = function(e){ game.fb.photo.canvas.brush.paintIE(); };
    }
	}
	
	this.disable = function()
	{
		mouseEvents.removeEvent("brush-move");
		mouseEvents.removeEvent("brush-paint");
	}
	
	this.render = function()
	{
		var e = gE('manipulator-brush');
		if(!e)
			return;
		
		e.innerHTML = this.stamp.render(256);
	}
}

function CStamp(parent, image, x, y)
{
	this.parent = parent;
	
	//position
	this.x = x;
	this.y = y;
	
	//stamp image
	this.image = new CStampImage(image);
	
	this.updatePosition = function(x, y)
	{
		this.x = x;
		this.y = y;
	}
	
	this.render = function(zvalue)
	{
		return "<div style=\"position: absolute; top: "+this.y+"px; left: "+this.x+"px; z-index: "+zvalue+";\">"+this.image.render()+"</div>";
		//return this.image.render();
	}
}

function CStampImage(image)
{
	this.sizex = null;
	this.sizey = null;
	
	this.image = null;
	
	switch(image)
	{
		default:
		case "default":
			this.sizex = 256;
			this.sizey = 36;
			//this.image = "<img src=\"images/brushes/default.gif\" alt=\"Stamp\" />";
			
			if(isIE())
        this.image = "<img src=\"images/brushes/brush.png\" alt=\"Stamp\" onclick=\"game.fb.photo.canvas.paint(mousex, mousey)\" />";
			else
			  this.image = "<img src=\"images/brushes/brush.png\" alt=\"Stamp\" />";
		break;
	}
	
	this.render = function()
	{
		return this.image;
	}
}

