/* --------------------------------------------------------------- 
   Author : Remi Palard
   October 2008
   remi.palard@gmail.com  
--------------------------------------------------------------- */

document.observe('dom:loaded', function() { 
  
  // Get page type
  var js = /main\.js(\?.*)?$/;
  var s = $$('head script[src]').find(function(s) { return s.src.match(js); });  
  var t = s.src.match(/\?.*type=([a-z,]*)/);
  
  // Load per page
  var load = {     
   common : function() {
    img.preload(['menu.png','menuCart.png']);
    carrousel = new UI.Carousel('carrouselBlock',{autoStart: 8,elementSize: 849,previousButton: '.carrouselLeft',nextButton: '.carrouselRight', container: '.carrousel'});
   },
   
   home : function() {    
    spot.show('reunion');
    news.start(8);     
   },
   
   country: function() {
    artisans.startCarrousel();
    artisans.showSpots(0);    
   },
   
   artisan: function() {
    flash.subtitle();
    carrouselArtisan = new UI.Carousel('carrouselArtisanBlock',{elementSize: 749,previousButton: '.carrouselArtisanLeft',nextButton: '.carrouselArtisanRight', container: '.carrouselArtisan'});    
    category.artisan_highlight('cat1');   
   }, 
   
   text: function() {
    flash.title();
   },
   
   product: function() {
    carrouselCategory = new UI.Carousel('carrouselCategoryBlock',{elementSize: 749,previousButton: '.carrouselCategoryLeft',nextButton: '.carrouselCategoryRight', container: '.carrouselCategory'});          
   },
   
   cart: function() {
    
   }
  };
  
  // Load js
  load['common']();
  if (t) load[t[1]]();
});

/* --------------------------------------------------------------- */ 

var spot = {
  selected: false,
  activated: false,
    
  show: function(el) {
    if (!$(el + 'Tag')) return;
    this.activated = true;
    if (this.selected) this.hide();
    this.selected = el; 
    $(el + 'Tag', el + 'Spot').invoke('show');   
  },
   
  hide: function (el) {
    if (! this.selected) return;
    if (! el) el = this.selected;
    $(el + 'Tag', el + 'Spot').invoke('hide');
  },
   
  autoHide: function(el) {
    this.activated = false;
    setTimeout(function() {      
      if (el == this.selected && ! this.activated) this.hide(el);
    }.bind(this),300);    
  },
   
  activate: function() {
    this.activated = true;
  }
};

/* --------------------------------------------------------------- */ 

var flash = {
  title: function() {
    if (typeof sIFR == "function"){
      sIFR.replaceElement('h1', named({sFlashSrc: 'swf/aerofoil.swf', sColor: '#000000',sWmode:'transparent'}));
    };       
  }, 
  subtitle: function() {
    if (typeof sIFR == "function"){
      sIFR.replaceElement('h2', named({sFlashSrc: 'swf/aerofoil.swf', sColor: '#000000',sWmode:'transparent'}));
    };       
  }   
}

/* --------------------------------------------------------------- */ 

var img = {
  URL: './img/',
  preload: function(elements) {
    elements.each(function(el) {
      this.dump = new Image();
      this.dump.setAttribute('src',this.URL + el);   
    }.bind(this));
  }
}

/* --------------------------------------------------------------- */ 

var menu = {
 selected : false,
 active: false, 
 timer : 0,
 timeBeforeAutoHide : 200,
   
 start: function(el) {
  this.active = true;
  
  // Prevent duplicate events
  if (this.selected == el.id) return;
  
  // Remove last element
  if (this.selected) this.hide(this.selected); 
    
  // Highlight current menu  
  this.highlight(el.id);
  
  // Set Selected Menu
  this.selected = el.id;  
  
  // Show menu
  this.show(el.id);
 },
 
 stop: function() {
  this.active = false;
  this.autoHide();
 }, 
    
 autoHide: function() {
   if (this.active) {
    this.timer = 0; 
    return; 
   } 
        
   if(this.timer < this.timeBeforeAutoHide){
    this.timer += 10; 
    setTimeout('menu.autoHide()',9); 
   } else { 
    this.timer = 0; 
    this.hide();     
   }  
 },
 
 hide: function(el) {
  if (! this.selected) return;  
  if (! el) el = this.selected;
  $('menu_' + el).hide();
  $('menuScotch').hide();  
  this.selected = false;
 },

 activate: function() { 
  this.active = true;
 },
 
 highlight: function(el) {
  if (el.indexOf('cart') > -1) return;
  topOffset = 7;
  leftOffset = 30;
  if (el == 'search') leftOffset = 10;
  var t = $(el).cumulativeOffset().top -topOffset;
  var l = $(el).cumulativeOffset().left -leftOffset;  
  $('menuScotch').setStyle({'left':l + 'px','top':t + 'px'}).show();
 },
 
 show: function(el) {  
   var dump = new Effect.SlideDown('menu_' + el, {duration:0.2});
 }
};

