Team:ETH Zurich/Templates/HeaderNew/SearchJS

From 2011.igem.org

$(document).ready(function(){

var config = { siteURL : '2011.igem.org/Team:ETH_Zurich', // Change this to your site searchSite : true, type : 'web', append : false, perPage : 4, // A maximum of 8 is allowed by Google page : 0 // The start page }


// Focusing the input text box: $('#s').focus();

$('#searchForm').submit(function(){ googleSearch(); return false; });


function googleSearch(settings){

// If no parameters are supplied to the function, // it takes its defaults from the config object above:

settings = $.extend({},config,settings); settings.term = settings.term || $('#s').val();

if(settings.searchSite){ // Using the Google site:example.com to limit the search to a // specific domain: settings.term = 'site:'+settings.siteURL+' '+settings.term; }

// URL of Google's AJAX search API var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?'; var resultsDiv = $('#resultsDiv');

$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){

var results = r.responseData.results; $('#more').remove(); $('#closeSearch').remove();

if(results.length){

// If results were returned, add them to a pageContainer div, // after which append them to the #resultsDiv:

var pageContainer = $('
',{className:'pageContainer'});

for(var i=0;i<results.length;i++){ // Creating a new result object and firing its toString method: pageContainer.append(new result(results[i]) + ); }

if(!settings.append){ // This is executed when running a new search, // instead of clicking on the More button: resultsDiv.empty(); }

pageContainer.append('
')

.hide().appendTo(resultsDiv) .fadeIn('slow');

var cursor = r.responseData.cursor;

// Checking if there are more pages with results, // and deciding whether to show the More button:

if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){

$('
',{id:'more'}).appendTo(resultsDiv).click(function(){

googleSearch({append:true,page:settings.page+1}); $(this).fadeOut(); }); $('#more').text('More...'); }

$('
',{id:'closeSearch'}).appendTo(resultsDiv).click(

function(){ $('.searchUL').css('visibility', 'hidden'); $('#s').val(); $('#s').blur(); $('#resultsDiv').empty(); }); } else {

// No results were found for this search.

resultsDiv.empty();

$('

',{className:'notFound',html:'No Results Were Found!'}).hide().appendTo(resultsDiv).fadeIn(); } }); } function result(r) { // This is class definition. Object of this class are created for // each result. The markup is generated by the .toString() method. var arr = []; // GsearchResultClass is passed by the google API switch(r.GsearchResultClass){ case 'GwebSearch': arr = [ '

',

'<a href="',r.unescapedUrl,'" target="_blank">',r.title,'
', '',r.content,'</a>',

'
'

]; break; }

// The toString method. this.toString = function(){ return arr.join(); } }


});


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

/* Mediawiki Links: Incorporate into our design on our own terms! */ $(document).ready( function() {

   $(function() 
   {

$('.scrollable').scrollable({ mousewheel: true }).navigator(); if ($('#pt-logout').length) { $('#loginMenu').hide(); $('#editMenu').show();

$('#editLink').attr('href', $('.left-menu ul li a').get(2).href); $('#historyLink').attr('href', $('.left-menu ul li a').get(3).href); $('#logoutLink').attr('href', $('#pt-logout a').get(0).href); } else { $('#loginLink').attr('href', $('#pt-login a').get(0).href); }

   });
   var mouseInUL = false;
   $('#s').focus(function() {
       updateULSearch(true, mouseInUL);
   });
   $('#submitButton').focus(function() {
       updateULSearch(true, mouseInUL);
   });


   $('#s').blur(function() {
       updateULSearch(false, mouseInUL);
   });
   $('#submitButton').blur(function() {
       updateULSearch(false, mouseInUL);
   });
   $('.searchUL').mouseenter(function() {
       mouseInUL = true;
       updateULSearch(true, mouseInUL);
   });


   $('.searchUL').mouseleave(function() {
       mouseInUL = false;
       updateULSearch(false, mouseInUL);
   });

});

function updateULSearch (showUL, mouseInUL) {

 if (showUL)
 {
    if ($('.searchUL').css('visibility') != 'visible')
    {
       $('.searchUL').css('visibility', 'visible');
    } 
 } 
 else
 {
    if (!(mouseInUL || $('#s').is(":focus") || $('#submitButton').is(":focus")))
    {
       $('.searchUL').css('visibility', 'hidden');
    }
 }
}