var OptionsManagerWidget = new Class({
    Implements: [Events],
	//costruttore, assegno le variabili necessarie
    /**
     * container == elemnto in cui andrà il blocco descrizione
     * */
	initialize: function(container) {
		this.container = container;
        settings = {
            'container':'options_container',
            'template':'option_tpl',
            'label':'father_label',
            'clickable':'clickable',
            'selectSons':'sons_select',
            'selectEdges':'edges_select',
            'checkbox':'father_checkbox',
            'price':'price_label',
            'dependent':'dependent_id',
            'dependency':'dependency_id',
            'compatibility':'compatibility_id',
            'prefix':'z'
        };
        this.model = new OptionsModel();
        this.view = new OptionsView(settings);
        this.view.addEvent('sonReceived',this.sonReceived.bindWithEvent(this));
        this.view.addEvent('fatherReceived',this.fatherReceived.bindWithEvent(this));
        this.view.addEvent('edgeReceived',this.edgeReceived.bindWithEvent(this));
        this.view.addEvent('reqDependency',this.reqDependency.bindWithEvent(this));
        this.view.addEvent('reqDependent',this.reqDependent.bindWithEvent(this));
        this.view.addEvent('reqCompatibility',this.reqCompatibility.bindWithEvent(this));
	},
	//ricevo oggetto con le opzioni disponibili
    manageEstimate:function(options){
        if(options == undefined){
            return;
        }
         this.view.setPrices(options);
    },
	//ricevo oggetto con le opzioni disponibili
    manageResult:function(options){
        if(options == undefined){
            return;
        }
        this.model.setData(options);
        var opt = this.model.getSelectData();
        this.view.setWorkings(opt);
    },
    sonReceived:function(pair){
        this.model.addSon(pair);
    },
    fatherReceived:function(father){
        this.model.addFather(father);       
    },
    edgeReceived:function(edge){
        this.model.addEdge(edge);   
    },
    reqCompatibility:function(comp){
       
        var fn = function(responseText, responseXML) {
            this.compatibilityReceived(responseText);
        }.bind(this);
         var req = new Request({
              method: 'post',
              url: $(settings.compatibility).get('href'),
              data: {'data[div]':comp.father, 
                     'data[check]':comp.check 
                    },
              onSuccess : fn
              }).send();
    },
    compatibilityReceived:function(res){
       var result = JSON.decode(res);
      
       if(result.length > 0){
           this.view.setCompatibility(result);
       }
    },
    reqDependency:function(id){
        var fn = function(responseText, responseXML) {
            this.dependencyReceived(responseText);
        }.bind(this);
         var req = new Request({
              method: 'post',
              url: $(settings.dependency).get('href'),
              data: {'data[id]':id },
              onSuccess : fn
              }).send();
    },
    dependencyReceived:function(res){
       var result = JSON.decode(res);
       if(result.length > 0){
           this.view.setDependency(result);
       }
    },
    reqDependent:function(id){
         var fn = function(responseText,responseXML){
             this.dependentReceived(responseText);
             }.bind(this);
         var req = new Request({
              method: 'post',
              url: $(settings.dependent).get('href'),
              data: {'data[id]':id },
              onSuccess : fn
              }).send();  
    },
    dependentReceived:function(res){
       res = JSON.decode(res);
       if (res != undefined) {
           this.view.setDependent(res);
       }
    },
    getEstimateData:function(){
        return this.model.getEstimateData();   
    },
    //ripulisco il container
    clean:function(){
       this.view.clean();
       this.model.clean();
    }
});
