var OptionsView = new Class({
    Implements: [Events],
	//costruttore, assegno le variabili necessarie
    /**
     * container == elemnto in cui andrà il blocco descrizione
     * */
	initialize: function(settings) {
        this.settings = settings;
        this.optionBlocks = new Array();
        
	},
	//ricevo oggetto con le opzioni disponibili
    setWorkings:function(workings){
        workings.each(function(working){
            var optionBlock = new OptionBlock(this.settings,working);
            optionBlock.setBlock();
            optionBlock.addEvent('sonSelection',this.sonSelected.bindWithEvent(this));
            optionBlock.addEvent('fatherSelection',this.fatherSelected.bindWithEvent(this));
            optionBlock.addEvent('edgeSelection',this.edgeSelected.bindWithEvent(this));
            optionBlock.addEvent('getDependency',this.getDependency.bindWithEvent(this));
            optionBlock.addEvent('getDependent',this.getDependent.bindWithEvent(this));
            optionBlock.addEvent('getCompatibility',this.getCompatibility.bindWithEvent(this));
            this.optionBlocks.push(optionBlock);
        }.bind(this));
    },
	//ricevo oggetto con i prezzi delle opzioni selezionate
    setPrices:function(workings){
        workings.each(function(working){
            var blockId = this.settings.prefix + working.father;
            var block = $(blockId);
            var label = block.getElement('label[class="' + this.settings.price + '"]');
            if (working.price != 0) {
                label.set('text',working.price + " Euro");
            }else{
                label.set('text',"0.00 Euro. Errore!");
            }
        }.bind(this));
    },
    
    sonSelected:function(pair){
        this.fireEvent('sonReceived',pair);
    },
    fatherSelected:function(father){
        this.fireEvent('fatherReceived',father);
    },
    edgeSelected:function(edge){
        this.fireEvent('edgeReceived',edge);
    },
    getCompatibility:function(comp){
        this.fireEvent('reqCompatibility',comp);
    },
    getDependency:function(id){
        this.fireEvent('reqDependency',id);
    },
    getDependent:function(id){
        this.fireEvent('reqDependent',id);
    },
    setDependency:function(pair){
        alert("Verranno selezionate anche alcune opzioni necessare a questa lavorazione.")
        var masterId = this.settings.prefix + pair[0];
        var check = $(masterId).getElement('input[class="' + this.settings.checkbox + '"]');
        if(check.getProperty('checked') === true){
            var edges = $(masterId).getElement('select[class="' + this.settings.selectEdges + '"]');
            if(edges != undefined){
                var index = edges.selectedIndex;
                var slaveId = this.settings.prefix + pair[1];
                var slaveEdges = $(slaveId).getElement('select[class="' + this.settings.selectEdges + '"]');
                if (slaveEdges != undefined) {
                    slaveEdges.selectedIndex = index;
                    slaveEdges.fireEvent('change');
                }
            }
        }else{
           check.setProperty('checked',true);
           check.fireEvent('click');
        }
    },
    setDependent:function(id){
        var divId = this.settings.prefix + id;
        var check = $(divId).getElement('input[class="' + this.settings.checkbox + '"]');
        if(check.getProperty('checked') === true){
           alert("Verranno deselezionate anche alcune opzioni per cui questa lavorazione è necessaria.")
           check.setProperty('checked',false);
           check.fireEvent('click');
        }
    },
    setCompatibility:function(result){
        this.optionBlocks.each(function(item){
           item.setState(result);
        }.bind(this));
    },
     //ripulisco il container
    clean:function(){
       this.optionBlocks.each(function(item){
           item.clean();
        }.bind(this));
        this.optionBlocks = new Array();
    }
});
