function getAllFollowingPosts(limit, offset){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/allfollowingposts.php",
		data: "limit="+limit+"&offset="+offset,
		beforeSend: function(){
			$('#loading').show();
		},
		success: function(msg){
			$('#loading').hide();
			$('#center-items').html(msg);
		}
	});
}

function getAllPosts(profileID, limit, offset){
	$.ajax({
		type: "GET",
		url: "/interface/allposts.php",
		data: "profileID="+profileID+"&limit="+limit+"&offset="+offset,
		beforeSend: function(){
			$('#loading').show();
		},
		success: function(msg){
			$('#loading').hide();
			$('#center-items').html(msg);
		}
	});
}

function getAllFavs(userID, limit, offset) {
	$.ajax({
		type: "GET",
		url: "/interface/favorites.php",
		data: "userID="+userID+"&limit="+limit+"&offset="+offset,
		beforeSend: function(){
			$('#loading').show();
		},
		success: function(msg){
			$('#loading').hide();
			$('#center-items').html(msg);
		}
	});
}

function getAllActivity(limit, offset){
	$.ajax({
		type: "GET",
		url: "/interface/allactivity.php",
		data: "limit="+limit+"&offset="+offset,
		beforeSend: function(){
			$('#loading').show();
		},
		success: function(msg){
			$('#loading').hide();
			$('#center-items').html(msg);
		}
	});
}

function invite(email){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/invite.php",
		data: "email="+email,
		beforeSend: function(){
			$('#invite-box img').show();
		},
		success: function(msg){
			if (msg == true) {
				$.lightbox("Invitation Sent!");
				setTimeout("$(document).trigger('close.lightbox');", 3000);
			}
			else {
				$.lightbox(msg);
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			$('#invite-box img').hide();
			$('#invite-email').val('');
		}
	});
}


function changePhoto(params){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/upload.php",
		data: params,
		dataType: "json",
		beforeSend: function(){
			if(params != 'action=delete')
				$.lightbox.loading();
		},
		success: function(response){
			if (response.errorMsg) {
				$.lightbox(response.errorMsg);
			}
			else if(response.content){
				var handler;
				if(response.crop){
					$.lightbox(response.content);
					handler = $('#thumbnail').imgAreaSelect({ instance: true, x1: 10, y1: 10, x2: 80, y2: 80, aspectRatio: '1:1', onSelectChange: preview , outerColor: 'black' , outerOpacity: '0.6' , minHeight: '40' , minWidth: '40', show: 'true', zIndex: '1600', parent: '#lightbox', handles: true });
					$('#save_thumb').click(function() {
						var x1 = $('#x1').val();
						var y1 = $('#y1').val();
						var x2 = $('#x2').val();
						var y2 = $('#y2').val();
						var w = $('#w').val();
						var h = $('#h').val();
						if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
							alert("You must make a selection first");
							return false;
						}else{
							handler.setOptions({hide: true});
							handler.update();
							changePhoto("action=submit&x1="+x1+"&y1="+y1+"&x2"+x2+"&y2"+y2+"&w="+w+"&h="+h);
							return true;
						}
					});
					$(document).bind('close.lightbox', function() {
						handler.setOptions({hide: true});
						handler.update();
					});					
					$(document).bind('close.lightbox', deleteCachedImage);
					$(document).unload(deleteCachedImage);
				} else if(response.select){
					$.lightbox(response.content);
					new AjaxUpload("#upload-pic", {
						action: "/interface/dashboard/upload.php",
						name: "image",
						onSubmit: function(file, extension) {
							$.lightbox.loading();
						},
						onComplete : function(file, response){
							if (response != 'success') {
								$.lightbox(response);
							} else
								changePhoto("action=crop");
						}
					});
				} else if(response.submit){
					$.lightbox(response.content);
				}
			} else if(response.done){
				$.lightbox.close();
			} else if(response.deleted){
				$(document).unbind('close.lightbox', deleteCachedImage);
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			
		}
	});
}

function deleteCachedImage(){
	changePhoto('action=delete');
}

