// =====
//
// Permissions library
//
// =====

$(document).ready(function() {
	
	// -----
	// Add user form
	//  - Send new user request to add user to DB
	//  - if HTTP return is 200, add the user in the user-list
	// -----
	$('form.userform').livequery(function(){
		$('form.userform').ajaxForm();
		return false;
	});
	
	// -----
	// Remove user by clicking a link
	//  - Remove the user from DB using post request
	//  - Remove the user from the user-list
	// -----
	$("a.remove_user").livequery('click', function(event) {
		
		// Fetch user_id
		user = this.rel.split("_").pop();
		
		// query on permission url with "remove_user" action
		$.post("/permissions", { permission_act: "rem_user", user_id: user } );
		
		// remove user from list
		$("#"+this.rel).hide();
		
		return false;
	});
	
	$("input.permission-item").click(function() {
		
		// -----
		// Add permission by checking a checkbox
		//  - fetch user_id and gallery_id from the checbox
		//  - Send request to add a permission on this gallery to this user
		// -----
		if (this.checked) {
		  	// Fetching rel info
			rel = this.name.split("_");
			// Fetch user_id
			user = rel.pop();
			// Fetch gallery_id
			gallery = rel.pop();

			// query on permission url with "add_perm" action
			$.post("/permissions", { permission_act: "add_perm", user_id: user, gallery_id: gallery } );
			this.checked = false;
		}
		
		// -----
		// Remove permission by unchecking a checkbox
		//  - fetch the permission id from the checkbox
		//  - Send request to destroy this permission
		// -----
		else {
			// Fetch perm_id
			perm = this.id.split("_").pop();

			// query on permission url with "remove_perm" action
			$.post("/permissions", { permission_act: "rem_perm", perm_id: perm } );
			this.checked = true;
		}
	});
});


@@ -801,13 +801,8 @@
 %09$(%22
-input
 .per
@@ -820,14 +820,27 @@
 m%22).
+livequery('
 click
-(
+', 
 func
@@ -840,24 +840,29 @@
 ', function(
+event
 ) %7B%0A%09%09%0A%09%09// 
@@ -1366,33 +1366,8 @@
  );%0A
-%09%09%09this.checked = false;%0A
 %09%09%7D%0A
@@ -1740,21 +1740,30 @@
 ;%0A%09%09
-%09
+%7D%0A%09%09%0A%09%09$(
 this
+)
 .checked
  = t
@@ -1762,20 +1762,34 @@
 cked
- = true;%0A%09%09%7D
+.toggle();%0A%09%09return false;
 %0A%09%7D)
@@ -1793,10 +1793,8 @@
 %09%7D);%0A%7D);
-%0A%0A

use livequery and toggle checkbox