//
//  application.js
//  trunk
//  
//  Created by Jorge on 2008-10-27.
//  Copyright 2008 Infonova Informática e Comunicacions, S,L.. All rights reserved.
//

var PresentationShow = Class.create();
PresentationShow.prototype = {
  initialize: function(element, options) {
    this.element = $(element);
    this.options = Object.extend({className: 'presentation', duration: 1}, options);
    this.presentations = $$("#"+element+" img."+this.options.className);
    
    this.preparePresentation();
    this.registerCallback();
  },
  
  preparePresentation: function() {
    this.currentPresentation = this.presentations.first();
    this.element.style.position = 'relative';
    
    this.element.style.height = this.presentations.max(function(presentation) {
      var visible = Element.visible(presentation), height;
      
      if (!visible) {
        Element.setStyle(presentation, {position: 'absolute', right: '-800px'});
        Element.show(presentation);
      }
      
      height = Element.getHeight(presentation);
      if (!visible) Element.hide(presentation);
      
      Element.setStyle(presentation, {position: 'absolute', right: '0pt', top: '0pt', zIndex: '1'});
      return height;
    }).toString() + 'px';
  },
  
  nextPresentation: function() {
    return this.presentations[(this.presentations.indexOf(this.currentPresentation) + 1) % this.presentations.length];
  },
  
  registerCallback: function() {
    var miliseconds = 500;
    if(this.currentPresentation == this.presentations.last()){
      var miliseconds = 3000;
    }
    
    window.setTimeout(this.tick.bind(this), this.options.duration * miliseconds);
  },
  
  tick: function() {
    var currentPresentation = this.currentPresentation, nextPresentation = this.nextPresentation();
    
    new Effect.Parallel([
      new Effect.Fade(currentPresentation, {sync: true}),
      new Effect.Appear(nextPresentation, {sync: true})
    ], {
      duration: 2,
      afterFinish: (function(effect) {
        this.currentPresentation = nextPresentation;
        this.registerCallback();
      }).bind(this)
    })
      }
      }
      
      var Navigation = Class.create();
    Navigation.prototype = {
      initialize: function(element, options) {
        this.element = element;
        this.active  = false;
        this.open    = false;
        
        this.sub_nav = this.element.select('ul').first();
        
        if(this.sub_nav) {
          this.prepare();
        }
      },
      prepare: function(){
        this.sub_nav.hide();
        this.element.removeClassName("root");
        Event.observe(this.element, 'mouseover', this.mouseover.bind(this));
        Event.observe(this.element, 'mouseout', this.mouseout.bind(this));
      },
      mouseover: function(event){
        if(!this.active && !this.open){
          this.active = true;
          Effect.BlindDown(this.sub_nav,
                           {
                             afterFinish: (function(effect){
                               if(this.open){
                                 this.sub_nav.hide();
                                 this.open = false;                                
                               }else{
                                 this.open = true;
                               }
                               
                               this.active  = false;                            
                             }).bind(this),
                             duration: "0.4"
                           }
                          );
        }
      },
      mouseout: function(e){
        var minX = this.element.positionedOffset()[0];
        var minY = this.element.positionedOffset()[1];
        var maxX = minX + this.element.getWidth();
        var maxY = minY + this.sub_nav.getHeight()+this.element.getHeight();
        
        if((e.pointerX() <= minX || e.pointerX() >= maxX) || (e.pointerY() <= minY || e.pointerY() >= maxY)){
          if(this.open){
            this.sub_nav.hide();
            this.active  = false;
            this.open    = false;
          }else{
            this.open    = true;
          }
        }
      }
    }
      
      function loadMap(mapa, coordenadas, info) {
      if (GBrowserIsCompatible()) {
      var map = new GMap2($(mapa));
    map.setMapType(G_HYBRID_MAP);
    map.addControl(new GSmallMapControl());
    
    var mapControl = new GMapTypeControl();
    map.addControl(mapControl);
    
    map.setCenter(coordenadas, 16);
    
    var marker = new GMarker(map.getCenter(), {icon:GIcon(G_DEFAULT_ICON)});
    
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(info);
    });
    
    map.addOverlay(marker);
  }
}
  
Event.observe(window, 'load', function() {
  $$("#nav > ul > li").each(function(e){
    new Navigation(e);
  });

  $$('a[href^=http\:\/\/]').each(function(e){
    e.onclick = function(){
      window.open(this.href);
      return false;
    }
  });
    
  $$('html').first().removeClassName('no-js');
  $$('html').first().addClassName('js');
});
