/*
 * EquatorialDwarf theora embedding functions © Wicher Minnaard 2009.
 * Feel free to use this under the BSD license, human-readable version
 * here: http://creativecommons.org/licenses/BSD/ 
 * Originally based on the public domain ArcticGiant script by
 * MaikMerten - see http://wiki.transmission.cc/index.php/ArcticGiant
 *
 * This script will mangle specially setup <img>-elements into embedded
 * video. Use like this:
 * <img longdesc="http://example.com/path/to/video.ogv" width="400" height="300" src="http://example.com/path/to/placeholderimage" onClick="insertVid(this);"/>
 * A Wordpress-plugin is available from http://smorgasbord.gavagai.nl/topics/EmbedTheora
 * 
*/

//Set these to your needs:
//The image you want to show if no suitable browser plugin can be found
var deadimg = "http://smorgasbord.gavagai.nl/wp-content/plugins/embedtheora/vid_dead_thumb.png";

//The text you want to display if no suitable browser plugin can be found. %OGVURL% will be replaced with the actual URL to the file.
//var explanatorytext = "You have no video media plugin that can play Theora video. <a href='/video-afspelen'>Read how to fix that</a>. Alternatively, <a href='%OGVURL%'>download the videofile</a> if you want to try and open it in a standalone mediaplayer."
var explanatorytext = "<code>Helaas, je computer is niet toegerust op het afspelen van deze video. <a href='/video-afspelen/'>Lees dit</a> als je het wilt oplossen, of <a href='%OGVURL%'>download de video</a> om te proberen 'm buiten je browser af te spelen.</code>"

//No editing beyond this point, unless you know what you're doing.

var vlcPlugin = false;
var videoElement = false;
var oggPlugin = false;
var searchedMimeTypes = false;
document.open();
document.write('<video id="nullvideo" style="display:none"></video>');
document.close();

function detectplayers()
  {
  if(document.getElementsByTagName("video").length > 0)
    {
    var myvideo = document.getElementById("nullvideo");
    if(myvideo.play) videoElement = true;
    }

  if(navigator.mimeTypes && navigator.mimeTypes.length > 0)
    {
    searchedMimeTypes = true;
    for (var i = 0; i < navigator.mimeTypes.length; i++)
      {
      if(navigator.mimeTypes[i].type.indexOf("application/ogg") > -1) oggPlugin = true;
      if(navigator.mimeTypes[i].type.indexOf("application/x-vlc-plugin") > -1) vlcPlugin = true;
      }
    }
  }

function insertVid(placeholderimg)
  {
  detectplayers();

  var ogvurl = placeholderimg.getAttribute("longdesc");
  var ogvwidth = placeholderimg.getAttribute("width");
  var ogvheight = placeholderimg.getAttribute("height");
  var elstyle = placeholderimg.getAttribute("style");
  var mamma = placeholderimg.parentNode;
  var zusje = placeholderimg.nextSibling;
  var oldimage = mamma.removeChild(placeholderimg);

  if(videoElement) mamma.insertBefore(embedVideoElement(ogvurl,ogvwidth,ogvheight,elstyle),zusje);
  else if(vlcPlugin) mamma.insertBefore(embedVlcPlugin(ogvurl,ogvwidth,ogvheight,elstyle),zusje);
  else if(oggPlugin) mamma.insertBefore(embedOggPlugin(ogvurl,ogvwidth,ogvheight,elstyle),zusje);

  else
    {
    var deaddiv = document.createElement("div");
    oldimage.setAttribute("src",deadimg);
    oldimage.setAttribute("style",elstyle);
    oldimage.setAttribute("onClick","return;");
    deaddiv.appendChild(oldimage);
    var exptext = document.createElement("p");
    exptext.innerHTML=explanatorytext.replace(/%OGVURL%/g,ogvurl);
    deaddiv.appendChild(exptext);
    mamma.insertBefore(deaddiv,zusje);
    }
  }

function embedVideoElement(waaro, ogvwidth, ogvheight, elstyle)
  {
  var videoObj = document.createElement("video");
  videoObj.setAttribute("src",waaro);
  videoObj.setAttribute("controls",true);
  videoObj.setAttribute("style",elstyle);

  //~ videoObj.setAttribute("width",ogvwidth);
  //~ videoObj.setAttribute("height",ogvheight);
  videoObj.setAttribute("autoplay","true");
  return videoObj;
  }

function embedOggPlugin(waaro, ogvwidth, ogvheight, elstyle)
  {
  var videoObj = document.createElement("object");
  videoObj.setAttribute("data",waaro);
  videoObj.setAttribute("width",ogvwidth);
  videoObj.setAttribute("height",ogvheight);
  videoObj.setAttribute("style",elstyle);
  videoObj.setAttribute("type","application/ogg");
  return videoObj;
  }

function embedVlcPlugin(waaro, ogvwidth, ogvheight, elstyle)
  {
  var videoObj = document.createElement("embed");
  videoObj.setAttribute("target",waaro);
  videoObj.setAttribute("width",ogvwidth);
  videoObj.setAttribute("height",ogvheight);
  videoObj.setAttribute("style",elstyle);
  videoObj.setAttribute("type","application/x-vlc-plugin");
  videoObj.setAttribute("pluginspage","http://www.videolan.org");
  videoObj.setAttribute("version","VideoLAN.VLCPlugin.2");
  return videoObj;
  }
