$(document).ready(function() {
	
	// Saving recipes
	$('.recipesave').click(function(e) {
		e.preventDefault();
		$this = $(this);
		var recipeId = string_subtract($this.attr('id'),'save_recipe_');
		
		$.ajax({
			url: getBaseURL()+'recipes/like',
			type: 'POST',	
			dataType: 'json',
			data: {recipeId: recipeId},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				if(data.status == "success") 
				{
					$this.find('span.margin-link').html('SAVED');
				}
				else
				{
					alert(data.text);
				}
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
				alert('error occurred');
			}
		});
	});
	
	
	// Saving articles
	$('.articlesave').click(function(e) {
		e.preventDefault();
		$this = $(this);
		var articleId = string_subtract($this.attr('id'),'save_article_');
		
		$.ajax({
			url: getBaseURL()+'articles/like',
			type: 'POST',	
			dataType: 'json',
			data: {articleId: articleId},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				if(data.status == "success") 
				{
					$this.find('span.margin-link').html('SAVED');
				}
				else
				{
					alert(data.text);
				}
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
				alert('error occurred');
			}
		});
	});
	
	
	// Saving supplements
	$('.supplementsave').click(function(e) {
		e.preventDefault();
		$this = $(this);
		var supplementId = string_subtract($this.attr('id'),'save_supplement_');
		
		$.ajax({
			url: getBaseURL()+'supplements/like',
			type: 'POST',	
			dataType: 'json',
			data: {supplementId: supplementId},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				if(data.status == "success") 
				{
					$this.find('span.margin-link').html('SAVED');
				}
				else
				{
					alert(data.text);
				}
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
				alert('error occurred');
			}
		});
	});
	
	
	// Saving videos
	$('.videosave').click(function(e) {
		e.preventDefault();
		$this = $(this);
		var videoId = string_subtract($this.attr('id'),'save_video_');
		
		$.ajax({
			url: getBaseURL()+'videos/like',
			type: 'POST',	
			dataType: 'json',
			data: {videoId: videoId},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				if(data.status == "success") 
				{
					$this.find('span.margin-link').html('SAVED');
				}
				else
				{
					alert(data.text);
				}
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
				alert('error occurred');
			}
		});
	});
	
	// Saving posts
	$('.postsave').click(function(e) {
		e.preventDefault();
		$this = $(this);
		var postId = string_subtract($this.attr('id'),'save_post_');
		
		$.ajax({
			url: getBaseURL()+'posts/like',
			type: 'POST',	
			dataType: 'json',
			data: {postId: postId},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				if(data.status == "success") 
				{
					$this.find('span.margin-link').html('SAVED');
				}
				else
				{
					alert(data.text);
				}
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
				alert('error occurred');
			}
		});
	});
	
});

