
function debug(x) {
    if (window && window.console && window.console.debug) window.console.debug(x);
}

function hoverOver() {
    var desc = this.getAttribute('title');
    if (!desc) return;
    if (!window.bubb) {
        bubb = document.createElement('span');
        bubb.style.background = 'url(/img/bubble.png)';
        bubb.style.position = 'absolute';
        bubb.style.verticalAlign = 'middle';
        bubb.style.width='320px';
        bubb.style.height='60px';
        bubb.style.marginTop='-40px';
        bubb.style.padding = '20px 30px 20px 50px';
    }
    if (bubb.parentNode) bubb.parentNode.removeChild(bubb);
    bubb.innerHTML = desc;
    this.parentNode.appendChild(bubb);
}

function hoverOut() {
    if (window.bubb && bubb.parentNode) bubb.parentNode.removeChild(bubb);
}

function switchToLowBandwidth() {
    var c = $('embed');
    for (var i = 0; i < c.length; i++) {
        if (c[i].src.match(/blip.tv\/play/))
            c[i].src = c[i].src + "?allowm4v=true";
    }
    var c = $('param[name=src]');
    for (var i = 0; i < c.length; i++) {
        if (c[i].value.match(/blip.tv\/play/))
            c[i].value = c[i].value + "?allowm4v=true";
    }
    $('a.bwswitch').text('Switch to high-bandwidth video');
    $('a.bwswitch').attr('href', 'javascript:switchToHighBandwidth()');
}

function switchToHighBandwidth() {
    var c = $('embed');
    for (var i = 0; i < c.length; i++) {
        if (c[i].src.match(/blip.tv\/play/))
            c[i].src = c[i].src.replace(/\?allowm4v=true/, '');
    }
    var c = $('param[name=src]');
    for (var i = 0; i < c.length; i++) {
        if (c[i].value.match(/blip.tv\/play/))
            c[i].value = c[i].value.replace(/\?allowm4v=true/, '');
    }   
    $('a.bwswitch').text('Switch to low-bandwidth video');
    $('a.bwswitch').attr('href', 'javascript:switchToLowBandwidth()');
}

function postLoad() {
    if (!$('#bodyfinished')) {
        setTimeout(500, postLoad);
        return;
    }
    $('.blogroll a').hover(hoverOver, hoverOut);
    $('a:contains(Download M4V)').before("<a class='bwswitch' href='javascript:switchToLowBandwidth()'>Problems? Switch to low-bandwidth video</a> | ");
    $('a:contains(Download MP3 audio)').after("<br />Subscribe to videos: <a href='http://www.peaceamidthechaos.com/subscribe/'>E-mail</a> | <a href='http://www.peaceamidthechaos.com/feed/'>RSS</a> | <a href='http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=294181957'> iTunes (video)</a> | <a href='http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=294182306'>iTunes (audio)</a>");
    $('embed').before("<p><strong>Please <a href='http://www.peaceamidthechaos.com/subscribe/'>register</a> to receive an e-mail when a new video is posted.  That way you'll never miss a message!</strong></p>");
}

$(postLoad);
