// {{{ function gTrackEvent(category, action, optional_label, optional_value, optional_noninteraction)
function gTrackEvent(category, action, optional_label, optional_value, optional_noninteraction)
{
    if (typeof(_gaq) != "undefined" && typeof(action) == 'string' && typeof(category) == 'string')
    {
        var data = ['_trackEvent', category, action];
        if (typeof(optional_label) == 'string')
        {
            data.push(optional_label);
            if (typeof(optional_value) == 'number')
            {
                data.push(optional_value);
            }
        }
        _gaq.push(data);
    }
}
// }}}
(function ($) {
    "use strict";
    $(function () {
        var slideshow = $('.slideshow'),
            header = $('#header'),
            nav = $('.nav', header),
            nav_items = $('> ul > li', nav),
            breadcrumbs = $('.breadcrumbs', header),
            site_functions = $('.site-functions', header),
            search = $('.search', site_functions),
            search_input = $('input', search),
            search_width = search.width();
        // {{{ slideshow
        if (slideshow !== undefined && slideshow.length) {
            var slides = $('> .slide', slideshow),
                slideshow_height = slideshow.height(),
                slideshow_timeout,
                first_slide = slides.first(),
                last_slide = slides.last(),
                input_delay = $('> input.delay', slideshow),
                slide_counter = $('> .slide-counter', header),
                delay = input_delay !== undefined && input_delay.length
                    ? input_delay.val() + '000'
                    : 3000, // default delay
                animate_previous = function (slide) {
                    if (!slideshow.data('animating')) {
                        var prev_slide = slide.next('.slide').length
                                ? slide.next('.slide')
                                : first_slide;
                        slideshow.data('animating', true);
                        slide
                            .fadeOut(2000);
                        prev_slide
                            .fadeIn(1000, function () {
                                if (slideshow.data('playing')) 
                                {
                                    slideshow_timeout = setTimeout(function () { animate_previous(prev_slide); }, delay);
                                }
                                slideshow.data('animating', false);
                            });
                        slide_counter.trigger('update', [-1]);
                    }
                },
                animate_next = function (slide) {
                    if (!slideshow.data('animating')) {
                        var next_slide = slide.prev('.slide').length
                                ? slide.prev('.slide')
                                : last_slide;
                        slideshow.data('animating', true);
                        slide
                            .fadeOut(3000);
                        next_slide
                            .fadeIn(3000, function () {
                                if (slideshow.data('playing')) {
                                    slideshow_timeout = setTimeout(function () { animate_next(next_slide); }, delay);
                                }
                                slideshow.data('animating', false);
                            });
                        slide_counter.trigger('update', [1]);
                    }
                },
                controls = $('> .controls', slideshow),
                control_pause,
                control_prev, 
                control_next, 
                control_play;
            slide_counter
                .data('total', slides.length)
                .data('current', 0)
                .bind('update', function (e, increment) {
                    var el = $(this),
                        count = el.data('current'),
                        total = el.data('total'),
                        new_count = count + increment;
                    if (total > 1) {
                        if (new_count < 1) {
                            count = total;
                        } else if (new_count > total) {
                            count = 1;
                        } else {
                            count = new_count;
                        }
                        el.data('current', count)
                            .html('<span>' + count + '</span> / ' + total);
                    }
                });
            slideshow.data('playing', false);
            slideshow.data('animating', false);
            slides
                .eq(Math.floor(Math.random() * slides.length))
                .siblings('.slide')
                .hide();
            if (slides.length > 1) {
                if (controls !== undefined && controls.length) {
                    control_next = $('<img src="/images/next-button.png" alt="Next" />')
                        .click(function () {
                            clearTimeout(slideshow_timeout);
                            animate_next($('> .slide:visible', slideshow));
                        });
                    control_pause = $('<img src="/images/pause-button.png" alt="Pause" />')
                        .click(function () {
                            slideshow.data('playing', false);
                            clearTimeout(slideshow_timeout);
                            $(this).detach();
                            control_prev.after(control_play);
                        });
                    control_play = $('<img src="/images/next-button.png" alt="Play" />')
                        .click(function () {
                            slideshow.data('playing', true);
                            slideshow_timeout = setTimeout(function () { animate_next($('> .slide:visible', slideshow)); }, delay);
                            $(this).detach();
                            control_prev.after(control_pause);
                        });
                    control_prev = $('<img src="/images/prev-button.png" alt="Previous" />')
                        .click(function () {
                            clearTimeout(slideshow_timeout);
                            animate_previous($('> .slide:visible', slideshow));
                        });
                    controls
                        .append(control_prev)
                        .append(control_play)
                        .append(control_next);
                    control_play.click();
                } else {
                    slideshow.data('playing', true);
                    slideshow_timeout = setTimeout(function () { animate_next($('> .slide:visible', slideshow)); }, delay);
                }
            }
            slide_counter.trigger('update', [1]);
        }
        // }}}
        // {{{ search
        search_input
            .bind('keypress', function(e){
                if (e.which == 13)
                {
                    $(this).closest('form').submit();
                }
            });
        $('img', search)
            .css('cursor', 'pointer')
            .click(function () {
                var new_width = $('.viewer', search).width();
                if (search.width() > search_width)
                {
                    new_width = search_width;
                    search_input.focusout();
                }
                else
                {
                    search_input.focus();
                }
                search
                    .animate({
                        width: new_width
                    });
            });
        // }}}
        // {{{ nav
        nav_items
            .hover(function () {
                var el = $(this),
                    subnav = $('ul', el);
                if (subnav.length)
                {
                    subnav.show();
                    breadcrumbs.css('visibility', 'hidden');
                }
            }, function () {
                var el = $(this);
                $('ul', el).hide();
                breadcrumbs.css('visibility', 'visible');
            });
        // }}}
    });
    //{{{ language switcher
    $(function () {
        var switcher = $('#language-switcher'),
            current = $('> img', switcher),
            choices = $('> div', switcher),
            languages = $('> img', choices);
        languages
            .click(function () {
                var lang = $(this).data('language');
                gTrackEvent('language', 'click', lang);
                location.href = location.pathname + '?language=' + lang;
            });
        current
            .click(function () {
                choices.toggle();
            });
    });
    //}}}
})(jQuery);

