var babicka = {
    
    /* */
    // vars
    
    ieversion: null,
    widont_tags: 'h1, h2, h3, p, blockquote',
    
    
    /* */
    // methods
    
    set_ieversion: function() {
        if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            this.ieversion = Number(RegExp.$1);
        }
    },
    
    add_fade_overlays: function() {
        // add fader gradient to scrollable areas
        $(".scroll-pane").each(function() {

            var scroll_pane = $(this);
            var viewport = scroll_pane.find(".viewport");
            var pane_content = scroll_pane.find(".overview");
            var has_scrollbar = ! scroll_pane.find(".scrollbar").hasClass("disable");

            // only if element has scrollbar
            if(has_scrollbar) {
                viewport.append('<div class="fader-bottom"></div>');
                pane_content.append('<div class="fader-bottom-push"></div>');
                setTimeout(function() {
                    scroll_pane.tinyscrollbar_update();
                }, 250);
            }
        });
    },
    
    scroll_panes: function() {
    
        var t = this;
        var scroll_panes = $('.scroll-pane');
    
        if(!scroll_panes.length) {
            return false;
        }
    
        scroll_panes.wrapInner('<div class="viewport"><div class="overview"></div></div>');
        scroll_panes.append('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>');
        scroll_panes.tinyscrollbar();
        
        if(!t.ieversion) {
            t.add_fade_overlays();
        }

        $(window).load(function() {
            scroll_panes.tinyscrollbar_update();
        });
        
    },

    widont: function(elements) {
        $(elements).each(function() {
            var html = $.trim($(this).html());
            var last_space = html.lastIndexOf(" ");
            var nbsp = "&nbsp;";
            
            // exclude elements with children
            if($(this).children().length == 0 && last_space > -1) {
                html = html.substr(0, last_space) + nbsp + html.substr(last_space + 1);
                $(this).html(html);
            }
            
        });
    },
    
    home_page: function() {

        // remove video for mobile devices - don't want to eat up data
        if(Modernizr.touch || navigator.userAgent.match(/iPad|iPhone|android|blackberry|Windows Phone/i)) {
            $("#home-video-wrapper video").remove();
        }
        
        // firefox-friendly loop video
        $("video").bind('ended', function() {
            this.play();
        });
        
        // quote cycler
        
        var quote_container = $("#quotes");
        var quotes = quote_container.find("div");
        
        var current_quote = 0;
        var fade_speed = 400;
        var display_time = 3500;

        quotes.hide();
        
        function cycle_next_quote() {
            quotes.eq(current_quote).fadeIn(fade_speed).delay(display_time).fadeOut(fade_speed, function() {
                current_quote = (current_quote + 1 >= quotes.length) ? 0 : current_quote + 1;
                cycle_next_quote();
            });
        }
        cycle_next_quote();
        
    },
    
    
    /* 
     * timeline[] example:

        var timeline = [
            {
                selector: "#background-holder",
                duration: 600,
                delay_next_by: 200
            },
            {
                selector: ".content",
                duration: 600,
                delay_next_by: 200
            },
            {
                selector: "article h1, article h2, article h3, article p, article blockquote, article li, article .social-toolbar",
                duration: 600,
                delay_next_by: 50
            },
            {
                selector: "footer",
                duration: 400,
                delay_next_by: 0
            }
        ];
     *
     * optional options[] example:
     
         var options = {
             build_in: true,
             build_out: true
         }
     
     * add class "do-not-animate" to any link to avoid triggering exit animation
     */
    page_transitions: function(timeline, options) {
        
        if(!timeline) {
            return false;
        }
        
        if(!options) {
            var options = {
                build_in: true,
                build_out: true
            }
        }
        
        if(typeof jQuery.fn.reverse == "undefined") {
            jQuery.fn.reverse = [].reverse;
        }
        
        var timeline_current_time = 0;

        // add ff to each element to be animated
        $(timeline).each(function(index) {
            $(timeline[index].selector).addClass("ff");
        });
        
        function animate_frame(timeline_index, reverse_selector) {
            
            if(!$(timeline[timeline_index].selector).length) {
                return; // element not on page
            }
        
            if(reverse_selector) {
                var frame_contents = $(timeline[timeline_index].selector).reverse();
                var fade_to_opacity = 0;
            } else {
                var frame_contents = $(timeline[timeline_index].selector);
                var fade_to_opacity = 1;
            }
        
            frame_contents.each(function() {
                $(this).css({visibility: 'visible'}).delay(timeline_current_time).fadeTo(timeline[timeline_index].duration, fade_to_opacity);
                timeline_current_time += timeline[timeline_index].delay_next_by;
            });
            
        }
        
        if(options.build_in) {
            // hide all elements in the timeline
            $(timeline).each(function(index) {
                $(timeline[index].selector).css({visibility: 'hidden', opacity: 0});
            });
        
            $(window).load(function() {
            
                // run timeline
                $(timeline).each(function(index) {
                
                    animate_frame(index);
                
                });
            
            });
        }
        
        if(options.build_out) {
            $("a").not(function() {
                
                // if any of these evaluate to true, clicking the link will not trigger a build_out animation.
                return this.href.match(/^mailto\:/) ||
                this.hostname != location.hostname ||
                this.target == "_blank" ||
                this.getAttribute("href").charAt(0) == "#" ||
                $(this).hasClass("do-not-animate")
                
            }).click(function(event) {
            
                event.preventDefault();
                
                var t = this;
                timeline_current_time = 0;
            
                $(timeline.reverse()).each(function(index) {
                
                    animate_frame(index, true);
                                
                });
            
                // go to the link href
                setTimeout(function() {
                    window.location = $(t).attr("href");
                }, timeline_current_time);
                
            });
        }
    },
    
    init : function() {
        
        var t = this;
        
        t.set_ieversion();
        
        $.preloadCssImages();
                
        if(typeof t.widont_tags != "undefined" && t.widont_tags != "") {
            t.widont($(t.widont_tags));
        }
        
        // page specific
        var template = $(".content").attr("id");
        switch(template) {
            case "home":
                t.home_page();
                break;
        }
        
        $('.wrapper').css({'visibility':'visible'});
        
        t.scroll_panes();

        // page transitions
        if(!t.ieversion) {
            // splash page timeline
            if($("#splashcontent").length) {
                var default_transitions = [
                    {
                        selector: "#background-holder",
                        duration: 600,
                        delay_next_by: 300
                    },
                    {
                        selector: 'h1',
                        duration: 600,
                        delay_next_by: 400
                    },
                    {
                        selector: "h2, h2 span",
                        duration: 600,
                        delay_next_by: 200
                    },
                    {
                        selector: "p, #splashawards",
                        duration: 600,
                        delay_next_by: 200
                    }
                ];
                var options = {
                    build_in: true,
                    build_out: false
                }
            
                t.page_transitions(default_transitions, options);
            
            } else {
            
                // regular timeline
            
                var default_transitions = [
                    {
                        selector: "#background-holder",
                        duration: 600,
                        delay_next_by: 200
                    },
                    {
                        selector: ".content, .content figure",
                        duration: 600,
                        delay_next_by: 200
                    },
                    {
                        selector: "article h1, article h2, article h3, article p, article blockquote, article li, article .social-toolbar",
                        //selector: "article",
                        duration: 550,
                        delay_next_by: 50
                    },
                    {
                        selector: "footer",
                        duration: 400,
                        delay_next_by: 0
                    }
                ];
            
                t.page_transitions(default_transitions);
            
            }
        }
                
    }
    
}


$(document).ready(function() {

    babicka.init();
    
});
