/**
 * default.js
 *
 * Version history (please keep backward compatible):
 * 1.0, 2009-02-26: Cornelius Hansjakob
 *
 * @author Cornelius Hansjakob <cha@massiveart.com>
 * @version 1.0
 */

Default = Class.create({

  initialize: function() {
    this.reValue = false;
  },
  
  /**
   * updateLiveSearch
   */
  updateLiveSearch: function(selectedElement){
    if($('searchfield')){      
      var link = selectedElement.childNodes[0].getAttribute('href');
        if(link != ''){
      return self.location.href = link;  
      }
    }
  },
  
  /**
   * search
   */
  search: function(){
    if($('searchfield')){
      if(($('searchfield').value != '')){
          $('searchForm').submit();
      }
    } 
  },
  
  /**
   * imgGalleryShowAll
   */
  imgGalleryShowAll: function(elementHide){
    if($('divImageGallery')){
      var images = $('divImageGallery').innerHTML;
      $('divImageGallery').remove();
      if($(elementHide)) $(elementHide).hide();
      new Insertion.After($(elementHide), images);
    }
  },
  
  /**
   * toggleElement
   */
  toggleElement: function(elementId, dblDuration){
    if(typeof(dblDuration) == 'undefined'){
      dblDuration = 0.2;   
    }
    if($(elementId)){
      if($(elementId).style.display == 'none'){
        Effect.SlideDown(elementId, {duration: dblDuration});
      }else{
        Effect.SlideUp(elementId, {duration: dblDuration});
      }
    }
  },
  
  /**
   * submitForm
   */
  submitForm: function(formId){
    if($(formId)){
      
      /**
       * validation
       */
      this.retValue = true;
      
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('mail');
      
      if(this.retValue == true) {
	      if($('noticebox')) $('noticebox').hide();
	      //this.addBusyClass('divFormButton');
	      
	      /**
	       * serialize form
	       */
	      var serializedForm = $(formId).serialize();
	      
	      new Ajax.Request($(formId).readAttribute('action'), {
	        parameters: serializedForm,
	        evalScripts: true,
	        onComplete: function(transport) {         
	          //this.removeBusyClass('divFormButton');
	          this.toggleElement('divEventForm', 0.5);
	          if($('succesbox')){
	            $('succesbox').show();
	            new Effect.Highlight('succesbox', { startcolor: '#ffff99', endcolor: '#ffffff' });
	            setTimeout('$("succesbox").fade()', 1500);
	          }    
	        }.bind(this)
	      });
	    }else{
	      //this.removeBusyClass('divFormButton');
        if($('noticebox')) $('noticebox').show();
	    }
    }
  },
  
  /**
   * validateInput
   */
  validateInput: function(element, baseValue) {
    if(($(element) && $F(element).blank()) || $F(element) == baseValue){
      $(element).addClassName('missinginput');
      this.retValue = false;
    }else{
      $(element).removeClassName('missinginput');
    }
  },  
  
  /**
   * validateInputEmail
   */
  validateInputEmail: function(element){
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if($(element)){
      if(!filter.test($F(element))){
        $(element).addClassName('missinginput');
        this.retValue = false;
      }else{
        $(element).removeClassName('missinginput');
      }
    }
  },
  
  /**
   * changeTestMode
   */
  changeTestMode: function(status){
    new Ajax.Request('/zoolu-website/testmode/change', {
      parameters: { TestMode: status },
      evalScripts: true,
      onComplete: function(transport) {         
        window.location.href = window.location.href;
      }.bind(this)
    });
  },
  
  /**
   * addBusyClass
   */
  addBusyClass: function(busyElement, blnDisplay) {
    if($(busyElement)){
      $(busyElement).addClassName('busy');
      if(blnDisplay) $(busyElement).show();
    }
  },

  /**
   * removeBusyClass
   */
  removeBusyClass: function(busyElement, blnDisplay) {
    if($(busyElement)){
      $(busyElement).removeClassName('busy');
      if(blnDisplay) $(busyElement).hide();
    }
  },
  
  /************************************************************
   *                         FORMULAR                         *
   ************************************************************/
  
  /**
   * initFormular
   */
  initFormular: function(){
    // observe for arrangementTotal
    if($('arrangementPerson')){
      $('arrangementPerson').observe('keyup', function(event){
        this.calcArrangement(300);
        this.calcPriceTotal();
        this.formDeactivate();
      }.bind(this));
    }
    
    // observe for single events
    $$('.formEvents input[type=checkbox]').each(function(element){
      element.observe('click', function(event){
        this.calcEvent(element.id);
        this.calcPriceTotal();
        this.formDeactivate();
      }.bind(this));
    }.bind(this));
    
    // observe eventPerson
    if($('eventPerson')){
      $('eventPerson').observe('keyup', function(event){
        this.calcAllEvents();
        this.calcPriceTotal();
        this.formDeactivate();
      }.bind(this));
    }
    
    // observe form send
    if($('bttnForm')){
      $('bttnForm').observe('click', function(event){
        this.sendForm();
      }.bind(this));
    }
  },
  
  /**
   * sendForm
   */
  sendForm: function(){
    this.retValue = true;
    
    this.validateInput('fname');
    this.validateInput('sname');
    this.validateInput('address');
    this.validateInput('email');
    this.validateInputEmail('email');
    this.validateInput('phone');
    
    if(this.retValue == false){
      new Effect.ScrollTo('fname', {duration: 0.2, offset: -80});
    }
    else {
      $('eventTotal').enable();
      $('arrangementTotal').enable();
      $('priceTotal').enable();
      $('form_participation').submit();
    }
  },
  
  /**
   * calcPriceTotal
   */
  calcPriceTotal: function(){
    if($('priceTotal')){
      $('priceTotal').setValue(parseInt($F('arrangementTotal')) + parseInt($F('eventTotal')));
    }
  },
  
  /**
   * calcArrangement
   */
  calcArrangement: function(price) {
    var person = 0;
    if($('arrangementPerson') && $F('arrangementPerson') != '') {
      person = $F('arrangementPerson');
      $('arrangementTotal').setValue(0);
    }
    $('arrangementTotal').value = price * person;
  },
  
  /**
   * calcEvent
   */
  calcEvent: function(elementId) {
    if(($('eventPerson') && $F('eventPerson') > 0) && $('total_'+elementId) && $('price_'+elementId) && $(elementId)){
      if($(elementId).checked){
        $('total_'+elementId).setValue($F('eventPerson') * $F('price_'+elementId));
      }else{
        $('total_'+elementId).setValue(0);
      }
    }else{
      if($('total_'+elementId)) $('total_'+elementId).setValue(0);
    }
    this.calcEventsTotal();
  },
  
  /**
   * calcAllEvents
   */
  calcAllEvents: function(){
    $$('.formEvents input[type=checkbox]').each(function(element){
      this.calcEvent(element.id);
    }.bind(this));
  },
  
  /**
   * calcEventsTotal
   */
  calcEventsTotal: function(){
    if($('eventTotal')){
      var sum = 0;
      $$('.formEvents input[type=checkbox]').each(function(element){
        if(element.checked){
          sum = sum + parseInt($F('total_'+element.id));
        }
      });
      $('eventTotal').setValue(sum);
    }
  },
    
  /**
   * changeFormats
   */
  changeFormats: function(){
    if($('creditcard').checked){
      $('creditcardPayment').setStyle({color: '#000000'});
      $('cardowner').enable();
      $('cardnumber').enable();
      $('month').enable();
      $('year').enable();
    }else{
      $('creditcardPayment').setStyle({color: '#cccccc'});
      this.cleanField('cardowner');
      this.cleanField('cardnumber');
      this.cleanField('month');
      this.cleanField('year');
    }
  },
  
  /**
   * cleanField
   */
  cleanField: function(elementId){
    if($(elementId)){
      $(elementId).value = '';
      $(elementId).disable();
    }
  },
  
  /**
   * formDeactivate
   */
  formDeactivate: function(elementId){
    if($F('arrangementPerson') != '') {
      $('formEvents').addClassName('disabled');
      $('formArrangement').removeClassName('disabled');
      $('arrangementPerson').enable();
      $('eventPerson').disable();
      $$('.formEvents input[type=checkbox]').each(function(element){
        this.checkboxDeactivate(element);
      }.bind(this));
    }else if($F('eventPerson') != ''){
      $('formEvents').removeClassName('disabled');
      $('formArrangement').addClassName('disabled');
      $('arrangementPerson').disable();
      $('eventPerson').enable();
      $$('.formEvents input[type=checkbox]').each(function(element){
        element.enable();
      }.bind(this));
    }else if($F('eventPerson') == '' && $F('arrangementPerson') == ''){
      $('formEvents').removeClassName('disabled');
      $('formArrangement').removeClassName('disabled');
      $('arrangementPerson').enable();
      $('eventPerson').enable();
      $$('.formEvents input[type=checkbox]').each(function(element){
        element.enable();
      }.bind(this));
    }
  },
  
  /**
   * checkboxDeactivate
   */
  checkboxDeactivate: function(element){
    if(element){
      element.disable();  
      element.checked = false;
    }
  }
});
