var category_selected = new Array();		// collect category or list of categories that have been selected by user
var indexterm_selected = new Array();		// collects indexterm or list of indexterms that have been selected by the user
var doc_selected = '';						// reference the document id that has been selected, opened
var status_selected = '';					// reference the status of browsing (pending OR public) documents
var search_selected = '';					// to keep track of the search criteria
var browsing = '';							// detects the main browsing: Categories OR Indexterms - possible values are: 'categories' OR 'indexterms'				
var doc_list = new Array();					// list the documents retrieved by the browse, so we can do the previous-next feature

// JavaScript Document
var ADMIN_MENU_PREFIX = 'admin_menu.php';
var LEFT_MENU_PREFIX = 'left_menu.php';
var LIST_PREFIX = 'compose_list.php?';
var SUBMENU_PREFIX = 'submenu.php?';
var SHOWOPTIONS_PREFIX = 'advanced_search.php?';

var cat_original_slider_height = 0;				
var cat_original_spaceToScroll = 0;
var index_original_slider_height = 0;				
var index_original_spaceToScroll = 0;


function admin_menu() {
// load the admin menu on the top
	if (loggedIn) {
	// LOGIN
		var http_menu = new Asynchronous( ); // We create the HTTP Object
		http_menu.call('GET', ADMIN_MENU_PREFIX);
		http_menu.complete = function(status, statusText, responseText, responseXML) {
			$('#header').prepend(responseText);
		}
	} else {
		// LOGOUT
		$('#account-bar').remove();
	}
}

