function create_workspace_ajax(record_id, title, user_id, tags, img_no, thumb){
	$.ajax({
		type: "GET",
		url: "/xml/workspace_create.php",
		dataType: "xml",
		data: "create_workspace=1&record_id="+record_id+"&user_id="+user_id+"&img_no="+img_no+"&thumb="+thumb+"&title="+encodeURIComponent(title.value)+"&tags="+encodeURIComponent(tags.value)+"",
		success: function(xml) {
			$(xml).find('workspace').each(function(){
				var created = $(this).find('created').text();
				var reason = $(this).find('reason').text();
				if(created == 1){	
					$('#workspace_status').html('You have successfully created a new workspace.');	
					return true;
				} else {
					alert(reason);
					return false;
				}
			});
		}
	});
}
function add_to_workspace_ajax(record_id, workspace_id, img_no, thumb){
	$.ajax({
		type: "GET",
		url: "/xml/workspace_add.php",
		dataType: "xml",
		data: "add_to_workspace=1&record_id="+record_id+"&img_no="+img_no+"&thumb="+thumb+"&workspace_id="+workspace_id.value+"",
		success: function(xml) {
			$(xml).find('workspace').each(function(){
				var added = $(this).find('added').text();
				var reason = $(this).find('reason').text();
				if(added == 1){	
					$('#workspace_status').html('You have added this media to your workspace.');
					return true;
				} else {
					alert(reason);
					return false;
				}
			});
		}
	});
}
function add_to_workspace_session(record_id, media_urn, type, img, thumb){
	
	if(type == "a"){
		
		$.ajax({
			type: "GET",
			url: "/xml/workspace_add.php",
			dataType: "xml",
			data: "session_lightbox_add=1&record_id="+record_id+"&media_urn="+media_urn+"&img="+img+"&thumb="+thumb+"",
			success: function(xml) {
				$(xml).find('workspace').each(function(){
					$('#workspace_options_'+record_id+'').empty();
					$('#workspace_options_'+record_id+'').html('<a href="record.php?id='+record_id+'&amp;session_lightbox_del=1&amp;record_id='+record_id+'&amp;media_urn='+media_urn+'&amp;img='+img+'&amp;thumb='+thumb+'" class="add-r" onclick="add_to_workspace_session('+record_id+', '+media_urn+', \'r\', '+img+', '+thumb+'); return false;">remove from workspace</a>');
					return true;
				});
			}
		});
		
	} else if(type == "r") {
		
		$.ajax({
			type: "GET",
			url: "/xml/workspace_add.php",
			dataType: "xml",
			data: "session_lightbox_del=1&record_id="+record_id+"&media_urn="+media_urn+"&img="+img+"&thumb="+thumb+"",
			success: function(xml) {
				$(xml).find('workspace').each(function(){
					$('#workspace_options_'+record_id+'').empty();
					$('#workspace_options_'+record_id+'').html('<a href="record.php?id='+record_id+'&amp;session_lightbox_add=1&amp;record_id='+record_id+'&amp;media_urn='+media_urn+'&amp;img='+img+'&amp;thumb='+thumb+'" class="add" onclick="add_to_workspace_session('+record_id+', '+media_urn+', \'a\', '+img+', '+thumb+'); return false;">add to workspace</a>');
					return true;
				});
			}
		});
		
	}
	
}
function workspace_share(workspace_id){
	var share;
	if($('#public').attr('checked') == false){
		share = "N";
	} else if($('#public').attr('checked') == true) {
		share = "Y";
	}
	$.ajax({
		type: "GET",
		url: "/xml/workspace_shared.php",
		dataType: "xml",
		data: "share_workspace=1&workspace_id="+workspace_id+"&share="+share+"",
		success: function(xml) {
			$(xml).find('workspace').each(function(){
				var shared = $(this).find('shared').text();
				$('#public_'+workspace_id+'').empty;	
				$('#public_'+workspace_id+'').html(shared);	
				if(shared == "Y"){	
					alert('You have shared this workspace.');
					$('#public').attr('checked', true);
					return true;
				} else if(shared == "N") {
					alert('This is now a private workspace.');
					$('#public').attr('checked', false);
					return true;
				} else {
					alert('There was a problem with this share request. Please try again.');
					return true;
				}
			});
		}
	});
}
function add_ws_tag(new_tag, add_to_tags){
	var tag = encodeURIComponent(new_tag.value);
	var workspace_id = add_to_tags.value;
	if(tag == null || tag == ''){
		alert('You must enter a tag first!');
		return false;
	}
	$.ajax({
		type: "GET",
		url: "/xml/workspace_tags.php",
		dataType: "xml",
		data: "add_to_tags="+workspace_id+"&tag="+tag+"",
		success: function(xml) {
			$(xml).find('workspace').each(function(){
				var tags_affected = $(this).find('tags_affected').text();
				if(tags_affected == 1){	
					alert('You have tagged this workspace with \''+new_tag.value+'\'.');
					document.getElementById('create-tag').value = '';
					ws_tag_cloud(workspace_id);
					return true;
				} else {
					alert('You have already tagged this workspace with \''+new_tag.value+'\'.');
					return true;
				}
			});
		}
	});
}
function ws_tag_cloud(workspace_id){
	$('#ws-tag-cloud').fadeOut("slow", function () {
		$('#ws-tag-cloud').empty();
		$.ajax({
			type: "GET",
			url: "/xml/workspace_tags_cloud.php",
			cache: false,
			dataType: "html",
			data: "id="+workspace_id+"",
			success: function(html) {
				$("#ws-tag-cloud").append(html);
			}
		});
		$('#ws-tag-cloud').fadeIn("slow");
	});
	return true;	
}
function send_ws_invitation(id, title, email_to, email_msg){
	var workspace_id = id.value;
	var workspace_title = title.value;
	var to = email_to.value;
	var msg = email_msg.value;
	if(to == null || to == ''){
		document.email_form.email_to.focus();
		alert('You must tell us where to send the invitation!');
		return false;
	}
	$('#email_invitation_form').fadeOut("slow", function () {
		$('#email_invitation_form').empty();
		$.ajax({
			type: "GET",
			url: "/workspace_email.php",
			cache: false,
			dataType: "html",
			data: "mail=1&to="+encodeURIComponent(to)+"&msg="+encodeURIComponent(msg)+"&id="+workspace_id+"&title="+encodeURIComponent(workspace_title)+"",
			success: function(html) {
				$("#email_invitation_form").append(html);
			}
		});	
		$('#email_invitation_form').fadeIn("slow");
	});
}
function add_ws_notes(id, notes, new_title){
	var workspace_id = id.value;
	var workspace_notes = notes.value;
	var workspace_title = new_title.value;
	$('#notes_status').fadeOut("slow", function () {
		$.ajax({
			type: "POST",
			url: "/xml/workspace_notes.php",
			cache: false,
			dataType: "xml",
			data: "add_notes=1&id="+encodeURIComponent(workspace_id)+"&notes="+encodeURIComponent(workspace_notes)+"&title="+encodeURIComponent(workspace_title)+"",
			success: function(xml) {
				$(xml).find('workspace').each(function(){
					var rows = $(this).find('rows').text();
					var notes = $(this).find('notes').text();
					var title = $(this).find('title').text();
					$('#notes_status').empty;	
					if(rows > 0){	
						$('#notes_status').html('You have updated this workspace');
						$('#ws_notes').fadeOut("slow", function () {
							$('#ws_notes').empty;	
							$('#ws_notes').html(notes);
							$('#workspace_title').empty;	
							$('#workspace_title').html(title);
						});
						$('#workspace_title').fadeIn("fast");
						$('#ws_notes').fadeIn("slow");
						return true;
					} else {
						$('#notes_status').html('Failed to update this workspace.');
						return false;
					}
				});
			}
		});	
		$('#notes_status').fadeIn("slow");
	});
}