﻿function createWhyBuyWidget(div)
{
	var scene = new ExpMIX_WhyBuy.Page();
	Silverlight.createObjectEx({
		//source: "../silverlight/why-buy-widget/xaml/Page.xaml",
		source: webRoot + ExpMIX_WhyBuy.Config.xamlSource,
		parentElement: document.getElementById(div),
		id: "WhyBuyWidget",
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0",
			background:"transparent",
			framerate: "25",
			isWindowless: "true"
		},
		events: {
			onLoad: Silverlight.createDelegate(scene, scene.handleLoad),
			onError: function(sender, args) {
				var errorDiv = document.getElementById("errorLocation");
				if (errorDiv != null) {
					var errorText = args.errorType + "- " + args.errorMessage;
							
					if (args.ErrorType == "ParserError") {
						errorText += "<br>File: " + args.xamlFile;
						errorText += ", line " + args.lineNumber;
						errorText += " character " + args.charPosition;
					}
					else if (args.ErrorType == "RuntimeError") {
						errorText += "<br>line " + args.lineNumber;
						errorText += " character " +  args.charPosition;
					}
					errorDiv.innerHTML = errorText;
				}	
			}
		}
	});
}

if (!window.Silverlight) 
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}
 function delegate( target, callback )
 {
	var func;
    if(arguments.length > 2)
    {
      var _params = [];
      for(var n = 2; n < arguments.length; n++) _params.push(arguments[n]);
      func = function() { 
		var params = [];
		for(var n = 0; n < arguments.length; n++) params.push(arguments[n]);
		for(var n = 0; n < _params.length; n++) params.push(_params[n]);
		callback.apply(target,params); 
	  }
    }
    else{
		func =  function() {  callback.apply(target,arguments); }
	}
	
	return func;
	
 }
 
String.prototype.replaceAll  = function(s1,s2){   
   return this.replace(new RegExp(s1,"gm"),s2);   
}  