function left_menu() {
	var http_left = new Asynchronous( ); // We create the HTTP Object
	http_left.call('GET', LEFT_MENU_PREFIX);
	http_left.complete = function(status, statusText, responseText, responseXML) {
		if (document.getElementById('left') != null){
			document.getElementById('left').innerHTML = '';
			$('div #left').append(responseText);
			// Make slider visible and active for the left menu
			go_slider_left('categories');go_slider_left('indexterms');
			// Per default: only categories list is visible
			$(".show_hide_i").hide();
			// Make the search button active
			$("#submitnormalsearch").click(function() {
				if (document.getElementById('keywords').value != ''){status_selected=''; search_action('search_keyword='+document.getElementById('keywords').value, 'left');}
			});
			document.getElementById('keywords').focus();
			$(".search_options").click(function() {			alert("line 106");
				if ($(".searchoptions_display").get(0) != null) {return false;}
				if ($("#document").get(0) != null) {return false;}
				var http_searchoptions = new Asynchronous( );
				http_searchoptions.call('GET', SHOWOPTIONS_PREFIX+'anyName=' + new Date().getTime());
				http_searchoptions.complete = function(status, statusText, responseText, responseXML) {
					$("#centrecontent").prepend(responseText);
					$(".searchoptions_display").slideDown("slow");
					$("#searchoptions_close").click(function() {
						$(".searchoptions_display").slideUp("slow", function() {$(this).remove()});
					})
					.mouseover(function() {$(this).toggleClass("searchmouse")})
					.mouseout(function() {$(this).toggleClass("searchmouse")});
					$("#searchoptions_top > IMG").click(function() {
						$(".searchoptions_display").slideUp("slow", function() {$(this).remove()});
					})
					
					$("#submitadvancedsearch").click(function() {
						if (document.forms['advancedsearch'].elements[0].value != '' || document.forms['advancedsearch'].elements[1].value != '' || document.forms['advancedsearch'].elements[2].value != '' || document.forms['advancedsearch'].elements[3].value != '' || document.forms['advancedsearch'].elements[4].value != '' || document.forms['advancedsearch'].elements[5].value != '' ){
							post = "name_value="+document.forms['advancedsearch'].elements['field_name'].value;
							post += "&web_value="+document.forms['advancedsearch'].elements['field_web'].value;
							post += "&category_value="+document.forms['advancedsearch'].elements['field_category'].value;
							post += "&indexterm_value="+document.forms['advancedsearch'].elements['field_indexterm'].value;
							post += "&precis_value="+document.forms['advancedsearch'].elements['field_precis'].value;
							post += "&description_value="+document.forms['advancedsearch'].elements['field_description'].value;
							search_action(post, 'left');
						}				
					});	

					
				}
			})
			.mouseover(function() {$(this).toggleClass("searchmouse")})
			.mouseout(function() {$(this).toggleClass("searchmouse")});
				
		}
	 
	$("#cat_plus_minus").mouseover(function() {
				$(this).css("cursor","pointer");
			}).click(function() {
				$(".show_hide_c").toggle();
				$(".show_hide_i").toggle();
					if (cat_original_slider_height == 0){record_slider_original("categories");}
				$(this).toggleClass("menu_plus").toggleClass("menu_minus");
				$("#indexterm_plus_minus").toggleClass("menu_plus").toggleClass("menu_minus");
			})
	$("#indexterm_plus_minus").mouseover(function() {
				$(this).css("cursor","pointer");
			}).click(function() {
				$(".show_hide_i").toggle();
				$(".show_hide_c").toggle();
					if (index_original_slider_height == 0){record_slider_original("indexterms");}				
				$(this).toggleClass("menu_minus").toggleClass("menu_plus");
				$("#cat_plus_minus").toggleClass("menu_minus").toggleClass("menu_plus");
			})
			
	if (category_selected.length!=0 && browsing =='categories'){
			$(".show_hide_i").hide();
			
			//If we relaunch the left menu after login/logoff, and that the previously selected category is no longer listed, we clear the content of the central panel
			if ($("#category"+category_selected[0]).get(0) == null){document.getElementById('centrecontent').innerHTML = "";}
			
			$("#category"+category_selected[0]).each(function(i){
				$(this).toggleClass("category_selected");
				var http_submenu = new Asynchronous( ); // We create the HTTP Object
				http_submenu.call('GET', SUBMENU_PREFIX+'category='+category_selected[0]);
					var toinsert = $("#category"+category_selected[0]).parent();
					http_submenu.complete = function(status, statusText, responseText, responseXML) {
						toinsert.after(responseText);
						if (cat_original_slider_height == 0){record_slider_original("categories");}
						// Fetch data that only match the category selected
						if (doc_selected == null || doc_selected == '' || doc_selected == 0) {	// we display the list of document sonly if there is no document selected (this is used after login/logout)
							list_all_documents ();											
						}
						set_behaviour_submenu_elements('indexterms');
						$(".submenu").slideDown("slow", function(){adaptSlider('categories');slideto();});
						// If we relaunch the left panel after login/logoff, we need to set selected the indexterms in the submenu.
						for(i=0; i<indexterm_selected.length; i++){
							$("#subindexterm"+indexterm_selected[i]).addClass("submenu_selected");
						}
					}
			})

	} else if (indexterm_selected.length!=0 && browsing == 'indexterms'){
			$(".show_hide_i").show(); 
			$(".show_hide_c").hide();
			$("#cat_plus_minus").toggleClass("menu_minus").toggleClass("menu_plus");
			$("#indexterm_plus_minus").toggleClass("menu_plus").toggleClass("menu_minus");
			
			//If we relaunch the left menu after login/logoff, and that the previously selected category is no longer listed, we clear the content of the central panel
			if ($("#indexterm"+indexterm_selected[0]).get(0) == null){document.getElementById('centrecontent').innerHTML = "";}
			

			$("#indexterm"+indexterm_selected[0]).each(function(i){
				$(this).toggleClass("indexterm_selected");
				var http_submenu = new Asynchronous( ); // We create the HTTP Object
				http_submenu.call('GET', SUBMENU_PREFIX+'indexterm='+indexterm_selected[0]);
					var toinsert = $("#indexterm"+indexterm_selected[0]).parent();
					http_submenu.complete = function(status, statusText, responseText, responseXML) {
						toinsert.after(responseText);
						if (index_original_slider_height == 0){record_slider_original("indexterms");}
						// Fetch data that only match the category selected
						if (doc_selected == null || doc_selected == '' || doc_selected == 0) {	// we display the list of document sonly if there is no document selected (this is used after login/logout)
							list_all_documents ();											
						}
						set_behaviour_submenu_elements('categories');
						$(".submenu").slideDown("slow", function(){adaptSlider('indexterms');slideto();});
						// If we relaunch the left panel after login/logoff, we need to set selected the indexterms in the submenu.
						for(i=0; i<category_selected.length; i++){
							$("#subcategory"+category_selected[i]).addClass("submenu_selected");
						}
					}
			})
	}
	
	// set the behaviour of the categories and indexterms displayed on the left menu
		// CATEGORIES
		$("div.left_cloud_content_cat").find("span.sub_item").click(function() {
		alert(341);
			// Clear the potentially selected public/pending squares on the left panel
			$(".pending_selected").removeClass("pending_selected");
			$(".public_selected").removeClass("public_selected");
			// Clear the status_selected (public or pending or -empty-)
			status_selected = '';
			browsing = 'categories';
			
			var this_item = $(this);
			if (this_item.attr("class").indexOf("category_selected") != -1) {
				// CASE: the category is already selected and displays sub menu
				if ($(".submenu").css("display") == "none"){
					$(".submenu").slideDown("slow", function(){adaptSlider('categories');});
				} else {
					FixBuggWhenScrolling('categories');	// Traitement BUG - category - 
					$(".submenu").slideUp("slow", function(){adaptSlider('categories');});
//					$("div.left_cloud_content_cat").animate({top:futurTop},"slow");
				}	
				
			} else {
				// CASE: the category selected is a new one
				var id = this_item.attr("id");
				id = id.slice(8); // 8 is the length of "category"
				category_selected[0] = id;
				indexterm_selected.length = 0;
				list_all_documents ();
				
				if ($("div.left_cloud_content_index").find(".submenu").get(0) != null){
					// There is an INDEXTERM SELECTED already
//					alert("there is a category selected already \r\n Let's get rid of it properly !");
					$("div.left_cloud_content_index").find("span.indexterm_selected").removeClass("indexterm_selected");
					if ($(".submenu").css("display") == "none") {
						$(".submenu").remove();				
					} else {
						$("div.left_cloud_content_index").css("top",0+"px");
						$("div.left_cloud_content_index").find(".submenu").remove();
						original_slider('indexterms');
					}
					
					
				} else if ($("div.left_cloud_content_cat").find(".submenu").get(0) != null) {
					// There is a CATEGORY SELECTED
//					alert("There is an indexterm selected already \r\n Let's get rid of it");
					$(".left_cloud_content_cat").find(".category_selected").removeClass("category_selected");
					if ($(".submenu").css("display") == "none"){
						$(".submenu").remove();
					} else {
						// Traitement BUG - category - 
						FixBuggWhenScrolling("categories");
						var todelete_temp = $(".submenu");
						todelete_temp.slideUp("slow",function(){ todelete_temp.remove();/*adaptSlider('categories');*/});
					}
				
				} else {
					// There is NO CATEGORY SELECTED (i.e. after a search)
					
				}


				this_item.toggleClass("category_selected");

				var http_submenu = new Asynchronous( ); // We create the HTTP Object
				http_submenu.call('GET', SUBMENU_PREFIX+'category='+id);
					var toinsert = this_item.parent();
					http_submenu.complete = function(status, statusText, responseText, responseXML) {
						toinsert.after(responseText);
						set_behaviour_submenu_elements('indexterms');						
						$(".submenu").slideDown("slow", function(){adaptSlider('categories');});
					}
			}



		}) // end click behaviour for categories
		.hover(function(){
  			 $(this).addClass("category_hover");
		},function(){
   			$(this).removeClass("category_hover");
		});
		
		
		
		// INDEXTERMS
		$("div.left_cloud_content_index").find("span.sub_item").click(function() {
		
			// Clear the potentially selected public/pending squares on the left panel
			$(".pending_selected").removeClass("pending_selected");
			$(".public_selected").removeClass("public_selected");
			// Clear the status_selected (public or pending or -empty-)
			status_selected = '';
			browsing = 'indexterms';
			
			var this_item = $(this);
			if (this_item.attr("class").indexOf("indexterm_selected") != -1) {
				// CASE: this item is already selected and displays sub menu
				if ($(".submenu").css("display") == "none"){
					$(".submenu").slideDown("slow", function(){adaptSlider('indexterms');});
				} else {
					FixBuggWhenScrolling("indexterms");
					$(".submenu").slideUp("slow", function(){adaptSlider('indexterms');});
				}	
			} else {
				// CASE: the item is not yet selected
				var id = this_item.attr("id");
				id = id.slice(9); // 9 is the length of "indexterm"
				indexterm_selected[0] = id;
				category_selected.length = 0;
				
				list_all_documents ();
				
				if ($("div.left_cloud_content_cat").find(".submenu").get(0) != null){
					// There is a CATEGORY SELECTED already
//					alert("there is a category selected already \r\n Let's get rid of it properly !");
					$("div.left_cloud_content_cat").find("span.category_selected").removeClass("category_selected");
					if ($(".submenu").css("display") == "none") {
						$(".submenu").remove();				
					} else {
						$("div.left_cloud_content_cat").css("top",0+"px");
						$("div.left_cloud_content_cat").find(".submenu").remove();
						original_slider('categories');
					}
					
					
					
					
				} else if ($("div.left_cloud_content_index").find(".submenu").get(0) != null) {
					// There is an INDEXTERM SELECTED
//					alert("There is an indexterm selected already \r\n Let's get rid of it");
					$(".left_cloud_content_index").find(".indexterm_selected").removeClass("indexterm_selected");
					if ($(".submenu").css("display") == "none"){
						$(".submenu").remove();
					} else {
						// Traitement BUG - category - 
						FixBuggWhenScrolling("indexterms");
						var todelete_temp = $(".submenu");
						todelete_temp.slideUp("slow",function(){ todelete_temp.remove();/*adaptSlider('indexterms');*/});
					}
				
				} else {
					// There is NO ITEM SELECTED (i.e. after a search)
					
				}
					

				this_item.toggleClass("indexterm_selected");

				var http_submenu = new Asynchronous( ); // We create the HTTP Object
				http_submenu.call('GET', SUBMENU_PREFIX+'indexterm='+id);
					var toinsert = this_item.parent();
					http_submenu.complete = function(status, statusText, responseText, responseXML) {
						toinsert.after(responseText);
						set_behaviour_submenu_elements('categories');
						$(".submenu").slideDown("slow", function(){adaptSlider('indexterms');});
					}
			
				

			}
		})// end click behaviour for indexterms
		.hover(function(){
  			 $(this).addClass("indexterm_hover");
		},function(){
   			$(this).removeClass("indexterm_hover");
		});
		
		
		// SET the behaviour of the pending/public count when an administrator is logged-in
				// PUBLIC
		$(".public_count").click(function(){
			var this_element = $(this);
			status_selected = 'public';
			$(".public_selected").removeClass("public_selected");
			category_selected.length=0;
			indexterm_selected.length = 0;
			if (this_element.attr("id").indexOf("catpublic") != -1) { // This is in the category panel
				cat_id = this_element.attr("id").slice(9);
				category_selected[0]= cat_id;
				browsing = 'categories';
			} else if (this_element.attr("id").indexOf("indexpublic") != -1) {
				index_id = this_element.attr("id").slice(11);
				indexterm_selected[0] = index_id;
				browsing = 'indexterms';
			}
			clear_left_panel();
			this_element.toggleClass("public_selected");
			list_all_documents();
		}).hover(function(){
  			 $(this).addClass("public_hover");
		},function(){
   			$(this).removeClass("public_hover");
		});
		
						// PENDING
		$(".pending_count").click(function(){
			var this_element = $(this);
			status_selected = 'pending';
			category_selected.length=0;
			indexterm_selected.length = 0;
			if (this_element.attr("id").indexOf("catpending") != -1) { // This is in the category panel
				cat_id = this_element.attr("id").slice(10);
				category_selected[0]= cat_id;
				browsing = 'categories';
			} else if (this_element.attr("id").indexOf("indexpending") != -1) {
				index_id = this_element.attr("id").slice(12);
				indexterm_selected[0] = index_id;
				browsing = 'indexterms';
			}
			clear_left_panel();
			this_element.toggleClass("pending_selected");
			list_all_documents();
		}).hover(function(){
  			 $(this).addClass("pending_hover");
		},function(){
   			$(this).removeClass("pending_hover");
		});

		
	} // end of asynchronous callback
	
			// Clear the status_selected (public or pending or -empty-)
			status_selected = '';

}

