function addScrap (el) {
	$.getJSON(el.attr('href'), function(data) {
		if (data['response'] == 200) {
			$('body').append(
				'<div id="dialog-scrap-message">'+
					'<p>'+data['message']+'</p>'+
				'</div>'
			);
			
			$("#dialog:ui-dialog").dialog( "destroy" );
			$("#dialog-scrap-message").dialog({
				modal: true,
				closeOnEscape: false,
				draggable: false,
				resizable: false,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						$('#dialog-scrap-message').remove();
					}
				},
				close: function(event, ui) {
					$('#dialog-scrap-message').remove();
				}
			});
		} else {
			alert('Error adding this item to your Scrapbook');
		}
	});
}

function deleteScrap(el) {
	s = el;
	if (confirm('Are you sure you want to delete this Scrapbook item?') == true) {
		$.getJSON(el.attr('href'), function(data) {
			if (data['response'] == 200) {
				s.parent().parent().slideUp();
				setTimeout("s.parent().parent().remove();", 1000);
			}
		});
	}
}

function showHideScrapNoteForm(el) {
	if (el.parent().parent().children('.note-form').children('#note_form').is(':visible')) {
		el.parent().parent().children('.note-form').children('#note_form').remove();
	} else {
		$.get(el.attr('href'), function(data) {
			data = jQuery.parseJSON(data);
			if (data['response'] == 200){
				el.parent().parent().children('.note-form').append(data['template']);
			} else{
				if (data['error']){
					alert(data['error']);
				} else {
					alert('Opps!\nSomething has gone wrong on our end!\nSorry about that. Please try reloading the page')
				}
			}
		});
	}
}

function addEditScrapNote(el) {
	var note = el.children('#id_note').val();
	var params = {
		'note': note
	}

	$.post(el.attr('action'), params, function(data) {
		var data = eval('('+data+')');
		if (data['response'] == 200) {
			el.parent().parent().children('.sb-item-buttons').children('.add-a-note').attr('href', data['edit_url']);
			el.parent().parent().children('.sb-item-buttons').children('.add-a-note').text('+ Edit note');
			el.parent().parent().children('.notes').html(
				'<div class="sb-item-note">' +
					'<strong>Note: </strong>' + note +
					'<a class="delete-note" href="'+data['delete_url']+'" onclick="deleteScrapNote($(this)); return false;">X</a>' +
				'</div>'
			);
			showHideScrapNoteForm(el);
		} else {
			alert('Error processing note.');
		}
	});
}

function deleteScrapNote(el) {
	e = el;
	$.getJSON(el.attr('href'), function(data) {
		if (data['response'] == 200) {
			e.parent().parent().parent().children('.sb-item-buttons').children('.add-a-note').text('+ Add a note');
			e.parent().slideUp();
			e.parent().parent().parent().children('.note-form').children().remove();
			setTimeout("e.parent().remove();", 1000);
		} else {
			alert('Error deleting note.');
		}
	});
}

