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

var ROOT_URL = '/english/';
var FLASH_URL = ROOT_URL + 'medias/new-home/';
var LANGUAGE = 'fr';

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

$(document).observe("dom:loaded", function() {	
  flash.start();  
  news.start(); 
  if (Prototype.Browser.IE6) supersleight.run();
});

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

// Flash
var flash = {  
  plugin: 'Shockwave Flash',
  start: function() {
    // Check flash version
    if (this.checkPlugin()) {
      // Patch for ie7 - swfobject - bug      
      if ($('flashObjectCenter')) $('flashObjectCenter').show();
      if ($('flashObjectRight')) $('flashObjectRight').show();      
    // Start flash elements   
    this.main();
    this.ads();
    // Hide robots block
    if ($('lightPage')) $('lightPage').hide();
    }
  },
  checkPlugin: function() {
    var check = DetectFlashVer(8,0,0); // cf AC_OETags.js
    return check;
  },
  ads : function() {    
    var _f = 'events.swf';
    var params = { urlBase: ROOT_URL, urlXml: ROOT_URL + 'xml/new-home-structure.xml' };
    swfobject.embedSWF(FLASH_URL + _f, 'flashRightReplacement', '160', '160','8.0.0',false,params);    	
  },
  main: function() {
    var _f = 'main.swf';    
    var params = { 
      urlBase:ROOT_URL,

      urlXml: ROOT_URL + 'xml/new-home-structure.xml',
      first: 8643,
      range: 280,
      debut: 20,
      fin: 22.5   
    };
    swfobject.embedSWF(FLASH_URL + _f, 'flashReplacement', '623', '293','8.0.0',false,params);        
  }
};

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

var news = {
  nb: 0,
  delay : 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() {    
    // Select news
    this.nodes = $('news').select('li');
    
    // Init
    this.nodes.invoke('hide');
    this.show(0);
    //this.nodes[this.nb].show();
    
    // Start periodical
    var dump = new PeriodicalExecuter(function(pe) {
      this.display();
      this.pe = pe;
    }.bind(this), this.delay);
  }, 
  show: function(i) {
    if(! isset(i)) i = 1;
    $(this.nodes[this.nb]).show();  
    if (i > 0 && ! Prototype.Browser.IE6) {
      new Effect.Pulsate(this.nodes[this.nb].down(), { 
        pulses: 2, 
        duration: 1.2         
      });     
    }
  },  
  _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 cite = {
  on : 'cite-on',
  off : 'cite-off',
  link: '',
  timeBeforeAutoHide : 3000,  
  activated: false,
  show: function(link) {
    this.link = Element.identify(link);
    this.activate();  
    $(this.off).hide();  
    new Effect.Appear(this.on,{duration: 0.5}); 
    $(this.link).blur();
  },
  hide: function() {
    if (this.activated) return;
    this.quickHide();     
  },    
  activate: function () {
    this.activated = true;
  },
  stop: function() {
    this.activated = false;  
    setTimeout(function(){
      this.hide();
    }.bind(this),this.timeBeforeAutoHide);  
  },
  quickHide: function() {
    new Effect.Parallel(
    [
      new Effect.Fade(this.on), 
      new Effect.Appear(this.off)
    ], { 
      duration: 0.8
    });        
    $(this.link).blur();     
  }
};

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

function goto(URL) {
 setTimeout( function() { window.location.href = URL; } ,0); 
} 

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

function search() {
  value = $('searchfield').value;
  URL = 'http://www.cite-sciences.fr/webservices/search/search.do?lang=en&q=' + value;
  document.location.href = URL;
}

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

var newsletter = {
  subscribe: function() {
    if (this.check()) $('newsletterForm').submit();
    else alert("L'email que vous avez saisie est invalide");
  },  
  check: function() {
    var email = $('newsletterfield').value;
    var reg = /^[^@]+@(([\w\-]+\.){1,4}[a-zA-Z]{2,4}|(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5]))$/  
  	return ( reg.test(email) );
  }
};

/* --------------------------------------------------------------- */	
function isset() {    
  var a=arguments; var l=a.length; var i=0;
  if (l==0) { 
    throw new Error('Empty isset'); 
  }
  while (i!=l) {
    if (typeof(a[i])=='undefined' || a[i]===null) { 
      return false; 
    } else { 
      i++; 
    }
  }
  return true;
}
/* --------------------------------------------------------------- */ 