/**
 * 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.retValue = false;
    this.ge = null;    
    
    /**
     * address field (Ort oder PLZ)
     */    
    if($('address')) {    	
      if($F('address') != '' && $('address').value == 'Ort oder PLZ'){
        $('address').setStyle({color: '#ccc'});	
      }
    	
      $('address').observe('focus', function(){ 
        if($F('address') != '' && $('address').value == 'Ort oder PLZ'){
        	$('address').value = '';
        	$('address').setStyle({color: '#333'});
        }
      }.bind(this));
      
      $('address').observe('blur', function(){ 
    	if($('address').value == ''){
    		$('address').setStyle({color: '#ccc'});
          $('address').value = 'Ort oder PLZ';  	
        }    
      }.bind(this));
      
      /**
       * 'enter' key search
       */
       $('address').observe('keydown', function(event){
         if(event.keyCode == 13){
           this.searchRealEstate();
         }    
       }.bind(this));
    }   
  },
  
  /**
   * toggleElement
   */
  toggleElement: function(elementId, dblDuration){
    if(typeof(dblDuration) == 'undefined'){
      dblDuration = 0.2;   
    }
    if($(elementId)){
      if($(elementId).style.display == 'none'){
        Effect.SlideDown(elementId, {duration: dblDuration});
        if('propAdvSearch') {          	
          if($('propAdvSearchLink')) $('propAdvSearchLink').innerHTML = 'Einfache Suche';
          if($('advanced')) $('advanced').value = 1;
        }
      }else{
        Effect.SlideUp(elementId, {duration: dblDuration});
        if('propAdvSearch') {
          if($('propAdvSearchLink')) $('propAdvSearchLink').innerHTML = 'Erweiterte Suche';
          if($('advanced')) $('advanced').value = 0;
        }
      }
    }
  },
  
  /**
   * toggleNavPortals
   */
  toggleNavPortals: function(){
    if($('businessImg') && $('businessDiv')){
      var strImgSrc = $('businessImg').getAttribute('src');
      if(strImgSrc.indexOf('off') == -1 && $('businessDiv').style.display != 'none'){
      	$('businessImg').setAttribute('src', '/website/themes/dietrich-bau/images/navigation/geschaeftsfelder_off.gif');
      	$('businessDiv').hide();
      }else{
      	$('businessImg').setAttribute('src', '/website/themes/dietrich-bau/images/navigation/geschaeftsfelder_on.gif');
      	$('businessDiv').show();
      }
    }  
  },
  
  /**
   * searchRealEstate
   */
  searchRealEstate: function(){
    if($('propSearchForm')){
  	  if($F('address') == 'Ort oder PLZ'){
  	    $('address').value = '';  
  	  }
  	  if($F('advanced') == 0){
    		$('typ').value = '';
    		$('rooms').value = '';
    		$('country').value = '';
  	  }
      $('propSearchForm').submit();
    }  
  },
  
  /**
   * setAdvancedSearch 
   */
  setAdvancedSearch: function(value, exceptElement){
    $('advanced').value = value;
    if(exceptElement != 'typ'){
      $('typ').value = '';
    }
    if(exceptElement != 'rooms'){
      $('rooms').value = '';
    }
    if(exceptElement != 'country'){
      $('country').value = '';
    }
  },
  
  /**
   * toggleMapHeader
   */
  toggleMapHeader: function(minHeight, maxHeight){
    if(typeof(minHeight) == 'undefined') minHeight = 340;
    if(typeof(maxHeight) == 'undefined') maxHeight = 540;
	  
    if($('header').offsetHeight > minHeight){
    	$('headerGMap').setStyle({height: minHeight-1+'px' }); 
    	$('header').setStyle({height: minHeight+'px' });
    	
    	if($('mapStrapIcon')){
    		$('mapStrapIcon').removeClassName('pointerUp'); 
    		$('mapStrapIcon').addClassName('pointerDown');
    	}
    	if($('mapStrapText')) $('mapStrapText').innerHTML = 'Ansicht vergr&ouml;&szlig;ern';
    	if($('headlineMap')) $('headlineMap').setStyle({ top: '-270px'});
    	
    	//myGMap.showAdresses();
    	
    }else{
    	$('headerGMap').setStyle({height: maxHeight-1+'px' }); 
    	$('header').setStyle({height: maxHeight+'px' });
    	
    	if($('mapStrapIcon')){
    		$('mapStrapIcon').removeClassName('pointerDown'); 
    		$('mapStrapIcon').addClassName('pointerUp');
    	}
    	if($('mapStrapText')) $('mapStrapText').innerHTML = 'Ansicht verkleinern';
    	if($('headlineMap')) $('headlineMap').setStyle({ top: '-470px'});
    	
    	//myGMap.showAdresses();
    }	  
  },
  
  /**
   * toggleSingleMapHeader
   */
  toggleSingleMapHeader: function(minHeight, maxHeight){
    if(typeof(minHeight) == 'undefined') minHeight = 155;
    if(typeof(maxHeight) == 'undefined') maxHeight = 540;
	  
    if($('header').offsetHeight > minHeight){
    	$('headerGMap').setStyle({height: minHeight-1+'px' }); 
    	$('header').setStyle({height: minHeight+'px' });
    	
    	if($('mapStrapIcon')){
    		$('mapStrapIcon').removeClassName('pointerUp'); 
    		$('mapStrapIcon').addClassName('pointerDown');
    	}
    	if($('mapStrapText')) $('mapStrapText').innerHTML = 'Ansicht vergr&ouml;&szlig;ern';
    	if($('headlineMap')) $('headlineMap').setStyle({ top: '-150px'});
    	
    	//myGMap.showSingleAddress('headerGMap');
    	
    }else{
    	$('headerGMap').setStyle({height: maxHeight-1+'px' }); 
    	$('header').setStyle({height: maxHeight+'px' });
    	
    	if($('mapStrapIcon')){
    		$('mapStrapIcon').removeClassName('pointerDown'); 
    		$('mapStrapIcon').addClassName('pointerUp');
    	}
    	if($('mapStrapText')) $('mapStrapText').innerHTML = 'Ansicht verkleinern';
    	if($('headlineMap')) $('headlineMap').setStyle({ top: '-535px'});
    	
    	//myGMap.showSingleAddress('headerGMap');
    }	  
  },
  
  /**
   * openGEarth
   */
  openGEarth: function(){
    if($('overlayContainer')){
      this.putCenter('overlayContainer');
      $('overlayContainer').show();

	  myGMap.initEarth();
    }	  
  },
  
  /**
   * openVideo
   */
  openVideo: function(){
	if($('overlayContainerVVO')){
	  this.putCenter('overlayContainerVVO');
	  $('overlayContainerVVO').show();
	}  
  },
  
  /**
   * close
   */
  close: function(elementId){
    if($(elementId)) $(elementId).hide();	  
  },
   
  /**
   * imgGalleryShowAll
   */
  imgGalleryShowAll: function(elementHide){
    if($('divImageGallery')){
      var images = $('divImageGallery').innerHTML;
      $('divImageGallery').remove();
      if($(elementHide)) $(elementHide).hide();
      new Insertion.After($(elementHide), images);
    }
  },
  
  /**
   * submitForm
   */
  submitForm: function(formId){
    if($(formId)){
      
      /**
       * validation
       */
      this.retValue = true;
       
      this.validateInput('fname');
      this.validateInput('sname');
      this.validateInput('phone');
      this.validateInput('mail');
      this.validateInputEmail('mail');
      
      if(this.retValue == true) {
        this.addBusyClass('sendButton');
        
        /**
         * serialize form
         */
        var serializedForm = $(formId).serialize();
        
        new Ajax.Request($(formId).readAttribute('action'), {
          parameters: serializedForm,
          evalScripts: true,
          onComplete: function(transport) {         
            this.removeBusyClass('sendButton');         
            if($(formId)) $(formId).hide();
            if($('succesMsg')){             
              $('succesMsg').appear();
            }
            setTimeout('$(\'overlayContactForm\').hide();', 3000);
          }.bind(this)
        });
      }else{
        this.removeBusyClass('sendButton');
      }
    }
  },
  
  /**
   * validateInput
   */
  validateInput: function(element, baseValue) {
    if(($(element) && $F(element).blank()) || $F(element) == baseValue){
      $(element).addClassName('missinginput');
      if($($(element).id+'Label')) $($(element).id+'Label').addClassName('missing'); 
      this.retValue = false;
    }else{
      $(element).removeClassName('missinginput');
      if($($(element).id+'Label')) $($(element).id+'Label').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');
        if($($(element).id+'Label')) $($(element).id+'Label').addClassName('missing'); 
        this.retValue = false;
      }else{
        $(element).removeClassName('missinginput');
        if($($(element).id+'Label')) $($(element).id+'Label').removeClassName('missing');
      }
    }
  },
  
  /**
   * 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)
    });
  },
  
  /**
   * toggleContactForm
   */
  toggleContactForm: function(linkElement, overlayElement){
    if($(linkElement)){
      var dimensions = $(linkElement).getDimensions();
      var width = dimensions.width;
      var height = dimensions.height;
      var pos = $(linkElement).cumulativeOffset();
      
      this.resetContactForm();
      
      if($(overlayElement)){
        if($(overlayElement).style.display != 'none'){
          $(overlayElement).hide();    
        }else{
          $(overlayElement).show();          
          if(Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined")){
            $(overlayElement).setStyle({top: (pos[1]-320)+'px', left: (pos[0]+105)+'px'}); 
            alert('ie6');
          }else{
            $(overlayElement).setStyle({top: (pos[1]-338)+'px', left: (pos[0]+85)+'px'});  
          }          
        }
      }
    }
  },
  
  /**
   * resetContactForm
   */
  resetContactForm: function(){
    if($('contactform')){
      $('contactform').show();
      if($('succesMsg')) $('succesMsg').hide();
      if($('fname')) $('fname').value = '';
      if($('sname')) $('sname').value = '';
      if($('phone')) $('phone').value = '';
      if($('mail')) $('mail').value = '';
      if($('message')) $('message').value = '';
      if($('phoneMe')) $('phoneMe').checked = false;
    }
  },
  
  /**
   * putCenter
   */
  putCenter: function(item) {
    item = $(item);
    var xy = item.getDimensions();
    var win = this.windowDimensions();
    var scrol = this.scrollOffset();
    item.style.left = (win[0] / 2) - (xy.width / 2) + "px";
    item.style.top = (win[1] / 2) - (xy.height / 2) + "px";
  },

  /**
   * fullScreen
   */
  fullScreen: function(item) {
    item = $(item);
    var win = this.windowDimensions();
    var scroll = this.scrollOffset();
    item.style.height = scroll[1] + win[1] + "px";
  },

  /**
   * windowDimensions
   */
  windowDimensions: function() {
    var x, y;
    if (self.innerHeight) {
      // all except Explorer
      x = self.innerWidth;
      y = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      // Explorer 6 Strict Mode
      x = document.documentElement.clientWidth;
      y = document.documentElement.clientHeight;
    } else if (document.body) {
      // other Explorers
      x = document.body.clientWidth;
      y = document.body.clientHeight;
    }
    if (!x) x = 0;
    if (!y) y = 0;
    arrayWindowSize = new Array(x,y);
    return arrayWindowSize;
  },

  /**
   * scrollOffset
   */
  scrollOffset: function() {
    var x, y;
    if (self.pageYOffset) {
      // all except Explorer
      x = self.pageXOffset;
      y = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      // Explorer 6 Strict
      x = document.documentElement.scrollLeft;
      y = document.documentElement.scrollTop;
    } else if (document.body) {
      // all other Explorers
      x = document.body.scrollLeft;
      y = document.body.scrollTop;
    }
    if (!x) x = 0;
    if (!y) y = 0;
    arrayScrollOffset = new Array(x,y);
    return arrayScrollOffset;
  },
  
  /**
   * 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();
    }
  }
});