 

////////////////////////////////////////////////////////////////////
//// Site Tree javascript representation ///////
////////////////////////////////////////////////////////////////////
// Example of use :
// Retrieve siteRoot Section : 
//	var siteRoot = sectionById[siteRootId];
// Retrieve first level Sections
//	var firstLevel = childrenById[siteRootId];
var siteRootId = 93;
var sectionById = new Object();
var childrenById = new Object();

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Section is a javascript Object that represent a Smart Section
//// Interface description :
////	id : integer, primary key value of this Section
////	parentId : id of this Section parent Section
////	screenorder : order of this Section among its brother Sections
////	name : name to be displayed to the user.
////	url : url of the page that represent this Section
////	getChildren() : return the children (as an Array) of this Section. May return null.
////	getParent() : return the parent Section of this Section
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//var roles = new Array();
function Section(id, parentId, screenorder, name, aUrl, allowedRights) {
	this.id 	 = id;
	this.parentId	 = parentId;
	this.screenorder = screenorder;
	this.name 	 = name;
	this.url 	 = aUrl;
	this.allowedRights = allowedRights;

//	if(this.id == siteRootId || this.isAllowedFor(roles)) {
		// this Section is allowed, so register it
		sectionById[this.id] = this;
		var brothers = childrenById[this.parentId];
		if(brothers == null) {
			brothers = new Array();
			childrenById[this.parentId] = brothers;
		}
		brothers.push(this);
//	}
}

Section.EMPTY_ARRAY = new Array(0); 

Section.compare = function(section1, section2) {
	return section1.screenorder - section2.screenorder ;
}

Section.prototype.getChildren = function() {
	var children = childrenById[this.id];
	if(children == null) {
		children = childrenById[this.id] = Section.EMPTY_ARRAY ;
	}

	if(!this.childrenHasBeenOrdered) {
		children.sort(Section.compare);
	}

	return children;
}

Section.prototype.getParent = function() {
	return sectionById[this.parentId];
}

Section.prototype.getLevel = function() {
	var parent = this.getParent();
	if(parent != null) {
		return parent.getLevel()+1;
	} else {
		return 0;
	}
}

Section.prototype.isAllowedFor = function(roleArray) {
	var result = false;
	var i = 0;
	while(!result && i<roleArray.length) {
		var cRole = roleArray[i++];
		result = "ADMIN" == cRole ||  this.allowedRights.indexOf('|'+cRole+'|') != -1;
	}

	return result;
}

////////////////////////////////////////////////////////////////////////////////////////
//// Represents a process on the Site Tree
////////////////////////////////////////////////////////////////////////////////////////
function SectionVisitor() {
}

SectionVisitor.prototype.visit = function(aSection) {
	var shouldProcessChildren = this.process(aSection);
	if(shouldProcessChildren) {
		var children = aSection.getChildren();
		if(children != null) {
			for(var i=0; i<children.length; ++i) {
				this.visit(children[i]);
			}
		}
	}
}

SectionVisitor.prototype.process = function(aSection) {
	alert("SectionVisitor.process() on "+aSection.id+" - "+aSection.name+". Should be overriden to perform custom processing.");
	return false;   // Return if process should be called on children Sections
}

new Section(93, null, 0, "Evialis", "/fre/", "|");


   new Section(1260, 93, 2, "Présentation", "/fre/presentation", "|");
  
  
      new Section(1264, 1260, 1, "Expertise et savoir faire", "/fre/presentation/expertise-et-savoir-faire", "|");
  
      new Section(1265, 1260, 2, "Chiffres clés", "/fre/presentation/chiffres-cles", "|");
  
      new Section(1266, 1260, 3, "Historique", "/fre/presentation/historique", "|");
  
      new Section(1289, 1260, 3, "Les achats", "/fre/presentation/les-achats", "|");
  
      new Section(1290, 1260, 3, "La formulation", "/fre/presentation/la-formulation", "|");
  
      new Section(1291, 1260, 3, "L’industriel", "/fre/presentation/l-industriel", "|");
  
      new Section(1292, 1260, 3, "Evialis Institute", "/fre/presentation/evialis-institute", "|");
  
      new Section(1267, 1260, 4, "Politique Qualité", "/fre/presentation/politique-qualite", "|");
  
      new Section(1390, 1260, 5, "Evialis et l'environnement", "/fre/presentation/evialis-environnement", "|");
  
      new Section(1396, 1260, 6, "L'humain au coeur de l'entreprise", "/fre/presentation/humain-au-coeur", "|");
  

   new Section(1262, 93, 3, "R & D", "/fre/r-d", "|");
  
  
      new Section(1293, 1262, 3, "Un outil au service des clients", "/fre/r-d/un-outil-au-service-des-clients", "|");
  
      new Section(1274, 1262, 4, "LAREAL", "/fre/r-d/lareal", "|");
  

   new Section(1261, 93, 4, "Implantation", "/fre/implantation-et-organisation", "|");
  
  
      new Section(1269, 1261, 1, "Commerciale et industrielle", "/fre/implantation-et-organisation/commerciale-industrielle", "|");
  
      new Section(1270, 1261, 3, "Réseau de distribution", "/fre/implantation-et-organisation/reseau-de-distribution", "|");
  
      new Section(1386, 1261, 4, "Lien avec sites distributeurs", "/fre/implantation-et-organisation/lien-avec-sites-distributeurs", "|");
  

   new Section(1275, 93, 5, "Produits ", "/fre/produits", "|");
  
  
      new Section(1278, 1275, 1, "Vache laitière", "/fre/produits/vache-laitiere", "|");
  
      new Section(1280, 1275, 2, "Caprin", "/fre/produits/caprin", "|");
  
      new Section(1279, 1275, 2, "Bovin viande", "/fre/produits/bovin-viande", "|");
  
      new Section(1282, 1275, 3, "Ovin viande", "/fre/produits/ovin-viande", "|");
  
      new Section(1281, 1275, 4, "Ovin lait", "/fre/produits/ovin-lait", "|");
  
      new Section(1398, 1275, 5, "Spécialités ruminants", "/fre/produits/evialis-specialites-ruminants", "|");
  
      new Section(1283, 1275, 7, "Volaille", "/fre/produits/volaille", "|");
  
      new Section(1284, 1275, 7, "Lapin", "/fre/produits/lapin", "|");
  
      new Section(1287, 1275, 8, "Basse cour", "/fre/produits/belle-moisson", "|");
  
      new Section(1285, 1275, 8, "Porc", "/fre/produits/porc", "|");
  
      new Section(1286, 1275, 9, "Pet food", "/fre/produits/chien", "|");
  
      new Section(1288, 1275, 11, "Cheval et Gibier", "/fre/produits/autres-activites", "|");
  

   new Section(1263, 93, 6, "Contact", "/fre/contact", "|");
  
  



// If only one subsection, then do not show the subsection
function postInit(aSection) {
	var children = aSection.getChildren();
	if(children != null && children.length == 1) {
		var onlyChildSection = children[0];
		aSection.url = onlyChildSection.url;
		if(aSection.getLevel() > 0) {
			childrenById[aSection.id] = childrenById[onlyChildSection.id];
		}
	}
	/*if(children != null && children.length > 1) {
		var firstSection = children[0];
		aSection.url = firstSection.url;
	}*/
	return true;
}
var postInitVisitor = new SectionVisitor();
postInitVisitor.process = postInit;
postInitVisitor.visit(sectionById[siteRootId]);