/**
 * 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;
    
    this.formInputs = new Array(); 
    this.hash = '';
    this.INET_OVERLAY_COOKIE = 'inet_ovelay';
    
    this.tmpJobProfile = '';
  },  
  
  getOverlayContent: function(){
    new Ajax.Request('/zoolu-website/content/overlay', {
      onSuccess: function(transport) {
        result = transport.responseText.evalJSON();
        if ($('overlayText')) $('overlayText').innerHTML = result.overlay;
        myDefault.hash = result.hash;
        myDefault.checkCookies();
      }
    });
  },
  
  checkCookies: function(){
    if($('overlayText').innerHTML != ''){
    	if (!(this.getCookie(this.INET_OVERLAY_COOKIE) == this.hash)) {
    	  $('overlayBlue').show();
    	  $('wrapper').style.marginBottom = '40px';
    	}
    }
  },
  
  closeOverlay: function() {
	  $('overlayBlue').hide();
	  this.setCookie(this.INET_OVERLAY_COOKIE, this.hash);
  },
  
  getCookie: function(name){
	   var i=0;  //Suchposition im Cookie
	   var suche = name+"=";
	   while (i<document.cookie.length){
	      if (document.cookie.substring(i, i+suche.length)==suche){
	         var ende = document.cookie.indexOf(";", i+suche.length);
	         ende = (ende>-1) ? ende : document.cookie.length;
	         var cook = document.cookie.substring(i+suche.length, ende);
	         return unescape(cook);
	      }
	      i++;
	   }
	   return null;
  },
  
  setCookie: function(name, wert){
	   var cook = name+"="+wert+"; path=/";
	   document.cookie = cook;
  },
    
  /**
   * showTextBlockInfo
   */
  showTextBlockInfo: function(counter){
    if($('block_'+counter)){
      $('desc_'+counter).show();
    }
  },
  
  
  /**
   * showTextBlockInfo
   */
  hideTextBlockInfo: function(counter){
    if($('block_'+counter)){
      $('desc_'+counter).hide();
    }
  },
  
  
  /**
   * toggleElement
   */
  toggleElement: function(elementId, dblDuration, blnSlide, elementScrollTo){
    if(typeof(dblDuration) == 'undefined'){
      dblDuration = 0.2;   
    }
    if(typeof(blnSlide) == 'undefined'){
    	blnSlide = true;   
    }
    if(typeof(elementScrollTo) == 'undefined'){
      elementScrollTo = '';   
    }
    if($(elementId) && blnSlide == true){
      if($(elementId).style.display == 'none'){
        Effect.SlideDown(elementId, {duration: dblDuration});
        if($(elementScrollTo)){
          new Effect.ScrollTo(elementScrollTo);
        }
        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($(elementScrollTo)){
          new Effect.ScrollTo(elementScrollTo);
        }
        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();
  	  }
  	} 
  },
  
  /**
   * inputFormat
   */
  inputFormat: function(inputElement) {
    if(this.formInputs[inputElement.id] == null) {
      this.formInputs[inputElement.id] = inputElement.value;
      inputElement.value=''; 
      inputElement.style.color='#333';
      if(inputElement.style.textAlign != 'left'){
        inputElement.style.textAlign = 'left';
      }
    }
  },
  
  /**
   * 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;
  	      
  	      var newsletterUrl = '/zoolu-website/datareceiver/subscribe';
  	      
  	      new Ajax.Request(newsletterUrl, {
  	        method: 'post',
  	    	  parameters: {
    	    	  email: $F('mail'),
    	    	  Salutation: $F('salutation'),
    	    	  Firstname: $F('fname'),
    	    	  Surname: $F('sname')
  	        },
  	  	    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();
  	  }
    }
  },
  
  /**
   * submitCareerForm
   */
  submitCareerForm: function(formId){
    if($(formId)){      
      /**
       * validation
       */
      this.retValue = true;
      if ($('position').checked) {
    	this.validateInput('profileDescription');
      }
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('address');
      this.validateInput('plz');
      this.validateInput('city');
      this.validateInput('phone');
      this.validateInput('mail');
      this.validateInputEmail('mail');
      
      $$('.mandatory-radio').each(function(element){
        this.validateRadiobutton(element);
      }.bind(this));
      
      if(this.retValue == true) {
        if($('noticebox')) $('noticebox').hide();
        this.addBusyClass('bttnSend');
        
        /**
         * serialize form
         *
        var serializedForm = $(formId).serialize();
          
        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)
        });*/
        $(formId).submit();
      }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');
      this.validateInput('preselection');
      
      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();
      }
    }
  },
  
  /**
   * sendRecommendation
   */
  sendRecommendation: function(formId){
    if($(formId)){
      /**
       * validation
       */
      this.retValue = true;
      
      this.validateInput('mail_to');
      this.validateInputEmail('mail_to');
      this.validateInput('mail_from');
      this.validateInputEmail('mail_from');
      this.validateInput('name_to');
      this.validateInput('name_from');
      
      if(this.retValue == true) {
        if($('noticebox')) $('noticebox').hide();
        if($('bttnSend')) $('bttnSend').onclick = new Function("return false"); 
       
        /**
         * serialize form
         */
        var serializedForm = $(formId).serialize();
       
        new Ajax.Request($(formId).readAttribute('action'), {
          parameters: serializedForm,
          evalScripts: true,
          onComplete: function(transport) {
            if($('recommendationInner')) $('recommendationInner').hide();
            if($('bttnRecommendation')) $('bttnRecommendation').hide();
            if($('succesbox')) $('succesbox').appear();
          }.bind(this)
        });
      }else{
        if($('noticebox')) $('noticebox').show();
      }
    }
  },
  
  /**
   * login
   */
  login: function(){
    if($('loginForm')){
      /**
       * validation
       */
      this.retValue = true;
        
      this.validateInput('salutation', 'please_choose');
      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();
    	
  	    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;
  	      
  	      var newsletterUrl = '/zoolu-website/datareceiver/subscribe';
  	    
  	      new Ajax.Request(newsletterUrl, {
  	        method: 'post',
  	    	  parameters: {
    	    	  email: $F('mail'),
    	    	  Salutation: $F('salutation'),
    	    	  Firstname: $F('fname'),
    	    	  Surname: $F('sname')
  	        },
  	  	    evalScripts: true,
  	  	    onComplete: function(transport) { 
  	  	      this.reload(serializedForm);
  	  	    }.bind(this)
  	  	  });  	    
  	    } else {
  	    	this.reload(serializedForm);
  	    }        
      }else{
        this.removeBusyClass('bttnLogin');
        if($('noticebox')) $('noticebox').show();  
      }
    }	
  },  
  
  /**
   * reload
   */
  reload: function(serializedForm){
    new Ajax.Request($('loginForm').readAttribute('action'), {
      parameters: serializedForm,
		  evalScripts: true,
		  onComplete: function(transport) {
    	  if(transport.responseText == true || transport.responseText == 'true'){
    		  window.location.href = window.location.href+'#download';
    		  return window.location.reload();
    	  }
      }.bind(this)
    });  
  },
  
  /**
   * 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');
    }
  },
  
  /**
   * validateRadiobutton
   */
  validateRadiobutton: function(element) {
    var checked = $$('input:checked[type="radio"][name="'+element.name+'"]').pluck('value').length;
    if(!checked){
      this.retValue = false;
      if (this.firstMissingElem == '') {
        this.firstMissingElem = element;
      }
      if($('lbl_'+element.id)) $('lbl_'+element.id).addClassName('missing');
    }else{      
      if($('lbl_'+element.id)) $('lbl_'+element.id).removeClassName('missing');
    }
  },
  
  
  /**
   * 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();
    }
  },
  
  /**
   * getWindowHeight
   */
  getWindowHeight: function(win) {
  if (win == undefined) win = window;
	if (win.innerHeight) {
	  return win.innerHeight;
	} else {
	  if (win.document.documentElement && win.document.documentElement.clientHeight) {
	    return win.document.documentElement.clientHeight;
	  }
	  return win.document.body.offsetHeight;
	}
  },
  
  /**
   * resize
   */
  resize: function(){
    $('errorWrapper').style.minHeight = (myDefault.getWindowHeight()-30) + 'px';
  },
  
  /**
   * toggleProfileDesc
   */
  toggleProfileDesc: function() {
	if ($('position').checked) {
	  $('profileDescription').value = this.tmpJobProfile;
	  $('itemProfileDescription').show();
	} else {
	  this.tmpJobProfile = $F('profileDescription');
	  $('itemProfileDescription').hide();
	  $('profileDescription').value = '';
	}
  },
  
  /**
   * toggleCareernightForm
   */
  toggleCareernightForm: function() {
    if ($('radio_group').checked) {
      $('careerNightGroup').show();
      $('careerNightSinglePerson').hide();
    }
    if ($('radio_single').checked) {
      $('careerNightSinglePerson').show();
      $('careerNightGroup').hide();
    }
  },
  
  checkEmailRepeat: function(element,element2) {
    if($(element) && $(element2)) {
      if($F(element) != $F(element2) || $F(element).blank() || $F(element2).blank()){
        $(element).addClassName('missinginput');
        $(element2).addClassName('missinginput');
        this.retValue = false;
      }else{
        $(element).removeClassName('missinginput');
        $(element2).removeClassName('missinginput');
      }
    }
  },
  
  /**
   * expireCache
   */
  expireCache: function(elId){
    this.addBusyClass(elId, false);
    new Ajax.Request('/zoolu-website/content/expire-cache', {
      method: 'get',
      evalScripts: true,
      onComplete: function(transport) {
        this.removeBusyClass(elId, false);
        window.location.href = window.location.href;
      }.bind(this)
    });
  },  
  
  /**
   * submitCareerNightGroupForm
   */
  submitCareerNightForm: function(formId){
     if($(formId)){      
       this.retValue = true;

       if(formId == 'single') {
         this.validateInput('fname');
         this.validateInput('sname');
         this.validateInput('mail');
         this.validateInputEmail('mail');
         this.checkEmailRepeat('mail','mail2');
       }
       
       if(formId == 'group') {
         this.validateInput(formId+'_companyname');
         this.validateInput(formId+'_numberofpersons');
         this.validateInput(formId+'_fname');
         this.validateInput(formId+'_sname');
         this.validateInput(formId+'_mail');
         this.validateInputEmail(formId+'_mail');
         this.checkEmailRepeat(formId+'_mail',formId+'_mail2');
       }
       
       if(this.retValue == true) {
         //if($('noticebox')) $('noticebox').hide();
         $(formId).submit();
       }else{
         //if($('noticebox')) $('noticebox').show(); 
       }
     }     
  }
  
});
