
/**
 * Handle email addresses
 *
 * @param string domain The domain
 * @param string local The local part
 */
function Mail(domain, local) {
  this.domain= domain;
  this.local= local;

  /**
   * Open new mail window
   *
   */
  this.compose= function() {
    var at= '%40';
    var prefix= 'm'+'ai'+'lt'+'o:';
    
    window.location.href = prefix+this.local+at+this.domain;
    
    return false;
  }
}

/**
 * Represents a teaser object on start page
 *
 * @param string id The id of the DOM element
 */
function Teaser(id) {
  this.main= $(id);
  this.icon= this.main.children('img');
  this.title= this.main.children('span');
  this.text= this.main.children('item');
  
  /**
   * Activate teaser item
   *
   */
  this.activate= function() {
    this.main.css('border-bottom', '1px solid #000');
    this.main.css('background', '#eeeeee');
    this.icon.attr('src', this.icon.attr('src').replace('_sw.png', '.png'));
    this.title.css('color', '#000');
    this.title.css('textDecoration', 'underline');
  }
  
  /**
   * Deactivate teaser item
   *
   */
  this.deactivate= function() {
    this.main.css('border-bottom', '0px');
    this.main.css('background', 'none');
    this.icon.attr('src', this.icon.attr('src').replace('.png', '_sw.png'));
    this.title.css('color', '#555');
    this.title.css('textDecoration', '');
  }
}

/**
 * Bandwith advisor, which calculates a required internet bandwidth for
 * given criterias and factors.
 *
 */
function BandwidthAdvisor() {
  this.factors= new Array();
  this.observers= new Array();
  this.total= null;
  
  /**
   * Adds a criteria and factor
   *
   * @param string name The name of factor
   * @param float max The maximum value
   * @param float step The step increase
   * @param float factor The factor value
   */
  this.addFactor= function(name, factor) {
    this.factors[name]= {
      'factor' : factor,
      'value'  : 0
    };
  }
  
  /**
   * Set value for given factor
   *
   * @param string name The name of factor
   * @param float value The value
   */
  this.setFactor= function(name, value) {
    this.total= null;
    this.factors[name]['value']= value;
    this.notify('changed', [ this, name, this.factors[name] ]);
  }
  
  /**
   * Adds observer for update notifies
   *
   * @param mixed obj The object
   */
  this.addObserver= function(obj) {
    this.observers[this.observers.length]= obj;
  }
  
  /**
   * Notifies observers
   *
   * @param string event The type of event
   * @param mixed[] args The event handler arguments
   */
  this.notify= function(event, args) {
    for(i= 0; i<this.observers.length; i++) {
      this.observers[i].trigger(event, args);
    }
  }
  
  /**
   * Calculate total sum of bandwidth required
   *
   * @return float
   */
  this.sum= function() {
    if (this.total == null) {
      this.total= 0;
      for(name in this.factors) {
        this.total += this.factors[name]['value'] * this.factors[name]['factor'];
      };
    }
    
    return this.total;
  }
  
  /**
   * Return total sum in giga bytes
   *
   * @return int
   */
  this.sumGb= function() {
    return Math.ceil(this.sum() / 1024 * 10) / 10;
  }
}

/**
 * Connects bandwidth advisor with a given jquery slider
 *
 * @param string id The id of the slider DIV tag
 */
function BandwidthAdvisorSlider(id) {
  this.id= id;
  this.slider= $(id);
  this.slider.slider();
  
  /**
   * Set maximum value
   *
   * @param float value The maximum value
   */
  this.setMax= function(value) {
    this.slider.slider('option', 'max', value);
  }
  
  /**
   * Set minimum value
   *
   * @param float value The minimum value
   */
  this.setMin= function(value) {
    this.slider.slider('option', 'min', value);
  }
  
  /**
   * Set slider step
   *
   * @param float value The step
   */
  this.setStep= function(value) {
    this.slider.slider('option', 'step', value);
  }
  
  /**
   * Connects the slider to given factor criteria of given bandwidth
   * adivsor
   *
   * @param BandwidthAdivsor advisor The internet bandwidth advisor
   * @param string name The factor to associate
   */
  this.connect= function(advisor, name) {
    this.slider.data('advisor', advisor);
    this.slider.data('advisorFactor', name);
    this.slider.slider('option', 'slide', function(event, ui) {
      advisor= $(this).data('advisor');
      advisor.setFactor($(this).data('advisorFactor'), ui.value);
    });
  }
}

/**
 * Connects bandwidth advisor with a given display item
 *
 * @param string id The id of display element
 */
function BandwidthAdvisorDisplay(id) {
  this.id= id;
  this.display= $(id);
  this.display.bind('changed', function(event, advisor, name, factor) {
    display= $(this);
    if (display.data('advisorFactor') == name) display.text(factor.value);
  });
  
  /**
   * Connets the input to given factor criteria of a given bandwidth
   * advisor
   *
   * @param BandwidthAdivsor advisor The internet bandwidth advisor
   * @param string name The factor to associate
   */
  this.connect= function(advisor, name) {
    this.display.data('advisorFactor', name);  // store factor to react on
    advisor.addObserver(this.display);
  }
}

