//
// Phil's Super Amazing Flowplayer Plugin..
// This bit of grotty javascript is to make it easy to embed videos in web pages by creating
// special div tags with a class "mce-flowplayer", and some cunning stuff in the ID tag.
// This function recursively traverses the DOM looking for these and does the magic javascript
// to get the video playing using flowplayer and flashembed.
//

//
// Copyright 2008, Philip Scott. All rights reserved.
//

function doSubstitution(node,depth)
{ 
    if(node.attributes != null && node.attributes["class"] != null && node.attributes["class"].value == "mce-flowplayer")
    {
      	id = node.attributes["id"].value;
        info = eval('(' + id + ')');
        
      	newid = "flowplayer" + Math.floor(Math.random()*1000000000);
      	node.id = newid;
      	node.setAttribute("style", "width:" + info.width + "px;height:" + info.height + "px;")
      	
      	// delete any child nodes as this confuses flowplayer
        if ( node.hasChildNodes() )
        {
            while ( node.childNodes.length >= 1 )
            {
                node.removeChild( node.firstChild );       
            } 
        }      	
        flowplayer(newid, "/files/flash/flowplayer-3.1.1.swf", { playlist: [info.image, {url: info.video, autoPlay: false}] });	
    }
    
    for(var local_i = 0; local_i < node.childNodes.length; local_i++)
    {
        doSubstitution(node.childNodes[local_i], depth+1);
    }
}

window.addEvent('domready', function() {
     doSubstitution(document.documentElement,0);
});