	$(document).ready(
		function(){			
			if(isCurrentUser) {
				$(".profilePhotolistitem").hover(
						function(e){
							$(this).find(".deletethumbnail").show().bind("click",onThumbClick);
							if (gallery!='fav')
								$(this).find(".editthumbnail").show().bind("click",onThumbClick);
						},function(e){
							$(this).find(".deletethumbnail").hide().unbind("click",onThumbClick);
							if (gallery!='fav')
								$(this).find(".editthumbnail").hide().unbind("click",onThumbClick);
						});
				
			}
		}
	);
	
	
	function onThumbClick(event){
		var targetId = event.target.id;
		
		var parts = targetId.split("_");
		var photoId = parts[0];		
		var action = parts[1];
		
		
		if(action == "del") {
			showDeleteDialog(photoId);
		} else if(action == "edit") {
			location.href="edit.php?photo_id="+photoId;
		}

	}
	
	function processDeletePhoto(photoId) {
		var el = $("#"+photoId+"_item");
		el.fadeOut("fast", function() {el.remove();});		
		
		var url = "ajax_photo_actions.php?photo_id="+photoId+(gallery=="fav" ? "&fav=1" : "&delete=1"); 
		
		$.ajax({
			  "url": url,
			  "success": function(data) {
				var resp = eval("("+data+")");				  
				if(resp.success) {
					$("#"+photoId+"_img").mouseover(function () {});
					var tt = $("#tooltip-"+photoId+"_img");
					if(tt) {
						tt.remove();					
					}											
				}
			  }
			});		
	}	

	function showDeleteDialog(photoId){			
			var addTxt = (gallery=="fav" ? " from your favorites" : "");			
			var text = _LG.lg_confirm_delete_photo+addTxt+".";
			
			var params = {
				content: text,
				buttons: [{label: "OK", onclick: "Dialog.hide(\'Del\');processDeletePhoto("+photoId+")"},
							{label:_LG.lg_cancel}
						]
			}			
			
			Dialog.show('Del', params);
	}

	var cropX1=0,cropX2=100,cropY1=0,cropY2=100,cropW,cropH;

	function onEditThumbnail() {
		if($("#userPic").attr("src").indexOf("no_photo") != -1) {
			return;
		}

	    jQuery(function() {
			var params = {
						aspectRatio: 1,
						//setSelect: [0, 0, 0, 0],
						bgColor: 'black',
						bgOpacity: 0.7,
						minSize:[50,50],
						onSelect: function(c){
												cropX1 = c.x;
												cropX2 = c.x2;
												cropY1 = c.y;
												cropY2 = c.y2;
												cropW  = c.w;
												cropH  = c.h;

											}
					}

    	    window.c = $.Jcrop('#userPic',params);
	    });

		c.setSelect([cropX1,cropY1,cropX2,cropY2]);
		$("#editThumbnail").fadeIn("slow");
		$("#uploadUserPicForm").css("display", "none");
		setTimeout(function(){$("#editUserPic").hide("fast")}, 500)
	}

	function closeEditThumbnail() {
		$("#editThumbnail").fadeOut("slow");

		removeJCrop();
	}

	function saveThumbnail() {
		var loadingId = "profile_loading";
		showLoading(loadingId);
		
		$.ajax({
			  url: 'upload_userpic.php?crop=1&x='+cropX1+'&y='+cropY1+'&d='+cropW,
			  success: function(data) {
				var resp = eval("("+data+")");
				closeEditThumbnail();
				showThumbnailDialog(resp.src);
				clearLoading(loadingId);
			  }
			});
	}

	function removePic(){
		var loadingId = "profile_loading";
		showLoading(loadingId);
		$.ajax({
			  url: 'upload_userpic.php?delUserPic=1',
			  success: function(data) {
				var resp = eval("("+data+")");

				if(resp.success) {
					clearLoading(loadingId);
					$("#userPic").attr("src", "./data/users/no_photo.jpg");
					$("#editPhotoGroup").css("display", "none");
				}
			  }
			});
	}

	function showThumbnailDialog(thumbSrc){
		var w = 350, h = 115;
		
		var html = [];

			html.push('<table width="100%" border="0" cellspacing="8" cellpadding="0">	');
			html.push('<tr>');
			html.push('<td width="50" valign="top"><img SRC="'+thumbSrc+'" width="50" height="50" align="left" /></td>');
			html.push('<td  valign="bottom">');
			html.push('<span class="regular_text">'+_LG.lg_confirm_avatar+'</span>');

			html.push('</td></tr>	');
			html.push('</table>');

		var params = {
						content: html.join(''),
						w: w, h: h,
						type: "Standard",
						buttons: 
							[
							 	{label: "OK"}, 
								{label: _LG.lg_cancel, onclick: "Dialog.hide(\'thumb\');onEditThumbnail();"}
							]
						};
		Dialog.show('thumb', params);
	}

	function nothing(e){
		e.stopPropagation();
		e.preventDefault();
		return false;
	};

	function removeJCrop() {
		$(".jcrop-holder").remove();
		$(".jcrop-holder").empty();
		$("#userPic").css("display", "block");
	}
	
	function seeAllFollows(userId) {
		if(isCurrentUser) {
			document.location.href = "follow_list.php";			
		} else {
			showAllFollowsDialog(userId);
		}
	}
	
	function showAllFollowsDialog(userId) {
		var w = 375, h = 305;		
		var dlgId = 'SendMsg';
		var html = [];

		html.push('<div style="margin:0px 10px;padding-top:5px;"><a id="flw1" href="javascript: getFollows('+userId+', \'followers\')" class="orange text_bold" style="margin-right:1px;">'+_LG.lg_followers+'</a>');
	    html.push(	'<a id="flw2" href="javascript: getFollows('+userId+', \'following\')" class="bullet blue_links">'+_LG.lg_following+'</a></div>');				
		html.push('<div id="flwArea" style="height: 200px; overflow: auto; border: 1px solid #E5E5E5; padding: 2px; margin:15px 10px;">');		
		html.push('</div>');				
		
		var params = {
						content: html.join(''),
						w: w, h: h,
						type: "Standard",
						buttons : [
							{label: _LG.lg_close},
						]};
		


		Dialog.show(dlgId, params);
		
		getFollows(userId, "followers");		
	}
	
	function getFollows(userId, viewType, page) {
		if(!page) {
			page = 1;
		}
		
		$("#flwArea").html("<img src='tpl/img/ajax-loader-small.gif'> Loading "+viewType+"s ...");
		$.ajax({
			  url: "ajax_follow_list.php?user_id="+userId+"&view="+viewType+"&page="+page,
			  success: function(data) {
				$("#flwArea").html(data);
				
				if(viewType == "followers") {
					$id("flw1").className = "orange text_bold";
					$id("flw2").className = "bullet blue_links";					
				} else {
					$id("flw1").className = "blue_links";															
					$id("flw2").className = "bullet orange text_bold";
				}
			  }
			});		
	}