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

$(document).observe("dom:loaded", function() {
  
  // Scroller for actu
  var actu = new Control.Scroller('webdoc-actu-list','webdoc-actu-handle', 'webdoc-actu-track');
  
  // Zoom news reader
  var zoom = new NewsReader('webdoc-zoom-list',330,'webdoc-zoom-launchPad');
  if (Prototype.Browser.IE) setTimeout(function() {zoom.start(1,0);},500);
  
  // Custom Selectbox for search
  var selectbox = new Autocompleter.SelectBox('webdoc-quicksearch-type',{
    keepSelectWidth:false,
    autocompleteClassName: 'webdoc-quicksearch-select',
    afterUpdateElement: function() { quickSearch._focus(); },
    customizeResult: function(v) { return v.substring(0,7); }
  }); 
  
  // Search Box
  quickSearch.init();
  
  // Rss
  Blog.update();

});

/* --------------------------------------------------------------- */ 
 
NewsReader = Class.create();
NewsReader.instances = [];
NewsReader.getInstance = function(i){ return NewsReader.instances[i]; };
NewsReader.prototype = {
  initialize: function(container, maxWidth, launchPad) {
    this.container = container;
    this.maxWidth = maxWidth;
    this.launchPad = launchPad;
    this.selected = 1;
    this.manualLaunch = 0;
    this.speed = 0.3; 
    this.autoZoomCount = 1; 
    this.autoZoomTimer = 10;
    
    // Instances
    this.index = NewsReader.instances.length;
    NewsReader.instances[this.index] = this;    
    
    // Build launch pad
    this.nbNews = $(this.container).childElements().length;
    if (this.nbNews > 10) this.nbNews = 10;
    var html = '';
    for (var i = this.nbNews;i > 0 ; i--) {     
      if (Prototype.Browser.IE) classOn = 'webdoc-zoom-button';
      else classOn = (this.selected == i) ? 'webdoc-zoom-button-on' : 'webdoc-zoom-button';
        
      html += [
      '<div class="'+ classOn +'" id="webdoc-zoom-button_' + i + '">',
        '<a style="color:#ffffff" href="javascript:;" onclick="NewsReader.getInstance(\'' + this.index + '\').start(' + i + ',1);this.blur();">' + i + '</a>',
      '</div>'
      ].join("\r\n");
    }
    $(this.launchPad).update(html);
    
    // AutoStart
    this.autoStart();
  },
  
  /* ----------------------------------- */   
  
  start: function(nb,active) {  
    
    // Manually launched
    if (active == 1) this.manualLaunch = 1;
    
    // Set timing
    var absDiff = Math.abs(nb - this.selected);    
    timing = Math.round(((this.speed * absDiff)*10))/10;
    
    // Reinit
    this.selected = nb; 
    
    // Start morphing
    position = (1 - this.selected) *this.maxWidth;
    $(this.container).morph("opacity:0.5;", {duration: 0.2, afterFinish: function() {
      $(this.container).morph("left: " + position + "px", {
        duration: timing,
        delay: 0.2,
        afterFinish: function() {
          $(this.container).morph("opacity:1;", {duration: 0.2});
        }.bind(this)
      });
    }.bind(this)});
    
    // HightLight Launch Pad
    this.hightlight();
  },
  
  /* ----------------------------------- */    
  
  autoStart :function() {
    var tmp = new PeriodicalExecuter(function(pe) {
      
      // Wich zoom to display
      this.autoZoomCount++;
      if (this.autoZoomCount > this.nbNews) this.autoZoomCount = 1
      
      // Stop auto event when zoom has been clicked manually
      if (this.manualLaunch != 1) this.start(this.autoZoomCount);
      else pe.stop();
    }.bind(this), this.autoZoomTimer);
  },
  
  /* ----------------------------------- */  
  
  hightlight : function() {
    $$('#' + this.launchPad + ' div').each(function(el){
      var nb = parseInt(el.id.replace(/webdoc-zoom-button_/,''));
      if (nb == this.selected) el.className = 'webdoc-zoom-button-on';
      else el.className = 'webdoc-zoom-button';
    }.bind(this));
  }  
}

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

Control.Scroller = Class.create();
Control.Scroller.prototype = {
  initialize: function(container, handle, track) {
    this.container = $(container);    
    this.handle = $(handle);
    this.track = $(track);          
    
    this.slider = new Control.Slider(this.handle, this.track, {
      'axis' : 'vertical',
      onSlide: function(v) { this.scrollVertical(v); }.bind(this),
      onChange: function(v) { this.scrollVertical(v); }.bind(this)
    });     
      
    // Hide if not usefull
    this.autoHide();          
  },
  
   /* ----------------------------------- */ 

   // Scroll the element vertically based on its width and the slider maximum value
  scrollVertical: function(value) {
   this.currentValue = value;
   this.container.scrollTop = Math.round(value/this.slider.maximum*(this.container.scrollHeight-this.container.offsetHeight));
  },
  
   /* ----------------------------------- */ 
  
  // Disable vertical scrolling if text doesn't overflow the div
  autoHide: function() {
    if (this.container.scrollHeight <= this.container.offsetHeight) {
      this.slider.setDisabled();
      this.track.hide();
    }
  }
};

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

var Blog = {
  URL: 'http://bibliotheque.sciences-po.fr/php/bridge.php',
  update: function() {
    if (! $('webdoc-blog-post')) return;
    
    // Start call    
    var dump = new Ajax.Request(this.URL,{
      method: 'post',
      onSuccess: function(transport){
        var xmlDoc = transport.responseXML;
        
        // Build elements
        var _link = xmlDoc.getElementsByTagName('link')[0].firstChild.data; 
        var _title = xmlDoc.getElementsByTagName('title')[0].firstChild.data;  
        var _desc = xmlDoc.getElementsByTagName('description')[0].firstChild.data;      
        
        // Build html     
        var htmlLink = '<a href="'+ _link +'" target="_blank">' + _title + '</a>';          
        var maxLenght = (_title.length < 45 ) ? 95 : 53; // 1 or 2 lines
        var htmlDesc = '<a href="'+ _link +'" target="_blank">';
        htmlDesc += (_desc.length > maxLenght ) ? _desc.substring(0,maxLenght) + '...' : _desc;     
        htmlDesc += '</a>';
                  
        // Update
        $('webdoc-blog-post').update(htmlLink);
        $('webdoc-blog-post-description').update(htmlDesc);
        Effect.Appear('webdoc-blog-content');
      },
      onFailure: function() {
        var htmlLink = '<a href="http://blogs.sciences-po.fr/bibliotheque/">Acc&eacute;der au blog de la biblioth&egrave;que</a>';
        $('webdoc-blog-post').update(htmlLink);
        Effect.Appear('webdoc-blog-content');
      }
    }); 
  }
};

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