///////////////////////////////////////////////////////////////////////////////
//
//  installcreatesilverlight.js   			version 1.0
//
//  This file is provided by Microsoft as a helper file for websites that
//  incorporate Silverlight Objects. This file is provided under the Silverlight 
//  SDK 1.0 license available at http://go.microsoft.com/fwlink/?linkid=94240.  
//  You may not use or distribute this file or the code in this file except as 
//  expressly permitted under that license.
// 
//  Copyright (c) 2007 Microsoft Corporation. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////

if(!window.Silverlight) { window.Silverlight={}; }

Silverlight.InstallAndCreateSilverlight = function(version, SilverlightDiv, installExperienceHTML, installPromptDivID, createSilverlightDelegate) {
    var RetryTimeout=3000;	 //The interval at which Silverlight instantiation is attempted(ms)	
    if ( Silverlight.isInstalled(version) ){
		createSilverlightDelegate();
    } else {
		if ( installExperienceHTML && SilverlightDiv ) {
		    SilverlightDiv.innerHTML=installExperienceHTML;
		    document.body.innerHTML;
		}
        if (installPromptDivID){
	    	var installPromptDiv = document.getElementById(installPromptDivID);
		   	 if ( installPromptDiv ) {
				installPromptDiv.innerHTML = Silverlight.createObject(null, null, null, {version: version, inplaceInstallPrompt:true},{}, null);
			}
        }
		if ( ! (Silverlight.available || Silverlight.ua.Browser != 'MSIE' ) ) {
	    	TimeoutDelegate = function() {
	        	Silverlight.InstallAndCreateSilverlight(version, null, null, null, createSilverlightDelegate);
	    	}
	    	setTimeout(TimeoutDelegate, RetryTimeout);
		}
    }
}

/**
 * Silverlight error handler
 * @credit - http://msdn2.microsoft.com/en-us/library/bb404723.aspx
 */
Silverlight.error = function(sender, errorArgs) {
    // The error message to display.
    var errorMsg = "Silverlight Error: \n\n";

    // Error information common to all errors.
    errorMsg += "Error Type:    " + errorArgs.errorType + "\n";
    errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
    errorMsg += "Error Code:    " + errorArgs.errorCode + "\n";

    // Determine the type of error and add specific error information.
    switch(errorArgs.errorType) {
        case "RuntimeError":
            // Display properties specific to RuntimeErrorEventArgs.
            if (errorArgs.lineNumber != 0) {
                errorMsg += "Line: " + errorArgs.lineNumber + "\n";
                errorMsg += "Position: " +  errorArgs.charPosition + "\n";
            }
            errorMsg += "MethodName: " + errorArgs.methodName + "\n";
            break;
        case "ParserError":
            // Display properties specific to ParserErrorEventArgs.
            errorMsg += "Xaml File:      " + errorArgs.xamlFile      + "\n";
            errorMsg += "Xml Element:    " + errorArgs.xmlElement    + "\n";
            errorMsg += "Xml Attribute:  " + errorArgs.xmlAttribute  + "\n";
            errorMsg += "Line:           " + errorArgs.lineNumber    + "\n";
            errorMsg += "Position:       " + errorArgs.charPosition  + "\n";
            break;
        default:
            break;
    }
    // Display the error message.
    //Schematic.debug(errorMsg);
}

/**
 * Post install message for browsers that need restart
 */
Silverlight.postInstall = function() {
	var PostInstallGuidance = document.getElementById('PostInstallGuidance');
	if ( document.getElementById('PostInstallGuidance') ){
	    if ( Silverlight.ua.Browser == "MSIE" ) {
			if ( Silverlight.available ){
				PostInstallGuidance.innerHTML="When installation is complete, restart your browser to activate your Silverlight content.";
			} else {
				PostInstallGuidance.innerHTML= "";	    	
			}
		} else if ( Silverlight.ua.Browser == "Firefox" || Silverlight.ua.Browser == "Safari") {
	            PostInstallGuidance.innerHTML="When installation is complete,<br />restart your browser to activate your Silverlight content.";
	    } else {
	            PostInstallGuidance.innerHTML="Your browser may not be supported by Microsoft Silverlight.<br />Please visit http://www.microsoft.com/silverlight/system-requirements.aspx for more information.";
		}
	}
}

/**
 * Create the Silverlight plugin and Install Experience 
 */
Silverlight.createInstallExperience = function(plugin) {
	var installExperienceHTML = "<div id='InstallPrompt'></div>";
	
	if (plugin.postInstall) {
		installExperienceHTML += "<div id='PostInstallGuidance'></div>";
	}
	
	// find host div and set its size and background color according to the plugin
	var pluginHost = document.getElementById(plugin.host);
	pluginHost.style.width = plugin.width;
	pluginHost.style.height = plugin.height;
	//pluginHost.style.background = plugin.color;
	
	Silverlight.InstallAndCreateSilverlight(
		'1.0', 
		pluginHost, 
		installExperienceHTML,
		'InstallPrompt',
		function(){
			Silverlight.createObjectEx({   
				source: plugin.xaml, 
                parentElement: pluginHost, 
                id: plugin.id, 
                properties: { 
                	width: plugin.width, 
                	height: plugin.height, 
                	version:'1.0', 
                	background: plugin.color, 
                	isWindowless:'true' 
                }, 
                events : plugin.events 
            });
		}
	);
	
	if (plugin.postInstall) {
		Silverlight.postInstall();
	}
}

/**
 * Downloader object
 *
 * USAGE: Silverlight.Downloader(plugin, {onDownloadProgress(), onComplete()})
 */

Silverlight.Downloader = function(params){

	this.params = params;
	this.downloader = this.params.plugin.createObject('Downloader');
	
	this.onComplete = function() {
		if (this.params.onComplete) {
			this.params.onComplete(this.downloader, this.params);
		}
	}
		
	this.onDownloadProgress = function(sender) {
		if (this.params.onDownloadProgress) {
			this.params.onDownloadProgress(this.downloader, this.params);
		}
	}
	
	this.downloader.addEventListener('Completed', this.onComplete.bind(this));
	this.downloader.addEventListener('DownloadProgressChanged', this.onDownloadProgress.bind(this));
	
	this.downloader.open('GET', this.params.resource);
	this.downloader.send();

};
