var OptionBlock = new Class({
    Implements: [Events],
    
	//costruttore, assegno le variabili necessarie
    /**
     * container == elemnto in cui andrà il blocco descrizione
     * */
	initialize: function(settings,working) {
        this.settings = settings;
        this.working = working;
        this.lockers = new Array();
        this.disabled = false;
	},
	//ricevo oggetto con le opzioni disponibili
    setBlock:function(){
        //recupero container e template dei blocchi opzioni
        container = $(this.settings.container);
        template = $(this.settings.template);
        //clono template per creare il blocco opzioni
        this.block = template.clone();
        this.block.setProperty('id',this.settings.prefix+this.working.father.value);
        //assegno nome della famiglia di opzioni
        label = this.block.getElement('[class="'+this.settings.label+'"]');
        label.set('text',this.working.father.label);
        //recupero gli elementi da impostare
        selectEdges = this.block.getElement('[class="' + this.settings.selectEdges + '"]');
        selectEdges.set('id',null);
        selectSons = this.block.getElement('[class="'+this.settings.selectSons+'"]'); 
        selectSons.set('id',null);
        checkbox = this.block.getElement('[class="'+this.settings.checkbox+'"]'); 
        checkbox.set('id',null);
        clickable = this.block.getElement('[class="'+this.settings.clickable+'"]');
        clickable.set('id',null);
        
        //tolgo la select per i lati di applicazione se non necessaria, altrimenti la popolo
        if (this.working.father.edges == undefined) {
            selectEdges.dispose();
        }else{
            selectEdges.addEvent('change',this.edgeSelected.bindWithEvent(this,[selectEdges,this.block]));
            checkbox.addEvent('click',this.edgeCheck.bindWithEvent(this,[checkbox,selectEdges,this.block]));           
            selectEdges.setProperty('disabled',true);           
        }
        
        //controllo se devo mostrare checkbox o select con i figli, nel qual caso, la popolo
        if(this.working.sons == undefined){
            selectSons.dispose();           
            checkbox.addEvent('click',this.fatherSelected.bindWithEvent(this,[checkbox,this.block]));           
        }else{
            selectSons.getElement('option').destroy();
            this.working.sons.each(function(son){
                var sonWorking = new Element('option');
                sonWorking.set('text',son.label);
                sonWorking.set('value',son.value);
                sonWorking.inject(selectSons);
            }.bind(this));
            selectSons.addEvent('change',this.sonSelected.bindWithEvent(this,[selectSons,this.block]));           
            checkbox.addEvent('click',this.familySelected.bindWithEvent(this,[checkbox,selectSons,this.block]));           
            selectSons.setProperty('disabled',true);           
        }
        
        clickable.addEvent('click',this.labelClicked.bindWithEvent(this,checkbox));           
        
        //visualizzo il blocco
        this.block.removeClass('hr');
        this.block.inject(container);
    },
    sonSelected:function(e,sons,block){
        
        var sonId = sons.get('value');
        var fatherId = block.get('id').replace(this.settings.prefix,'');
        var pair = {'father':fatherId,'son':sonId};
        this.fireEvent('sonSelection',pair);
        //var comp = {'father':fatherId,'check':sonId};
        //this.fireEvent('getCompatibility',comp);
        
    },
    fatherSelected:function(e,check,block){
       
       var fatherId = block.get('id').replace(this.settings.prefix, '');
       var father = {
           'father': fatherId
       };
       this.fireEvent('fatherSelection', father);
       if (check.getProperty('checked') === true) {
           block.addClass('selected');
           this.fireEvent('getDependency', fatherId);
       }
       else {
           block.removeClass('selected');
           this.fireEvent('getDependent', fatherId);
           var label = this.block.getElement('[class="' + this.settings.price + '"]');
           label.set('text', '0.00 Euro');
       }
       var comp = {
           'father': fatherId,
           'check': fatherId
       };
       this.fireEvent('getCompatibility', comp);
    },
    
    familySelected:function(e,check,sons,block){
        sons.selectedIndex = 0;
        var sonId;
        var checkId;
        if (check.getProperty('checked') === true) {
            sons.setProperty('disabled', false);
            block.addClass('selected');
            sonId = sons.get('value');
            this.fireEvent('getDependency', fatherId);
        }
        else {
            sons.setProperty('disabled', true);
            block.removeClass('selected');
            sonId = -1;
            checkId = sons.get('value');
            var label = this.block.getElement('[class="' + this.settings.price + '"]');
            label.set('text', '0.00 Euro');
            this.fireEvent('getDependent', fatherId);
        }
        var fatherId = block.get('id').replace(this.settings.prefix, '');
        var pair = {
            'father': fatherId,
            'son': sonId
        };
        this.fireEvent('sonSelection', pair);
        sonId = (sonId != -1) ? sonId : checkId;
        var comp = {
            'father': fatherId,
            'check': sonId
        };
        this.fireEvent('getCompatibility', comp);
    },
    
    edgeSelected:function(e,edge,block){
        var edgeId = edge.get('value');
        var fatherId = block.get('id').replace(this.settings.prefix,'');
        var pair = {'father':fatherId,'edge':edgeId};
        this.fireEvent('edgeSelection',pair);
    },
    edgeCheck:function(e,item,edges,block){
        var edgeId;
        edges.selectedIndex = 0;
        if (item.getProperty('checked') === true) {
            edges.setProperty('disabled', false);
            edgeId = edges.get('value');
        }
        else {
            edges.setProperty('disabled', true);
            edgeId = 0;
        }
        
        var fatherId = block.get('id').replace(this.settings.prefix,'');
        var pair = {'father':fatherId,'edge':edgeId};
        this.fireEvent('edgeSelection',pair);
        
        
    },
    labelClicked:function(e,checkbox){
       if (!this.disabled) {
           var state = checkbox.getProperty('checked');
           checkbox.setProperty('checked', !state);
           checkbox.fireEvent('click');
       }
    },
    clean:function(){
        this.block.destroy();
    },
    setState:function(result){
       var idCaller = result[1];
       var idBlock = this.working.father.value;
       if(idCaller == idBlock){
           return;
       }
       var compatibles = result[0];
       var callerIndex = this.lockers.indexOf(idCaller);
       
       if(!compatibles.contains(idBlock)){
           if(callerIndex == -1){
                if(this.lockers.length == 0){
                    this.changeState(true);
                    this.block.addClass('disabled');
                }
                this.lockers.push(idCaller);
           }else{
               this.removeLock(idCaller);
           }
       }else{
           if (callerIndex != -1) {
               this.removeLock(idCaller);
           }
       } 
    },
    removeLock:function(caller){
        this.lockers.erase(caller);
        if(this.lockers.length == 0){
            this.changeState(false);
            this.block.removeClass('disabled');
        }
    },
    changeState:function(state){
        var checkbox = this.block.getElement('[class="'+this.settings.checkbox+'"]');
        checkbox.setProperty('disabled',state);
        this.disabled = state;
    }
});
