/* name : globalHeader file : jquery.globalheader.js author : Ali Hasan (c) Copyright 2009 AOL LLC $LastChangedDate: 2009-11-24 08:30:32 -0500 (Tue, 24 Nov 2009) $ $Rev: 134091 $ /////////////////////////// dependencies : jQuery 1.3.2, jquery.globalsearchbox.js /////////////////////////// description: Build global header. */ (function($) { $.fn.globalHeader = function( customOptions ) { /** * default options * These can be overridden by passing in an updates customOptions object * * ==== Using options ==== * Example to change the moreText below: * jQuery('#myElemId').globalHeader( { moreText : "I can change this value:" } ); * * * ==== Override Functions (if needed) ==== * You can also override the functions in the core Object. * To do this, just add your method to the fn Object. * An example to change the buildAuth method: * * jQuery('#myElemId').globalHeader( { fn : { buildAuth : function(){ * alert('I am changing the buildAuth!'); * // ... do stuff * } * } * }); * * * ==== Showing user as logged in (auth) ==== * To show the header in a signed in state, alter the value of the auth Object. * A simple example: * jQuery('#myElemId').globalHeader( { auth : { authenticated : true } } ); * */ var defaultOptions = { activeTab : null, moreLinks : [], morePromoCount : 2, moreText: 'También te gustaría:', moreAnd: ' y ', moreMore: 'Más', moreTextHeadline: 'También te podría gustar', uiHat : '#GH_hat', uiHatLinks : '#GH_hat_links', uiHatTools : '#GH_hat_tools', uiNavLi : 'li.GH_nav_LI', uiNavADd : '.GH_nav_dd_A', auth : { doAuth: false, authenticated: false, authState: null, unauthState: null }, // search params search : { uiSearch: '#GH_search', params : {} }, // options passed on to globalSearchBox fn : {} // functions to be overridden }, /* merged options */ options = {}, /** * target jQuery collection * Cache 'this' into the variable $this for later reference. */ $this = this, /* ui model*/ ui = {}, /* local data store */ local = { activeTab : null, moreLinksBuilt : false }, /* private functions, can be overriden, cannot be called directly */ core = { /** * This method is automatically fired as soon as the plugin is called * It *CAN* be overriden following the above steps. */ init : function(container) { ui.$d = $(document); ui.$c = $(container); ui.hat = $(options.uiHat)[0]; ui.hatLinks = $(options.uiHatLinks)[0]; ui.$hatTools = $(options.uiHatTools); ui.$search = $(options.search.uiSearch); ui.$searchInput = ui.$search.find('input:first'); ui.$searchSubmit = ui.$search.find('input:last'); ui.$navLi = ui.$c.find(options.uiNavLi); ui.$navADd = ui.$c.find(options.uiNavADd); core.setActiveTab(null, options.activeTab); if (options.auth.doAuth) { core.buildAuth(); } core.buildMoreLinks(); core.buildDropDowns(); ui.$c.bind('setActiveTab', function(e,data) { core.setActiveTab(e,data); }); ui.$c.bind('setAuthState', function(e,data) { core.buildAuth(e,data); }); // focus on input field on init if (options.search.params.initFocus !== undefined && options.search.params.initFocus) { /** * This immediately builds the autocomplete box. */ ui.$search.globalSearchBox(options.search.params); } else { /** * This event offloads the element building of the autocomplete box to user action * This technique helps speed up the render of the header. It doesn't penalize users who aren't searching. */ ui.$searchInput.bind('focus.GH', function(e) { core.buildSearch(e); }).attr('autocomplete','off'); ui.$searchSubmit.bind('mouseover.GH', function(e) { core.buildSearch(e); }); // preset search text if (options.search.params.searchText !== undefined && options.search.params.searchText !== '') { ui.$searchInput.val(options.search.params.searchText); } } }, /** * helper function to return private variables to extended functions * This method let's the code be minified, but still work with function overrides. */ getVars : function() { return { options : options, ui : ui, local : local }; }, /** * initialize global search box * This function has a dependency on an external file. jquery.globalSearchBox.js * The globalSearchBox is for autocomplete. The autocomplete is not auto-magic. You will need to set up a channel dictionary */ buildSearch : function(e) { ui.$searchInput.unbind('focus.GH'); ui.$searchSubmit.unbind('mouseover.GH'); if (e.target === ui.$searchInput[0]) { ui.$searchInput.addClass('GH_search_active').attr('value',''); } ui.$search.globalSearchBox(options.search.params); }, /* build auth and unauth states * user can sent auth params on globalheader init or trigger a 'setAuthState' event with necessary auth params * * eg. $("#GH_").trigger('setAuthState',{authenticated:false}) */ buildAuth : function(e, authObject) { if (authObject !== undefined) { $.extend(true, options.auth, authObject); } ui.$hatTools.empty().append((options.auth.authenticated) ? options.auth.authState : options.auth.unauthState); }, /** * build more links drop down * This is not the 'nav' items drop down, it's for the 'other sites' links in the top hat */ buildMoreLinks : function() { var ml = options.moreLinks, i = 0, l = ml.length; if (l >= options.morePromoCount) { // Build 'More' Line var hatMore, hatLi = $('
').addClass('GH_hat_LI').append( hatMore = $('