function preview(img, selection) { 
	var scaleX = 80 / selection.width;
	var scaleY = 80 / selection.height;
	$('#thumbnail + div > img').css({ 
		width: Math.round(scaleX * document.getElementById('thumbnail').width) + 'px', 
		height: Math.round(scaleY * document.getElementById('thumbnail').height) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
} 

function follow(withUserID, pointer){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "follow=1&withUserID="+withUserID,
		dataType: "json",
		beforeSend: function(){
			pointer.disabled = true;
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=follow&withUserID="+withUserID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).removeClass('follow-button');
				$(pointer).addClass('unfollow-button');
				$(pointer).html('');
				$(pointer).attr('title', 'unfollow');
				$(pointer).before('<div class="following-status">Following</div>');
				$(pointer).attr('onclick', '');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					unfollow(withUserID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			pointer.disabled = false;
		}
	});
}

function unfollow(withUserID, pointer){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "unfollow=1&withUserID="+withUserID,
		dataType: "json",
		beforeSend: function(){

		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=unfollow&withUserID="+withUserID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).removeClass('unfollow-button');
				$(pointer).addClass('follow-button');
				$(pointer).html('<label>+</label>Follow');
				$(pointer).attr('title', 'follow');
				$(pointer).prev().remove();
				$(pointer).attr('onclick', '');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					follow(withUserID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			
		}
	});
}

function addFav(postID, pointer){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "addFav=1&postID="+postID,
		dataType: "json",
		beforeSend: function(){
			$(pointer).hide();
			$(pointer).next().show();
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=addFav&postID="+postID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).removeClass('fav-button-off');
				$(pointer).addClass('fav-button-on');
				$(pointer).attr('onclick', '');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					delFav(postID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			$(pointer).next().hide();
			$(pointer).show();
		}
	});
}

function delFav(postID, pointer) {
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "delFav=1&postID="+postID,
		dataType: "json",
		beforeSend: function(){
			$(pointer).hide();
			$(pointer).next().show();
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=delFav&postID="+postID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {				
				$(pointer).removeClass('fav-button-on');
				$(pointer).addClass('fav-button-off');				
				$(pointer).attr('onclick', '');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					addFav(postID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			$(pointer).next().hide();
			$(pointer).show();
		}
	});
}

function sharePost(postID, pointer){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "share=1&postID="+postID,
		dataType: "json",
		beforeSend: function(){
			$(pointer).hide();
			$(pointer).next().show();
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=share&postID="+postID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).html('Unshare');
				$(pointer).attr('onclick', '');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					unsharePost(response.newPostID, postID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			$(pointer).next().hide();
			$(pointer).show();
		}
	});
}

function unsharePost(postID, parentPostID, pointer){
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "unshare=1&postID="+postID,
		dataType: "json",
		beforeSend: function(){
			$(pointer).hide();
			$(pointer).next().show();
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=unshare&postID="+postID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).html('Share');
				$(pointer).unbind('click');
				$(pointer).click(function(){
					sharePost(parentPostID, pointer);
				});
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			$(pointer).next().hide();
			$(pointer).show();
		}
	});
}

function addComment(postID, pointer) {
	if(!$(pointer).prev().val())
		return;
	pointer.disabled = true;
	$.ajax({
		type: "POST",
		url: "/interface/dashboard/actions.php",
		data: "addComment=1&postID="+postID+"&text="+$(pointer).prev().val(),
		dataType: "json",
		beforeSend: function(){
			$(pointer).prev().css('background-color', '#DDDDDD');
		},
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=addComment&postID="+postID+"&text="+$(pointer).prev().val();
				} else if(response.errorNum != 1)
					$.lightbox(response.errorMsg);
			}
			else {
				var comment = document.createElement('div');
				$(comment).html($('#dummy-comment').html());
				$(comment).find('a.uname').after(' '+$(pointer).prev().val());
				$(comment).find('.btm-info label').html("1 second ago")
				if($(pointer).parent().prev().is('.comment-lure')){
					$(pointer).parent().prev().before($(comment).html());
					$(pointer).parent().prev().prev().find('.btm-info a.link').click(function(){
						delComment(response.commentID, this);
					});
				} else {					
					$(pointer).parent().before($(comment).html());
					$(pointer).parent().prev().find('.btm-info a.link').click(function(){
						delComment(response.commentID, this);
					});
				}
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			pointer.disabled = false;
			$(pointer).prev().val('');
			$(pointer).prev().css('background-color', '#FFFFFF');
		}
	});
}

function delComment(commentID, pointer) {
	$.ajax({
		type: "GET",
		url: "/interface/dashboard/actions.php",
		data: "delComment=1&commentID="+commentID,
		dataType: "json",
		success: function(response){
			if (response.errorMsg) {
				if(response.errorNum == 1){
					document.location="/login.php?sendmeto="+document.location+"&action=delComment&commentID="+commentID;
				} else
					$.lightbox(response.errorMsg);
			}
			else {
				$(pointer).parent().parent().parent().fadeOut();
			}
		}
	});
}

function commentFocus(pointer) {
	$(pointer).parent().hide();
	$(pointer).parent().next().show();
	$(pointer).parent().next().children('textarea').focus();
}

function commentBlur(pointer){
	if (!$(pointer).val()) {
		$(pointer).parent().hide();
		$(pointer).parent().prev('.comment-lure').show();
	}
}
