// 
//  app.js
//  raw_html
//  
//  Created by Andrey Okonetchnikov on 2010-01-27.
//  Copyright 2010 wollzelle GmbH. All rights reserved.
// 

/**
 * @depends mootools-core.js
 * @depends mootools-more.js
 */
 
var Slider = new Class({
  
  Implements: [Events, Options],
  
  options: {
    loop: true,
    hideOverlayOnScroll: true
  },
  
  initialize: function(options){
    this.setOptions(options);
    
    this.currentSlide = 0;
    this.viewport = $('content-viewport');
    this.paginator = $('paginator');
    if(!$defined(this.viewport) || !$defined(this.paginator)) return;
    
    this.prevContentLink  = $('content_prev');
    this.nextContentLink  = $('content_next');
    this.infoOverlayLink  = $('link-info-overlay');
    this.infoOverlay      = $('info-overlay');
    
    if($defined(this.infoOverlay))
      this.infoOverlay.set('morph', {duration: 500, transition: Fx.Transitions.Cubic.easeInOut}).set('opacity', 0);
    
    this.slides = this.viewport.getElements('.b-slide');
    
    this.scrollFx = new Fx.Scroll(this.viewport);
    
    this.deploy();
  },
  
  deploy: function(){
    // 2010-02-23 andrew: Attach event for info overlay before main init code. Fixes #903
    if($defined(this.infoOverlayLink))
      this.infoOverlayLink.addEvent('click', this.toggleInfoOverlay.bindWithEvent(this));
      
    // 2010-01-29 andrew: Only one page? Then hide the whole paginator and return
    if(this.slides.length == 1) {
      this.paginator.set('display','none');
      return;
    }
    
    // Previous/Next links
    // 2010-01-29 andrew: Don't change those values. They are unicode! :)
    if(Browser.Engine.trident) {
      prevStr = '3'; 
      nextStr = '4';
    } else {
      prevStr = '&#61491;';
      nextStr = '&#61492;';
    }
    prevLnk =   new Element('a', {href: '#prev', html: prevStr, 'class': 'b-paginator__prev', events: { 'click': this.switchBy.bindWithEvent(this, -1) }});
    nextLnk =   new Element('a', {href: '#next', html: nextStr, 'class': 'b-paginator__next', events: { 'click': this.switchBy.bindWithEvent(this,  1) }});
    
    // Attach events for external links (if exist)
    if($defined(this.prevContentLink)) 
      this.prevContentLink.addEvents({
        'click': this.switchBy.bindWithEvent(this, -1)
      });
    
    if($defined(this.nextContentLink)) 
      this.nextContentLink.addEvents({
        'click': this.switchBy.bindWithEvent(this,  1)
      });
      
    this.viewport.addEvents({
      'slide:changed': function(event){
        
        if($defined(this.prevContentLink))
          this.prevContentLink.removeClass('disabled');
        if($defined(this.nextContentLink))
          this.nextContentLink.removeClass('disabled');
          
        if(this.options.hideOverlayOnScroll && $defined(this.infoOverlay)) 
          this.hideInfoOverlay();
          
        switch(event.slide) {
          case 0: // First slide
            if(!this.options.loop && $defined(this.prevContentLink))
              this.prevContentLink.addClass('disabled');
          break;
          case (this.slides.length-1): // Last slide
            if(!this.options.loop && $defined(this.nextContentLink))
              this.nextContentLink.addClass('disabled');
          break;
        }
      }.bindWithEvent(this)
    });
    
    // Generating link elments for every page
    pagesWrapper = new Element('span', {'class': 'b-paginator__pages'}); // Wrapper element for links
    this.pagesLinks = [];
    for(var idx=0; idx < this.slides.length; idx++) {
      pageNum = (idx+1).toString();
      linkEl = new Element('a', { 
        'text': pageNum,
        'href': '#' + pageNum,
        'events': {
          'click': this.switchTo.bindWithEvent(this, idx)
        }
      });
      linkEl.inject(pagesWrapper);
      this.pagesLinks.push(linkEl);
    }
    this.paginator.adopt(prevLnk, pagesWrapper, nextLnk);
    
    this.setActive(0);
  },
  
  switchTo: function(event, idx){
    event.stop();
    this.scrollFx.cancel().toElement(this.slides[idx]);
    this.setActive(idx);
    this.currentSlide = idx;
  },
  
  switchBy: function(event, dir){
    event.stop();
    idx = this.currentSlide + dir;
    
    if(idx > this.slides.length-1)
      idx = this.options.loop ? 0 : (this.slides.length-1);
    else if(idx < 0)
      idx = this.options.loop ? this.slides.length-1 : 0;
    
    this.switchTo(event, idx);
  },
  
  setActive: function(idx){
    this.pagesLinks[this.currentSlide].removeClass('active');
    this.pagesLinks[idx].addClass('active');
    this.viewport.fireEvent('slide:changed', { 'slide': idx });
  },
  
  toggleInfoOverlay: function(event){
    event.stop();
    if(!$(this.infoOverlayLink) || !$(this.infoOverlay)) throw "Required element not found";
    
    if(this.infoOverlay.hasClass('active')) {
      this.hideInfoOverlay();
    } else {
      this.showInfoOverlay();
    }
      
  },
  
  showInfoOverlay: function(){
    if(!$(this.infoOverlayLink) || !$(this.infoOverlay)) throw "Required element not found";
    var morphTo = '.overlay-short'; 
    if (this.infoOverlay.hasClass('overlay-long')) morphTo = '.overlay-long';
    if (this.infoOverlay.hasClass('overlay-long-wide')) morphTo = '.overlay-long-wide';
    this.infoOverlay.setStyle('display', 'block').morph(morphTo).addClass('active');
    this.infoOverlayLink.addClass('active');
  },
  
  hideInfoOverlay: function(){
    if(!$(this.infoOverlayLink) || !$(this.infoOverlay)) throw "Required element not found";
    
    this.infoOverlay.morph({
      'height': 0,
      'width': 0,
      'opacity': 0
    }).removeClass('active');
    this.infoOverlayLink.removeClass('active');
  }
  
  
});
 
