/*
 * bootstrap.js - basic JQuery bootstrap
 *
 * by Michael Guitton (michael/at/guitton/dot/name)
 *
 */

'function' == typeof jQuery && (function(global, $) {

    $.fn.extend({
        scrollTo: function(speed, easing) {
            return this.each(function() {
                var targetOffset = $(this).offset().top,
                                page = ['html', 'body'];
                if ($.browser.opera) {
                    page.pop();
                }
                $(page+'').animate({scrollTop: targetOffset}, speed, easing);
            });
        }
    });

    function jump(e) {
        $(this).blur();
        $(this.hash).scrollTo(800);
        e.preventDefault(); // prevent FOUC-like FX - mg :-/
    }

    function nop(e) {
        $(this).blur()
        e.preventDefault();
    }

    function toggle(e) {
        var target = $(this);
        target
            .toggleClass('collapse')
            .next('ul')
/*@cc_on
// prevent animation bug in IE7 - mg
            .css('position', 'relative')
@*/
            .slideToggle('normal')
/*@cc_on
            .css('position', 'static')
@*/
            .blur();
        if (e.data && e.data['sitemap']) {
            var type = target.hasClass('collapse') ? 'circle' : 'disc';
            target.parent().css('list-style-type', type);
        }
    }

    function slider(options)
    {
        if (0 == this.length) { return; }

        var timeout,
            i = 0,
            defaults = {
                easing: 'swing',
                pause: 2000,
                speed: 800,
                flags: {
                    nojs: 'no-js',
                    animated: 'animated',
                    paused: 'paused'
                }
            },
            settings = $.extend(true, {}, defaults, options),
            $wrapper,
            $that    = this.slice(0, 1),
            $items   = $that.find('li'),
            offset   = 0,
            padding  = parseInt($that.css('padding-bottom'), 10),
            y = [];

        function go()
        {
            var $this = $(this);

            if ($this.hasClass(settings.flags.animated)) { return; }

            $wrapper.css({ 'height': offset + 'px' });
            global.clearTimeout(timeout);
            timeout = global.setTimeout(
                function() {
                    $this.removeClass(settings.flags.paused);
                    scroll();
                },
                settings.pause
            );
        }

        function pause()
        {
            var kludge = 0, $this = $(this);

            if ($this.hasClass(settings.flags.animated)) { return; }

            $this.addClass(settings.flags.paused);

/*@cc_on
@if (@_jscript_version <= 5.7)
            kludge = $this.find('ins').height();
@end @*/

            global.clearTimeout(timeout);
            $wrapper.css({ 'height': ($this.height() + 2 * padding + kludge) + 'px' });
        }

        function scroll()
        {
            $items.addClass(settings.flags.animated);
            $that.stop().animate(
                { marginTop: y[i] },
                settings.speed,
                settings.easing,
                function() {
                    $items.removeClass(settings.flags.animated);
                    i = ++i % y.length;
                    go();
                }
            );
        }

        if (1 < $items.length) {

            $items.removeClass(settings.flags.nojs).css({
                'display': 'inline-block'
            }).each(function () {
                offset = Math.max(offset, $(this).height());
            }).css({
                'display': /*@cc_on 'inline-' + @*/ 'block',
/*@cc_on
@if (@_jscript_version == 5.6)
                'height': offset + 'px'
@else @*/
                'min-height': offset + 'px'
/*@end @*/
            }).hover(pause, go);

            offset += 2 * padding;
            while (y.length < $items.length) {
                y[y.length] = -offset * y.length;
            }

            $wrapper = $that.wrap('<div></div>').parent().css({
                'overflow': 'hidden'
            });

            go();
        }

    } /* =END slider() */

    $(document).ready(function() {

        var $subnav = $('#subnav'),
            $sitemap = $('.sitemap #root'),
            $filtering = $('.careers-with-us #filtering');

        $('ul', $subnav).hide();
        $('li span', $subnav)
            .addClass('menu')
            .click(toggle);
        $('.selected', $subnav)
            .parent()
            .siblings('span')
            .trigger('click');

        $('ul', $sitemap).hide();
        $('li span', $sitemap)
            .addClass('menu')
            .append('/')
            .bind('click', { sitemap: true }, toggle)
            .parent()
            .css('list-style-type', 'disc');

        $('ul', $filtering).hide();
        $('li strong', $filtering)
            .addClass('menu')
            .click(toggle);

        if ($.fn.lightbox) {
            $('#content .photos a, .careers-with-us #heading .location a').lightbox({
                data: {
                    'min-width': '120px',
                    'min-height': '90px'
                },
                overlay: {
                    'opacity': 0.65
                },
                img: {
                    'border': '#d6dfe1 4px solid'
                }
            });
        }

        $('#applying a[href^="mailto:"]').each(function() {
            var s = this.title || '',
                p = s.lastIndexOf('@');
            if (-1 !== p) {
                p = s.lastIndexOf(' ', p);
                s = s.substring(0, ++p) + s.substring(p).link(this.href);
                $('<p>' + s + '</p>').insertBefore(this.parentNode);
            }
        });

        $('#video a, #highlights a.more, #leads a.more').each(function() {
            var href = this.href,
                 $li = $(this).parents('li').slice(0, 1);

            function go(e) {
                if ((e.target.nodeName + '').toLowerCase() == 'a' && e.target.href) {
                    return;
                }
                global.location = href;
            }

            function over() {
                global.status = href;
                return true;
            }

            function out() {
                global.status = '';
            }

            $li.addClass('js').hover(over, out).click(go);
        });

        slider.call($('#overlay #video'));

        $('a[href^="#"]:not([href="#"]):not([href="#self"])').click(jump);
        $('a[href="#self"],a[href="#"]').click(nop);
    });

})(this, jQuery);

