// LOAD-ORDER: 90

/*

    C_L_A_S_S
  
 	The PlusMinusNode [+] / [-]
 	
 	Dependencies : [ajax], TimeoutFixer, DOMHelper

*/

var PlusMinusNode = Class.create( DOMHelper , AjaxRelogin , Overlay , {

	
	EXPANDABLE_CLASS : 'expandable',
	
	COLLAPSABLE_CLASS : 'collapsable',
	
	WAITING_CLASS : 'waiting',
	
	
  	 /* --------------------------------------------------------------(The Constructor) */
  	 initialize: function( li, targetChildRule , parentMachine ) { 
  	  	 	
  	 	this.n = $(li);
  	 	
  	 	this.targetChildRule = targetChildRule;
  	 	
  	 	this.machine = parentMachine;  	 	 	  	 
  	 	
  	 },
  	   	 
    	 
  	 /* -------------------------------------------------------------- */
  	 isExpandable: function(){
  	 
  	 	return this.n.hasClassName( this.EXPANDABLE_CLASS ) ;
  	 	
  	 },
  	 
  	 /* -------------------------------------------------------------- */
  	 insertButton: function ( bttn ) {
  	   	 			
		this.n.insert(  { 'top' : bttn } );
  	 	
  	 },
  	 
  	 /* -------------------------------------------------------------- */
	  toggleTargetChild: function(){
	  	
	  	 var _targetChild = this.n.down( this.targetChildRule );  // might NOT exist yet...	 	  	 
	  		  	
	   	if ( _targetChild ) 
	   	
			if ( this.isExpandable() )   	  		
	  			this.expandTargetChild( _targetChild );
	  		else	  		
	  			this.hideTargetChild( _targetChild, true );					
				
	   	else 
	   		this.collectTargetChildViaAjax( );
	   		  	  	
	  						
	  
	  },
	  
	  /* -------------------------------------------------------------- */
  	collectTargetChildViaAjax: function( ){
  	
  		this.enterWaitState();  				
  		
  		new Ajax.Updater( {success:this.n, failure:null}, 
							this.preparedAjaxUrl,
							 {	
							 	method: 'get' , 
							  	insertion: 'bottom'	,
							  	onComplete: this.ajaxComplete.bind(this) ,
							  	onFailure: this.ajaxFailed.bind(this) 			  	
							  }  		
						);		  		
  
 	 },
 	 
 	 /* -------------------------------------------------------------- */
 	 ajaxComplete: function(resp){
 	 	
 	 	this.exitWaitState(); 	 	
 	  	
 	 	var _targetChild = this.n.down( this.targetChildRule );  // MUST exist now...	  	 	
 	 
 	 	this.machine.registerNodes( _targetChild );
 	 	
 	 	/* global func !!! */
 	 	setupEndeSymbolEvents( _targetChild ); 
 	  	
 	 	this.toggleTargetChild();
 	 	
 	 },
 	 
 	 /* -------------------------------------------------------------- */
 	 ajaxFailed: function(resp){
 	 
 	 	this.exitWaitState();
 	 	
 	 	if ( resp.status == '401' )
				this.ajaxRelogin(  this.collectTargetChildViaAjax.bind(this)  );  //maybe add animation here!!!
		else 
				alert("Feil oppstod!"); 	
 	 	
 	 },
 	 
 	 
 	 /* -------------------------------------------------------------- */
 	 enterWaitState: function(){
 	 	this.n.addClassName( this.WAITING_CLASS ); 
 	 	this.n.removeClassName( this.EXPANDABLE_CLASS );  	 	
 	 },
     
     /* -------------------------------------------------------------- */
 	 exitWaitState: function(){
 	 	this.n.removeClassName( this.WAITING_CLASS ); 
 	 	this.n.addClassName( this.EXPANDABLE_CLASS ); 
 	 },
  	
  	    	 
 	 
	 /* -------------------------------------------------------------- */
	  getTargetHref: function(){
	  
	  	 return this.findFirstElem( this.n, 'a' ).readAttribute('href');   	  	 
	  
	  },
  
  /* ------------------+++------------------- */
  setPreparedAjaxUrl: function( strUrl ){
  
  		this.preparedAjaxUrl = strUrl;
  },
  
  
  /* -------------------------------------------------------------- */
  expandTargetChild: function( targetChild ){
  			
  			this.n.removeClassName( this.EXPANDABLE_CLASS );	
  			this.n.addClassName(this.COLLAPSABLE_CLASS);
  			targetChild.show();	
			
  },
  
  
  /* -------------------------------------------------------------- */
  hideTargetChild: function( targetChild , blnNestingAllowed ){
  			
  			targetChild.hide();
  			this.n.removeClassName(this.COLLAPSABLE_CLASS);
  			this.n.addClassName( this.EXPANDABLE_CLASS );

  }
  
    	 
  	   
});


