/**
 * 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;
    this.language = 'de';
    
    this.KEY_RETURN = 13;
    this.KEY_ARRUP = 38;
    this.KEY_ARRDOWN = 40;
  },  
  
  /**
   * 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, blnSlide){
    if(typeof(dblDuration) == 'undefined'){
      dblDuration = 0.2;   
    }
    if(typeof(blnSlide) == 'undefined'){
    	blnSlide = true;   
      }
    if($(elementId) && blnSlide == true){
      if($(elementId).style.display == 'none'){
        Effect.SlideDown(elementId, {duration: dblDuration});
        if(elementId == 'searchbox'){
        	setTimeout('$(\'searchfield\').focus()', dblDuration*1000);
        }
      }else{
        Effect.SlideUp(elementId, {duration: dblDuration});
      }
    }else if($(elementId) && blnSlide == false){    	
      if($(elementId).style.display == 'none'){
        $(elementId).appear({ duration: dblDuration });
        if(elementId == 'searchbox'){
        	setTimeout('$(\'searchfield\').focus()', dblDuration*1000);
        }
      }else{
    	$(elementId).fade({ duration: dblDuration });
      }
    }
  },
  
  /**
   * 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();
	  }
	} 
  },
  
  /**
   * submitForm
   */
  submitForm: function(formId){
    if($(formId)){
      
      /**
       * validation
       */
      this.retValue = true;
       
      this.validateInput('salutation');
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('company');
      this.validateInput('mail');
      this.validateInputEmail('mail');
      
      if(this.retValue == true) {
	    if($('noticebox')) $('noticebox').hide();
	    this.addBusyClass('bttnSend');
	      
	    /**
	     * serialize form
	     */
	    var serializedForm = $(formId).serialize();
	    
	    if($('newsletter').checked){	    	
	      var curDateTime = new Date();	      
	      var month = curDateTime.getMonth()+1;
	      var year = curDateTime.getYear();
	      var day = curDateTime.getDate();	      
	      if(day < 10) day = "0" + day;
	      if(month < 10) month= "0" + month; 
	      if(year < 1000) year += 1900;	      
	      var curHour = curDateTime.getHours();
	      var curMin = curDateTime.getMinutes();
	      var curSec = curDateTime.getSeconds();
	      
	      var curDate = year+'-'+month+'-'+day;
	      var curTime = ((curHour < 10) ? "0" : "") + curHour + ":" + ((curMin < 10) ? "0" : "") + curMin + ":" + ((curSec < 10) ? "0" : "") + curSec;
	      
	      new Ajax.Request('http://track.zeona-mail.com/oi/1/6a1ae2d6246970093fe82574e6c70b70', {
	        method: 'get',
	    	parameters: {
	    	  email: $F('mail'),
	    	  Salutation: $F('salutation'),
	    	  Firstname: $F('fname'),
	    	  Surname: $F('sname'),
	    	  Language: this.language.toUpperCase()
	        },
	  	    evalScripts: true,
	  	    onComplete: function(transport) { }.bind(this)
	  	  });
	    }
	      
	    new Ajax.Request($(formId).readAttribute('action'), {
	      parameters: serializedForm,
	      evalScripts: true,
	      onComplete: function(transport) {         
	        this.removeBusyClass('bttnSend');	        
	        if($(formId)) $(formId).hide();
	    	if($('succesbox')){	            
	          $('succesbox').appear();
	        }    
	      }.bind(this)
	    });
	  }else{
	    this.removeBusyClass('bttnSend');
        if($('noticebox')) $('noticebox').show();
	  }
    }
  },
  
  /**
   * validate newsletter form
   */
  sendNewsletterForm: function(formId){
    if($(formId)){
      
      /**
       * validation
       */
      this.retValue = true;
       
      this.validateInput('Salutation', 'please_choose');
      this.validateInput('Firstname');
      this.validateInput('Surname');
      this.validateInput('email');
      this.validateInputEmail('email');
      
      if(this.retValue == true) {
    	if($('noticebox')) $('noticebox').hide();
        $(formId).submit(); 
      }else{
    	if($('noticebox')) $('noticebox').show();  
      }
    }
  },
  
  /**
   * sendCall - Call-Me-Form
   */
  sendCall: function(){
    if($('callmeForm')){
       
      /**
       * validation
       */
      this.retValue = true;
      
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('company');
      this.validateInput('phone');
      
      if(this.retValue == true) {
 	   if($('noticebox')) $('noticebox').hide();
 	   this.addBusyClass('bttnSend');
 	     
 	   /**
 	    * serialize form
 	    */
 	   var serializedForm = $('callmeForm').serialize();
 	     
 	   new Ajax.Request($('callmeForm').readAttribute('action'), {
 	     parameters: serializedForm,
 	     evalScripts: true,
 	     onComplete: function(transport) {         
 	       this.removeBusyClass('bttnSend');	        
 	       if($('callmeForm')) $('callmeForm').hide();
 	   	   if($('succesbox')){	            
 	         $('succesbox').appear();
 	       }    
 	     }.bind(this)
 	   });
 	  }else{
 	    this.removeBusyClass('bttnSend');
        if($('noticebox')) $('noticebox').show();
 	  }
    }
  },
  
  /**
   * login
   */
  login: function(){
    if($('loginForm')){
      /**
       * validation
       */
      this.retValue = true;
        
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('company');
      this.validateInput('mail');
      this.validateInputEmail('mail');
      
      if(this.retValue == true) {
   	    if($('noticebox')) $('noticebox').hide();
   	    this.addBusyClass('bttnLogin');
    	
        /**
  	     * serialize form
  	     */
  	    var serializedForm = $('loginForm').serialize();
    	
        new Ajax.Request($('loginForm').readAttribute('action'), {
    	  parameters: serializedForm,
		  evalScripts: true,
		  onComplete: function(transport) {
        	if(transport.responseText == true || transport.responseText == 'true'){
        	  return window.location.reload();	  
		    }
		  }.bind(this)
        });
      }else{
    	this.removeBusyClass('bttnLogin');
        if($('noticebox')) $('noticebox').show();  
      }
    }	
  },
  
  /**
   * download
   */
  download: function(fileId){
    if(typeof(fileId) == 'undefined'){
      return false;
    }
    
    new Ajax.Request('/zoolu-website/download', {
  	  parameters: {
  	    fileId: fileId 
  	  },
	  evalScripts: true,
      onComplete: function(transport) {
        
      }.bind(this)
    });
  },
  
  /**
   * 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();
    }
  } 
});