/* --------------------------------------------------------------- */		

var tabs = {
 selected : false, 
 start: function(el) {
  
  // Prevent duplicate events
  if (this.selected == el.id) return;
  
  // Remove last element
  if (this.selected) this.hide(this.selected); 
    
  // Highlight current menu  
  this.highlight(el.id);
  
  // Set Selected Menu
  this.selected = el.id;  
  
  // Show menu
  this.show(el.id);
 },
 
 hide: function(el) {
  if (! this.selected) return;  
  if (! el) el = this.selected;
  $(el + 'Content').hide();
  this.lowlight(el);  
 },
 
 lowlight: function(el) {
  $(el).removeClassName('pTabOn');
 }, 
 
 highlight: function(el) {
  $(el).addClassName('pTabOn');
 },
 
 show: function(el) {  
   $(el + 'Content').show();
 }
};

/* --------------------------------------------------------------- */		

var news = {
  nb: 0,
  d : 10,
  nodes : [],
  pe : false,
  
  display: function() { 
    this.last = this.nb;
    this.nb = (this.nb +1 < this.nodes.length) ? this.nb +1 : 0;
    var dump = new Effect.Fade(this.nodes[this.last], { 
      duration : 1.2,
      afterFinish : function() { this.show(); }.bind(this)
    });    
  },
  
  start : function(d) {    
    this.delay = d || this.d;
    
    // Select news
    this.nodes = $('news').select('li');
    
    // Init
    this.nodes.invoke('hide');
    this.show();
    //this.nodes[this.nb].show();
    
    // Start periodical
    var dump = new PeriodicalExecuter(function(pe) {
      this.display();
      this.pe = pe;
    }.bind(this), this.delay);
  }, 
  
  show: function() {
    $(this.nodes[this.nb]).show();     
  },  
  
  _next: function() {
    // Kill periodical updater
    this.pe.stop();       
    if (this.nodes.length <= 1) return;
    this.last = this.nb;
    this.nb = (this.nb +1 < this.nodes.length) ? this.nb +1 : 0;    
    this.nodes[this.last].hide();
    this.nodes[this.nb].show();
  },
  
  _previous: function() {
    // Kill periodical updater
    if(this.pe) this.pe.stop(); 
    if (this.nodes.length <= 1) return;      
    this.last = this.nb;  
    this.nb = (this.nb == 0) ? (this.nodes.length-1): this.nb -1;
    this.nodes[this.last].hide();
    this.nodes[this.nb].show();
  }  
};

/* --------------------------------------------------------------- */		

var category = {
  artisan_highlight: function(el) {
    t = 455;
    leftOffset = 20;
    var l = $(el).cumulativeOffset().left -leftOffset;    
    $('carrouselCategoryImage').setStyle({'left':l + 'px','top':t + 'px'}).show();    
  }
};

/* --------------------------------------------------------------- */		

var product = {
  urlDetails : 'ajax/productDetails.html',
  startLayer: function() {
    
    // Get content
    var dump = new Ajax.Request(this.urlDetails, {
      method: 'post',
      onCreate: function() {},     
      onComplete: function(response) {  
        $('pLayerContent').update(response.responseText);        
        flash.title();                              
      },          
      onFailure: function() {      
        alert('Server error'); 
      }
    });
    
    // Set position
    el = 'carrouselCategoryBlock';
    topOffset = 12;
    leftOffset = -20;
    var t = $(el).cumulativeOffset().top -topOffset;      
    var l = $(el).cumulativeOffset().left -leftOffset;    
    $('productLayer').setStyle({'left':l + 'px','top':t + 'px'}).show();    
  },
  closeLayer: function() {
    $('productLayer').hide();     
  }
};

/* --------------------------------------------------------------- */		

if (!window.console || !window.console.firebugVersion){
  var names = ["log", "debug", "info", "warn", "error","assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd","count", "trace", "profile", "profileEnd"];
  window.console  =  {};
  for (i in names) {
    //window.console[names[i]] = function(){};
  }
} 

/* --------------------------------------------------------------- */ 