function CFBPhoto(parent)
{
	this.parent = parent;
	
	this.reqs = new CReqs(this);
	
	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>"+l.get("photo-head1", [])+"</h2><p>"+l.get("photo-text1", [])+"</p><h3>"+l.get("photo-head2", [])+"</h3><div class=\"scroller\"><div id=\"target-albums\" class=\"photos\"></div></div></div>";
		content += "<div id=\"target-photos-container\" style=\"display: none;\"><h3>"+l.get("photo-head3", [])+"</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>"+l.get("photo-head4", [])+"</h2><p>"+l.get("photo-text2", [])+"</p><div id=\"target-manipulator\" class=\"photos\"></div></div>";
		
		//photo confirmation
		content += "<div id=\"target-disclaimer-container\" style=\"display: none;\"><h2>"+l.get("photo-head5", [])+"</h2><div id=\"target-disclaimer\"></div>";
		
		game.messages.show("photo", content);
		
		this.selectUid(FB.Connect.get_loggedInUser());
	}

	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\">"+l.get("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=\""+l.get("photo-button-back", [])+"\" onclick=\"game.fb.photo.selection()\" /><input type=\"button\" value=\""+l.get("photo-button-clear", [])+"\" onclick=\"game.fb.photo.canvas.cleanup()\" /><input type=\"button\" value=\""+l.get("photo-button-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=\""+l.get("photo-button-back-selection", [])+"\" onclick=\"game.fb.photo.selection()\" /></div>";
		
		content += "<h3>"+l.get("photo-disclaimer-head", [])+"</h3><p>"+l.get("photo-disclaimer-text1", [])+"</p><p>"+l.get("photo-disclaimer-text2", [])+"</p>";
		content += "<div><input type=\"button\" value=\""+l.get("photo-disclaimer-decline", [])+"\" onclick=\"game.fb.photo.exit()\" /><input type=\"button\" value=\""+l.get("photo-disclaimer-accept", [])+"\" 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("photo.agreed");
		
		var content = "<div style=\"text-align: center; padding: 10px;\">"+l.get("photo-changed", [])+"</div>";
		content += "<div style=\"text-align: center;\"><input type=\"button\" onclick=\"game.messages.hide('photo-changed')\" value=\""+l.get("photo-changed-ok", [])+"\" /></div>";
		
		game.messages.showTimed("photo-changed", 3000, content);
		
		this.close();
	}
	
	this.exit = function()
	{
    this.selected = 1;
    
		this.reqs.get("photo.delete");
		
		this.close();
	}
	
	//display functions
	this.report = function(target)
	{
		this.reqs.post("photo.report", '{"target":"'+target+'"}', null, null);
		
		this.close();
	}
	
	this.publish = function(target)
	{
		this.reqs.post("photo.publish", '{"target":"'+target+'"}');
		
		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("photo");
	}
	
	this.display = function(target)
	{
		var content = "";
		
		content += "<div id=\"target-photo-display-container\"><h2 style=\"text-align: center;\">"+l.get("photo-target-asset", [])+"</h2>";
		
		content += "<div id=\"target-photo-display\"><img src=\"api?cmd=photo.load&target="+target+"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		
		content += "<div class=\"abuse\"><a href=\"javascript:game.fb.photo.report('"+game.fb.target.uid+"')\">"+l.get("photo-report", [])+"</a></div>";
		/*
		var friend = "";
		for(var i in game.fb.friends)
		{
			if(game.fb.target.uid == game.fb.friends[i])
			{
				friend = "<input type=\"button\" value=\""+l.get("photo-target-notify", [])+"\" onclick=\"game.fb.target.shareFriend("+game.fb.target.uid+")\" />";
				break;
			}
		}
		*/
		game.fb.publishActivity(l.get("fb-activity-steal-asset", [target]), l.get("fb-activity-steal", []), game.fb.getShareLink(null, target));
		
		//content += "<div><input id=\"target-photo-publish\" type=\"button\" value=\""+l.get("photo-save", [])+"\" onclick=\"game.fb.photo.publish('"+game.fb.target.uid+"')\" />"+friend+"<input type=\"button\" value=\""+l.get("photo-publish", [])+"\" onclick=\"game.fb.target.share('"+game.fb.target.uid+"')\" /><input type=\"button\" value=\""+l.get("photo-next", [])+"\" onclick=\"game.messages.hide()\" /></div>";
		content += "<div><input type=\"button\" value=\""+l.get("photo-next", [])+"\" onclick=\"game.fb.photo.displayClose('photo')\" /></div>";
		content += "</div>";
		
		game.messages.show("photo", content);
		game.messages.updatePosition();
	}
	
	this.displayClose = function()
	{
		for(var i in game.fb.friends)
		{
			if(game.fb.target.uid == game.fb.friends[i].uid)
			{
				game.fb.target.shareFriend(game.fb.target.uid);
				break;
			}
		}
		
		game.fb.photo.publish(game.fb.target.uid);
		game.messages.hide("photo");
	}
	
	this.current = function()
	{
		if(game.messages.isVisible())
			return;
		
		var content = "";
		content += "<h2 style=\"text-align: center;\">"+l.get("photo-asset", [])+"</h2>";
		content += "<div id=\"your-asset\"><img src=\"api?cmd=photo.load&target="+game.fb.user.uid+"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		
		var change = "";
		if(game.fb.photoPermission)
			change += "<input type=\"button\" value=\""+l.get("asset-change", [])+"\" onclick=\"game.fb.photo.change()\" />";
			
		content += "<div style=\"text-align: center;\">"+change+"<input type=\"button\" value=\""+l.get("asset-close", [])+"\" onclick=\"game.messages.hide('current-photo')\" /></div>";

		game.messages.show("current-photo", content);
		game.messages.updatePosition();
	}
	
	this.changed = function()
	{		
		var content = "";
		content += "<h2 style=\"text-align: center;\">"+l.get("photo-asset-changed", [])+"</h2>";
		content += "<div id=\"your-asset\"><img src=\"api?cmd=photo.load&target="+game.fb.user.uid+"\" alt=\"Photo\" onload=\"game.messages.updatePosition()\" /></div>";
		
		var change = "";
		if(game.fb.photoPermission)
			change += "<input type=\"button\" value=\""+l.get("asset-change", [])+"\" onclick=\"game.fb.photo.change()\" />";
			
		content += "<div style=\"text-align: center;\">"+change+"<input type=\"button\" value=\""+l.get("asset-close", [])+"\" onclick=\"game.messages.hide('current-photo')\" /></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();
		
		for(var i in this.stamps)
			json.push('{"x":"'+(this.stamps[i].x - x)+'","y":"'+(this.stamps[i].y - y)+'"}');
		
		this.reqs.post("photo.save", '{"pid":"'+this.pid+'","stamps":['+json.join(",")+']}', null, this.saveCallback);
	}
	
	this.saveCallback = function(type, data)
	{	
		var p = gE("target-photo-approval");
		if(!p)
			return;
			
		p.src = "api/?cmd=photo.load&target="+game.fb.user.uid;
		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 = 29;
			//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;
	}
}

