/** Generated by js.php */
/**
*	Les fonctions "switchTo..." sont appelées lors d'un clic sur un onglet du portlet "Mes favoris"
*
* */


switchToPublic = function(dontLoadContent){
	deactiveSwitchLink();
	clearElements();
	if (!dontLoadContent) new Ajax.Request('/categories/searchUrlByCategory/0',{onComplete:function(xhr,json) {
		generateUrlList(xhr,json);
		activeSwitchLink();
	}});
	
	displayPublicLayout();
	$('mainContentClearResultsButton').hide();
	if(dontLoadContent) {
		activeSwitchLink();
	}
}

switchToPrivate = function(dontLoadContent){

	deactiveSwitchLink();
	if (!dontLoadContent) clearElements();
	
	displayPrivateLayout();
	$('mainContentClearResultsButton').hide();

	if (User.isLogged == true) {
		Bookmark.USER_ID = null;
		if (!dontLoadContent) openFolder(0);	
	}
	activeSwitchLink();
}

switchToGroups = function()
{
	deactiveSwitchLink();
	clearElements();
	
	displayGroupLayout();
	$('mainContentClearResultsButton').hide();
	
	if (User.isLogged == true) {
		Group.get(0);
	}
	activeSwitchLink();
}

switchToCommunauty = function(communauty_id, folder_id)
{
	deactiveSwitchLink();
	clearElements();
	
	displayCommunautyLayout(communauty_id, folder_id);
	$('mainContentClearResultsButton').hide();
	activeSwitchLink();
}

/**
 * Met à jour le libellé du champ de recherche.
 */
updateSearchLabel = function(newLabel) {
	$('mainContentCriteria').value = '';
	$('mainContentSubmitSearch').value = newLabel;
}

/**
*	Les fonctions display...Layout sont appelées dans les fonctions switch et affichent les bons éléments
*
* */

displayPublicLayout = function() {
	activateTab('Public');
	activateSubMenu('none');
	
	// Change the Search Form
	updateSearchLabel(_mlt('SEARCH_PUBLIC_BUTTON','Chercher dans la partie publique'));
	
	Event.clearObservers($('searchForm'));
	Event.observe('searchForm','submit',function(e) {Event.stop(e); searchUrls($('mainContentCriteria').value) });
}

displayPrivateLayout = function() {
	activateTab('private');
	activateSubMenu('private');
	$('switchToPrivateTab').className = 'selected_tab';
	
	Communauty.currentCommunauty = -1;
	Communauty.currentFolder = -1;
	
	updateSearchLabel(_mlt('SEARCH_PRIVATE_BUTTON','Chercher dans la partie privée'));
	
	Event.clearObservers($('searchForm'));
	Event.observe('searchForm','submit',function(e) {Event.stop(e); searchBookmarks($('mainContentCriteria').value) });
	Event.clearObservers($('mainContentClearResultsButton'));
	Event.observe('mainContentClearResultsButton','click',function(e) {Event.stop(e); openFolder(0); });
	
	// SubMenu
	
	$('privateSubMenuDiv').style.display = 'block';
}

displayCommunautyLayout = function(communauty_id, folder_id) {
	activateTab('communauty');
	Communauty.currentCommunauty = -1;
	Communauty.currentFolder = -1;
	Communauty.buildSubMenu();
	activateSubMenu('communauty');
	updateSearchLabel(_mlt('SEARCH_COOMUNAUTY_BUTTON', 'Chercher dans la Coomunauté'));
	
	if(communauty_id) {
		Communauty.openCommunauty(communauty_id, folder_id);
	} else {
		// Si on ne demande pas l'ouverture d'une communauté
		// directement, on se contente de générer le navigateur
		// avec la liste des communautés.
		Communauty.generateNavigator();
	}
	
	Event.clearObservers($('searchForm'));
	Event.observe('searchForm','submit',function(e) {Event.stop(e); Communauty.search($('mainContentCriteria').value)});
	Event.clearObservers($('mainContentClearResultsButton'));
	Event.observe('mainContentClearResultsButton','click',function(e) {
		Event.stop(e); 
		$('mainContentCriteria').value = '';
		Communauty.currentCommunauty = -1;
		Communauty.currentFolder = -1;
		Communauty.buildSubMenu();
		Communauty.generateNavigator();
	});
}

