/* Common javascript functions */

/*
 *  "Suckerfish" drop-down menu support for IE
 *  http://www.htmldog.com/articles/suckerfish/dropdowns/
 */
/* jQuery version */
// if(jQuery.browser.msie) {
//   jQuery(document).ready(function() {
//       jQuery('#nav li').hover(
//           /* over: */ function() { jQuery(this).addClass('sfhover'); },
//           /* out:    */ function() { jQuery(this).removeClass('sfhover'); }
//       );
//   });
// }


/* menu mouse-over support */
var menulib = (function($) {
    var mouseover_registry = {};
    var menu_preload_cache = [];
    var superfish_used = false;

    var options = {
        root_selector: 'ul#nav',
        listitem_selector: '#nav li',
        hover_class: 'sfhover'
    };

    // menulib.configure({root_selector: 'ol#xoxo'});
    var configure = function(configuration_options) {
        jQuery.extend(options, configuration_options || {});
    };

    var make_image = function(source) {
        var img = new Image();
        img.src = source;
        return img;
    };

    /* register_menu_resources({image-id: {'normal': url, 'over': url}})
     * The main template calls this to signify the normal and hover states
     * for a particular image.
     */
    var register_menu_resources = function(resources) {
        jQuery.extend(mouseover_registry, resources || {});
    };

    /* apply_mouseover_registry()
     * Go through the mouseover registry and attach hover (mouseover, mouseout)
     * events to each image, while also pre-loading the 'over' state.
     */
    var apply_mouseover_registry = function() {
        jQuery.each(mouseover_registry, attach_resources_to_image);
    };

    var attach_resources_to_image = function(image_id, image_resources) {
        /* make an Image object for the 'over' state and push it into the preload
         * cache.
         */
        menu_preload_cache.push(make_image(image_resources.over));
    
        /* Attach event handlers for mouse-over and mouse-out */
        $('#'+image_id).hover(
            /* over: */ function() { this.src = image_resources.over; },
            /* out:  */ function() { this.src = image_resources.normal; }
        );
    };

    var setup_ie_event_handling = function() {
        /* Only apply IE event handling of superfish is not being used. */
        if(jQuery.browser.msie && !superfish_used) {
          $(options.listitem_selector).hover(
              /* over: */ function() { $(this).addClass('sfhover'); },
              /* out:    */ function() { $(this).removeClass('sfhover'); }
          );
        }
    };

    /* DOM-ready event handler */
    $(document).ready(function() {
        apply_mouseover_registry();
        setup_ie_event_handling();
    });

    /* Ensure that the cache and registry are cleared out between pages to
     * prevent memory leaks.
     */
    $(document).unload(function() {
        menu_preload_cache = null;
        mouseover_registry = null;
        options = null;
    });
    

    /* ``menulib.apply_superfish('ul#nav', {'hoverClass': 'sfhover', ...});``
     * Equivalent to:
     *
     * jQuery(document).ready(function() {
     *   jQuery(selector).superfish(options);
     * })
     */ 
    var apply_superfish = function(selector, options) {
        superfish_used = true;
        $(document).ready(function() {
            $(selector).superfish(options);
        });
    };
    
    /* Exported Names. `menulib.*` */
    return {
        'configure': configure,
        'register_resources': register_menu_resources,
        'apply_superfish': apply_superfish
    };
}(jQuery));

/*
 * Superfish v1.3.1 - jQuery menu widget
 *
 * Copyright (c) 2007 Joel Birch
 *  http://users.tpg.com.au/j_birch/plugins/superfish/
 *
 * Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 *
 * YOU MAY DELETE THIS CHANGELOG:
 * v1.2.1 altered: 2nd July 07. added hide() before animate to make work for jQuery 1.1.3. See comment in 'over' function.
 * v1.2.2 altered: 2nd August 07. changed over function .find('ul') to .find('>ul') for smoother animations
 *                   Also deleted the iframe removal lines - not necessary it turns out
 * v1.2.3 altered: jquery 1.1.3.1 broke keyboard access - had to change quite a few things and set display:none on the
 *                   .superfish rule in CSS instead of top:-999em
 * v1.3 :                Pretty much a complete overhaul to make all original features work in 1.1.3.1 and above.
 *                   .superfish rule reverted back to top:-999em (which is better)
 * v1.3.1 altered: 'li[ul]' to $('li:has(ul)') to work with jQuery 1.2
 */

(function($){
    $.fn.superfish = function(o){
        var $sf = this,
            defaults = {
                hoverClass  : 'sfHover',
                pathClass   : 'overideThisToUse',
                delay       : 800,
                animation   : {opacity:'show'},
                speed       : 'normal'
            },
            over = function(){
                clearTimeout(this.sfTimer);
                clearTimeout($sf[0].sfTimer);
                $(this)
                    .showSuperfishUl()
                    .siblings()
                    .hideSuperfishUl();
            },
            out = function(){
                var $$ = $(this);
                if ( !$$.is('.'+o.bcClass) ) {
                    this.sfTimer=setTimeout(function(){
                        $$.hideSuperfishUl();
                        if (!$('.'+o.hoverClass,$sf).length) { 
                            over.call($currents.hideSuperfishUl());
                        }
                    },o.delay);
                }       
            };

        $.fn.extend({
            hideSuperfishUl : function(){
                return this
                    .removeClass(o.hoverClass)
                    .find('ul:visible')
                        .hide()
                    .end();
            },
            showSuperfishUl : function(){
                return this
                    .addClass(o.hoverClass)
                    .find('>ul:hidden')
                        .animate(o.animation,o.speed)
                    .end();
            },
            applySuperfishHovers : function(){
                return this[($.fn.hoverIntent) ? 'hoverIntent' : 'hover'](over,out);
            }
        });

        o = $.extend({bcClass:'sfbreadcrumb'},defaults,o || {});
        var $currents = $('li:has(ul)',this).filter('.'+o.pathClass);
        if ($currents.length) {
            $currents.each(function(){
                $(this).removeClass(o.pathClass).addClass(o.hoverClass+' '+o.bcClass);
            });
        }
        var $sfHovAr=$('li:has(ul)',this)
            .applySuperfishHovers(over,out)
            .find('a').each(function(){
                var $a = $(this), $li = $a.parents('li');
                $a.focus(function(){
                    over.call($li);
                    return false;
                }).blur(function(){
                    $li.removeClass(o.hoverClass);
                });
            })
            .end()
            .not('.'+o.bcClass)
                .hideSuperfishUl()
            .end();
        $(window).unload(function(){
            $sfHovAr.unbind('mouseover').unbind('mouseout');
        });
        return this.addClass('superfish').blur(function(){
            out.call(this);
        });
    };
})(jQuery);