/*

    C_L_A_S_S
  
 	The PlusMinusMachine [+] / [-]
 	
 	Dependencies : PlusMinusNode, DOMHelper

*/
var PlusMinusMachine = Class.create( AjaxContract, DOMHelper, {

  
  BTTN_CLASS : "pm_bttn",
  
  
  /* @constructor */
  initialize: function( machineElem , components ) {
    	                      
     this.setBaseLinkLevels ( $(machineElem).select( components['context-node'] ) );
     
     this.targetChildRule = components['target-child'];
     
     this.nodesRule = components['nodes'];                
     
     this.registerNodes( $(machineElem) );
     
              
  },
  
  /* @public */
  registerNodes: function( parent ){
  
  	 var nodes = parent.select( this.nodesRule );    
  	 
  	//alert( "|PlusMinusMachine|" + nodes.length + " -- " + this.targetChildRule); 
  	
  	 var boundFunc = this.createButton.bind(this);    
  	 var this_machine = this;
  	 var this_targetChildRule = this.targetChildRule;
  	 this._firstNode = null;
  	 var set1stPMNodeFunc = this.set1stPMNode.bind(this);
     
     nodes.each( function(node) { 
     					var _pmN = new PlusMinusNode( node, this_targetChildRule, this_machine );
     					 set1stPMNodeFunc ( _pmN );
     					 boundFunc( _pmN );  
     					 }
     			);
       	
  
  },
  
  set1stPMNode: function( pmNode ){
  	
  	if( this._1stPMNode == null )
  		this._1stPMNode = pmNode;
  	
  },
  
  get1stPMNode: function() {
  	 return this._1stPMNode;
  },
  
  
  /* -------------------------------------------------------------- */
  createButton: function(pmNodeObj) {    				
 						
	var newBttn = $( document.createElement('div') );				
	newBttn.addClassName( this.BTTN_CLASS );	
	newBttn.observe( "click", pmNodeObj.toggleTargetChild.bind(pmNodeObj) );  //bind!
	pmNodeObj.insertButton( newBttn );	 	
	pmNodeObj.setPreparedAjaxUrl( this.adaptedTOCUrl( pmNodeObj.getTargetHref() , this.intBaseLinkLevels ) );		
 			 
  },
  
  /* -------------------------------------------------------------- */
  setBaseLinkLevels: function ( contextArr ){
  
	  	if ( contextArr.length>0 )
	     	this.intBaseLinkLevels = this.ancestorAndSelfCount( contextArr[0] ,'li' , this.BASELINK_LEVELS_TRGR_CLASS );
	    else
	    	this.intBaseLinkLevels = 0;
    	
  }
  
  
  
  
});


var M1_TOC = null;
var M2_LINKS = null;


document.observe(	'dom:loaded' , 
					function() { 
 						
 						M1_TOC = new PlusMinusMachine( 	$('toc'), 
												{ 
													'context-node' 	: 	"li a.context",
													'nodes'			:	"li:not([class ~= 'ende'])", //only one level?
													'target-child'	: 	"ul"
												} 
											);
						
						/* must check PREFs now.. */
 						
 						/*
						if ( $('links') )
							M2_LINKS = new PlusMinusMachine( 	$('links'), 
												{ 
													'context-node' 	: 	"",
													'nodes'			:	"li.endpoint)", //only one level?
													'target-child'	: 	"div.context-body"
												} 
											);*/
					}
				);


  
  