/**
		LAUNCH (cat, it)
		Called from the home page when selecting either a CAT or an IT in the list
		date: 9 march 2006
		author: Francois Chemillier
**/
function launch(cat,it) {

	category_selected.length = 0;	// initiate list of categories
	indexterm_selected.length = 0;	// initiate list of indexterms
	if (cat!='') {category_selected[0]=cat; browsing = 'categories';}
	else if (it!='') {indexterm_selected[0]=it; browsing = 'indexterms';}

	document.getElementById('centrecontent').innerHTML = '';
	$("BODY").attr("id","list");
	
	$("#outer2").attr("id", "outer");
	$("#clearheader").after("<div id='left'></div>");
						
//	compose_listtop(cat, it, status);

	left_menu();
	// Let's reset the right menu (Home - About- Contact)
	$(".li_selected").removeClass("li_selected");
	
}


function set_behaviour_submenu_elements(which) {
	switch (which){
	case 'indexterms':

		$(".submenuItem").click(function(){	// FUNCTION that set the behaviour of elements from the submenu
			$(this).toggleClass("submenu_selected");
			var http_submenuAction = new Asynchronous( );
			indexterm_selected.length = 0;
			$(".submenu_selected").each(function(i){
				var newId = $(this).attr("id");
				newId = newId.slice(12); //alert('newId='+newId); 	//12 is the length of 'subindexterm'
				indexterm_selected[i] = newId;
			});
			list_all_documents()
		})
	
	break;
	case 'categories':

		$(".submenuItem").click(function(){	// FUNCTION that set the behaviour of elements from the submenu
			var element = $(this);
			$(".submenu_selected").not(element).removeClass("submenu_selected");
			element.toggleClass("submenu_selected");
			var http_submenuAction = new Asynchronous( );
			category_selected.length = 0;
			$(".submenu_selected").each(function(i){
				var newId = element.attr("id");
				newId = newId.slice(11); //alert('newId='+newId); 	//12 is the length of 'subcategory'
				category_selected[i] = newId;
			});
			list_all_documents()
		})

	break;
	}

}

