// JavaScript Document
var ADMIN_ITEM_PREFIX = 'item_list.php?';
var person_title = new Array();

function item_list(the_item)
{
	var http_list = new Asynchronous( ); // We create the HTTP Object

	http_list.call('GET', ADMIN_ITEM_PREFIX+'item='+the_item);
	http_list.complete = function(status, statusText, responseText, responseXML) {
		if ($(".other_pages").get(0) != null){
			// when accessing categories/indexterms/users from home
			$(".other_pages").empty().append(responseText);
			var image_home = "<div id='back_home'><img src='_images/home.gif' alt='back to home page' border='0' width='13' height='15' style='padding:0px 0px 0px 5px;'/> Close</div>";
			$("DIV #item_block_menu").append(image_home);
			$("#back_home").click(function(){
				compose_page('home.php','centrecontent');return;
			})
		} else {
			// when accessing categories/indexterms/users not from home
			document.getElementById('centrecontent').innerHTML = responseText;		
		}
		try {if((the_item=='category' && login_rights.category_privilege!='R') || (the_item=='indexterm' && login_rights.indexterm_privilege!='R') || (the_item=='author' && login_rights.author_privilege!='R')){
			$('.editInPlace').each(function(i){
				setClickable(this, the_item);
			});
		}} catch(e){};
		try{if(the_item=='category' && login_rights.category_privilege!='E'){$("DIV #item_block_menu").empty();}}catch(e){}
		try{if(the_item=='indexterm' && login_rights.indexterm_privilege!='E'){$("DIV #item_block_menu").empty();}}catch(e){}
		try{if(the_item=='author' && login_rights.author_privilege!='E'){$("DIV #item_block_menu").empty();}}catch(e){}

		$("a.thickbox").click(function(){
		  var t = this.title || this.innerHTML || this.href;
		  TB_show(t,this.href);
		  this.blur();
		  return false;
		});
	}
	
	// search for the title in the database
	if(person_title == ''){get_title();} 
}

function get_title() {
	var http_title = new Asynchronous( ); // We create the HTTP Object
	var TITLE_PREFIX = 'item_get_title.php';
	http_title.call('GET', TITLE_PREFIX);
	http_title.complete = function(status, statusText, responseText, responseXML) {
		person_title = responseText.split('~~');
		if (person_title.length % 2 != 0){person_title.pop();}
	}
}

function author(obj) {
	var obj_array = new Array();
	obj_array = obj.split('~~');
	this.id = obj_array[0];
	this.title = obj_array[1];
	this.firstname = obj_array[2];
	this.lastname = obj_array[3];
	this.login = obj_array[4];
	this.password = obj_array[5];
	this.privilege = obj_array[6];
}

