/**
 * @fileOverview A collection of classes and functions for this project.
 * @name Project name
 * @author Tobias Kreß, kress@kupferwerk.com
 * @version 1.0
 */



/**
 * @class Description for this application class
*/
var Perbility = Class.create(KWStandard, {
	/**
	 * @constructor
	 */
	initialize: function() {
    
	}
});

var perbility = new Perbility;


var SSelect = Class.create(KWStandard, {
  
  hidden_selects: [],
  
  initialize: function() {    
    //check if its a ie6
    this.ie6 = new BrowserSwitch().acceptBrowser('ie6').accepted;
    
    this.sselect_items = $$('.sselect_item');
    this.sselect_items.each(function(select_item) {
      var select_item = select_item;
      
      document.observe('click', function(event) {
        var item_id = '#'+select_item.id;
        if(!event.element().up(item_id)) {
          if (select_item.down('.sselect_content').visible()) {
            this.closeContents();
          };
        } else {
          
        };
      }.bind(this));
      
      select_item.down('.sselect_label').observe('click', function(e) {
        e.stop();
        if (select_item.down('.sselect_content').visible()) {
          this.closeContents();          
        } else {
          this.openContent(select_item);
        };
        
      }.bind(this));
      
      select_item.select('a.close_sselect_content').first().observe('click', function(e){
        e.stop();
        this.closeContents();
      }.bind(this));
      
      select_item.select('a.reset_selection').first().observe('click', function(e){
        e.stop();
        this.resetSelection(select_item);
      }.bind(this));
      
      options = select_item.down('.sselect_content').select('ul li input');
      options.each(function(option){
        if (option.checked) {
          this.addToSelection(option);
        };
        
        option.observe('click', function(e){
          if (option.getValue()) {
            this.addToSelection(option);
          } else {
            this.removeFromSelection(option);
          };
        }.bind(this));
      }.bind(this));
    }.bind(this));
    
    return false;
	},
  
  openContent: function(container) {
    this.closeContents();
    container.addClassName('opened_sselect_item');
    container.down('.sselect_content').show();
    this.hideSelects(container);
  },
  
  closeContents: function() {
    this.showSelects();
    // container.down('.sselect_content').hide();
    this.sselect_items.each(function(select_item) {
      select_item.removeClassName('opened_sselect_item');
      select_item.down('.sselect_content').hide();
    });
  },
  
  hideSelects: function(container) {
    // this function hides all select form elements that are behind the custom overlay select. this is for only for ie6
    if (this.ie6) {
      selects = container.up('.detail_search_form').select('.select_item');
      container_offset = container.cumulativeOffset().top;
      container_full_height = container.getHeight() + container.down('.sselect_content').getHeight();
      selects.each(function(select){
        offset = select.cumulativeOffset().top;
        if (offset > container_offset && offset < (container_offset + container_full_height)) {
          this.hidden_selects.push(select);
          select.setStyle({visibility: 'hidden'});
        };
      }.bind(this));
    };
  },
  
  showSelects: function() {
    //reshows all hidden select form elements.
    if (this.ie6) {
      this.hidden_selects.each(function(select) {
        select.setStyle({visibility: 'visible'});
      });
    }
  },
  
  addToSelection: function(option) {
    var delete_link = new Element('a', {'href': '#', 'title': 'Aus Auswahl entfernen' }).update('<span>Entfernen</span>');
    delete_link.observe('click', function(e) {
      e.stop();
      this.removeFromSelection(delete_link);
    }.bind(this));
    var label = new Element('span').update(option.next('label').innerHTML);
    var li = new Element('li', { 'option_id': option.value }).update(delete_link).insert({'bottom': label});
    
    option.up('.options').next('.selected').down('ul').insert({'bottom': li});
    
    this.addToHiddenField(option.value, option);
  },
  
  removeFromSelection: function(element) {
    
    // if the checkbox is unchecked
    if (element.readAttribute('type') == "checkbox") {
      element.checked = false;
      this.removeFromHiddenField(element.value, element);
      element.up('.options').next('.selected').down('ul').childElements().each(function(li) {
        if (li.readAttribute('option_id') == element.value) {
          li.remove();
        };
      });
    } else {
      // if remove link was klicked
      element.up('.selected').previous('.options').down('ul').select('input').each(function(input) {
        if (input.readAttribute('value') == element.up('li').readAttribute('option_id')) {
          input.checked = false;
        };

      });
      this.removeFromHiddenField(element.up('li').readAttribute('option_id'), element);
      element.up('li').remove();
    };
  },
  
  resetSelection: function(container) {
    container.down('.sselect_content .options ul').select('input').each(function(input) {
      if (input.getValue()) {
        this.removeFromSelection(input);
      };
      
    }.bind(this));
  },
  
  addToHiddenField: function(id, element) {
     var hidden_field = element.up('.sselect_content').next('input[type="hidden"]');
     var new_values = hidden_field.getValue().split(',');
     new_values.push(id);
     new_values = new_values.uniq().without('');
     var new_count = new_values.length;
     hidden_field.value = new_values.join(',');
     this.updateDisplay(new_count, element);
  },
  
  removeFromHiddenField: function(id, element) {
     var hidden_field = element.up('.sselect_content').next('input[type="hidden"]');
     var new_values = hidden_field.getValue().split(',');
     new_values = new_values.without(id);
     var new_count = new_values.length;
     hidden_field.value = new_values.join(',');
     this.updateDisplay(new_count, element);
  },
  
  updateDisplay: function(count, element) {
    if (count == 0) {
      var new_label = '--- Bitte auswählen ---';
    } else {
      var new_label = count + ' ausgewählt';
    };
    element.up('.sselect_content').previous('.sselect_label').down('span').update(new_label);
  }
});
