/*
 0.1 - 2008.09.10@MV first release
*/

var w3gExpandSections = Class.create();

w3gExpandSections.prototype = {
  
  initialize: function(){
    try {
    // parametri obbligatori da passare al costruttore
    // (vedi API prototype per funzione $$) Takes an arbitrary number of CSS selectors (strings) and returns 
    // a document-order array of extended DOM elements that match any of them
    var srcRegExp; // 1° parametro: espressione regolare per trovare gli elementi ai quali associare i link con blind down
    var dstRegExp; // 2° parametro: espressione regolare per trovare gli elementi ai quali associare il blind down
    
   
    // verifica se il costruttore è stato inizializzato correttamente
    var optionIndex = 2;
    if (arguments.length >= 2 && typeof arguments[0] == "string" && typeof arguments[1]=="string" ) {
        srcRegExp = arguments[0];
        dstRegExp = arguments[1];
    } else {
        alert("w3gExpandSections - Constructor error: check arguments");
        return false;
    }
    
    // opzioni
    this.options = Object.extend({
         duration: 1.0,                      //durata dell'effetto (default: 1 secondo)
         event: 'click',
         afterFinishExpand: function() {},   // default: empty function
         //afterFinishExpand: function() { new Effect.Highlight(this)},
         afterFinishCollapse: function() {}  // default: empty function      
   }, arguments[optionIndex] || {});    
   
    this.srcRegExp = srcRegExp;
    this.dstRegExp = dstRegExp;
    this.srcList = $A(new Array());
    this.dstList = $A(new Array());
    
    // splitta l'espressione regolare sul carattere ',' nel caso contenga regolare css multiple (vedi regolare css)
    var srcRegExpSplitted = $A(srcRegExp.split(','));
    var dstRegExpSplitted = $A(dstRegExp.split(','));
    
    // array degli elementi sorgente che corrispondo all'espressione regolare (sui quali è associato il click per il blind down)
    this.srcList = document.getElementsByClassName(srcRegExpSplitted);
    this.dstList = document.getElementsByClassName(dstRegExpSplitted);
   
   /*
   for (i=0; i<srcRegExpSplitted.size();i++)
      this.srcList = this.srcList.concat($$(srcRegExpSplitted[i]));
   for (i=0; i<dstRegExpSplitted.size();i++)
      this.dstList = this.dstList.concat($$(dstRegExpSplitted[i]));
   */
  
    for (i=0; i<this.dstList.size();i++) {
        this.srcList[i].w3gExpandMain = this; // associa all'elemento la referenza a quest'oggetto
        this.srcList[i].w3gExpandId = i;      // associa all'elemento la sua id nell'oggetto main
        var display = $(this.dstList[i]).getStyle("display");
        this.srcList[i].w3gExpandExpanded = !(display=="none");

       Event.observe( this.srcList[i], this.options.event, this.expandCollapseElement.bindAsEventListener(this.srcList[i]));
    }
    } catch (e) {/*alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message);*/ } 
    
  },
  
  expandCollapseElement: function(event){  
    try {
       var srcReference = this;
       
       var oldHref = this.getAttribute("href");
       if (oldHref) // se esiste l'attributo href
          this.setAttribute("href", "javascript:void(0)");
        
       var dst = this.w3gExpandMain.dstList[this.w3gExpandId];
       
       //alert(this.w3gExpandMain.options.afterFinishExpand);
       
       if (!this.w3gExpandExpanded) { // non espanso
           this.effectBlind =  this.w3gExpandMain.options.expandEffect
           
            this.effectBlind = Effect.BlindDown(dst, { queue:'end', 
                                                       duration: this.w3gExpandMain.options.duration,
                                                       afterFinish: this.w3gExpandMain.options.afterFinishExpand.bind(dst)  
                                                      });                                                
          this.w3gExpandExpanded = true;            
       } else { // espanso
             this.effectBlind = Effect.BlindUp(dst, { queue:'end', 
                                                      duration: this.w3gExpandMain.options.duration,
                                                      afterFinish: this.w3gExpandMain.options.afterFinishCollapse.bind(srcReference)      
                                                    }); 
             this.w3gExpandExpanded = false;       
       }
       
       // ferma la propagazione dell'evento
       Event.stop(event);
    } catch (e) {alert(e)}
  }
 };