$(document).ready(function (event) {
    var scrollables = $('.content-wrap');
    scrollables.each(function (i) {
        var scrollable = $(this),
            content = scrollable.find('.content'),
            scroller = $('<div id="scroller" />').slider({
                animate: true,
                orientation: 'vertical',
                min: 0,
                max: 100,
                value: 100,
                slide: function (event, ui) {
                    var slider = $(this),
                        position = 100 - parseInt(slider.slider('value'), 10),
                        maxHeight = content.height() - scroller.height();
                    
                    if (position == 1) {
                        position = 0;
                    } else if (position == 99) {
                        position = 100;
                    }
                    
                    content.css('marginTop', (maxHeight / 100) * (position * -1));
                }
            });
        scrollable.css('overflow', 'hidden');
        scrollable.addClass('has-scroller');
        scrollable.after(scroller);
    });
    
    if ($('#share #embed').length > 0) {
        $('#share #embed').val($('#video').html().replace(/\s{2,}/g, ''));
    }
    
    $('.copy').each(function (i) {
        var copyLink = $(this).find('a'),
            inputField = $(this).prevAll('input');
        
        var clip = new ZeroClipboard.Client();
        clip.setText(inputField.val());
        clip.glue(copyLink.attr('id'));
    });
    
    function getMovie(name) {
        if (navigator.appName.indexOf("Microsoft") != -1) {
            return window[name];
        } else {
            return document[name];
        }
    }
    
    function playActionscript() {
        var movie = getMovie('scenario');
        movie.playActionscript();
    }
    
    function userVolume() {
        var movie = getMovie('scenario');
        movie.userVolume();
    }
    
    $('body.scenario #play a').toggle(
        // Toggle: Off
        function (event) {
            var image = $(this).find('img');
            image.attr('src', '/images/scenario/pause.png');
            playActionscript();
        },
        // Toggle: On
        function (event) {
            var image = $(this).find('img');
            image.attr('src', '/images/scenario/play.png');
            playActionscript();
        }
    );
    
    $('body.scenario #sound a').toggle(
        // Toggle: Off
        function (event) {
            var image = $(this).find('img');
            image.attr('src', '/images/scenario/sound-off.png');
            userVolume();
        },
        // Toggle: On
        function (event) {
            var image = $(this).find('img');
            image.attr('src', '/images/scenario/sound-on.png');
            userVolume();
        }
    );
   
    $('#sidebar .navigation li').hover(
        function (event) {
            $(this).addClass('hover');
        }, 
        function (event) {
            $(this).removeClass('hover');
        }
   );
    
});