

var Accordion = Class.create();

Accordion.prototype = 
{
    initialize: function(container_id, handles, bodys, options) 
    {
        this.options = this._set_options(options);        
        this.headers = $$(handles);
        this.bodys = $$(bodys);
        this.container_id = container_id;

        if(this.bodys.length != this.headers.length)
        {
            throw Error('Number of headers/bodys does not match!');
        }

        for(var i = 0; i < this.headers.length; i++)
        {        
            Event.observe(this.headers[i], this.options.event_trigger, this.show.bind(this, i));

            	this.bodys[i].style.display = "none";
        }
var open = -1;
// just hide others
         for(var i = 0; i < this.bodys.length; i++)
                {
               
                    if(this.bodys[i].style.display != 'none' && i != index)
                    {
                        new Effect.SlideUp(this.bodys[i]);
                    }
                 if (  this.options.open_question == this.headers[i].id)
                 open = i;

                }
                
    
            if (open <0)
            open =  this.options.default_open;
               
        if (open >= 0)
{
 this.bodys[open].style.display = "block";
 this.bodys[open].id = this.container_id+"visible";
 this.headers[open].className="active";

               
   
        }
        
              
        
  
  
  
  
  
     
    },

    show: function(index, force) 
    {        

        if ((index >= this.length) || (index < 0)) 
        {
            throw Error('Index out of range');
        }
//console.log(index);
        if (this.bodys[index].id == this.container_id+'visible')
        {   
                        
            if (typeof force == "boolean") 
            {          
             new Effect.SlideDown(this.bodys[index]);// alina        
                 
                this.options.OnStart(index, this.bodys[index]);

                // Force display the visible object                
                for(var i = 0; i < this.bodys.length; i++)
                {
                    if(this.bodys[i].style.display != 'none' && i != index)
                    {
                        new Effect.SlideUp(this.bodys[i]);
                    }

                }
             
			
			
                new Effect.SlideDown(this.bodys[index]);
               
        
            }
            
               else
                {
       
                //effects here
                new Effect.Parallel(
                [
                    new Effect.Fade(this.bodys[index]),                
                    new Effect.BlindUp(this.bodys[index])
                   
                ], {
                    duration: this.options.duration
                }
            );
            
                 this.headers[index].className="passive"; //alina
                    this.bodys[index].id = "";
                    return;
           
                //effects
               // new Effect.SlideUp(this.bodys[index]);
                
                }
            
            
            
            
        }
        else
        {  
     // console.log('not first time'  );    
            this.options.OnStart(index, this.bodys[index]);

            // Normal change
            var str_visibleName = this.container_id+'visible';
            if ($(str_visibleName) != null)
            {
            new Effect.Parallel(
                [
                    new Effect.Fade($(this.container_id+'visible')),                
                    new Effect.BlindUp($(this.container_id+'visible')),
                    new Effect.BlindDown(this.bodys[index]),
                    new Effect.Appear(this.bodys[index])
                ], {
                    duration: this.options.duration
                }
            );}
            else
            {
            new Effect.Parallel(
                [
                    new Effect.BlindDown(this.bodys[index]),
                    new Effect.Appear(this.bodys[index])
                ], {
                    duration: this.options.duration
                }
            );}
            
          if ($(str_visibleName) != null)
          {

            $(this.container_id+'visible').id = "";
            }
            this.bodys[index].id = this.container_id+"visible";
        }
     
            
                  for(var i = 0; i < this.bodys.length; i++) //alina
                {
                    
                    
                     this.headers[i].className="passive"; //alina
                }
                    this.headers[index].className="active"; //alina

        this.options.OnFinish(index, this.bodys[index]);
                    
    },

    _default_options: 
    {
        duration: 0.3, 
        open_question: 'none',       
        default_open: 1,
        event_trigger: 'click',
        OnStart: function() { },
        OnFinish: function() { }
    },

    _set_options: function(options)
    {
        if(typeof options != "undefined")
        {        
            var result = [];
            for(option in this._default_options)
            {
                if(typeof options[option] == "undefined")
                {
                    result[option] = this._default_options[option];
                }
                else
                {
                    result[option] = options[option];
                }
            }

            return result;
        }
        else
        {
            return this._default_options;
        }
    }        
};

	

Effect.Accordion = Accordion;