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

var quickSearch = {
  _default: 'Rechercher sur...',
  s : 'webdoc-quicksearch-query', 
  t : 'webdoc-quicksearch-type',
  
  /* ----------------------------------- */ 
  
  init: function() {
    if ($(this.s).value == '') $(this.s).value = this._default;
    $(this.s).observe('focus',this.onFocus.bindAsEventListener(this)); 
    $(this.s).observe('blur',this.onBlur.bindAsEventListener(this));
    $(this.s).observe('keydown',this.onKeyPress.bindAsEventListener(this));  
  },
  
  /* ----------------------------------- */ 
  
  onFocus: function() {
    if ($(this.s).value == this._default) $(this.s).value = '';    
  },
  
  /* ----------------------------------- */ 
  
  onBlur: function() {
    if ($(this.s).value == '') $(this.s).value = this._default;    
  },
  
  /* ----------------------------------- */ 
  
  _focus: function() {
    $(this.s).focus();    
  },
  
  /* ----------------------------------- */ 
  
  onKeyPress: function(event) {
    // Start on enter Key
    if (event.keyCode == 13) this.start();
  },     
  
/* ----------------------------------- */ 
  
  search: function() { 
    var qValue = $(this.s).value;
    var _type = $(this.t + '_hidden').value;
    
    if (qValue == '' || qValue == this._default) return;
    if (_type == '') _type = "federe";
    
    if (_type == "federe") { 
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL = "https://acces-distant.sciences-po.fr/http/wfxsearch.webfeat.org/wfsearch/search?Command=Search&Client_ID=11639&noserial=1&format=JS&nexturl=&useSessions=1&wf_codeset=utf-8&rtmpl=wfx_ui1.jsp&displayMode=1&wf_field1=wf_all&wf_term1=" + qValue + "&Databases=wf_sociologicalabstracts%2Cwf_abccliohistoryabstracts%2Cwf_socialservicesabs%2Cwf_nber_working_papers%2Cwf_csa_polcsi%2Cwf_abccliohistoryandlife%2Cwf_digitalnationalsecurityarchive%2Cwf_ebscoxml_sih%2Cwf_ebscoxml_ijh%2Cwf_ciao%2Cwf_csa_pais%2Cwf_csa_eric%2Cwf_webspirs_ibss%2Cwf_untreaty%2Cwf_ebscoxml_ecn%2Cwf_sourceoecd%2Cwf_ebscoxml_buh%2Cwf_nber_working_papers%2Cwf_ebscoxml_bwh%2Cwf_ebscoxml_eoah%2Cwf_jstorage";
    } else if (_type == "catalogue") {
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL ="http://catalogue.sciences-po.fr/ZonesL/?fn=search&q=" + qValue;
    } else if (_type == "revue") {
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL = "http://atoz.ebsco.com/titles.asp?KW=" + qValue + "&id=2592&linktype=search&SF=Titles&ST=Contains&cmdSearchSubmit=Recherche&sid=166835156&TabID=2";
    } else if (_type == "googlescholar") {
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL = "http://scholar.google.com/scholar?q=" + qValue + "&hl=en&lr=&btnG=Search";
    } else if (_type == "sudoc") {
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL = "http://www.sudoc.abes.fr/DB=2.1/SET=1/TTL=1/CMD?ACT=SRCHA&IKT=4&SRT=RLV&TRM=" + qValue;
    } else if (_type == "site") {
      var pattern = / /g;
      qValue = qValue.replace(pattern,'+');
      var searchURL = "http://www.google.com/cse?cx=012830618046463579984%3Arwcyvsqhdw8&ie=UTF-8&q=" + qValue;
    }
    
    // Go to
    window.location.href = searchURL; 
  }
  
  /* ----------------------------------- */ 
};  


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