// JavaScript Document
var DOC_PREFIX_1 = 'compose_document.php?';
var DOC_PREFIX_2 = 'edit_document.php?';

function compose_document(action, id_doc) {

	var http_doc = new Asynchronous( ); // We create the HTTP Object
	if (action == 'edit' || action == 'new') {DOC_PREFIX = DOC_PREFIX_2; close_doc_FCKissue();} else if (action == 'view') {DOC_PREFIX = DOC_PREFIX_1;}
	http_doc.call('GET', DOC_PREFIX+'id_doc='+id_doc+'&anyName=' + new Date().getTime());
	http_doc.complete = function(status, statusText, responseText, responseXML) {
		if (responseText == "error") { //When we open a document that is private while not logged-in
			doc_selected = '';
			list_all_documents();
		} else {
			if ($(".other_pages").get(0) != null){
				// when accessing categories/indexterms/users from home
				$(".other_pages").empty().append(responseText);
			} else {
				// when accessing categories/indexterms/users not from home
				document.getElementById('centrecontent').innerHTML = responseText;		
			}

			$("#header").ScrollTo(1000);
			doc_selected = id_doc;
			
			if (action == 'edit' || action == 'new'){
				$("a.thickbox").click(function(){
  				var t = this.title || this.innerHTML || this.href;
  				TB_show(t,this.href);
  				this.blur();
 				 return false;
				});
			}

			if ($(".other_pages").get(0) != null){
				// when accessing categories/indexterms/users from home
				var save_button  = "<span id='save' class='bouton_off'>save</span>&nbsp;|&nbsp;";
				var close_button = "<span id='close' class='bouton_off'>close</span>";
				
				if ($(".nav-buttons").find("#save").get(0) != null) {
				
					$("#document_menu > .nav-buttons").empty().append(save_button);
					$("#save").click(function() {
						document.forms['form_document'].submit();
					}).mouseover(function() {
						$(this).css("cursor","pointer");
						$(this).removeClass("bouton_off");
						$(this).addClass("bouton_on");
					}).mouseout(function(){
						$(this).removeClass("bouton_on");
						$(this).addClass("bouton_off");
					})
					$("#document_menu > .nav-buttons").append(close_button);
					$("#close").click(function() {
						compose_page('home.php','centrecontent');return;
					}).mouseover(function() {
						$(this).css("cursor","pointer");
						$(this).removeClass("bouton_off");
						$(this).addClass("bouton_on");
					}).mouseout(function(){
						$(this).removeClass("bouton_on");
						$(this).addClass("bouton_off");
					})
				}
				
				
			} else {
		
		 		var doc_key = 0;
 				while (doc_key < doc_list.length) { //returns the key of the array for the opened document
 					 if (doc_list[doc_key] == id_doc) break;
 	 				doc_key++;
 				};
			
			
				if (doc_key==0){
					$(".nav-buttons").find("#previous").hide();
				} else {
					$(".nav-buttons").find("#previous").click(function() {
						compose_document(action, doc_list[doc_key-1]);
					}).mouseover(function() {
						$(this).css("cursor","pointer");
					})
				}		
				if (doc_key == doc_list.length-1) {
					$(".nav-buttons").find("#next").hide();
				} else {
					$(".nav-buttons").find("#next").click(function() {
						compose_document(action, doc_list[doc_key+1]);
					}).mouseover(function() {
						$(this).css("cursor","pointer");
					})
				}

			
				$(".nav-buttons").find("#close").click(function() {
					if(search_selected !='') {
						search_action(search_selected, 'left'); // Search with the left menu already displayed (in opposition to search from the home page)
					} else {
						list_all_documents();
					}
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				}).end()
				.find("#edit").click(function() {
					compose_document('edit', id_doc);
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				}).end()
				.find("#delete").click(function() {
					delete_doc(id_doc);
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				}).end()
				.find("#save").click(function() {
					document.forms['form_document'].submit();
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				}).end()
				.find("#cancel").click(function() {
					if (id_doc != 0) {
						compose_document('view', id_doc);
					} else {
						if(search_selected != '') {
							search_action(search_selected, 'left'); // Search with the left menu already displayed (in opposition to search from the home page)
						} else {
							list_all_documents();
						}
					}
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				}).end()
				.find("#print").click(function() {
					print();
				}).mouseover(function() {
					$(this).css("cursor","pointer");
					$(this).removeClass("bouton_off");
					$(this).addClass("bouton_on");
				}).mouseout(function(){
					$(this).removeClass("bouton_on");
					$(this).addClass("bouton_off");
				})
				;
			
			} // End of "if ($(".other_pages").get(0) != null)"
				doc_selected = id_doc;
//					if (document.getElementById('status_pending')){situation.status='pending';} else {situation.status='public';}	COMMENTED ON JAN 15
			}
	} // end call
} // end function
