/**
* @author Jan Molak, jan.molak@cdprojektred.com
* @package UI controls
*/
jmFormInputField = Class.create();
jmFormInputField.prototype = {
  initialize: function(inputField) {
    this.inputField   = inputField;
    this.defaultValue = inputField.value;
    
    Event.observe(this.inputField, 'focus', this.onFocus.bindAsEventListener(this));
    Event.observe(this.inputField, 'blur', this.onBlur.bindAsEventListener(this));
  },              
  onFocus: function(event) {
    if(this.inputField.value == this.defaultValue) 
    {
      this.inputField.value = '';
    }
  },              
  onBlur: function(event) {
    if(this.inputField.value == '')
    {
      this.inputField.value = this.defaultValue;
    }
  }
}

jmFormPasswordField = Class.create();
jmFormPasswordField.prototype = {
  initialize: function(inputField) {
    this.inputField   = inputField;
    this.defaultValue = inputField.value;

    // show the default value, like 'enter password'
    if(this.defaultValue != '' 
      && this.inputField.type == 'password')
    {
      this.inputField.type = 'input';
    }
    
    Event.observe(this.inputField, 'focus', this.onFocus.bindAsEventListener(this));
    Event.observe(this.inputField, 'blur', this.onBlur.bindAsEventListener(this));
  },     
  onFocus: function(event) {
    if(this.inputField.value == this.defaultValue
      && this.inputField.type == 'text') 
    {
      this.inputField.type = 'password';
      this.inputField.value = '';
    }
  },     
  onBlur: function(event) {
    if(this.inputField.value == '')
    {
      this.inputField.type = 'text';
      this.inputField.value = this.defaultValue;
    }
  }
}

/**
* Simple accordion script based on accordion.js v2.0 created by Kevin P Miller | http://www.stickmanlabs.com
* @author Jan Molak
*/
jmAccordion = Class.create();
jmAccordion.prototype = {
  
  showAccordion : null,
  currentAccordion : null, 
  animating : false,
  duration : null,
  fps: 1,
  effects : [],
  
  initialize: function(container, options) {
    if (!$(container)) {
      throw(container+" doesn't exist!");
      return false;
    }
    
    this.options = Object.extend({
      resizeSpeed:  8,   
      classNames:   {
        toggle:   'accordion_toggle',
        toggleActive : 'accordion_toggle_active', 
        content:  'accordion_content'
      },
      defaultSize : {
        height : null,
        width : null
      },      
      onEvent : 'click'
    }, options || {});
    
    this.duration = ((11-this.options.resizeSpeed)*0.15);    
    
    var accordions = $$('#'+container+' .'+this.options.classNames.toggle); 

    accordions.each(function(accordion) {
      Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
      if (this.options.onEvent == 'click') {
        accordion.onclick = function() {return false;};
      }

      this.currentAccordion = $(accordion.next(0)).setStyle({height: '0px', display: 'none'});        
    }.bind(this)); 
  },
  
  activate : function(accordion) {
    if (this.animating) {
      return false;
    }
    
    this.effects = [];
  
    this.currentAccordion = $(accordion.next(0));
    this.currentAccordion.setStyle({
      display: 'block'
    });   
    
    this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);
    
    this.scaling = $H({
      scaleX: false,
      scaleY: true
    });                                                                                     
    
    if (this.currentAccordion == this.showAccordion) {
      // activate first link from the active section (ie. news)                             
      try {
        location.href = this.currentAccordion.childNodes[3].childNodes[1].childNodes[0].href;
      }
      catch(err) { }
    } else {
      this._handleAccordion();         
    }                               
  },
  
  deactivate : function() {  
      
    var options = $H({
      duration: this.duration,
      fps: this.fps,
      scaleContent: false,          
      transition: Effect.Transitions.linear,
      queue: {
        position: 'end', 
        scope: 'accordionAnimation',
        interval: 5
      },
      scaleMode: { 
        originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
        originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
      },      
      afterFinish: function() {
        this.showAccordion.setStyle({
          height: 'auto',
          display: 'none'
        });        
        this.showAccordion = null;
        this.animating = false;
      }.bind(this)
    });    
    options.merge(this.scaling);
    
    this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
    
    new Effect.BlindUp(this.showAccordion, options); 
  },
  
  _handleAccordion : function() {
  
    var options = $H({
      sync: true,
      scaleFrom: 0,   
      scaleContent: false,             
      transition: Effect.Transitions.linear,
      scaleMode: { 
        originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
        originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
      }      
    });
    options.merge(this.scaling);
    
    this.effects.push(
      new Effect.BlindDown(this.currentAccordion, options)
    );     

    if (this.showAccordion) {
      
      this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
      
      options = $H({
        sync: true,
        scaleContent: false,        
        transition: Effect.Transitions.linear
      });
      options.merge(this.scaling);
      
      this.effects.push(
        new Effect.BlindUp(this.showAccordion, options)
      );        
    }
    
    new Effect.Parallel(this.effects, {
      duration: this.duration, 
      fps: this.fps,
      queue: {
        position: 'end', 
        scope: 'accordionAnimation'
      },
      beforeStart: function() {
        this.animating = true;
      }.bind(this),
      afterFinish: function() {
        if (this.showAccordion) {
          this.showAccordion.setStyle({
            display: 'none'
          });        
        }
        $(this.currentAccordion).setStyle({
          height: 'auto'
        });
        this.showAccordion = this.currentAccordion;
        this.animating = false;
      }.bind(this)
    });    
  }
}

//location.href = $('m_twee').previous(0). childNodes[0].href