/**
Created: 20081110
Original Author: by Fabricio Zuardi (http://www.hideout.com.br)
Original URL: http://userscripts.org/scripts/review/8753

Extended by:  Steve Moitozo <god at zilla dot us> -- geekwisdom.com
Description: This is an example of a set of JavaScript functions for 
             automatically inserting inline audio players after 
             links to MP3 files.

             You probably won't want to insert three players
             for every link but this shows you how simple it is.
License: MIT License (see below)
---------------------------------------------------------------
Copyright (c) 2008 Steve Moitozo <god at zilla dot us>

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without 
restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following 
conditions:

   The above copyright notice and this permission notice shall 
be included in all copies or substantial portions of the 
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
OR OTHER DEALINGS IN THE SOFTWARE. 
---------------------------------------------------------------
*/

/**
* This function adds a Flash-based MP3 player button
* immediately following any link to an MP3 file.
*
* The function is designed to be executed by the body
* OnLoad event. This can take place by executing the 
* function directly or by calling the function from
* within another function.
*
* The function uses XSPF player to render the button
* player. So a working installation of XSPF player
* needs to exist somewhere.
*
* The button can be styled using CSS by styling the
* "mp3button" class. This can also be used to hide
* the buttons on a page by page basis. Here's the CSS
* to hide the buttons:
*
* <style type="text/css">
* .mp3button {
*   visibility: hidden;
*   width: 0;
*   height: 0;
* }
* </style>
*
* NOTE: The function is designed to be executed once per page
* rendering. Executing it multiple times will result in
* multiple buttons per link.
*
* @return void
*/ 
function insertButtonMp3Players() {
    // configure the path to the XSPF button player
    var xspfButtonPlayerUrl = "http://www.geekwisdom.com/xspfplayer/button/musicplayer.swf";

    // get all the links in the page
    var page_links = document.links;

    // loop over all the links
    for (var i=0; i<page_links.length; i++){

        // if the link references an MP3 file
        if (page_links[i].href.match(/\.mp3$/i)) {
	   
            // insert a span tag that contains the code for embedding the player button
            var span = document.createElement("span");
            var url = xspfButtonPlayerUrl + "?song_url="+escape(page_links[i].href)+"&song_title="+escape(page_links[i].innerHTML);
            var width = 17;
            var height = 17;
            code_str = "";
            code_str += " <object class=\"mp3button\" type=\"application/x-shockwave-flash\"\n";
            code_str += "data=\""+url+"\" \n";
            code_str += "width=\""+width+"\" height=\""+height+"\">\n";
            code_str += "<param name=\"movie\" \n";
            code_str += "value=\""+url+"\" />\n";
            code_str += "<param name=\"wmode\" \n";
            code_str +=    "value=\"transparent\" />\n";
            code_str += "</object>\n";
            span.innerHTML = code_str;
            page_links[i].parentNode.insertBefore(span, page_links[i].nextSibling);
        }
    }
}

/**
* This function adds a slim Flash-based MP3 player
* immediately following any link to an MP3 file.
*
* The function is designed to be executed by the body
* OnLoad event. This can take place by executing the 
* function directly or by calling the function from
* within another function.
*
* The function uses XSPF player to render the
* player. So a working installation of XSPF player
* needs to exist somewhere.
*
* The player can be styled using CSS by styling the
* "slimmp3player" class. This can also be used to hide
* the buttons on a page by page basis. Here's the CSS
* to hide the buttons:
*
* <style type="text/css">
* .slimmp3player {
*   visibility: hidden;
*   width: 0;
*   height: 0;
* }
* </style>
*
* NOTE: The function is designed to be executed once per page
* rendering. Executing it multiple times will result in
* multiple buttons per link.
*
* @return void
*/ 
function insertSlimMp3Players() {
    // configure the path to the XSPF button player
    var xspfPlayerUrl = "http://www.geekwisdom.com/xspfplayer/xspf_player_slim.swf";

    // get all the links in the page
    var page_links = document.links;

    // loop over all the links
    for (var i=0; i<page_links.length; i++){

        // if the link references an MP3 file
        if (page_links[i].href.match(/\.mp3$/i)) {
	   
            // insert a span tag that contains the code for embedding the slim player
            var span = document.createElement("span");
            var url = xspfPlayerUrl + "?song_url="+escape(page_links[i].href)+"&player_title=Play+audio&info_button_text=Play+Audio&song_title="+escape(page_links[i].innerHTML);
            var width = 220;
            var height = 17;
            code_str = "";
            code_str += " <object class=\"slimmp3player\" type=\"application/x-shockwave-flash\"\n";
            code_str += "data=\""+url+"\" \n";
            code_str += "width=\""+width+"\" height=\""+height+"\">\n";
            code_str += "<param name=\"movie\" \n";
            code_str += "value=\""+url+"\" />\n";
            code_str += "<param name=\"wmode\" \n";
            code_str +=    "value=\"transparent\" />\n";
            code_str += "</object>\n";
            span.innerHTML = code_str;
            page_links[i].parentNode.insertBefore(span, page_links[i].nextSibling);
        }
    }
}

/**
* This function adds the Flash-based MP3 player from 1pixelout
* immediately following any link to an MP3 file.
*
* The function is designed to be executed by the body
* OnLoad event. This can take place by executing the 
* function directly or by calling the function from
* within another function.
*
* The function uses XSPF player to render the
* player. So a working installation of the 1pixelout player
* needs to exist somewhere.
*
* The button can be styled using CSS by styling the
* "mp3button" class. This can also be used to hide
* the buttons on a page by page basis. Here's the CSS
* to hide the buttons:
*
* <style type="text/css">
* .pixeloutmp3player {
*   visibility: hidden;
*   width: 0;
*   height: 0;
* }
* </style>
*
* NOTE: The function is designed to be executed once per page
* rendering. Executing it multiple times will result in
* multiple buttons per link.
*
* @return void
*/ 
function insert1PixelOutMp3Players() {
    // configure the path to the XSPF button player
    var playerUrl = "/audioplayer/player.swf";

    // get all the links in the page
    var page_links = document.links;

    // loop over all the links
    for (var i=0; i<page_links.length; i++){

        // if the link references an MP3 file
        if (page_links[i].href.match(/\.mp3$/i)) {
	   
            // insert a span tag that contains the code for embedding the player
            var span = document.createElement("span");
            var url = playerUrl;
            var width = 290;
            var height = 24;
            code_str = "";
            code_str += " <object class=\"pixeloutmp3player\" type=\"application/x-shockwave-flash\"\n";
            code_str += "data=\""+url+"\" \n";
            code_str += "width=\""+width+"\" height=\""+height+"\">\n";
            code_str += "<param name=\"movie\" \n";
            code_str += "value=\""+url+"\" />\n";
            code_str += "<param name=\"FlashVars\" \n";
            code_str +=    "value=\"playerID=1&amp;soundFile="+escape(page_links[i].href)+"\" />\n";
            code_str += "<param name=\"quality\" value=\"high\">\n";
            code_str += "<param name=\"menu\" value=\"false\">\n";
            code_str += "<param name=\"wmode\" value=\"transparent\">\n";
            code_str += "</object>\n";
            span.innerHTML = code_str;
            page_links[i].parentNode.insertBefore(span, page_links[i].nextSibling);
        }
    }
}

/**
* This function executes both of the MP3 player insertion
* functions and assumes that CSS is being used to suppress
* one or the other.
*
* @return void
*/ 
function insertAudioPlayers(){
	insertButtonMp3Players();
	insertSlimMp3Players();
	insert1PixelOutMp3Players();
}

