
/**
 * InputElementView
 * Controls the view of a single input element.
 *
 */
var InputElementView = Backbone.View.extend({
  initialize:function(options) {
    _.bindAll(this, 'updateHandler');
    this.model = this.options.model;
    this.element = this.options.element;
    this.model.bind('change', this.updateHandler);

    var lSliderOptions = {  'reset_value': this.model.get('start_value'), 
                            'value':       this.model.get('user_value'), 
                            'step_value':  this.model.get('step_value'),
                            'min_value':   this.model.get('min_value'),  
                            'max_value':   this.model.get('max_value'), 
                            'name':        this.model.get('translated_name'), 
                            'share_group': this.model.get('share_group'),
                            'disabled':    this.model.get('disabled'),
                            'fixed':       this.model.get("input_element_type") == "fixed" || this.model.get("input_element_type") == "fixed_share", 
                            'formatter':   this.getFormatter(),
                            'precision':   this.getPrecision(),
                            'element':     this.element, 
                            'infoBox': {'disableDataBox':true}};
    
    this.sliderView = new AdvancedSliderView(lSliderOptions);
    
    this.set_full_label(this.model.get('label'));

    // make the toggle red if it's semi unadaptable and in a municipality.
    if(App.municipalityController.isMunicipality() && this.model.get("semi_unadaptable"))  
      this.sliderView.slider.toggleButton.element.addClass('municipality-toggle');
  
    this.initEventListeners();
  },
  
  set_full_label : function(text) {
    this.element.find(".label").html(text);
  },

  /**
   * Init event listeners.
   */
  initEventListeners:function() {
    this.sliderView.slider.sliderVO.bind('update', $.proxy(function() { 
      this.model.set({"user_value" : this.sliderView.slider.getValue()});
    }, this));
    this.sliderView.getInfoBox().bind('show', $.proxy(this.handleInputElementInfoBoxShowed, this));
    this.sliderView.bind('change', $.proxy(this.checkMunicipalityNotice, this));
    this.sliderView.slider.bind('change', $.proxy(this.handleSliderUpdate, this));
  },
  
  getPrecision:function() {
    var lPrecisionStr = this.model.get('step_value').toString() + "";
    lPrecision = lPrecisionStr.replace('.', '').length - 1;
    return lPrecision;
  },
  /**
   * Returns a formatter on basis of the step_value.
   */
  getFormatter:function() {
    var lPrecision = this.getPrecision();
    switch(this.model.get('unit')) {
      case "%":  
        return SliderFormatter.numberWithSymbolFactory("%");
      case "#":  
        return SliderFormatter.numberWithSymbolFactory("#");
      case "MW":  
        return SliderFormatter.numberWithSymbolFactory("MW");
      case "km2":  
        return SliderFormatter.numberWithSymbolFactory("km2");
      case "km":  
        return SliderFormatter.numberWithSymbolFactory("km");
      case "x":  
        return SliderFormatter.numberWithSymbolFactory("x");

      default:
        return null;
    }
  },
  
  /**
   * When the user does something on a slider this handler is invoked.
   */
  handleSliderUpdate:function() {
    this.disableUpdate = true;
    this.model.set({"user_value" : this.sliderView.slider.getValue()});
    this.sliderView.slider.setValue(this.model.get('user_value'), {'noEvent':true});
    this.disableUpdate = false;
  },
  

  /**
   * This checks if the municipality message has been shown. It is has not been shown, show it!
   */
  checkMunicipalityNotice:function() {
    if(this.model.get("semi_unadaptable") && App.municipalityController.showMessage()) 
      App.municipalityController.showMunicipalityMessage();
  },
  /**
   * Is called when something in the constraint model changed.
   * @override
   */
  updateHandler:function() {
    if(this.disableUpdate) return;

    this.sliderView.setValue(this.model.get('user_value'), {'noEvent':true});
    return false;
  },

  /**
   * Is called when then infobox is clicked.
   * @override
   */
  handleInputElementInfoBoxShowed:function() {
    this.trigger('show');
    if(this.model.get("has_flash_movie")) {
      flowplayer('a.player', '/flash/flowplayer-3.2.6.swf');
    }
  }
});





@@ -1381,16 +1381,53 @@
 tions);%0A
+    this.update_units_link_target();%0A
     %0A   
@@ -1766,16 +1766,340 @@
   %7D,%0A  %0A
+  // Some slider description links have a missing target=%22_blank%22 attribute%0A  // DEBT: We should better update the description table and remove this method%0A  // PZ - Wed 20 Jul 2011 11:32:37 CEST%0A  update_units_link_target : function() %7B%0A    this.element.find(%22.info-box a%5Bhref='/units'%5D%22).attr('target', '_blank');%0A  %7D,%0A  %0A
   set_fu

make /units link in slider descriptions open in new window. closes #155