var minicaster_player_config =
{
    'swf_path': 'http://morcegaofm.com.br/batplayer/minicaster.swf',
    'swfobject': 'http://morcegaofm.com.br/batplayer/swfobject.js',
    'theme': 'http://morcegaofm.com.br/batplayer/black/minicaster.css'
};

document.write(
        '<div id="minicaster-player-wrapper" style="visibility:hidden">'
    +   '<a href="http://morcegaofm.com.br/">'
    +   '   <img src="http://morcegaofm.com.br/wp-content/themes/Imprezz/images/logo.png" />'
    +   '</a>'
    +   '<div id="minicaster-player" class="minicaster-player-black">'
    +   '   <div id="minicaster-player-playstop"></div>'
    +   '   <a href="http://morcegaofm.com.br/morcegao.m3u"><div id="minicaster-player-visor">'
    +   '       <div id="minicaster-player-visor-text"></div>'
    +   '       <div id="minicaster-player-visor-text-click">Clique para ouvir</div>'
    +   '   </div></a>'
    +   '   <span class="minicaster-player-clear"></span>'
    +   '   <div id="minicaster-links">'
    +   '      <a href="http://morcegaofm.com.br/32kbps.m3u"><span>32</span>kbps</a>'
    +   '      <a href="http://morcegaofm.com.br/64kbps.m3u"><span>64</span>kbps</a>'
    +   '      <a href="http://morcegaofm.com.br/128kbps.m3u"><span>128</span>kbps</a>'
    +   '  </div>'
    +   '</div>'
    +   '<div id="minicaster-swfobject"></div>'
    +   '</div>'
);

var old_window_onload = window.onload;

window.onload = function()
{
    if (typeof old_window_onload == 'function')
    {
        old_window_onload();
    }

    var head = document.getElementsByTagName('head');

    if (head.length == 0)
    {
        return alert('missing <head> tag');
    }

    minicaster_player = new minicaster_player_obj();

    var css = document.createElement('link');

    css.setAttribute('type', 'text/css');
    css.setAttribute('rel', 'stylesheet');
    css.setAttribute('href', minicaster_player_config.theme)

    head[0].appendChild(css);

    var swfobject = document.createElement('script');

    swfobject.setAttribute('type', 'text/javascript');
    swfobject.setAttribute('src', minicaster_player_config.swfobject)

    head[0].appendChild(swfobject);

    minicaster_onload_css();
}

function minicaster_onload_css()
{
    if (minicaster_player.playbutton.offsetHeight == 30)
    {
        minicaster_onload_swf();
    }
    else setTimeout(minicaster_onload_css, 2000);
}

function minicaster_onload_swf()
{
    if (typeof SWFObject != 'undefined')
    {
        /*var so = new SWFObject(minicaster_player_config.swf_path, 'minicaster-fmp', '1', '1', '9', '#FF6600');

        so.addParam('quality', 'low');
        so.addParam('wmode', 'transparent');
        so.addParam('allowScriptAccess', 'always');

        so.write('minicaster-swfobject');

        minicaster_player.object = document.getElementById('minicaster-fmp');*/

        var player = document.getElementById('minicaster-player-wrapper');

        player.style.visibility = 'visible';

        minicaster_player.doUpdateTitle();
    }
    else setTimeout(minicaster_onload_swf, 2000);
}

var minicaster_player;
var minicaster_player_obj = function()
{
    var self = this;

    this.title = 'Morcegão FM';
    this.state = 0;

    this.song = '';

    this.object; // will be defined on swfobject startup

    this.playbutton = document.getElementById('minicaster-player-playstop');
    this.visor = document.getElementById('minicaster-player-visor-text');

    this.visor_width = this.visor.parentNode.offsetWidth;
    this.visor_text_width = this.visor.offsetWidth;
    this.visor.style.left = Math.ceil(this.visor_text_width/2)+'px';

    this.toUpdate = false;
    this.firstRun = true;

    this.playbutton.onclick = this.toggle = function()
    {
        self.object.playpause(1);
    }

    this.setState = function(on)
    {
        var isMSIE = /*@cc_on!@*/false;
        if (isMSIE) this.playbutton.className = on ? 'minicaster-player-playing' : '';
        else this.playbutton.setAttribute('class', on ? 'minicaster-player-playing' : '');
        this.state = on;
    }

    this.updateTitle = function()
    {
        self.toUpdate = true;
    }

    this.doUpdateTitle = function()
    {
        self.visor.innerHTML = self.title + (self.song && !/\[vinheta\]/.test(self.song) ? ' - ' + self.song : '');
        self.visor_text_width = self.visor.offsetWidth;
        self.visor_width = self.visor.parentNode.offsetWidth;
        self.toUpdate = false;
    }

    this.updateVisor = function()
    {
        with (self.visor.style)
        {
            if ((parseInt(left) + self.visor_text_width) < 0)
            {
                if (self.toUpdate)
                {  
                    self.doUpdateTitle();
                }
                left = (self.visor_width - 10) + 'px';
            }
            else
            {
                left = (parseInt(left) - 3) + 'px';
            }
        }

        setTimeout(self.updateVisor, 100);
    }

    this.updateVisor();

    get_current_song();

    return self;
};

function xtrace(_st)
{
    switch (parseInt(_st.substr(0, 3)))
    {
        case 114:
            if (__minicaster_autoplay)
            {
                setTimeout(minicaster_player.toggle, 200);
                minicaster_player.object.parentNode.style.marginTop = '1px';
            }
            break;
        case 120: // play
            minicaster_player.setState(1);
            minicaster_player.updateTitle(minicaster_player.title);
            break;
        case 106: // stream title
            minicaster_player.title = _st.split(':')[2];
            minicaster_player.updateTitle(minicaster_player.title);
            break;
        case 501: // stop
            if (/button\spress/.test(_st))
            {
                minicaster_player.setState(0);
                minicaster_player.updateTitle(minicaster_player.title);
            }
            break;
    }
}

function player_current_song(song)
{
    if (typeof minicaster_player != 'object')
    {
        return;
    }

    song = song.replace(/^[\s-]+|[\s-]+$/g, '');

    if (minicaster_player.song != song)
    {
        minicaster_player.song = song;
        minicaster_player.updateTitle(minicaster_player.title);
    }
}

function get_current_song()
{
    var url = 'http://morcegaofm.com.br/currentsong.php';

    var head = document.getElementsByTagName('head')[0];
    var scripts = head.getElementsByTagName('script');

    for (var i in scripts)
    {
        if (scripts[i].src && /\?rand/.test(scripts[i].src))
        {
            head.removeChild(scripts[i]);
            break;
        }
    }

    var current_song_script = document.createElement('script');

    current_song_script.type = 'text/javascript';
    current_song_script.src = url + '?rand' + Math.random();

    head.appendChild(current_song_script);

    setTimeout(get_current_song, 7000);
}