 

////////////////////////////////////////////////////////////////////
//// Site Tree javascript representation ///////
////////////////////////////////////////////////////////////////////
// Example of use :
// Retrieve siteRoot Section : 
//	var siteRoot = sectionById[siteRootId];
// Retrieve first level Sections
//	var firstLevel = childrenById[siteRootId];
var siteRootId = 1260;
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(1260, null, 0, "Ocialis", "/por/", "|");


   new Section(1290, 1260, 2, "Ocialis", "/por/ocialis-groupe", "|");
  
  
      new Section(1291, 1290, 1, "Home page", "/por/ocialis-groupe/accueil", "|");
  
      new Section(1292, 1290, 2, "Apresentação", "/por/ocialis-groupe/presentation", "|");
  
      new Section(1293, 1290, 3, "Operações industriais", "/por/ocialis-groupe/implantation", "|");
  
      new Section(1294, 1290, 4, "Equipe", "/por/ocialis-groupe/hommes", "|");
  

   new Section(1295, 1260, 3, "Valores", "/por/valeurs", "|");
  
  
      new Section(1296, 1295, 1, "Trabalhando juntos", "/por/valeurs/travailler-ensemble", "|");
  
      new Section(1297, 1295, 2, "Resultados consistentes", "/por/valeurs/resultats-mesurable", "|");
  
      new Section(1298, 1295, 3, "Desenvolvimento sustentável", "/por/valeurs/dev-durable", "|");
  
      new Section(1299, 1295, 4, "Orgulho de nossos produtos", "/por/valeurs/amour-du-bon-produit", "|");
  

   new Section(1300, 1260, 4, "Pesquisa e desenvolvimento", "/por/recherche-developpement", "|");
  
  
      new Section(1301, 1300, 1, "Estratégia voltada á pesquisa", "/por/recherche-developpement/depuis-60ans", "|");
  
      new Section(1302, 1300, 2, "Ganha-Ganha", "/por/recherche-developpement/biomoteur", "|");
  
      new Section(1303, 1300, 3, "Lareal", "/por/recherche-developpement/lareal", "|");
  
      new Section(1304, 1300, 4, "Locais de P&D", "/por/recherche-developpement/r-d-internationnal", "|");
  

   new Section(1305, 1260, 5, "Produtos", "/por/offre", "|");
  
  
      new Section(1306, 1305, -5, "Ração", "/por/offre/aliment", "|");
  
      new Section(1307, 1305, -5, "Premix e serviços", "/por/offre/additifs", "|");
  
      new Section(1308, 1305, -5, "Produtos para nutrição de larvas", "/por/offre/larvaires", "|");
  
      new Section(1309, 1305, -4, "Controle e rastreabilidade", "/por/offre/controle-tracabilite", "|");
  

   new Section(1310, 1260, 6, "Contatos", "/por/contacts", "|");
  
  



// 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]);
