FoldTree = Class.create();
FoldTree.prototype =
{
 initialize: function(_root,_docRoot,_options)
 {
 	this.root=$(_root);
 	this.docRoot=$(_docRoot);
        if(typeof(_options)=="Array")
        {
            this.folderClass=_options["folderClass"]||"folder";
            this.foldClass=_options["foldClass"]||"fold";
            this.unfoldClass=_options["unfoldClass"]||"unfold";
            /*this.highlight_class=_options["higlightClass"]||"highlight";*/
        }
        else
        {
            this.folderClass="folder";
            this.foldClass="fold";
            this.unfoldClass="unfold";
            /*this.highlight_class="highlight";*/
        }
 	this.foldersElements=$(_root.getElementsByClassName("folder"));

 	this.refresh();
 },
 
 refresh: function()
 {
 	this.folders=new Array();
 	this.foldersElements.each(function(item,i){this.folders[i]=new DocFolder(item,this.docRoot)}.bind(this));
 }/*,
 
 unfoldCurrentDocument: function()
 {
 	
 	/*$A(this.root.getElementsByClassName(this.highlight_class)).each(function(item,i){item.removeClassName("highlight");});*/
 	// this.folders.each(function(item,i){item.unfoldCurrentDoc();});
        
 	// trouver l'highlight:
        /*
 	var h=$A(this.root.getElementsByTagName("A")).detect(
 			function(item,i){
 				// console.log("__parseParameters(item.href)['idDoc']:"+__parseParameters(item.href)["idDoc"]);
 				// console.log("__parseParameters(item.href):"+__parseParameters(item.href));
 				// console.log("item.href:"+item.href);
 				if(__parseParameters(item.href)["idDoc"]==__docId)
 					return item;
 				else
 					return false;});
 	if($(h))
 		$(h).addClassName("highlight");
         
 }*/
}

DocFolder = Class.create();
DocFolder.prototype =
{
 initialize: function(_element,_foldedElement,_togglingEvent,_foldClass,_unfoldClass,_foldSwitchClass,_foldDuration)
 {
	 this.element=_element||document;

	 this.foldClass=_foldClass||"fold";
	 this.unfoldClass=_unfoldClass||"unfold";
         this.foldSwitchClass=_foldSwitchClass||"foldSwitch";
         
	 this.foldDuration = _foldDuration||0.2;
	 this.togglingEvent=_togglingEvent||"click";
         /*this.docElement=_docElement;*/
         this.foldedElement=_foldedElement||$(this.element.getElementsByClassName("folded").first());// FIXME
	 /*this.leafs=$A(this.element.getElementsByClassName("fiche"));// ajax*/
         
	 if(this.element.hasClassName(this.foldClass))
	 	this.foldedElement.hide();
	 
	 /*this.unfoldCurrentDoc();*/
	 
	 this.refresh();
 },
 /*unfoldCurrentDoc: function(){
 	var item=this.containsId(__docId);
 	if(item)
 	{
 		// alert(this.foldedElement.inspect());
 		// this.foldedElement.show();
 		// pas beau faudrait une fonction pr l'effet
 		if(this.isFolded())
 		{
 			this.element.removeClassName(this.foldClass);
 			this.element.addClassName(this.unfoldClass);
 			// Effect.toggle(this.foldedElement, 'blind', {duration:this.foldDuration});
 			this.foldedElement.show();
 			// alert(item.inspect());
 			
 		}
 		// $(item).addClassName("highlight");
 		
 	}
 },*/
 refresh: function()
 {
 	this.element.observe(this.togglingEvent, this.toggle.bindAsEventListener(this),false);
 	/*this.leafs.each(function(item,i)
 		{
 			item.observe("click",this.docUpdate.bindAsEventListener(this),false);
 		}.bind(this));*/
 },
 /*devrais etre externalis�*/
 /*containsId: function(_id)
 {
 	return $A(this.foldedElement.getElementsByTagName("A")).detect(
 			function(item,i){
 				if(__parseParameters(item.href)["idDoc"]==_id)
 					return item;
 				else
 					return false;});
 },*/

 isFolded: function()
 {
 	if(this.element.hasClassName(this.foldClass))
 		return true;
 	return false;
 },
 
 isUnfolded: function()
 {
 	if(this.element.hasClassName(this.unfoldClass))
 		 return true;
 	return false;
 },
 
 toggleClass: function()
 {
 	if(this.isFolded())
 	{
 		this.element.removeClassName(this.foldClass);
 		this.element.addClassName(this.unfoldClass);
 	}
 	else if(this.isUnfolded())
 	{
 		this.element.removeClassName(this.unfoldClass);
 		this.element.addClassName(this.foldClass);
 	}
 	else
 	{
 		this.foldedElement.hide();
 		this.element.addClassName(this.foldClass);
 	}
 },
 
 toggle: function(event)
 {
     var tgt=event.target || event.srcElement;
     if(tgt.hasClassName(this.foldSwitchClass))
 	{
 		this.toggleClass();
 		Effect.toggle(this.foldedElement, 'blind', {duration:this.foldDuration});
 	}
        Event.stop(event);
        return false;
 }
}


