(function($) {
    
    
    var phenomenon = $.phenomenon = {};
    
    
    $.phenomenon.slideshareCarousel = function() {
        
        // Make the slideshows embed.
        var slideshows = $("#slideshare-slideshows a");
        
        slideshows.click(function() {
            var slideshow = $(this);
            var embed = $(slideshow.attr("data-embed"));
            Shadowbox.open({
                content: embed.find("object").wrap("<div>").parent().html() + '<div class="slideshare-caption">' + slideshow.attr("data-description") + '</div>',
                player: "html",
                title: slideshow.attr("title"),
                height: 423,
                width: 425
            })
            return false;
        });
        
    }
    
    
    $.phenomenon.playVimeo = function(url) {
        $.getJSON("http://vimeo.com/api/oembed.json?callback=?", {
            url: url,
            autoplay: "true",
            width: 640,
            height: 360
        }, function(data) {
            Shadowbox.open({
                content: '<div class="vimeo-player">' + data.html + '<div class="vimeo-caption">' + data.description + "</div></div>",
                player: "html",
                title: data.title,
                height: 423,
                width: 640
            });
        });
    }
    
    
    $.phenomenon.vimeoCarousel = function() {
        
        // Make the channel tabs work.
        
        var tabs = $("#vimeo-channel-tabs a");
        var channels = $("#vimeo-channels > li");
        
        tabs.click(function() {
            var tab = $(this);
            // Set the here class.
            tabs.not(tab).removeClass("here");
            tab.addClass("here");
            // Show the right channel.
            var channel = $(tab.attr("href"));
            channels.not(channel).hide();
            channel.show();
            // Stop propagation.
            return false;
        });

        // Allow deep-linking.
        var oneSelected = false;
        tabs.each(function() {
            var tab = $(this);
            if (tab.attr("href") == location.hash) {
                tab.click();
                oneSelected = true;
            }
        });
        if (!oneSelected) {
            tabs.eq(0).click();
        }
        
        // Make the videos open in shadowbox.
        
        var videos = channels.find(".vimeo-video");
        videos.click(function() {
            var video = $(this).find("img");
            phenomenon.playVimeo(video.attr("data-video-url"));
        });
        
    }
        
        
    $.phenomenon.speakerCarousel = function() {
    
        // Create the speaker carousel.
        
        var speakerImages = $(".speaker-image");
        var speakers = $(".speaker");
        
        function selectSpeaker() {
            speakerImage = $(this);
            speakerImages.removeClass("active");
            speakerImage.addClass("active");
            speakers.hide();
            speakers.filter(speakerImage.attr("href")).show();
            return false;
        }
        
        speakerImages.click(selectSpeaker);
        selectSpeaker.call(speakerImages.eq(Math.floor(Math.random() * speakerImages.length)));
        
    }
       
        
    $.phenomenon.sponsorCarousel = function() {
            
        // Create the sponsor carousel.
        
        var sponsorPages = $(".sponsor-page");
        var currentPage = 0;
        var numPages = sponsorPages.length;
        
        function showPage(index) {
            if (index == currentPage) {
                return;
            }
            sponsorPages.eq(currentPage).fadeOut("slow");
            sponsorPages.eq(index).fadeIn("slow");
            currentPage = index;
        }
        
        function nextPage() {
            var nextPage = currentPage + 1;
            if (nextPage >= numPages) {
                nextPage = 0;
            }
            showPage(nextPage);
        }
        
        sponsorPages.slice(1).hide();
        sponsorPages.css({
            position: "absolute",
            top: "0px",
            left: "0px"
        });
        
        setInterval(nextPage, 8000);
            
    }
    
        
    $.phenomenon.speakers = function() {
        
        // Create the speaker bio expansion.
        
        var speakerBios = $(".speaker-profile");
        
        speakerBios.each(function() {
            var fullBio = $(".full-bio", this).hide();
            var showHide = $(".display-full-bio", this);
            showHide.click(function() {
                if (fullBio.is(":visible")) {
                    fullBio.slideUp();
                    showHide.html("Full profile");
                } else {
                    fullBio.slideDown();
                    showHide.html("Hide full profile");
                }
                return false;
            });
        });
        
        if (window.location.hash.length > 0) {
            var activeSpeaker = $("#" + window.location.hash.substr(1));
            $(".full-bio", activeSpeaker).show();
            $(".display-full-bio", activeSpeaker).html("Hide full profile");
            
            // Re-jump the page now that the speaker bios have collapsed.
            window.location.hash = window.location.hash;
        }
    }
    
    
    $(function() {
       
       // Add inline playback to vimeo links.
       $('a.vimeo-play').click(function() {
           var link = $(this);
           $.phenomenon.playVimeo(link.attr("href"));
           return false;
       });
        
    });
    
   
})(jQuery);
