﻿var hosts  = new Object();

function wpfeHost(hostElementID, id, height, width, backgroundcolor, sourceelement, source, windowlessmode, framerate, loadedhandler, errorhandler) 
{
    var hostElement = document.getElementById(hostElementID);
    var innerHTML;
    
    //assign error handler

        if(errorhandler == null) {
        
        errorhandler = "aghost_errorhandler";
        
        }

       
         innerHTML = '<embed id="'+id+'" width="'+width+'" height="'+height+'" pluginspage="http://go.microsoft.com/fwlink/?LinkID=77793&clcid=0x409';
         if (source != null) {
             innerHTML += '" Source="'+source;
         }
         if (sourceelement != null) {
          innerHTML += '" SourceElement="'+sourceelement;
         }
         if (framerate != null) {
             innerHTML += '" MaxFrameRate="'+framerate;
         }
         if (errorhandler != null) {
             innerHTML +='" OnError="'+errorhandler;
         }
         if (backgroundcolor != null) {
             innerHTML += '" BackgroundColor="'+backgroundcolor;
         }
         if (windowlessmode != null) {
             innerHTML += '" WindowlessMode="'+windowlessmode;
         }
         innerHTML += '" type="application/x-silverlight"/>';




    hostElement.innerHTML = innerHTML;

    if (loadedhandler) 
    {
        hosts[id] = hostElement.childNodes[0];
        hosts[id].loadedhandler = loadedhandler;
        hosts[id].updateTimer = setInterval("invokeLoadedHander('" + id + "')", 500);
    }
}

function invokeLoadedHander(hostID) 
{
    var host = hosts[hostID];

    if (host) 
    {
        clearTimeout(host.updateTimer);
        host.loadedhandler(host);
        hosts[hostID] = null;
    }
}
    
function aghost_errorhandler(line, col, hr, string) 
{
    if(line !=0 && col !=0)
    {
      var str = "("+line+","+col+"): "+string+"\n";
      str += "HRESULT: "+hr;
      alert(str);
    }
}
 