function setClickable(the_obj,the_item){
$(the_obj).click(function() {
	$('SPAN#cancelButton').trigger("click");	

	var id = $(the_obj).get(0).getAttribute('id');

if(the_item=='author') // CASE: author only
{
	var obj_author = new author($(the_obj).get(0).getAttribute('name'));
	var input_title = 'title: <select name="title">';
	for (j=0; j<person_title.length; j=j+2) {input_title += '<option value="'+person_title[j]+'"'; if(person_title[j] == obj_author.title){input_title +=' selected="selected" ';} input_title +='>'+person_title[j+1]+'</option>';}
	input_title += '</select><br>';
	var input_firstname = 'firstname: <input id="'+obj_author.id+'" class="normal" type="text" value="'+obj_author.firstname+'" name="firstname"><br>';
	var input_lastname = 'lastname: <input class="normal" id="" type="text" value="'+obj_author.lastname+'"><br>';
	var input_login = 'login: <input class="normal" id="" type="text" value="'+obj_author.login+'"><br>';
	var input_password = 'password: <input class="normal" id="" type="text" value="'+obj_author.password+'"><br>';
	var input_privilege = '<div class="privilege_block"><form name="form_item"><div class="privilege"><div class="element">&nbsp;</div><div class="element">Read</div><div class="element">Write</div><div class="element">Create/Del</div></div><br>';
	input_privilege += '<div class="privilege"><div class="element">Document</div><div class="element"><input type="checkbox" name="document_privilege" value="R"></div><div class="element"><input type="checkbox" name="document_privilege" value="W"></div><div class="element"><input type="checkbox" name="document_privilege" value="E"></div></div><br>';
	input_privilege += '<div class="privilege"><div class="element">Categories</div><div class="element"><input type="checkbox" name="category_privilege" value="R"></div><div class="element"><input type="checkbox" name="category_privilege" value="W"></div><div class="element"><input type="checkbox" name="category_privilege" value="E"></div></div><br>';
	input_privilege += '<div class="privilege"><div class="element">Indexterms</div><div class="element"><input type="checkbox" name="indexterm_privilege" value="R"></div><div class="element"><input type="checkbox" name="indexterm_privilege" value="W"></div><div class="element"><input type="checkbox" name="indexterm_privilege" value="E"></div></div><br>';
	input_privilege += '<div class="privilege"><div class="element">Authors</div><div class="element"><input type="checkbox" name="author_privilege" value="R"></div><div class="element"><input type="checkbox" name="author_privilege" value="W"></div><div class="element"><input type="checkbox" name="author_privilege" value="E"></div></div><br>';
	input_privilege += '<div class="spacer"></div></form></div><span style="padding-left:120px">&nbsp;</span>';
//	var input_privilege = 'author privilege: <input class="" id="" type="checkbox" value="YES" '; if (obj_author.privilege == 'YES'){input_privilege += 'checked="checked" ';} input_privilege +='>';
	var input = '<div class="author">'+input_title+input_firstname+input_lastname+input_login+input_password+input_privilege;

}else { // CASE: indexterm & category
	var input = '<div><input class="value" id='+$(the_obj).get(0).getAttribute('id')+' name="'+$(the_obj).get(0).getAttribute('name')+'" type="text" value="'+$(the_obj).get(0).getAttribute('name')+'" />';
}
	var revert = $(the_obj).get(0).getAttribute('name');
	var button = '<span class="Item_Button_Off" id="saveButton">Save</span>  <span class="Item_Button_Off" id="cancelButton">Cancel</span>';
	try{if(the_item=='category' && login_rights.category_privilege=='E'){button+='<span class="Item_Button_Off" id="deleteButton">Delete</span>';}} catch(e){} 
	try{if(the_item=='indexterm' && login_rights.indexterm_privilege=='E'){button+='<span class="Item_Button_Off" id="deleteButton">Delete</span>';}} catch(e){} 
	try{if(the_item=='author' && login_rights.author_privilege=='E'){button+='<span class="Item_Button_Off" id="deleteButton">Delete</span>';}} catch(e){} 
	button += '</div>';
	$(the_obj).after(input+button).remove();
	$('SPAN#saveButton').click(function(){saveChanges(this, false, the_item);}).mouseover(function(){$(this).removeClass("Item_Button_Off").addClass("Item_Button_On");}).mouseout(function(){$(this).removeClass("Item_Button_On").addClass("Item_Button_Off");});
	$('SPAN#cancelButton').click(function(){saveChanges(this, revert, the_item);}).mouseover(function(){$(this).removeClass("Item_Button_Off").addClass("Item_Button_On");}).mouseout(function(){$(this).removeClass("Item_Button_On").addClass("Item_Button_Off");});
	$('SPAN#deleteButton').click(function(){delete_item(id, the_item);}).mouseover(function(){$(this).removeClass("Item_Button_Off").addClass("Item_Button_On");}).mouseout(function(){$(this).removeClass("Item_Button_On").addClass("Item_Button_Off");});
	document.getElementById(id).focus();
	if (the_item=='author'){privilege_set(obj_author.privilege); try{if(login_rights.author_privilege!='E'){block_check();}} catch(e){}}
	
}).hover(function() {
	$(this).addClass("editable");
},function(){
	$(this).removeClass("editable");
});
}


function saveChanges(obj, cancel, the_item) {
	if(!cancel) {
		if (the_item=='author'){
			var id = obj.parentNode.childNodes[4].getAttribute('id');
			var t =id+'~~'+obj.parentNode.childNodes[1].value+'~~'+obj.parentNode.childNodes[4].value+'~~'+obj.parentNode.childNodes[7].value+'~~'+obj.parentNode.childNodes[10].value+'~~'+obj.parentNode.childNodes[13].value+'~~'+privilege_save();
		} else {
			var t = $(obj).siblings(0).val();
			var id = obj.parentNode.childNodes[0].getAttribute('id');
		}
		var http_item = new Asynchronous( ); // We create the HTTP Object
		http_item.call("GET", "item_modify.php?item="+the_item+"&id="+id+"&value="+t);
		http_item.complete = function(status, statusText, responseText, responseXML) {
		}
	}
	else {
		var t = cancel;
	}
	
	
	
	if (the_item=='author'){
		var id = obj.parentNode.childNodes[4].getAttribute('id');
		var t_author = new author(t);
		$(obj).parent().after('<div class="editInPlace" style="margin:4px 0px 4px 0px;" id="'+obj.parentNode.childNodes[4].getAttribute('id')+'" name="'+t+'">'+t_author.firstname+' '+t_author.lastname+'</div>').remove() ;
	} else {
		var id = obj.parentNode.childNodes[0].getAttribute('id');
		$(obj).parent().after('<div class="editInPlace" id="'+obj.parentNode.childNodes[0].getAttribute('id')+'" name="'+t+'">'+t+'</div>').remove() ;
	}
	
	setClickable(document.getElementById(id),the_item);
}
