 

////////////////////////////////////////////////////////////////////
//// Site Tree javascript representation ///////
////////////////////////////////////////////////////////////////////
// Example of use :
// Retrieve siteRoot Section : 
//	var siteRoot = sectionById[siteRootId];
// Retrieve first level Sections
//	var firstLevel = childrenById[siteRootId];
var siteRootId = 1299;
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(1299, 93, 7, "Extranet", "/fre/extranet", "|010-EXT|");
4

   new Section(1302, 1299, 2, "Produits", "/fre/extranet/produits", "|");
  
  
      new Section(1447, 1302, 5, "Especes", "/fre/extranet/produits/especes", "|");
        
        
            new Section(1456, 1447, 1, "Vache Laitière", "/fre/extranet/produits/especes/vache-laitiere", "|");
        
            new Section(1457, 1447, 2, "Caprin", "/fre/extranet/produits/especes/caprin", "|");
        
            new Section(1458, 1447, 3, "Bovin viande", "/fre/extranet/produits/especes/bovin-viande", "|");
        
            new Section(1459, 1447, 4, "Ovin viande", "/fre/extranet/produits/especes/1459", "|");
        
            new Section(1460, 1447, 5, "Ovin lait", "/fre/extranet/produits/especes/ovin-lait", "|");
        
            new Section(1461, 1447, 6, "Gibier", "/fre/extranet/produits/especes/gibier", "|");
        
            new Section(1462, 1447, 7, "Lapin", "/fre/extranet/produits/especes/lapin", "|");
        
            new Section(1463, 1447, 8, "Porc", "/fre/extranet/produits/especes/porc", "|");
        
            new Section(1464, 1447, 9, "Volaille", "/fre/extranet/produits/especes/volaille", "|");
        
            new Section(1465, 1447, 10, "Basse cour", "/fre/extranet/produits/especes/basse-cour", "|");
        
            new Section(1466, 1447, 11, "Pet food", "/fre/extranet/produits/especes/pet-food", "|");
        
            new Section(1467, 1447, 12, "Cheval", "/fre/extranet/produits/especes/cheval", "|");
        
  
      new Section(1448, 1302, 6, "Minéraux", "/fre/extranet/produits/mineraux", "|");
        
        
  
      new Section(1449, 1302, 7, "Blocs / Pierres", "/fre/extranet/produits/blocs", "|");
        
        
  
      new Section(1450, 1302, 8, "Laits", "/fre/extranet/produits/laits", "|");
        
        
  
      new Section(1451, 1302, 9, "Hygiène / Diététique", "/fre/extranet/produits/hygiene", "|");
        
        
  
      new Section(1452, 1302, 10, "Aliments liquides", "/fre/extranet/produits/aliments", "|");
        
        
  
      new Section(1453, 1302, 11, "Noyaux", "/fre/extranet/produits/noyaux", "|");
        
        
  
      new Section(1454, 1302, 12, "TAF", "/fre/extranet/produits/taf", "|");
        
        
  

   new Section(1303, 1299, 3, "Distribution", "/fre/extranet/distribution", "|");
  
  
      new Section(1316, 1303, 4, "Actualités réseau", "/fre/extranet/distribution/actualites2", "|");
        
        
  
      new Section(1326, 1303, 5, "Objets pub", "/fre/extranet/distribution/publicitaires", "|");
        
        
  
      new Section(1325, 1303, 6, "Signalétique", "/fre/extranet/distribution/signaletique", "|");
        
        
  
      new Section(1430, 1303, 7, "Forum réseau Elite", "/fre/extranet/distribution/forum-elite", "|050-ELITE|");
        
        
  
      new Section(1320, 1303, 8, "Formation", "/fre/extranet/distribution/formation", "|");
        
        
            new Section(1336, 1320, 1, "Catalogue planning", "/fre/extranet/distribution/formation/catalogue-planning", "|");
        
            new Section(1337, 1320, 2, "Financement", "/fre/extranet/distribution/formation/financement", "|");
        
            new Section(1338, 1320, 3, "Besoin en formation", "/fre/extranet/distribution/formation/besoin-en-formation", "|");
        
  

   new Section(1305, 1299, 5, "Qualité / Service", "/fre/extranet/service", "|");
  
  
      new Section(1324, 1305, 9, "Actualités service", "/fre/extranet/service/actualites", "|");
        
        
  
      new Section(1441, 1305, 10, "Vidéo sur la traçabilité", "/fre/extranet/service/tracabilite", "|");
        
        
  

   new Section(1446, 1299, 11, "Contact", "/fre/extranet/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]);
