//this is done when "Share On Facebook" is clicked.
$('#photoShare').live("click", function(){	
	u='http://www.trinipulse.com/entertainment/lime.php?id='+image;
	t='triniPULSE.com :: Limelight :: '+GalleryName;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
});

//this is done when "Tag This Photo" is clicked.
$('#photoTag').live("click", function(){	
	$('#fbUploadMessage').css('display','none'); //hide fb upload message	
	$('#fbTagBox').html('Error Loading Friends List.<br>Please refresh page and try again.');	
	if(AccoutType == 'facebook'){
		if(AccountID != ''){
			$('#fbTagBox').html('<img src="../images/loader_phototag.gif" width="16" height="16" alt=""><br>Loading Friends List.');	
			//get friends list.
			$.ajax({
				url: root + '/includes/ajax_photoTagList.php',
				type: 'POST',
				data: 'AlbumID=0&PhotoID=' + image,
				success: function(message){
					$('#fbTagBox').html(message);
				}
			});				
		}		
	}else{
		$message  = '<p>You need to be logged in with your<br>facebook account to tag photos.</p>';
		if(AccoutType == 'trinipulse'){	
			$message += '<p><a href="../logout.php?ref=entertainment/lime.php?id=' + image + '" class="Red11"><b>Click Here to log out of your triniPULSE.com account then login with your facebook account.</b></a></p>'; 
			$message += '<p><a href="../facebook/aboutconnect.php" class="Red10">[learn about facebook login here]</a></p>';
			$('#fbTagBox').html($message);
		}else{ 
			$message += '<p><fb:login-button size="medium" background="dark" length="long"></fb:login-button></p>'; 
			$message += '<p><a href="../facebook/aboutconnect.php" class="Red10">[learn about facebook login here]</a></p>';
			$('#fbTagBox').html($message);
			FB.XFBML.Host.parseDomTree();
		}
	}	
	$('#fbTagBox').toggle(200);	
});

//tag photo.
$('div[type=tagCheckbox]').live("click", function(){	
	friendID = $(this).attr('friendID');	
	friendName = $(this).attr('friendName');	
	$('#friend'+friendID).css('display','none'); //hide selected friend
	
	//add tag here.
	$.ajax({
		url: root + '/includes/ajax_photoTagAdd.php',
		type: 'POST',
		data: 'AlbumName=' + GalleryName + '&AlbumID=0&GalleryRoot=0&GalleryID=0&PhotoID=' + image + '&Tagged=' + friendID,
		success: function(message){
			//update photo tags...
			updateTags(image);
		}
	});	
});

//this opens the tag box
function updateTags(i){
	$('#phototags').css('display','none'); //hide photo tags
	$.ajax({
		url: root + '/includes/ajax_photoTagUpdate.php',
		type: 'POST',
		data: 'AlbumID=0&PhotoID=' + i,
		success: function(content){
			if(content != ''){
				$('#phototags').html(content);
				$('#phototags').slideDown(200);				
				setTimeout(function(){FB.XFBML.Host.parseDomTree();},200);
			}
		}
	});	
}

//remove tag from photo.
$('a[type=removeTag]').live("click", function(){	
	$('#phototags').slideUp(200);
	tagID = $(this).attr('tagID');
	photoID = $(this).attr('photoID');
	//remove tag here.
	$.ajax({
		url: root + '/includes/ajax_photoTagRemove.php',
		type: 'POST',
		data: 'AlbumID=0&id=' + tagID,
		success: function(message){
			//update photo tags...
			updateTags(photoID);
		}
	});	
});

//close tag box.
$('a[type=tagButton]').live("click", function(){	
	$('#fbTagBox').slideUp(200);

	//get tag ids for feed.
	$.ajax({
		url: root + '/includes/ajax_photoTagFeed.php',
		type: 'POST',
		data: 'AlbumID=0&PhotoID=' + image,
		success: function(tags){	
			if(tags != ''){			
				
				//GalleryLink = encodeURI('http://www.facebook.com/tos.php?api_key=e4bd252023cf9fc7e33e68ad9ecc9b3d&next=http://www.trinipulse.com/entertainment/limelight.php&v=1.0&canvas#');
				
				//'http://www.trinipulse.com/entertainment/lime.php?id='+image;
				GalleryLink = 'http://www.trinipulse.com/entertainment/lime.php?id='+image;
				GalleryPhoto = imagePath;
				var feed_data = {"gallery":GalleryName,
								 "feed-link":GalleryLink,
								 "tagged":tags,
								 "images":[{"src":GalleryPhoto,"href":GalleryLink}]};
				FB.ensureInit(function(){
					FB.Connect.showFeedDialog(179940875230,feed_data);
				});				
			}
		}
	});	
});

//this is done when "Upload To Facebook" is clicked.
$('#photoUpload').live("click", function(){	
	$('#fbTagBox').css('display','none'); //hide fb tab box	
	$('#fbUploadMessage').html('');
	
	if(AccoutType == 'facebook'){	
		if(AccountID != ''){
			//[START]: Check for permission to upload to FB ---------------------------------------------.
			FB.Facebook.apiClient.users_hasAppPermission('photo_upload',function(status){
				if(status == 0){
					// render the permission dialog
					FB.Connect.showPermissionDialog('photo_upload',function(permission){uploadPic(permission)});
				}else{
					 // permission already granted.
	           		 uploadPic('photo_upload');
				}			
			});
			//[END]: Check for permission to upload to FB -----------------------------------------------.
		
			//[START]: this function uploads the photo once the correct permission is granted.			
			function uploadPic(status){						
				if(status == 'false' || status == ''){
					$('#fbUploadMessage').html('You must allow triniPULSE.com permission to publish content to your facebook account to upload photos.<br>Please try again.');
					$('#fbUploadMessage').slideDown(200);
				}else{
					$('#fbUploadMessage').html('<img src="../images/loader_photoupload.gif" width="16" height="16" alt=""><br>Uploading photo to your facebook galleries.');
					$('#fbUploadMessage').slideDown(200);
					//upload photo here.
					$.ajax({
						url: root + '/includes/ajax_limelightUpload.php',
						type: 'POST',
						data: 'PhotoID=' + image,
						success: function(message){
							$('#fbUploadMessage').html(message);
						}
					});	
				}
			}		
			//[END]: this function uploads the photo once the correct permission is granted.						
		}
	}else{
		$message  = '<p>You need to be logged in with your<br>facebook account to upload photos.</p>';
		if(AccoutType == 'trinipulse'){	
			$message += '<p><a href="../logout.php?ref=entertainment/lime.php?id=' + image + '" class="Red11"><b>Click Here to log out of your triniPULSE.com account then login with your facebook account.</b></a></p>'; 
			$message += '<p><a href="../facebook/aboutconnect.php" class="Red10">[learn about facebook login here]</a></p>';
			$('#fbUploadMessage').html($message);
			$('#fbUploadMessage').slideDown(200);
		}else{ 
			$message += '<p><fb:login-button size="medium" background="dark" length="long"></fb:login-button></p>'; 
			$message += '<p><a href="../facebook/aboutconnect.php" class="Red10">[learn about facebook login here]</a></p>';
			$('#fbUploadMessage').html($message);
			$('#fbUploadMessage').slideDown(200);
			FB.XFBML.Host.parseDomTree();
		}
	}
});
