 

////////////////////////////////////////////////////////////////////
//// 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, "Royal Horse", "/fre/", "|");


   new Section(1127, 93, 3, "Présentation", "/fre/presentation", "|");
  
  
      new Section(1128, 1127, 1, "Royal Horse en Bref", "/fre/presentation/rh-en-bref", "|");
  
      new Section(1130, 1127, 2, "Historique", "/fre/presentation/historique", "|");
  
      new Section(1145, 1127, 4, "Actualités", "/fre/presentation/actualites", "|");
  
      new Section(1131, 1127, 4, "Chiffres clés", "/fre/presentation/chiffres-cles", "|");
  
      new Section(1132, 1127, 5, "Recherche et développement", "/fre/presentation/recherche-developpement", "|");
  
      new Section(1133, 1127, 6, "Certification", "/fre/presentation/certification", "|");
  
      new Section(1146, 1127, 7, "CNEF", "/fre/presentation/cnef", "|");
  
      new Section(1198, 1127, 8, "Sites partenaires", "/fre/presentation/sites-partenaires", "|");
  

   new Section(1098, 93, 4, "Team RH", "/fre/team-royal-horse", "|");
  
  
      new Section(1099, 1098, 1, "Le team", "/fre/team-royal-horse/team-royal-horse", "|");
  
      new Section(1197, 1098, 2, "Actualités du team", "/fre/team-royal-horse/team-news", "|");
  

   new Section(1088, 93, 5, "Points de vente", "/fre/implantations", "|");
  
  
      new Section(1120, 1088, 1, "Réseau de distribution", "/fre/implantations/reseau-distribution", "|");
  

   new Section(1089, 93, 6, "Produits", "/fre/produits", "|");
  
  
      new Section(1121, 1089, 1, "Recherche produits", "/fre/produits/produits-alimentation-cheval", "|");
  
      new Section(1102, 1089, 2, "Recherche par centres d'interêt", "/fre/produits/recherche-centre-interet", "|");
  

   new Section(1090, 93, 7, "Conseils", "/fre/conseils", "|");
  
  
      new Section(1103, 1090, 1, "Etat général", "/fre/conseils/etat-general", "|");
  
      new Section(1104, 1090, 2, "Problèmes nutritionnels", "/fre/conseils/problemes-nutritionnels", "|");
  
      new Section(1105, 1090, 3, "Elevage", "/fre/conseils/elevage", "|");
  
      new Section(1106, 1090, 4, "Vieux chevaux", "/fre/conseils/vieux-chevaux", "|");
  
      new Section(1107, 1090, 5, "Performances", "/fre/conseils/performances", "|");
  
      new Section(1108, 1090, 6, "Pathologies", "/fre/conseils/pathologies", "|");
  
      new Section(1142, 1090, 7, "Conseils pratiques", "/fre/conseils/conseils", "|");
  
      new Section(1202, 1090, 8, "adapt : quelques caractéristiques", "/fre/conseils/gamme-adapt-quelques-caracteristiques", "|");
  

   new Section(1091, 93, 8, "Votre Cheval", "/fre/votre-cheval", "|");
  
  
      new Section(1110, 1091, 1, "Système digestif du cheval", "/fre/votre-cheval/systeme-digestif", "|");
  
      new Section(1111, 1091, 2, "Bases de l'alimentation", "/fre/votre-cheval/bases-alimentation", "|");
  
      new Section(1113, 1091, 4, "Evaluer son poids", "/fre/votre-cheval/evaluer-son-poids", "|");
  
      new Section(1114, 1091, 5, "Calculer sa ration", "/fre/votre-cheval/caluler-sa-ration", "|");
  
      new Section(1115, 1091, 6, "Mots clés", "/fre/votre-cheval/mots-cles", "|");
  

   new Section(1092, 93, 9, "Contact", "/fre/contact", "|");
  
  
      new Section(1123, 1092, 2, "Numéro vert", "/fre/contact/numero-vert", "|");
  



// 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]);
