
    var sdrive = {};
    sdrive.jQuery = jQuery.noConflict(true);
    sdrive.jQuery(document).ready(function($) {

    if($.fn.colorbox){
        $('#stream_subscribe').click(function(event){
            event.preventDefault();
            $.fn.colorbox({ href: subscribe_form_action, width:"700px", height:"400px", opacity:"0.5", iframe:true});
        });
    }

    var i = 0;

    // scan the stream posts for urls that end in certain suffixes (mp3, m4a, oga, mp4, ogv and webm)

    $("a[href$=.mp3]").each(function(){
        i++;
        addPlayer($(this), i, "mp3");
    });

    $("a[href$=.m4a]").each(function(){
        i++;
        addPlayer($(this), i, "m4a");
    });

        
    $("a[href$=.m4v]").each(function(){
        i++;
        addPlayer($(this), i, "m4v");
    });


    function addPlayer(link, i, suffix) {
        var text = link.text();
        var href = link.attr('href');

        var url_pieces = href.split("/");
        var file_name = url_pieces [ url_pieces.length - 1 ];
    
        var player_id = 'jquery_jplayer_' + i;
        var interface_id = 'jp_interface_' + i;

        var is_audio = false;
        
        if (suffix == "mp3" || suffix == "oga" || suffix == "m4a" || suffix == "wav") {
            is_audio = true;
        }    

        // this removes the original a tag from the post and replaces it with the player
        
        link.replaceWith(newMarkup(link, i, file_name, is_audio, player_id, interface_id));
        
        if (suffix == "m4v") {

            $('#' + player_id).jPlayer({
                ready: function () {
                    $(this).jPlayer("setMedia", {
                        m4v: href
                    });
                },
                swfPath: "http://static.coffeecup.com/javascript/jplayer",
                supplied: "m4v",
                solution: "flash,html",
                cssSelectorAncestor: '#' + interface_id
            }).bind($.jPlayer.event.play, function() {
                $(this).jPlayer("pauseOthers");
            });
            
        } else if (suffix == "m4a") {
            $('#' + player_id).jPlayer({
                ready: function () {
                    $(this).jPlayer("setMedia", {
                        m4a: href
                    });
                },
                swfPath: "http://static.coffeecup.com/javascript/jplayer",
                supplied: "m4a",
                solution: "html, flash",
                cssSelectorAncestor: '#' + interface_id
            }).bind($.jPlayer.event.play, function() {
                $(this).jPlayer("pauseOthers");
            });            
                        
        } else if (suffix == "mp3") {

            $('#' + player_id).jPlayer({
                ready: function () {
                    $(this).jPlayer("setMedia", {
                        mp3: href
                    });
                },
                swfPath: "http://static.coffeecup.com/javascript/jplayer",
                supplied: "mp3",
                solution: "html,flash",
                cssSelectorAncestor: '#' + interface_id
            }).bind($.jPlayer.event.play, function() {
                $(this).jPlayer("pauseOthers");
            });            

        }
    }

    // this function generates the appropriate markup for the player to live in
    // the is_audio flag determines which player div to use
    function newMarkup(link, i, file_name, is_audio, player_id, interface_id) {

        var markup = "";

        if (is_audio) {
            markup = '<div class="jp-audio">';
        } else {
            markup = '<div class="jp-video jp-video-270p">';            
        }

        markup = markup +
        '    <div class="jp-type-single">' +
        '    <div id="' + player_id + '" class="jp-jplayer"></div>' +
        '        <div id="'+ interface_id +'" class="jp-interface">';

        if (!is_audio) {
            markup = markup + '<div class="jp-video-play"></div>';
        }

        markup = markup +
        '            <ul class="jp-controls">' +
        '                <li><a href="#" class="jp-play" tabindex="1">play</a></li>' +
        '                <li><a href="#" class="jp-pause" tabindex="1">pause</a></li>' +
        '                <li><a href="#" class="jp-stop" tabindex="1">stop</a></li>' +
        '                <li><a href="#" class="jp-mute" tabindex="1">mute</a></li>' +
        '                <li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>' +
        '            </ul>' +
        '            <div class="jp-progress">' +
        '                <div class="jp-seek-bar">' +
        '                    <div class="jp-play-bar"></div>' +
        '                </div>' +
        '            </div>' +
        '            <div class="jp-volume-bar">' +
        '                <div class="jp-volume-bar-value"></div>' +
        '            </div>' +
        '            <div class="jp-current-time"></div>' +
        '            <div class="jp-duration"></div>' +
        '        </div>' +
        '        <div id="jp_playlist_' + i + '" class="jp-playlist">' +
        '            <ul>' +
        '                <li>' + file_name + '</li>' +
        '            </ul>' +
        '        </div>' +
        '    </div>' +
        '</div>';
        
        return markup;
    }

});

