/**
 * default.js
 *
 * Version history (please keep backward compatible):
 * 1.0, 2010-05-04: Michael Trawetzky
 *
 * @author Michael Trawetzky <mtr@massiveart.com>
 * @version 1.0
 */

Default = Class.create({

  initialize: function() {
	  this.map = null;
    this.geocoder = null;
    this.marker = null;
	
    this.reValue = false;
    this.language = 'de';
    
    this.bannerCounter = 1;
    this.bannerDirection = 'right';
    this.bannerStop = false;
    
    this.currElement = '';
    this.newElement = '';
  },
  
  init: function(language){
    if(typeof(language) != 'undefined') this.language = language;
	  if($('slidePrev')) $('slidePrev').observe('click', function(){ this.bannerStop = true; }.bind(this));
    if($('slideNext')) $('slideNext').observe('click', function(){ this.bannerStop = true; }.bind(this));
    
    this.currElement = 'slideshow_' + this.language + '_' + this.bannerCounter;
    this.newElement = 'slideshow_' + this.language + '_' + this.bannerCounter;
    
    setTimeout('myDefault.bannerCarousel()', 3000);
  },
  
  /**
   * initGmap
   */
  initGmap: function(){
    if($('locationMap')){
      if(GBrowserIsCompatible()) {
        this.map = new GMap2($('locationMap'));
        var center = new GLatLng(47.22982711058905, 9.897308349609375);
        this.map.setCenter(center, 7);
        this.geocoder = new GClientGeocoder();
      }
    }
  },
  
  /**
   * showAddress
   */
  showAddress: function(address) {
    if(typeof(address) != 'undefined') {
      if (this.geocoder) {
        var myMap = this.map;
        var myMaker = this.marker;
        this.geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              myMap.setCenter(point, 14);
              myMaker = new GMarker(point);
              myMap.addOverlay(myMaker);
            }
          }
        );
      }
    }
  },
  
  /**
   * bannerCarousel
   */
  bannerCarousel: function(){
    if(this.bannerStop == false){
      if($('slideshow_' + this.language + '_' + this.bannerCounter)){
	      if(this.bannerDirection == 'right' && !($('slideshow_' + this.language + '_' + (this.bannerCounter+1)))){
	        this.bannerDirection = 'left';
	      }
	      else if(this.bannerDirection == 'left' && !($('slideshow_' + this.language + '_' + (this.bannerCounter-1)))){
	        this.bannerDirection = 'right';
	      }
	      this.slideShow(this.bannerDirection);
	      setTimeout('myDefault.bannerCarousel()', 3000); 
	    } 
    }
  },
  
  /**
   * 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();  
      }
    }
  },
  
  /**
   * slideShow
   */
  slideShow: function(direction){
    if(typeof(direction) == 'undefined') direction = 'right';
    currElement = 'slideshow_' + this.language + '_' + this.bannerCounter;
    if(direction == 'left'){      
      this.bannerSlideLeft(currElement, 'slideshow_' + this.language + '_' + (this.bannerCounter-1));
    }else{
      this.bannerSlideRight(currElement, 'slideshow_' + this.language + '_' + (this.bannerCounter+1));
    } 
  },  
  
  /**
   * bannerSlideRight
   */
  bannerSlideRight: function(currElement, newElement, blnAjax){
	if(typeof(blnAjax) == 'undefined') blnAjax = false;
	if($(newElement)){
		this.currElement = currElement;
		this.newElement = newElement;
		//if(parseInt($(newElement).style.left) <= 399 && parseInt($(newElement).style.left) > 0){
        /*new Effect.Move(newElement, { x: 0, y: 0, mode: 'absolute', duration: 0.6 });
        new Effect.Move(currElement, { x: -399, y: 0, mode: 'absolute', duration: 0.6 });*/
    	$(currElement).fade({ duration: 0.4 });    	
    	$(newElement).appear({ duration: 0.4 });
    	$('slideshow_'+(this.bannerCounter+1)+'_pic').addClassName('selected');
    	$('slideshow_'+this.bannerCounter+'_pic').removeClassName('selected');
        this.bannerCounter++;
      //}
    }else if(blnAjax){
      /*var newElementContainer = '<img id="" style="left:-399px;" src="" alt=""/>';
      new Insertion.Before(this.currEventCalListId, newElementContainer);*/
    }
  },
  
  /**
   * bannerSlideLeft
   */
  bannerSlideLeft: function(currElement, newElement, blnAjax){
	if(typeof(blnAjax) == 'undefined') blnAjax = false;	
	if($(newElement)){
		this.currElement = currElement;
		this.newElement = newElement;
		//if(parseInt($(newElement).style.left) >= -399 && parseInt($(newElement).style.left) < 0){
        /*new Effect.Move(newElement, { x: 0, y: 0, mode: 'absolute', duration: 0.6 });
        new Effect.Move(currElement, { x: 399, y: 0, mode: 'absolute', duration: 0.6 });*/
        $(currElement).fade({ duration: 0.4 });        
    	$(newElement).appear({ duration: 0.4 });
    	$('slideshow_'+(this.bannerCounter-1)+'_pic').addClassName('selected');
    	$('slideshow_'+this.bannerCounter+'_pic').removeClassName('selected');
        this.bannerCounter--;
      //}
    }else if(blnAjax){
    	/*var newElementContainer = '<img id="" style="left:399px;" src="" alt=""/>';
        new Insertion.After('', newElementContainer);*/
        
        // ajax load image
        /*if($(this.newEventCalListId)){                    
          this.addBusyClass('', true);
          new Ajax.Updater(this.newEventCalListId, '/zoolu-website/event/list', {
            parameters: { 
              
            },
            insertion: Insertion.Bottom,
            evalScripts: true,
            onComplete: function() {         
              this.removeBusyClass('');
            }.bind(this)
          });
        }*/
    }
  },
  
  /**
   * selectSlide
   */
  selectSlide: function(idNumber){
	//alert('selectSlide: add - slideshow_'+idNumber+'_pic // remove - slideshow_'+this.bannerCounter+'_pic');
	if($('slideshow_'+idNumber+'_pic')){
      if($(this.currElement)){
    	this.bannerStop = true;
    	if(idNumber != this.bannerCounter){
          $('slideshow_'+idNumber+'_pic').addClassName('selected');
          $('slideshow_'+this.bannerCounter+'_pic').removeClassName('selected');
          this.slideToElement(idNumber);	
    	}
      }		
	}
	
  },
  
  /**
   * slideToElement
   */
  slideToElement: function(counter){
	//alert('slideToElement: fade - '+this.newElement + ' // appear: slideshow_'+this.language+'_'+counter);
	if($('slideshow_'+this.language+'_'+counter)){
      $(this.newElement).fade({ duration: 0.6 });        
	  $('slideshow_'+this.language+'_'+counter).appear({ duration: 0.6 });
	  this.newElement = 'slideshow_'+this.language+'_'+counter;
	  this.bannerCounter = counter;	
	}
	//alert(this.bannerCounter);
  }, 
  
  /**
   * imgGalleryShowAll
   */
  imgGalleryShowAll: function(elementHide){
    if($('div_image_gallery')){
      var images = $('div_image_gallery').innerHTML;
      $('div_image_gallery').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();
	      
	      /**
	       * serialize form
	       */
	      var serializedForm = $(formId).serialize();
	      
	      new Ajax.Request($(formId).readAttribute('action'), {
	        parameters: serializedForm,
	        evalScripts: true,
	        onComplete: function(transport) { 
	          this.toggleElement('eventFormContainer', 0.5);
	          if($('succesbox')){
	            $('succesbox').show();
	            new Effect.Highlight('succesbox', { startcolor: '#ffff99', endcolor: '#ffffff' });
	            setTimeout('$("succesbox").fade()', 1500);
	          }    
	        }.bind(this)
	      });
	    }else{
          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');
    }
  },
  
  /**
   * 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();
    }
  }
  
});