/** * jQuery.timers - Timer abstractions for jQuery * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com) * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/). * Date: 2009/10/16 * * @author Blair Mitchelmore * @version 1.2 * **/ jQuery.fn.extend({ everyTime: function(interval, label, fn, times) { return this.each(function() { jQuery.timer.add(this, interval, label, fn, times); }); }, oneTime: function(interval, label, fn) { return this.each(function() { jQuery.timer.add(this, interval, label, fn, 1); }); }, stopTime: function(label, fn) { return this.each(function() { jQuery.timer.remove(this, label, fn); }); } }); jQuery.extend({ timer: { global: [], guid: 1, dataKey: "jQuery.timer", regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/, powers: { // Yeah this is major overkill... 'ms': 1, 'cs': 10, 'ds': 100, 's': 1000, 'das': 10000, 'hs': 100000, 'ks': 1000000 }, timeParse: function(value) { if (value == undefined || value == null) return null; var result = this.regex.exec(jQuery.trim(value.toString())); if (result[2]) { var num = parseFloat(result[1]); var mult = this.powers[result[2]] || 1; return num * mult; } else { return value; } }, add: function(element, interval, label, fn, times) { var counter = 0; if (jQuery.isFunction(label)) { if (!times) times = fn; fn = label; label = interval; } interval = jQuery.timer.timeParse(interval); if (typeof interval != 'number' || isNaN(interval) || interval < 0) return; if (typeof times != 'number' || isNaN(times) || times < 0) times = 0; times = times || 0; var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {}); if (!timers[label]) timers[label] = {}; fn.timerID = fn.timerID || this.guid++; var handler = function() { if ((++counter > times && times !== 0) || fn.call(element, counter) === false) jQuery.timer.remove(element, label, fn); }; handler.timerID = fn.timerID; if (!timers[label][fn.timerID]) timers[label][fn.timerID] = window.setInterval(handler,interval); this.global.push( element ); }, remove: function(element, label, fn) { var timers = jQuery.data(element, this.dataKey), ret; if ( timers ) { if (!label) { for ( label in timers ) this.remove(element, label, fn); } else if ( timers[label] ) { if ( fn ) { if ( fn.timerID ) { window.clearInterval(timers[label][fn.timerID]); delete timers[label][fn.timerID]; } } else { for ( var fn in timers[label] ) { window.clearInterval(timers[label][fn]); delete timers[label][fn]; } } for ( ret in timers[label] ) break; if ( !ret ) { ret = null; delete timers[label]; } } for ( ret in timers ) break; if ( !ret ) jQuery.removeData(element, this.dataKey); } } } }); jQuery(window).bind("unload", function() { jQuery.each(jQuery.timer.global, function(index, item) { jQuery.timer.remove(item); }); }); // JavaScript Document $(document).ready(function() { if ($('#osu-top-hat #osu-home-search-terms').val() == '') { $('#osu-top-hat #osu-home-search-terms').val('Anket ara..'); } // collect default formdata for list items that already exist $('#osu-top-hat .search-menu ul li').each(function() { $(this).data('formdata', {'handler':$('#osu-top-hat form.search') .attr('action'), 'terms':$('#osu-top-hat form.search input:text') .attr('name')}); }); // add new list items and set formdata var newItem; newItem = $('
  • People
  • '); newItem .data('formdata', {'handler':'http://directory.oregonstate.edu/', 'terms':'cn'}) .data('hidden', {'type':'search'}); $('#osu-top-hat .search-menu ul').append(newItem); newItem = $('
  • Media
  • '); newItem .data('formdata', {'handler':'http://media.oregonstate.edu/', 'terms':'search_term'}); $('#osu-top-hat .search-menu ul').append(newItem); // display menu on hover $('#osu-top-hat .search-menu img') .hover(function() {$(this) .css('cursor', 'pointer'); }, function() { $(this).css('cursor', 'auto'); }); $('#osu-top-hat form.search') .hover(function() { $('#osu-top-hat .search-menu ul') .stopTime('top-hat-search-menu-slide') .slideDown('fast'); }, function() { $('#osu-top-hat .search-menu ul') .oneTime(1000, 'top-hat-search-menu-slide', function() { $(this).filter(':not(:animated)').slideUp('normal'); }); }); // show/hide default search text $('#osu-top-hat form.search input:text').each(function() { var default_value = 'Anket ara..'; $(this).css('color', '#888') .focus(function() { if(this.value == default_value) { $(this).val('').css('color', '#252525'); } }) .blur(function() { if(this.value == '') { $(this).val(default_value).css('color', '#888'); } }); }); // handle search submit and redirect to the appropiate page $('#osu-top-hat form.search').submit(function() { if($('#osu-top-hat .search-menu ul li:eq(1) .checked').length > 0) { var searchTerms = $('#osu-top-hat form.search input:text').attr('value'); // if drupal we redirect to drupal search page - Drupal sets a variable // simply called 'Drupal' if(typeof Drupal !== "undefined" && Drupal) { // validate text searchTerms = searchTerms .replace(/[^\a-zA-z"'@, ]+/g, '') .replace(/ +/g, '+'); // set new location to go to. window.location = top_hat_drupal_search_url + '/' + searchTerms + '/0/1'; // do not let the form submit return false; } else if(typeof top_hat_site_search_domain !== "undefined" && top_hat_site_search_domain) { $('#osu-top-hat form.search input:text') .attr('value', 'site:'+top_hat_site_search_domain+' '+searchTerms); return true; } } else if($('#osu-top-hat .search-menu ul li#osu-home-search-media .checked').length > 0) { // This is a Media Search // The default 'get' will not work for MediaSpace as it is looking for // /#search_term= not /?search_term= // So we just rewrite the URL and pass it on var searchTerms = $('#osu-top-hat form.search input:text').attr('value'); window.location = 'http://media.oregonstate.edu/#search_term=' + searchTerms; return false; } }); // change form when a menu item is selected $('#osu-top-hat .search-menu input[name=osu-home-search-types]').each(function() { $(this).click(function() { var inputSelected = $(this).index('input[name=osu-home-search-types]'); $('#osu-top-hat form.search') .attr('action', $("#osu-top-hat form.search li:eq("+inputSelected+")") .data('formdata')['handler']); $('#osu-top-hat form.search input:text') .attr('name', $("#osu-top-hat form.search li:eq("+inputSelected+")") .data('formdata')['terms']); $('#osu-top-hat form.search input:hidden').remove(); for(key in $("#osu-top-hat form.search li:eq("+inputSelected+")").data('hidden')) { var value = $("#osu-top-hat form.search li:eq("+inputSelected+")").data('hidden')[key]; $('#osu-top-hat form.search') .append(''); } // move check mark $('#osu-top-hat .search-menu .checked') .prependTo($("#osu-top-hat form.search li:eq("+inputSelected+")")); }); }); // change form when a menu item is selected $('#osu-top-hat .search-menu ul li') .hover(function() { $(this).css('cursor', 'pointer'); },function() { $(this).css('cursor', 'auto'); }) .click(function() { $('#osu-top-hat form.search').attr('action', $(this).data('formdata')['handler']); $('#osu-top-hat form.search input:text').attr('name', $(this).data('formdata')['terms']); $('#osu-top-hat form.search input:hidden').remove(); for(key in $(this).data('hidden')) { var value = $(this).data('hidden')[key]; $('#osu-top-hat form.search') .append(''); } // move check mark $('#osu-top-hat .search-menu .checked').prependTo(this); }); });