var vidIndex = -1;  
var vidIndices = [];  
var vidURLs = [];

$(document).ready(function () {
    insertVideos();
});

//parses blog entries for video tags and replaces them with 
//video holder div elements
function insertVideos() {
    allEntries = $('.ms-PostBody');
    //for each blog entry, parse entry and create video holders
    for (var i = 0; i < allEntries.length; i++) {
        entryContent = allEntries[i].firstChild.innerHTML;
        entryContent = entryContent.replace(/\[\[BLOGVIDEO:\s*(.*)\]\]/gi, 
            function (m) { return getHolderCode(m) });
        allEntries[i].innerHTML = entryContent;
    }

    //for each video on the page, inject the media player into 
    //the appropriate holder
    for (k = 0; k < vidURLs.length; k++) {
        insertVideo($get('videoHolder' + vidIndices[k]), vidURLs[k]);
    }
}

//provides an incremented ID for a videoHolder div element and associates 
//this videoHolder ID with the video URL
function getHolderCode(matched) {
    if (matched.match(/\[\[BLOGVIDEO:\s*(.*)\]\]/i)) vidURL = RegExp.lastParen;
    if (matched.match(/<A href="(.*)"/i)) vidURL = RegExp.lastParen;
    vidIndex++;
    vidURLs.push(vidURL);
    vidIndices.push(vidIndex);
    return "<div id='videoHolder" + vidIndex + "'></div>";
}

                
//inserts a media web part with given video into a given video holder div
function insertVideo(videoHolder, videoURL) {
    swfobject.embedSWF(videoURL, videoHolder.id, "450", "350", "9.0.0");     
}
