/**
 * Include the MP3 player in teh given place,
 * automatically adapting colors as necessary.
 * @copyright 2008-2009 TRIBAX <http://www.tribax.com>
 * @author Matthias Sommerfeld
 * @version 0.2.0 2009-02-12
 */
var pathSWF = '/swf/mp3player.swf';
var pathMP3 = '/mp3';
var cookieName = 'cs';
/**
 * Call this function for including a MP3 player where you need it
 * @param string  file name (relative URI or absolute URL)
 * @param string  caption (i.e. Artist and Song Name) to show in the player
 * @param mixed  Either the ID of the container element as a string or the elemnt itself as object
 * @since 0.0.1
 */
function mp3_include(container, fileName) {
    var finalURL, bgColor, txtColor, barBgColor, posBarColor;

    if (typeof(container) != 'object') {
        container = document.getElementById(container);
        if (typeof(container) != 'object') return; // Element not found
    }

    finalURL = pathSWF + '?animation=no&amp;width=360&amp;animation=no&amp;initialvolume=100&amp;transparentpagebg=yes&amp;soundFile=';
    finalURL += encodeURIComponent(pathMP3 + '/' + fileName + '/' + mp3_getCookie(cookieName) + '/');

    bgColor = getStyle(container, 'background-color');
    if (!bgColor) bgColor = getStyle(container, 'background');
    txtColor = getStyle(container, 'color');
    barBgColor = getStyle(container, 'border-top-color');
    if (!barBgColor) barBgColor = getStyle(container, 'border-color');

    finalURL += '&amp;bg=' + rgb2Hex(txtColor, true);
    finalURL += '&amp;leftbg=' + rgb2Hex(barBgColor, true);
    finalURL += '&amp;rightbg=' + rgb2Hex(barBgColor, true);
    finalURL += '&amp;rightbghover=' + rgb2Hex(barBgColor, true);
    finalURL += '&amp;voltrack=' + rgb2Hex(bgColor, true);
    finalURL += '&amp;volslider=' + rgb2Hex(txtColor, true);
    finalURL += '&amp;lefticon=' + rgb2Hex(txtColor, true);
    finalURL += '&amp;righticon=' + rgb2Hex(txtColor, true);
    finalURL += '&amp;righticonhover=' + rgb2Hex(txtColor, true);
    finalURL += '&amp;loader=' + rgb2Hex(barBgColor, true);

    $('#'+container.id).empty().append('<object height="25" width="275" data="/swf/mp3player.swf" id="' +container.id + '_mp3" type="application/x-shockwave-flash">'+
        '<param name="quality" value="high" />'+
        '<param name="movie" value="/swf/mp3player.swf" />'+
        '<param name="wmode" value="transparent" />'+
        '<param name="allowScriptAccess" value="sameDomain" />'+
        '<param name="flashvars" value="movie='+finalURL+'" />'+
        '</object>');                
}

/**
 * Just a simple cookie function
 * @param string  name of the cookie key to get the value for
 * @return string|null  Value of the cookie if found, NULL if not
 * @since 0.0.1
 */
function mp3_getCookie(cookieName) {
    var results = document.cookie.match('(^|;) ?' + cookieName + '=([^;]*)(;|$)');
    return (results) ? (unescape(results[2])) : null;
}