diff --git a/apps/calendar/css/style.css b/apps/calendar/css/style.css
index 0204f2fc12faa96274963a09d6455161881c9c8e..6555c25a76797912ac0dbcca83df5ac0b76d61f4 100644
--- a/apps/calendar/css/style.css
+++ b/apps/calendar/css/style.css
@@ -17,6 +17,8 @@
 #editentry_dialog {display: none;}
 #parsingfail_dialog{display: none;}
 
+#loading { display: none; left: 40%; position: fixed; top: 4.4em; z-index: 100; }
+
 #calendar_holder {position: relative;bottom: 0; right: 0; left: 0; top: 3em;}
 .fc-content{padding:2px 4px;}
 #listview {margin: 0; padding: 10px; background: #EEEEEE;}
diff --git a/apps/calendar/export.php b/apps/calendar/export.php
index 9b3ea5005d647070b0201222c5fda97a7519fb85..ce1a4aa046fff4e0b07916dbe5ddb8b1fd25c295 100644
--- a/apps/calendar/export.php
+++ b/apps/calendar/export.php
@@ -6,25 +6,25 @@
  * See the COPYING-README file.
  */
 
-require_once ("../../lib/base.php");
+require_once ('../../lib/base.php');
 OC_Util::checkLoggedIn();
 OC_Util::checkAppEnabled('calendar');
-$cal = isset($_GET["calid"]) ? $_GET["calid"] : NULL;
-$event = isset($_GET["eventid"]) ? $_GET["eventid"] : NULL;
+$cal = isset($_GET['calid']) ? $_GET['calid'] : NULL;
+$event = isset($_GET['eventid']) ? $_GET['eventid'] : NULL;
 if(isset($cal)){
 	$calendar = OC_Calendar_App::getCalendar($cal);
 	$calobjects = OC_Calendar_Object::all($cal);
-	header("Content-Type: text/Calendar");
-	header("Content-Disposition: inline; filename=calendar.ics"); 
+	header('Content-Type: text/Calendar');
+	header('Content-Disposition: inline; filename=' . $calendar['displayname'] . '.ics'); 
 	foreach($calobjects as $calobject){
-		echo $calobject["calendardata"] . "\n";
+		echo $calobject['calendardata'] . '\n';
 	}
 }elseif(isset($event)){
-	$data = OC_Calendar_App::getEventObject($_GET["eventid"]);
-	$calendarid = $data["calendarid"];
+	$data = OC_Calendar_App::getEventObject($_GET['eventid']);
+	$calendarid = $data['calendarid'];
 	$calendar = OC_Calendar_App::getCalendar($calendarid);
-	header("Content-Type: text/Calendar");
-	header("Content-Disposition: inline; filename=" . $data["summary"] . ".ics"); 
-	echo $data["calendardata"];
+	header('Content-Type: text/Calendar');
+	header('Content-Disposition: inline; filename=' . $data['summary'] . '.ics'); 
+	echo $data['calendardata'];
 }
 ?>
diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js
index c3644b537041b95a2e6ac5237824a614226ecdfd..6e0a450f6a8f980f50ae545ff8b5df881e3d634e 100644
--- a/apps/calendar/js/calendar.js
+++ b/apps/calendar/js/calendar.js
@@ -8,7 +8,15 @@
 
 Calendar={
 	UI:{
+		loading: function(isLoading){
+			if (isLoading){
+				$('#loading').show();
+			}else{
+				$('#loading').hide();
+			}
+		},
 		startEventDialog:function(){
+			Calendar.UI.loading(false);
 			$('.tipsy').remove();
 			$('#calendar_holder').fullCalendar('unselect');
 			Calendar.UI.lockTime();
@@ -41,6 +49,7 @@ Calendar={
 				// TODO: save event
 				$('#event').dialog('destroy').remove();
 			}else{
+				Calendar.UI.loading(true);
 				$('#dialog_holder').load(OC.filePath('calendar', 'ajax', 'neweventform.php'), {start:start, end:end, allday:allday?1:0}, Calendar.UI.startEventDialog);
 			}
 		},
@@ -50,13 +59,16 @@ Calendar={
 				// TODO: save event
 				$('#event').dialog('destroy').remove();
 			}else{
+				Calendar.UI.loading(true);
 				$('#dialog_holder').load(OC.filePath('calendar', 'ajax', 'editeventform.php') + '?id=' + id, Calendar.UI.startEventDialog);
 			}
 		},
 		submitDeleteEventForm:function(url){
 			var post = $( '#event_form' ).serialize();
 			$('#errorbox').empty();
+			Calendar.UI.loading(true);
 			$.post(url, post, function(data){
+					Calendar.UI.loading(false);
 					if(data.status == 'success'){
 						$('#calendar_holder').fullCalendar('removeEvents', $('#event_form input[name=id]').val());
 						$('#event').dialog('destroy').remove();
@@ -69,8 +81,10 @@ Calendar={
 		validateEventForm:function(url){
 			var post = $( "#event_form" ).serialize();
 			$("#errorbox").empty();
+			Calendar.UI.loading(true);
 			$.post(url, post,
 				function(data){
+					Calendar.UI.loading(false);
 					if(data.status == "error"){
 						var output = missing_field + ": <br />";
 						if(data.title == "true"){
@@ -107,8 +121,10 @@ Calendar={
 		},
 		moveEvent:function(event, dayDelta, minuteDelta, allDay, revertFunc){
 			$('.tipsy').remove();
+			Calendar.UI.loading(true);
 			$.post(OC.filePath('calendar', 'ajax', 'moveevent.php'), { id: event.id, dayDelta: dayDelta, minuteDelta: minuteDelta, allDay: allDay?1:0, lastmodified: event.lastmodified},
 			function(data) {
+				Calendar.UI.loading(false);
 				if (data.status == 'success'){
 					event.lastmodified = data.lastmodified;
 					console.log("Event moved successfully");
@@ -120,8 +136,10 @@ Calendar={
 		},
 		resizeEvent:function(event, dayDelta, minuteDelta, revertFunc){
 			$('.tipsy').remove();
+			Calendar.UI.loading(true);
 			$.post(OC.filePath('calendar', 'ajax', 'resizeevent.php'), { id: event.id, dayDelta: dayDelta, minuteDelta: minuteDelta, lastmodified: event.lastmodified},
 			function(data) {
+				Calendar.UI.loading(false);
 				if (data.status == 'success'){
 					event.lastmodified = data.lastmodified;
 					console.log("Event resized successfully");
@@ -210,6 +228,7 @@ Calendar={
 				if($('#choosecalendar_dialog').dialog('isOpen') == true){
 					$('#choosecalendar_dialog').dialog('moveToTop');
 				}else{
+					Calendar.UI.loading(true);
 					$('#dialog_holder').load(OC.filePath('calendar', 'ajax', 'choosecalendar.php'), function(){
 						$('#choosecalendar_dialog').dialog({
 							width : 600,
@@ -217,13 +236,16 @@ Calendar={
 								$(this).dialog('destroy').remove();
 							}
 						});
+						Calendar.UI.loading(false);
 					});
 				}
 			},
 			activation:function(checkbox, calendarid)
 			{
+				Calendar.UI.loading(true);
 				$.post(OC.filePath('calendar', 'ajax', 'activation.php'), { calendarid: calendarid, active: checkbox.checked?1:0 },
 				  function(data) {
+					Calendar.UI.loading(false);
 					if (data.status == 'success'){
 						checkbox.checked = data.active == 1;
 						if (data.active == 1){
@@ -520,6 +542,7 @@ $(document).ready(function(){
 				}
 			});
 		},
+		loading: Calendar.UI.loading,
 		eventSources: eventSources
 	});
 	$('#oneweekview_radio').click(function(){
diff --git a/apps/calendar/templates/calendar.php b/apps/calendar/templates/calendar.php
index 2003b7efc491f7ac38e96c86e7b3a90a7598d0d4..13bc8bc1bb1659fab2298486b3cbc964425bf165 100755
--- a/apps/calendar/templates/calendar.php
+++ b/apps/calendar/templates/calendar.php
@@ -19,6 +19,7 @@
 				var missing_field_dberror = '<?php echo $l->t('There was a database fail') ?>';
 				var totalurl = '<?php echo OC_Helper::linkTo('apps/calendar', 'caldav.php', null, true); ?>/calendars';
 				</script>
+				<div id="loading"><img src="<?php echo OC_Helper::imagePath('core', 'loading.gif'); ?>" /></div>
 				<div id="controls">
 					<div>
 						<form>
diff --git a/apps/contacts/ajax/activation.php b/apps/contacts/ajax/activation.php
new file mode 100644
index 0000000000000000000000000000000000000000..f4a2c94a148da46a21dcccecbfaf806c9203cc39
--- /dev/null
+++ b/apps/contacts/ajax/activation.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once ("../../../lib/base.php");
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+$bookid = $_POST['bookid'];
+OC_Contacts_Addressbook::setActive($bookid, $_POST['active']);
+$book = OC_Contacts_App::getAddressbook($bookid);
+
+/* is there an OC_JSON::error() ? */
+OC_JSON::success(array(
+	'active' => OC_Contacts_Addressbook::isActive($bookid),
+	'bookid' => $bookid,
+	'book'   => $book,
+));
diff --git a/apps/contacts/ajax/addbook.php b/apps/contacts/ajax/addbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..36acb9af3914d9b4e737e773a21460a2c3402d0c
--- /dev/null
+++ b/apps/contacts/ajax/addbook.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once('../../../lib/base.php');
+$l10n = new OC_L10N('contacts');
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+$book = array(
+	'id' => 'new',
+	'displayname' => '',
+);
+$tmpl = new OC_Template('contacts', 'part.editaddressbook');
+$tmpl->assign('new', true);
+$tmpl->assign('addressbook', $book);
+$tmpl->printPage();
+?>
diff --git a/apps/contacts/ajax/chooseaddressbook.php b/apps/contacts/ajax/chooseaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..b0a10bb311851ee5d0101362b13ddb2b88528086
--- /dev/null
+++ b/apps/contacts/ajax/chooseaddressbook.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once('../../../lib/base.php');
+$l10n = new OC_L10N('contacts');
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$output = new OC_TEMPLATE("contacts", "part.chooseaddressbook");
+$output -> printpage();
+?>
diff --git a/apps/contacts/ajax/contacts.php b/apps/contacts/ajax/contacts.php
new file mode 100644
index 0000000000000000000000000000000000000000..b34cf41424994725e8ce6c7b74eb7561b756bda2
--- /dev/null
+++ b/apps/contacts/ajax/contacts.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+function contacts_namesort($a,$b){
+	return strcasecmp($a['fullname'],$b['fullname']);
+}
+
+require_once('../../../lib/base.php');
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$addressbooks = OC_Contacts_Addressbook::active(OC_User::getUser());
+$contacts = array();
+foreach( $addressbooks as $addressbook ){
+	$addressbookcontacts = OC_Contacts_VCard::all($addressbook['id']);
+	foreach( $addressbookcontacts as $contact ){
+		if(is_null($contact['fullname'])){
+			continue;
+		}
+		$contacts[] = $contact;
+	}
+}
+usort($contacts,'contacts_namesort');
+$tmpl = new OC_TEMPLATE("contacts", "part.contacts");
+$tmpl->assign('contacts', $contacts);
+$page = $tmpl->fetchPage();
+
+OC_JSON::success(array('data' => array( 'page' => $page )));
+?>
diff --git a/apps/contacts/ajax/createaddressbook.php b/apps/contacts/ajax/createaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..f94ad34e8dc4fae7d3dddad511b1ac7ca71497b6
--- /dev/null
+++ b/apps/contacts/ajax/createaddressbook.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+require_once('../../../lib/base.php');
+
+$l10n = new OC_L10N('contacts');
+
+// Check if we are a user
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$userid = OC_User::getUser();
+$bookid = OC_Contacts_Addressbook::add($userid, $_POST['name'], null);
+OC_Contacts_Addressbook::setActive($bookid, 1);
+$addressbook = OC_Contacts_App::getAddressbook($bookid);
+$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
+$tmpl->assign('addressbook', $addressbook);
+OC_JSON::success(array(
+	'page' => $tmpl->fetchPage(),
+	'addressbook' => $addressbook,
+));
diff --git a/apps/contacts/ajax/deletebook.php b/apps/contacts/ajax/deletebook.php
index 3ede17ab88647880b717ee294d1dee245a6b95c5..a89c00575e9fc8f15119b76080b9522bc6b2e867 100644
--- a/apps/contacts/ajax/deletebook.php
+++ b/apps/contacts/ajax/deletebook.php
@@ -23,11 +23,13 @@
 // Init owncloud
 require_once('../../../lib/base.php');
 
+$l10n = new OC_L10N('contacts');
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$id = $_GET['id'];
+//$id = $_GET['id'];
+$id = $_POST['id'];
 $addressbook = OC_Contacts_App::getAddressbook( $id );
 
 OC_Contacts_Addressbook::delete($id);
diff --git a/apps/contacts/ajax/editaddressbook.php b/apps/contacts/ajax/editaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..ced673ce8070a2bb56f83c4cdbacc2f8f5e7934e
--- /dev/null
+++ b/apps/contacts/ajax/editaddressbook.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once('../../../lib/base.php');
+$l10n = new OC_L10N('contacts');
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+$addressbook = OC_Contacts_App::getAddressbook($_GET['bookid']);
+$tmpl = new OC_Template("contacts", "part.editaddressbook");
+$tmpl->assign('new', false);
+$tmpl->assign('addressbook', $addressbook);
+$tmpl->printPage();
+?>
diff --git a/apps/contacts/ajax/updateaddressbook.php b/apps/contacts/ajax/updateaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..516736cc502d13b988c304242ecef2c654078ba4
--- /dev/null
+++ b/apps/contacts/ajax/updateaddressbook.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once('../../../lib/base.php');
+
+$l10n = new OC_L10N('contacts');
+
+// Check if we are a user
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$bookid = $_POST['id'];
+OC_Contacts_Addressbook::edit($bookid, $_POST['name'], null);
+OC_Contacts_Addressbook::setActive($bookid, $_POST['active']);
+$addressbook = OC_Contacts_App::getAddressbook($bookid);
+$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
+$tmpl->assign('addressbook', $addressbook);
+OC_JSON::success(array(
+	'page' => $tmpl->fetchPage(),
+	'addressbook' => $addressbook,
+));
diff --git a/apps/contacts/css/styles.css b/apps/contacts/css/styles.css
index f351589fe12e6e32c790d634e77a8491ce829f74..f961767a0d62a90bcd0ce03616f2a55ad8958fdf 100644
--- a/apps/contacts/css/styles.css
+++ b/apps/contacts/css/styles.css
@@ -1,3 +1,4 @@
+#chooseaddressbook {margin-right: 170px; float: right;}
 #contacts_details_name { font-weight:bold;font-size:1.1em;margin-left:25%;}
 #contacts_details_photo { margin:.5em 0em .5em 25%; }
 
diff --git a/apps/contacts/export.php b/apps/contacts/export.php
new file mode 100644
index 0000000000000000000000000000000000000000..fd2d7da175084501d937aab1102d39dfa528ea44
--- /dev/null
+++ b/apps/contacts/export.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Copyright (c) 2011 Georg Ehrke <ownclouddev at georgswebsite dot de>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once ("../../lib/base.php");
+OC_Util::checkLoggedIn();
+OC_Util::checkAppEnabled('contacts');
+$book = isset($_GET['bookid']) ? $_GET['bookid'] : NULL;
+$contact = isset($_GET['contactid']) ? $_GET['contactid'] : NULL;
+if(isset($book)){
+	$addressbook = OC_Contacts_App::getAddressbook($book);
+	if($addressbook['userid'] != OC_User::getUser()){
+		OC_JSON::error();
+		exit;
+	}
+	$cardobjects = OC_Contacts_VCard::all($book);
+	header('Content-Type: text/directory');
+	header('Content-Disposition: inline; filename=' . str_replace(' ', '_', $addressbook['displayname']) . '.vcf'); 
+	for($i = 0;$i <= count($cardobjects); $i++){
+		echo $cardobjects[$i]['carddata'];
+		//echo '\r\n';
+	}
+}elseif(isset($contact)){	
+	$data = OC_Contacts_App::getContactObject($contact);
+	$addressbookid = $data['addressbookid'];
+	$addressbook = OC_Contacts_App::getAddressbook($addressbookid);
+	if($addressbook['userid'] != OC_User::getUser()){
+		OC_JSON::error();
+		exit;
+	}
+	header('Content-Type: text/directory');
+	header('Content-Disposition: inline; filename=' . $data['fullname'] . '.vcf'); 
+	echo $data['carddata'];
+}
+?>
diff --git a/apps/contacts/index.php b/apps/contacts/index.php
index 58ddc74f53225950e533d5162df756bd1f894405..a6af54c8789e757716eb73dc10c1cf4a796aba04 100644
--- a/apps/contacts/index.php
+++ b/apps/contacts/index.php
@@ -21,7 +21,7 @@
  */
 
 function contacts_namesort($a,$b){
-	return strcmp($a['name'],$b['name']);
+	return strcmp($a['fullname'],$b['fullname']);
 }
 
 // Init owncloud
@@ -31,17 +31,8 @@ require_once('../../lib/base.php');
 OC_Util::checkLoggedIn();
 OC_Util::checkAppEnabled('contacts');
 
-// Check if the user has an addressbook
-$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
-if( count($addressbooks) == 0){
-	OC_Contacts_Addressbook::add(OC_User::getUser(),'default','Default Address Book');
-	$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
-}
-$prefbooks = OC_Preferences::getValue(OC_User::getUser(),'contacts','openaddressbooks',null);
-if(is_null($prefbooks)){
-	$prefbooks = $addressbooks[0]['id'];
-	OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',$prefbooks);
-}
+// Get active address books. This creates a default one if none exists.
+$addressbooks = OC_Contacts_Addressbook::active(OC_User::getUser());
 
 // Load the files we need
 OC_App::setActiveNavigationEntry( 'contacts_index' );
@@ -51,17 +42,15 @@ $id = isset( $_GET['id'] ) ? $_GET['id'] : null;
 
 // sort addressbooks  (use contactsort)
 usort($addressbooks,'contacts_namesort');
-// Addressbooks to load
-$openaddressbooks = explode(';',$prefbooks);
 
 $contacts = array();
-foreach( $openaddressbooks as $addressbook ){
-	$addressbookcontacts = OC_Contacts_VCard::all($addressbook);
+foreach( $addressbooks as $addressbook ){
+	$addressbookcontacts = OC_Contacts_VCard::all($addressbook['id']);
 	foreach( $addressbookcontacts as $contact ){
 		if(is_null($contact['fullname'])){
 			continue;
 		}
-		$contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
+		$contacts[] = $contact;
 	}
 }
 
@@ -74,6 +63,13 @@ if( !is_null($id)/* || count($contacts)*/){
 	$details = OC_Contacts_VCard::structureContact($vcard);
 }
 
+// Include Style and Script
+OC_Util::addScript('contacts','interface');
+OC_Util::addStyle('contacts','styles');
+OC_Util::addStyle('contacts','formtastic');
+OC_Util::addScript('', 'jquery.multiselect');
+OC_Util::addStyle('', 'jquery.multiselect');
+
 $property_types = OC_Contacts_App::getAddPropertyOptions();
 $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
 $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
diff --git a/apps/contacts/js/interface.js b/apps/contacts/js/interface.js
index eb81e87268285cf6b25c4add6823e56d88d2fda4..02470d5a76770c0171d092faa45241ff8f1505b9 100644
--- a/apps/contacts/js/interface.js
+++ b/apps/contacts/js/interface.js
@@ -1,3 +1,118 @@
+Contacts={
+	UI:{
+		showCardDAVUrl:function(username, bookname){
+			$('#carddav_url').val(totalurl + '/' + username + '/' + bookname);
+			$('#carddav_url').show();
+			$('#carddav_url_close').show();
+		},
+		Addressbooks:{
+			overview:function(){
+				if($('#chooseaddressbook_dialog').dialog('isOpen') == true){
+					$('#chooseaddressbook_dialog').dialog('moveToTop');
+				}else{
+					$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'chooseaddressbook.php'), function(){
+						$('#chooseaddressbook_dialog').dialog({
+							width : 600,
+							close : function(event, ui) {
+								$(this).dialog('destroy').remove();
+							}
+						});
+					});
+				}
+			},
+			activation:function(checkbox, bookid)
+			{
+				$.post(OC.filePath('contacts', 'ajax', 'activation.php'), { bookid: bookid, active: checkbox.checked?1:0 },
+				  function(data) {
+					/*
+					 * Arguments:
+					 * data.status
+					 * data.bookid
+					 * data.active
+					 */
+					if (data.status == 'success'){
+						checkbox.checked = data.active == 1;
+						Contacts.UI.Contacts.update();
+					}
+				  });
+			},
+			newAddressbook:function(object){
+				var tr = $(document.createElement('tr'))
+					.load(OC.filePath('contacts', 'ajax', 'addbook.php'));
+				$(object).closest('tr').after(tr).hide();
+				/* TODO: Shouldn't there be some kinda error checking here? */
+			},
+			editAddressbook:function(object, bookid){
+				var tr = $(document.createElement('tr'))
+					.load(OC.filePath('contacts', 'ajax', 'editaddressbook.php') + "?bookid="+bookid);
+				$(object).closest('tr').after(tr).hide();
+			},
+			deleteAddressbook:function(bookid){
+				var check = confirm("Do you really want to delete this address book?");
+				if(check == false){
+					return false;
+				}else{
+					$.post(OC.filePath('contacts', 'ajax', 'deletebook.php'), { id: bookid},
+					  function(data) {
+						if (data.status == 'success'){
+							$('#chooseaddressbook_dialog').dialog('destroy').remove();
+							Contacts.UI.Contacts.update();
+							Contacts.UI.Addressbooks.overview();
+						} else {
+							alert('Error: ' + data.message);
+						}
+					  });
+				}
+			},
+			submit:function(button, bookid){
+				var displayname = $("#displayname_"+bookid).val();
+				var active = $("#edit_active_"+bookid+":checked").length;
+				var description = $("#description_"+bookid).val();
+
+				var url;
+				if (bookid == 'new'){
+					url = OC.filePath('contacts', 'ajax', 'createaddressbook.php');
+				}else{
+					url = OC.filePath('contacts', 'ajax', 'updateaddressbook.php');
+				}
+				$.post(url, { id: bookid, name: displayname, active: active, description: description },
+					function(data){
+						if(data.status == 'success'){
+							$(button).closest('tr').prev().html(data.page).show().next().remove();
+						}
+					});
+				Contacts.UI.Contacts.update();
+			},
+			cancel:function(button, bookid){
+				$(button).closest('tr').prev().show().next().remove();
+			}
+		},
+		Contacts:{
+			update:function(){
+				$.getJSON('ajax/contacts.php',{},function(jsondata){
+					if(jsondata.status == 'success'){
+						$('#contacts').html(jsondata.data.page);
+					}
+					else{
+						alert(jsondata.data.message);
+					}
+				});
+				/*
+				var contactlist = $('#contacts');
+				var contacts = contactlist.children('li').get();
+				//alert(contacts);
+				contacts.sort(function(a, b) {
+					var compA = $(a).text().toUpperCase();
+					var compB = $(b).text().toUpperCase();
+					return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
+				})
+				$.each(contacts, function(idx, itm) { contactlist.append(itm); });
+				*/
+			}
+		}
+	}
+}
+
 $(document).ready(function(){
 	/*-------------------------------------------------------------------------
 	 * Event handlers
@@ -79,6 +194,11 @@ $(document).ready(function(){
 		return false;
 	});
 
+	$('#chooseaddressbook').click(function(){
+		Contacts.UI.Addressbooks.overview();
+		return false;
+	});
+	
 	$('#contacts_newcontact').click(function(){
 		$.getJSON('ajax/showaddcard.php',{},function(jsondata){
 			if(jsondata.status == 'success'){
diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php
index 87477ed7ed41913ad505b9c92641fd76935d8eae..6f2f34225de755d7ba75dd224b65c659b3060ef8 100644
--- a/apps/contacts/lib/addressbook.php
+++ b/apps/contacts/lib/addressbook.php
@@ -44,7 +44,7 @@ class OC_Contacts_Addressbook{
 	 * @return array
 	 */
 	public static function all($uid){
-		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ?' );
+		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ORDER BY displayname' );
 		$result = $stmt->execute(array($uid));
 
 		$addressbooks = array();
@@ -140,12 +140,98 @@ class OC_Contacts_Addressbook{
 		return true;
 	}
 
+	/**
+	 * @brief Get active addressbooks for a user.
+	 * @param integer $uid User id. If null current user will be used.
+	 * @return array
+	 */
+	public static function activeIds($uid = null){
+		if(is_null($uid)){
+			$uid = OC_User::getUser();
+		}
+		$prefbooks = OC_Preferences::getValue($uid,'contacts','openaddressbooks',null);
+		if(is_null($prefbooks)){
+			$addressbooks = OC_Contacts_Addressbook::all($uid);
+			if(count($addressbooks) == 0){
+				OC_Contacts_Addressbook::add($uid,'default','Default Address Book');
+				$addressbooks = OC_Contacts_Addressbook::all($uid);
+			}
+			$prefbooks = $addressbooks[0]['id'];
+			OC_Preferences::setValue($uid,'contacts','openaddressbooks',$prefbooks);
+		}
+		return explode(';',$prefbooks);
+	}
+
+	/**
+	 * @brief Returns the list of active addressbooks for a specific user.
+	 * @param string $uid
+	 * @return array
+	 */
+	public static function active($uid){
+		$active = self::activeIds($uid);
+		$addressbooks = array();
+		/** FIXME: Is there a way to prepare a statement 'WHERE id IN ([range])'?
+		*/
+		foreach( $active as $aid ){
+			$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ? ORDER BY displayname' );
+			$result = $stmt->execute(array($aid,));
+
+			while( $row = $result->fetchRow()){
+				$addressbooks[] = $row;
+			}
+		}
+
+		return $addressbooks;
+	}
+
+	/**
+	 * @brief Activates an addressbook
+	 * @param integer $id
+	 * @param integer $name
+	 * @return boolean
+	 */
+	public static function setActive($id,$active){
+		// Need these ones for checking uri
+		//$addressbook = self::find($id);
+
+		if(is_null($id)){
+			$id = 0;
+		}
+
+		$openaddressbooks = self::activeIds();
+		if($active) {
+			if(!in_array($id, $openaddressbooks)) {
+				$openaddressbooks[] = $id;
+			}
+		} else { 
+			if(in_array($id, $openaddressbooks)) {
+				unset($openaddressbooks[array_search($id, $openaddressbooks)]);
+			}
+		}
+		sort($openaddressbooks, SORT_NUMERIC);
+		// FIXME: I alway end up with a ';' prepending when imploding the array..?
+		OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',implode(';', $openaddressbooks));
+
+		return true;
+	}
+
+	/**
+	 * @brief Checks if an addressbook is active.
+	 * @param integer $id ID of the address book.
+	 * @return boolean
+	 */
+	public static function isActive($id){
+		OC_Log::write('contacts','OC_Contacts_Addressbook::isActive('.$id.'):'.in_array($id, self::activeIds()), OC_Log::DEBUG);
+		return in_array($id, self::activeIds());
+	}
+
 	/**
 	 * @brief removes an address book
 	 * @param integer $id
 	 * @return boolean
 	 */
 	public static function delete($id){
+		self::setActive($id, false);
 		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
 		$stmt->execute(array($id));
 
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index a573f40f7d91d9e80e5ecd48858b80810da812b9..7285761fd58706ded153162076d2b8d254c1945f 100644
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -47,15 +47,15 @@ class OC_Contacts_VCard{
 	 * ['carddata']
 	 */
 	public static function all($id){
-		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ?' );
+		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
 		$result = $stmt->execute(array($id));
 
-		$addressbooks = array();
+		$cards = array();
 		while( $row = $result->fetchRow()){
-			$addressbooks[] = $row;
+			$cards[] = $row;
 		}
 
-		return $addressbooks;
+		return $cards;
 	}
 
 	/**
diff --git a/apps/contacts/templates/index.php b/apps/contacts/templates/index.php
index 649c4807dd5f71587022f9b990b25bb18bfd7076..8592ffe1c4c651a056470f8c92adedb217c857c0 100644
--- a/apps/contacts/templates/index.php
+++ b/apps/contacts/templates/index.php
@@ -1,16 +1,14 @@
-<?php // Include Style and Script
-OC_Util::addScript('contacts','interface');
-OC_Util::addStyle('contacts','styles');
-OC_Util::addStyle('contacts','formtastic');
-?>
-
+<script type='text/javascript'>
+	var totalurl = '<?php echo OC_Helper::linkTo('contacts', 'carddav.php', null, true); ?>/addressbooks';
+</script>
 <div id="controls">
 	<form>
 		<input type="button" id="contacts_newcontact" value="<?php echo $l->t('Add Contact'); ?>">
+		<input type="button" id="chooseaddressbook" value="<?php echo $l->t('Address Books'); ?>">
 	</form>
 </div>
 <div id="leftcontent" class="leftcontent">
-	<ul>
+	<ul id="contacts">
 		<?php echo $this->inc("part.contacts"); ?>
 	</ul>
 </div>
@@ -24,3 +22,9 @@ OC_Util::addStyle('contacts','formtastic');
 		}
 	?>
 </div>
+<!-- Dialogs -->
+<div id="dialog_holder"></div>
+<div id="parsingfail_dialog" title="Parsing Fail">
+	<?php echo $l->t("There was a fail, while parsing the file."); ?>
+</div>
+<!-- End of Dialogs -->
diff --git a/apps/contacts/templates/part.chooseaddressbook.php b/apps/contacts/templates/part.chooseaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..ba008837f0e58b68276ef211e904f5f0f78d9c85
--- /dev/null
+++ b/apps/contacts/templates/part.chooseaddressbook.php
@@ -0,0 +1,24 @@
+<div id="chooseaddressbook_dialog" title="<?php echo $l->t("Choose active Address Books"); ?>">
+<table width="100%" style="border: 0;">
+<?php
+$option_addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
+for($i = 0; $i < count($option_addressbooks); $i++){
+	echo "<tr>";
+	$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
+	$tmpl->assign('addressbook', $option_addressbooks[$i]);
+	$tmpl->assign('active', OC_Contacts_Addressbook::isActive($option_addressbooks[$i]['id']));
+	$tmpl->printpage();
+	echo "</tr>";
+}
+?>
+<tr>
+	<td colspan="5">
+		<a href="#" onclick="Contacts.UI.Addressbooks.newAddressbook(this);"><?php echo $l->t('New Address Book') ?></a>
+	</td>
+</tr>
+<tr>
+	<td colspan="5">
+		<p style="margin: 0 auto;width: 90%;"><input style="display:none;width: 90%;float: left;" type="text" id="carddav_url" onmouseover="$('#carddav_url').select();" title="<?php echo $l->t("CardDav Link"); ?>"><img id="carddav_url_close" style="height: 20px;vertical-align: middle;display: none;" src="../../core/img/actions/delete.svg" alt="close" onclick="$('#carddav_url').hide();$('#carddav_url_close').hide();"/></p>
+	</td>
+</tr>
+</table>
diff --git a/apps/contacts/templates/part.chooseaddressbook.rowfields.php b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
new file mode 100644
index 0000000000000000000000000000000000000000..f612e39ecafdef5a70f77061b2ea248020c5c424
--- /dev/null
+++ b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
@@ -0,0 +1,5 @@
+<?php
+
+	echo "<td width=\"20px\"><input id=\"active_" . $_['addressbook']["id"] . "\" type=\"checkbox\" onClick=\"Contacts.UI.Addressbooks.activation(this, " . $_['addressbook']["id"] . ")\"" . (OC_Contacts_Addressbook::isActive($_['addressbook']["id"]) ? ' checked="checked"' : '') . "></td>";
+	echo "<td><label for=\"active_" . $_['addressbook']["id"] . "\">" . $_['addressbook']["displayname"] . "</label></td>";
+	echo "<td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.showCardDAVUrl('" . OC_User::getUser() . "', '" . $_['addressbook']["uri"] . "');\" title=\"" . $l->t("CardDav Link") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/public.svg\"></a></td><td width=\"20px\"><a href=\"export.php?bookid=" . $_['addressbook']["id"] . "\" title=\"" . $l->t("Download") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/download.svg\"></a></td><td width=\"20px\"><a  href=\"#\" title=\"" . $l->t("Edit") . "\" class=\"action\" onclick=\"Contacts.UI.Addressbooks.editAddressbook(this, " . $_['addressbook']["id"] . ");\"><img class=\"svg action\" src=\"../../core/img/actions/rename.svg\"></a></td><td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.Addressbooks.deleteAddressbook('" . $_['addressbook']["id"] . "');\" title=\"" . $l->t("Delete") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/delete.svg\"></a></td>";
diff --git a/apps/contacts/templates/part.contacts.php b/apps/contacts/templates/part.contacts.php
index 6664a3671acddda8c70d6e0df5d4183ce27fd6ec..8d89e9c7ad1857b12beb80f529be95dc6937f428 100644
--- a/apps/contacts/templates/part.contacts.php
+++ b/apps/contacts/templates/part.contacts.php
@@ -1,3 +1,3 @@
 <?php foreach( $_['contacts'] as $contact ): ?>
-	<li data-id="<?php echo $contact['id']; ?>"><a href="index.php?id=<?php echo $contact['id']; ?>"><?php echo $contact['name']; ?></a> </li>
+	<li book-id="<?php echo $contact['addressbookid']; ?>" data-id="<?php echo $contact['id']; ?>"><a href="index.php?id=<?php echo $contact['id']; ?>"><?php echo $contact['fullname']; ?></a> </li>
 <?php endforeach; ?>
diff --git a/apps/contacts/templates/part.editaddressbook.php b/apps/contacts/templates/part.editaddressbook.php
new file mode 100644
index 0000000000000000000000000000000000000000..cb1371731b1a50f0db0a3b7deff58e738a6f4b01
--- /dev/null
+++ b/apps/contacts/templates/part.editaddressbook.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+?>
+<td id="<?php echo $_['new'] ? 'new' : 'edit' ?>addressbook_dialog" title="<?php echo $_['new'] ? $l->t("New Address Book") : $l->t("Edit Address Book"); ?>" colspan="6">
+<table width="100%" style="border: 0;">
+<tr>
+	<th><?php echo $l->t('Displayname') ?></th>
+	<td>
+		<input id="displayname_<?php echo $_['addressbook']['id'] ?>" type="text" value="<?php echo $_['addressbook']['displayname'] ?>">
+	</td>
+</tr>
+<?php if (!$_['new']): ?>
+<tr>
+	<td></td>
+	<td>
+		<input id="edit_active_<?php echo $_['addressbook']['id'] ?>" type="checkbox"<?php echo  OC_Contacts_Addressbook::isActive($_['addressbook']['id']) ? ' checked="checked"' : '' ?>>
+		<label for="edit_active_<?php echo $_['addressbook']['id'] ?>">
+			<?php echo $l->t('Active') ?>
+		</label>
+	</td>
+</tr>
+<?php endif; ?>
+</table>
+<input style="float: left;" type="button" onclick="Contacts.UI.Addressbooks.submit(this, <?php echo $_['new'] ? "'new'" : $_['addressbook']['id'] ?>);" value="<?php echo $_['new'] ? $l->t("Save") : $l->t("Submit"); ?>">
+<input style="float: left;" type="button" onclick="Contacts.UI.Addressbooks.cancel(this, <?php echo $_['new'] ? "'new'" : $_['addressbook']['id'] ?>);" value="<?php echo $l->t("Cancel"); ?>">
+</td>
diff --git a/index.php b/index.php
index 2d759d68d7dcc30d95ceac6aff3d0b2f55535c66..9bd460be3530266525e10e6c30cf4ea0a1994acb 100644
--- a/index.php
+++ b/index.php
@@ -100,14 +100,15 @@ else {
 			$error = true;
 		}
 	}
-		// The user is already authenticated using Apaches AuthType Basic... very usable in combination with LDAP
-		elseif(isset($_SERVER["PHP_AUTH_USER"]) && isset($_SERVER["PHP_AUTH_PW"])){
-			if (OC_User::login($_SERVER["PHP_AUTH_USER"],$_SERVER["PHP_AUTH_PW"]))	{
-				OC_User::unsetMagicInCookie();
-				OC_Util::redirectToDefaultPage();
-			}else{
-				$error = true;
-			}
+	// The user is already authenticated using Apaches AuthType Basic... very usable in combination with LDAP
+	elseif(isset($_SERVER["PHP_AUTH_USER"]) && isset($_SERVER["PHP_AUTH_PW"])){
+		if (OC_User::login($_SERVER["PHP_AUTH_USER"],$_SERVER["PHP_AUTH_PW"]))	{
+			//OC_Log::write('core',"Logged in with HTTP Authentication",OC_Log::DEBUG);
+			OC_User::unsetMagicInCookie();
+			OC_Util::redirectToDefaultPage();
+		}else{
+			$error = true;
 		}
+	}
 	OC_Template::printGuestPage('', 'login', array('error' => $error, 'redirect' => isset($_REQUEST['redirect_url'])?$_REQUEST['redirect_url']:'' ));
 }