var Interrupter = new Class({

 Implements: [Events, Options],

 options: {
   delay: 20000
 },

 initialize: function(options){
   this.setOptions(options);
   var _this = this;

   this.setTimer();

   document.addEvents({
     'mousemove': this.setActive.bindWithEvent(_this),
     'state:active': this.setTimer.bindWithEvent(_this)
   });
 },

 setTimer: function(){
   this.timer = this.setIdle.periodical(this.options.delay, this);
 },

 setIdle: function(){
   $clear(this.timer);
   document.fireEvent('state:idle');
 },

 setActive: function(){
   $clear(this.timer);
   document.fireEvent('state:active');
 }
});

var GMaps = {
  
	geoPoints: new Array(),
  
  initialize: function(){
    this.map = new GMap2($('g-maps'));
		this.map.disableDragging();
		this.map.setMapType(G_PHYSICAL_MAP);
    this.map.setCenter(new GLatLng(47.50000,15.148773), 7);
    this.map.savePosition();		

		var geocoder = new GClientGeocoder();
		
		$$('.vcard .adr').each(function(address, idx){
		  address.getElement('.onmap').set('id', 'office_' + idx);
		  var lat = address.getElement('.latitude').get('title');
		  var lng = address.getElement('.longitude').get('title');
		  this.addPointsToMap(lat, lng);
	  }.bind(this));
  },
  
  addPointsToMap: function(lat, lng) {
    var point = new GLatLng(lat, lng);    
    var marker = new GMarker(point);
    var _this = this;

    this.map.addOverlay(marker);    
    this.geoPoints.push(point);
    
    GEvent.addListener(marker, 'click', function(point){
      _this.map.showMapBlowup(point, { mapType: G_NORMAL_MAP });
    });
    
    GEvent.addListener(this.map, 'infowindowclose', function(){
      _this.map.returnToSavedPosition();
    });
    
    $('office_' + this.geoPoints.indexOf(point)).addEvents({
			'click': function(event){
				event.stop();
				_this.map.showMapBlowup(_this.geoPoints[this.id.charAt(this.id.length - 1)], { mapType: G_NORMAL_MAP });
			}
		});
  }
  
};

// ===================
// = Initializiation =
// ===================

(function(){

  window.addEvent('domready', function(){
    var mySlider = new Slider({loop: false});
    if ($chk($('g-maps'))) GMaps.initialize();
  });

})();

