/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Silverlight 貼り付けコード
// 
// latest update : 2009.05.13
// 
/////////////////////////////////////////////////////////////////////////////////////////////////////////


var XAPObject = function(id, source, width, height)
{
	this.id = id;
	this.source = source;
	this.width = width;
	this.height = height;
	
	XAPObject.__setInstance(id, this);
};

XAPObject.prototype.id                     = null;
XAPObject.prototype.source                 = null;
XAPObject.prototype.width                  = "100%";
XAPObject.prototype.height                 = "100%";
XAPObject.prototype.initParams             = null;
XAPObject.prototype.background             = null;
XAPObject.prototype.minRuntimeVersion      = "2.0.31005.0";
XAPObject.prototype.autoUpgrade            = true;
XAPObject.prototype.enableHtmlAccess       = true;
XAPObject.prototype.allowHtmlPopupWindow   = null;
XAPObject.prototype.splashScreenSource     = null;
XAPObject.prototype.windowless             = false;
XAPObject.prototype.enableFramerateCounter = false;
XAPObject.prototype.maxFramerate           = 30;
XAPObject.prototype.noPluginCode           = '<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">'
                                           + '<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Microsoft Silverlight を取得" style="border-style: none"/>'
										   + '</a>';

XAPObject.prototype.onLoad                    = null;
XAPObject.prototype.onResize                  = null;
XAPObject.prototype.onFullScreenChanged       = null;
XAPObject.prototype.onSourceDownloadComplete  = null;
XAPObject.prototype.onSourceDownloadProgress  = null;
XAPObject.prototype.onError = function(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 2 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);
};

XAPObject.prototype.embedXAP = function()/* HTMLObjectElement */
{
	function getScriptCode(methodName, objectId)
	{
		var script_code = 'var __xapobject__[METHOD]_[ID] = function(sender, args){ var _id = "[ID]"; XAPObject.getInstanceById(_id).[METHOD](sender, args); };';
		script_code = script_code.split("[METHOD]").join(methodName);
		script_code = script_code.split("[ID]").join(objectId);
		return script_code;
	}
	
	var event_code = '<script language="javascript">';
	if(this.onLoad                   instanceof Function) event_code += getScriptCode("onLoad",                          this.id);
	if(this.onResize                 instanceof Function) event_code += getScriptCode("onResize",                        this.id);
	if(this.onFullScreenChanged      instanceof Function) event_code += getScriptCode("onFullScreenChanged",             this.id);
	if(this.onError                  instanceof Function) event_code += getScriptCode("onError",                         this.id);
	if(this.onSourceDownloadComplete instanceof Function) event_code += getScriptCode("onSourceDownloadComplete",        this.id);
	if(this.onSourceDownloadProgress instanceof Function) event_code += getScriptCode("onSourceDownloadProgress", this.id);
	event_code += '</script>';
	
	var _initParams = XAPObject.objectToParameterString(this.initParams);
	var source_code = "";
	source_code += '<div id="' + this.id + 'Host">';
	source_code += '<object id="' + this.id + '" data="data:application/x-silverlight," type="application/x-silverlight-2" width="' + this.width + '" height="' + this.height + '">';
	source_code += '<param name="source" value="' + this.source + '"/>';
	source_code += '<param name="initParams" value="' + _initParams + '" />';
	source_code += '<param name="windowless" value="' + this.windowless + '" />';
	
	source_code += '<param name="enableFramerateCounter" value="' + this.enableFramerateCounter + '" />';
	source_code += '<param name="maxFramerate" value="' + this.maxFramerate + '" />';
	source_code += '<param name="enablehtmlaccess" value="' + this.enableHtmlAccess + '" />';
	source_code += '<param name="minRuntimeVersion" value="' + this.minRuntimeVersion + '" />';
	source_code += '<param name="autoUpgrade" value="' + this.autoUpgrade + '" />';
	 
	if(this.background                                   ) source_code += '<param name="background" value="' + this.background + '" />';
	if(this.splashScreenSource                           ) source_code += '<param name="splashscreensource" value="' + this.splashScreenSource + '" />';
	
	if(this.onLoad                   instanceof Function) source_code += '<param name="onload" value="__xapobject__onLoad_' +this.id+ '" />';
	if(this.onResize                 instanceof Function) source_code += '<param name="onresize" value="__xapobject__onResize_' +this.id+ '" />';
	if(this.onFullScreenChanged      instanceof Function) source_code += '<param name="onfullscreenchanged" value="__xapobject__onFullscreen_' +this.id+ '" />';
	if(this.onError                  instanceof Function) source_code += '<param name="onerror" value="__xapobject__onError_' +this.id+ '" />';
	if(this.onSourceDownloadComplete instanceof Function) source_code += '<param name="onsourcedownloadcomplete" value="__xapobject__onSourceDownloadComplete_' +this.id+ '" />';
	if(this.onSourceDownloadProgress instanceof Function) source_code += '<param name="onsourcedownloadprogresschanged" value="__xapobject__onSourceDownloadProgress_' +this.id+ '" />';
	
	source_code += this.noPluginCode;
	source_code += '</object>';
	source_code += '</div>';
	
	document.write(event_code);
	document.write(source_code);
	
	return document.getElementById(this.id);
};

XAPObject.__container = {};
XAPObject.__setInstance = function(id, target)
{
	XAPObject.__container[id] = target;
}
XAPObject.getInstanceById = function(objectId)
{
	return XAPObject.__container[objectId];
}

XAPObject.objectToParameterString = function(params)
{
	if(!params) return "";
	var list = [];
	for(var val in params) list.push(val + "=" + params[val]);
	return list.join(",");
};

XAPObject.getJSQuery = function(uri, name)
{
	if(uri && (uri = uri.match(/\?(.+)/)) && (uri = uri[1])) return String(uri.split(name + "=")[1]).split("&")[0];
	return null;
};

XAPObject.getSourceLocation = function(script_id)
{
	var node = document.getElementById(script_id);
	if(node && (node = node.attributes["src"])) return node.value;
	return null;
};


