/* underlying assumption for this file as a whole:
   all OBJECT are youtube videos unless their ID contains "FlashID";
   all EMBED are youtube videos                                        */ 


function fetchObject()
{
    var objlist = document.getElementsByTagName('object');
    var h;
    for(h=0;h<objlist.length;h++)
    {
       if(!objlist[h].id || (objlist[h].id.indexOf("flowplayer") < 0 && objlist[h].id.indexOf("FlashID") < 0))
       {  /* latter clause intended to not replace Flash menus */ 
          return objlist[h];
       }
    }   
    /* if there are no OBJECTs, this might be IE6, so try a different way.
       This because IE6 incorrectly constructs the DOM when there if free
       text inside an OBJECT.
       
       Underlying assumption: EMBED is used only for youtube videos.      */
    
    if(/msie|MSIE 6/.test(navigator.userAgent)) 
    {
        objlist = document.getElementsByTagName('embed');
        return (objlist && objlist.length !=0)?objlist[0]:null;
    }
}

function sortNumber(a,b)
{
  return a - b;
}


function FixMissingYouTube()
{
  if(typeof addVideosToQuicklist != 'function')
  {
    var obj;
    for(var counter=0;obj = fetchObject();counter++)
    {
      width = obj.width;
      height = obj.height;
      var par = obj.parentNode;
      var j;
      var url=null;
      
      for (j=0;obj.childNodes && j<obj.childNodes.length;j++)
      {
        if(obj.childNodes[j].tagName && obj.childNodes[j].tagName == "PARAM" && obj.childNodes[j].name && obj.childNodes[j].name == "movie" && obj.childNodes[j].value)
        {
          url = obj.childNodes[j].value;
          break;
        } 
      }
      
      url = (!url && obj.tagName == 'EMBED')?obj.src:url; /* catching IE6 case where OBJECT may be missing from DOM */
      if(url)
      {
        var firstamp    = url.indexOf("&");     firstamp = (firstamp<0)?url.length:firstamp;
        var firstquery  = url.indexOf("?");     firstquery = (firstquery<0)?url.length:firstquery;
        var firstequals = url.indexOf("=");     firstequals = (firstequals<0)?url.length:firstequals;
        var lastslash   = url.lastIndexOf("/"); lastslash = (lastslash<0)?url.length:lastslash;
        
        var positions  = new Array (firstamp,firstquery,firstequals,lastslash);
        positions.sort(sortNumber);        
        
        url = url.substring(positions[0]+1,positions[1]);
        var ptemp = document.createElement('P');
        ptemp.innerHTML = '<object width="' + width + '" height="' + height + '" id="flowplayer' + counter + '" name="_player" data="/flowplayer/flowplayer-3.2.2.swf" type="application/x-shockwave-flash"><param name="movie" value="/flowplayer/flowplayer-3.2.2.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="flashvars" value=\'config={"clip":{"url":"http://www.plumis.co.uk/videos/'+url+'.flv","autoPlay":false,"autoBuffering":true}}\' /></object>';
        var newobj = ptemp.firstChild;
        par.replaceChild(newobj,obj);
      }
    }
  }
}

FixMissingYouTube();