displayGroupLayout = function() {
	activateTab('group');
	activateSubMenu('group');
	
	updateSearchLabel(_mlt('SEARCH_GROUP_BUTTON','Chercher dans la partie partagée'));
	
	Event.clearObservers($('searchForm'));
	Event.observe('searchForm','submit',function(e) {Event.stop(e); searchBookmarksInGroup($('mainContentCriteria').value); });
	Event.clearObservers($('mainContentClearResultsButton'));
	Event.observe('mainContentClearResultsButton','click',function(e) {Event.stop(e); Group.get(0); });
}

deactiveSwitchLink = function() {
	Event.clearObservers($('switchToPrivateTab'));
	Event.clearObservers($('switchToGroupTab'));
	Event.clearObservers($('switchToCommunautyTab'));
	Event.clearObservers($('switchToPublicTab'));
}

activeSwitchLink = function() {
	Event.observe('switchToPublicTab','click',function() {switchToPublic(false);});
	Event.observe('switchToPrivateTab','click',function() {switchToPrivate(false);});
	Event.observe('switchToCommunautyTab','click',function() {switchToCommunauty(false);});
	Event.observe('switchToGroupTab','click',function() {switchToGroups(false);});
}

// Vide les �l�ments du layout
clearElements = function(){
	$('pathInfo').innerHTML = '';
	clearBookmarkContainer();
	clearNavigator();
}


clearNavigator = function() {
	$('navigator').innerHTML = '';
}

clearBookmarkContainer = function() {
	$('bookmarksContainer').innerHTML = '';
}

// fonction qui rend le bouton "Chercher ..."  actif si au moins 3 caractères sont entrés
activeSearchButton = function() {
	if ($('mainContentCriteria').value.length >= 3) {
		$('mainContentSubmitSearch').disabled= false;
	} else {
		$('mainContentSubmitSearch').disabled= true;
	}
}

activateSubMenu = function(subMenu) {
	subMenuId = subMenu + 'SubMenuDiv';
	Element.immediateDescendants($('subMenusDiv')).each(function(el) {
		if(el.getAttribute('id') != subMenuId) {
			el.hide();
		} else {
			el.show();
		}
	});
}

displayGroupsSubMenu = function(subSubMenu) {
	$('groupSubSubMenu').show();
	subSubMenuId = subSubMenu+'GroupSubMenuList';
	Element.immediateDescendants($('groupSubSubMenu')).each(function(el) {
		if(el.getAttribute('id') != subSubMenuId) {
			el.hide();
		} else {
			el.show();
		}
	});
}

hideGroupsSubMenu = function() {
	$('groupSubSubMenu').hide();
}

activateGroupsSubMenuLink = function(indice) {
	Element.immediateDescendants($('groupSubMenuList')).each(function(el,i) {
		if (i <= indice) {
			el.show();
		} else {
			el.hide();
		}
	})
}

/**
 * Active l'onglet dont le nom ('Private', 'Communauty', etc.) est
 * passé en paramètre. Les autres onglets sont automatiquement désactivés.
 */
activateTab = function(tab) {
	tabId = 'switchTo' + tab.capitalize() + 'Tab'; 
	$$('#switchMenu span a').each(function(el) {
		if(el.getAttribute('id') == tabId) {
			el.addClassName('selected_tab');
			el.removeClassName('unselected_tab');
		} else {
			el.removeClassName('selected_tab');
			el.addClassName('unselected_tab');
		}
	});
}

openFolder = function(folderId) {
	
	if (folderId == 0) {
		$('mainContentClearResultsButton').hide();
		$('mainContentCriteria').value = '';
		activeSearchButton();
	}
	if ((Bookmark.USER_ID) && (Bookmark.USER_ID != '')){
		hideGroupsSubMenu();
		activateGroupsSubMenuLink(Group.BOOKMARKS_MENU_POSITION);
		Event.clearObservers($('groupSubMenuBookmarksMenu'));
		Event.observe('groupSubMenuBookmarksMenu','click',function(e) {Event.stop(e); openFolder(0); });
	} else {
		$('privateSubMenuDiv').setStyle({display:'block'});
	}
	getFolders(folderId);
	getBookmarks(folderId);
	
}

