var virtualDirectory = "/twitter";

var silverlightInstallHtml = "<img src='"+ window.location.protocol + "//" + window.location.host + virtualDirectory + "/Images/EventTweet_Install.jpg' alt='Get Microsoft Silverlight' style='border-style: none' onclick='onSilverlightInstallClick();'/>"
var silverlightInstallingHtml = "<img src='" + window.location.protocol + "//" + window.location.host + virtualDirectory +  "/Images/EventTweet_Installing.jpg' alt='Installing Microsoft Silverlight ...' style='border-style: none' />";
var silverlightRestartHtml = "<img src='" + window.location.protocol + "//" + window.location.host + virtualDirectory +  "/Images/EventTweet_Reboot.jpg' alt='Microsoft Silverlight is installed.  Please restart your browser.' style='border-style: none' />";
var silverlightVersion = "3.0";



function onSourceDownloadProgressChanged(sender, eventArgs) {
    sender.findName("progressBar").Width = eventArgs.progress * sender.findName("progressBarBackground").Width;
}
function onSilverlightInstallClick() {
    window.location = "http://go2.microsoft.com/fwlink/?linkid=124807";
    document.getElementById("silverlightControlHost").innerHTML = silverlightInstallingHtml;
}

function injectSilverlightApp(isNewSilverlightInstall) {

    var newInstallValue = "false";
    if (isNewSilverlightInstall && isNewSilverlightInstall == true) {
        newInstallValue = "true";
    }
    var params = "photoCount=100,tweetCount=35,isNewInstall=" + newInstallValue;
    Silverlight.createObjectEx(
            {
                source: window.location.protocol + "//" + window.location.host + virtualDirectory +  "/ClientBin/FanClient8.xap",
                parentElement: document.getElementById("silverlightControlHost"),
                properties: {
                    version: silverlightVersion,
                    background: "white",
                    width: "100%",
                    height: "100%",
                    autoUpgrade: "true",
                    onerror: 'onSilverlightError'

                },
                initParams: params
            }
            );
}

 function onSilverlightError(sender, args) {
        
            var appSource = "";
            if (sender != null && sender != 0) {
                appSource = sender.getHost().Source;
            } 
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;
            
            var errMsg = "Unhandled Error in Silverlight 3 Application " +  appSource + "\n" ;

            errMsg += "Code: "+ iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError")
            {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError")
            {           
                if (args.lineNumber != 0)
                {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " +  args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }

function onSilverlightInstalled() {
    injectSilverlightApp(true);
}

function silverlightAppLoad() {
    Silverlight.onSilverlightInstalled = onSilverlightInstalled;
    if (Silverlight.isRestartRequired) {
        document.getElementById("silverlightControlHost").innerHTML = silverlightRestartHtml;
        return;
    }
    if (Silverlight.isInstalled(silverlightVersion)) {
        injectSilverlightApp(false);
    }
    else {

        document.getElementById("silverlightControlHost").innerHTML = silverlightInstallHtml;

    }

}

if (window.addEventListener) {
    window.addEventListener('load', silverlightAppLoad, false);
}
else {
    window.attachEvent('onload', silverlightAppLoad);
}
   