/* Utility script to detect and load starlight plugins */
var StarlightProxy = null;
function getStreamingProxy() {
	if(StarlightProxy) {
		return StarlightProxy;
	}
	
	if(!starlight_isAvail()) {
		return null;
	}
	
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	var is_safari = navigator.userAgent.toLowerCase().indexOf('applewebkit') > -1;
	var is_ie = navigator.userAgent.toLowerCase().indexOf('msie') > -1;
    var proxy = null;
    var container = document.createElement("DIV");
    container.style.visibility = "hidden";
    container.style.width = "1px";
    container.style.height = "1px";
    document.body.appendChild(container);
    
    
    
    if(is_ie) {
    	container.innerHTML = "<OBJECT classid='clsid:5CCEF05B-17B2-48F0-A577-CC019FE455D7'></OBJECT>";
    	proxy = container.firstChild;
    } else {
    	proxy = document.createElement("OBJECT");
    	proxy.setAttribute("type", "application/x-vnd-starlight-proxy");
    	container.appendChild(proxy);
    }

    //For Google Chrome, we have to proxy all Silverlight<->Native communication
    //through javascript due to a bug in the process isolation in Chrome.
    if (is_chrome || is_safari) {
        var real_proxy = proxy;
        proxy = {
            FetchNSC: function(url) {
                return real_proxy.FetchNSC(url);
            },

            StartStreaming: function(addr, port, source, target) {
                var proxyTarget = {
                    AddPacketBase64: function(data) {
                        target.AddPacketBase64(data);
                    }
                };

                real_proxy.StartStreaming(addr, port, source, proxyTarget);
            },

            StopStreaming: function() {
                real_proxy.StopStreaming();
            },

            Test: function(msg) {
                real_proxy.Test(msg);
            }
        };
    }
    
    StarlightProxy = proxy;
    return proxy;
}

function starlight_isAvail() {
	if (navigator.plugins && navigator.plugins.length > 0) {
		for(var i = 0 ; i < navigator.plugins.length; i++) {
			var p = navigator.plugins[i];
			if(p["application/x-vnd-starlight-proxy"]) {
				return true;
			}
		}
	} else if(window.ActiveXObject) {
		try {
			var obj = new ActiveXObject("Starlight.MulticastProxy");
			return true;
		} catch(e) {
			return false;
		}
	}
	return false;
}