/*
	date: 20 March 2007
	author: Francois Chemillier
	objectives: list all documents after a document is closed. Relate to the indexterms and categories selected

*/
function list_all_documents() {//alert(934);
		// if we list docs, that means no individual doc is opened (view/edit), so:
		doc_selected = '';
		//var http_submenuAction = new Asynchronous( );
		//http_submenuAction.call('GET', LIST_PREFIX+'indexterms='+indexterm_selected.toString(",")+'&categories='+category_selected.toString(",")+'&status='+status_selected);				//chris the above call is removed
		//http_submenuAction.complete = function(status, statusText, responseText, responseXML) {
			//document.getElementById('centrecontent').innerHTML = responseText;
			list_in_array();
			$("#header").ScrollTo(1000);
			search_selected = '';
		//}

}

function list_in_array() {
		doc_list.length = 0;
		$(".list").each(function(i){
			doc_list[i] = $(this).attr('id');
		}).mouseover(function(){
			$(this).find(".title").addClass("title_hover");
		}).mouseout(function(){
			$(this).find(".title").removeClass("title_hover");
		})//chris removed .click(function() {
			//compose_document('view',$(this).attr('id'));
		//})
}

function clear_page() {
		 if($("#left").get(0) != null){
			document.getElementById('centrecontent').innerHTML = '';
	 		$("#left").remove();
			$("#outer").attr("id", "outer2");
			category_selected.length = 0;
			indexterm_selected.length = 0;
			search_selected = '';
			status_selected = '';
			doc_selected = '';
		 }
}

function clear_left_panel() {
		$(".left_cloud_content_cat").find(".category_selected").removeClass("category_selected");
		$(".left_cloud_content_index").find(".indexterm_selected").removeClass("indexterm_selected");
		$(".submenu").each(function(i){
			var todelete = $(this);
			if (todelete.css("display") == "none") {
				todelete.remove();
			} else {
				FixBuggWhenScrolling(browsing);
				todelete.slideUp("slow", function(){
					todelete.remove();
					adaptSlider(browsing);
				});
			}
		})
		$(".pending_selected").removeClass("pending_selected");
		$(".public_selected").removeClass("public_selected");
}
