// Writes out the object & embed tags required for displaying a YouTube video.
function PlayYouTubeVideo(parentElement, videoUrl, videoWidth, videoHeight)
{
    //<object width="" height="">
    //    <param name="movie" value=""></param>
    //    <param name="allowFullScreen" value="true"></param>
    //    <embed src="" type="application/x-shockwave-flash" allowfullscreen="true" width="" height=""></embed>
    //</object>

    if (parentElement != null)
    {           
        // Must write out using innerHTML due to bugs in IE.  Using appendChild produces 
        // an Invalid Argument error in IE.
        
        if (agent.ie)
        {        
            //alert('ie');
            var objectStart = "<object width='" + videoWidth + "px' height='" + videoHeight + "px'>";
            var movieParam = "<param name='movie' value='" + videoUrl + "&hl=en&fs=1'></param>";
            var allowParam = "<param name='allowFullScreen' value='true'></param><param name='salign' value='l'></param>";
            var embedTag = "<embed src='" + videoUrl + "&hl=en&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' width='" + videoWidth + "px' height='" + videoHeight + "px'></embed>";
            var objectEnd = "</object>";
            var entireTag = objectStart + movieParam + allowParam + embedTag + objectEnd;
            
            if (agent.ver < 7)
                entireTag = FixEmbedCode(entireTag);
                
            //alert(entireTag);
                
            parentElement.innerHTML = entireTag
        }
        else
        {
            //alert('other browsers');
            parentElement.innerHTML = "";
            var objectTag = document.createElement("object");
            objectTag.setAttribute("width", videoWidth + "px");
            objectTag.setAttribute("height", videoHeight + "px");
            
            var param1 = document.createElement("param");
            param1.setAttribute("name", "movie");
            param1.setAttribute("value", videoUrl + "&hl=en&fs=1");            
            objectTag.appendChild(param1);
            
            var param2 = document.createElement("param");
            param2.setAttribute("name", "allowFullScreen");
            param2.setAttribute("value", "true");            
            objectTag.appendChild(param2);
            
            var embedTag = document.createElement("embed");
            embedTag.setAttribute("src", videoUrl + "&hl=en&fs=1");
            embedTag.setAttribute("type", "application/x-shockwave-flash");
            embedTag.setAttribute("allowfullscreen", "true");
            embedTag.setAttribute("width", videoWidth + "px");
            embedTag.setAttribute("height", videoHeight + "px");
            objectTag.appendChild(embedTag);
            
            parentElement.appendChild(objectTag)
            //alert(parentElement.innerHTML);
        }        
        parentElement.style.width = videoWidth + 10 + "px";
        parentElement.style.height = videoHeight + 10 + "px";
        
        // most videos are 425 wide and 40px padding looked right.  This allows for some variation.
        parentElement.style.paddingLeft = (465 - videoWidth) + "px"; 
    }
}

// IE6 needs a classID on the object tag.  Without it the video will not render.
// Thanks Tremend Tech Blog!
// http://blog.tremend.ro/2008/08/06/dinamically-embed-youtube-videos-on-ie6-using-innerhtml/
function FixEmbedCode(embedCode) 
{
   if (embedCode && embedCode.toLowerCase().indexOf('classid') == -1) 
   {
      var objPos = embedCode.toLowerCase().indexOf('object ') + 'object '.length;
      return embedCode.substr(0, objPos) + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + embedCode.substr(objPos);
   } 
   else 
   {
      return embedCode;
   }
}

