/* Helper to embed the 3DVIA player, see <http://player.studio.3dvia.com/> */
function IsOperatingSystemSupported(){
	var ag = navigator.userAgent.toLowerCase();
	var is_mac = (ag.indexOf("mac")!=-1); 
	var is_windows = (ag.indexOf("windows")!=-1);  
	return is_windows /*|| is_mac*/;
}

function AutomaticReload() 
{
	navigator.plugins.refresh();
     if(player3dvia.isInstalled())
  	    setTimeout('window.location.reload()', 5000)		            	
	setTimeout('AutomaticReload()', 500)
}


var player3dvia = {
	MIME_TYPE: 'application/x-3dvia-mpweb',
	INSTALLER: 'http://player.studio.3dvia.com/3DVIAPlayer-Installer.exe',
	PLUGINSPAGE: 'http://player.studio.3dvia.com/3DVIAPlayer-Installer.exe',
	CONTENT_ROOT: 'http://www.3dvia.com/3dsearch/Content/',
	isInstalled: function()
    {
    	var installed = true;
    	if (navigator.mimeTypes && navigator.mimeTypes.length)
    	{
    		var plugin = navigator.mimeTypes[player3dvia.MIME_TYPE];
    		if ((!plugin) || (!plugin.enabledPlugin))
    		{
    			installed = false;
    		}
    	}
    	return installed;
    },
	generateHTML: function(width, height, experienceId, playerId, params)
	{
		/* Arguments validation */
		if(arguments.length < 3) return;
		var playerId = arguments[3] || '3dvia-player';
		/* HTML output generation */
		var html = '';
		if(navigator.appName == 'Microsoft Internet Explorer' || player3dvia.isInstalled())
		{
			html += '<object ' + (navigator.appName == 'Microsoft Internet Explorer' ? 'id="' + playerId + '" ' : '') + 'width="' + width + '" height="' + height + '" classid="CLSID:F705A1E9-0E4C-4F32-A647-2DE40809969A" codebase="' + player3dvia.INSTALLER + '">';
   			html += '<param name="content_src" value="' + experienceId + '"/>';
			if(params != null) { html += '<param name="startup_options" value="' + params + '"/>'; }
   			html += '<embed ' + (navigator.appName == 'Netscape' ? 'id="' + playerId + '"' : '') + 'width="' + width + '" height="' + height + '" type="' + player3dvia.MIME_TYPE + '" pluginspage="' + player3dvia.PLUGINSPAGE + '" content_src="' + experienceId + '"';
			if(params != null) { html += ' startup_options="' + params + '"'; }
			html += '/>';
			html += '</object>';
		}
		else
		{
			html += '<div style="width:' + width + 'px;height:' + height + 'px;text-align:center;font-size:12px;font-family:arial,helvetica,geneva,sans-serif;">';
			if(IsOperatingSystemSupported() && !player3dvia.isInstalled() && navigator.appName == 'Netscape') {				
				if(parseInt(width) < 271 || parseInt(height) < 151) {
					html += '<a style="display:block;margin:0 auto 0 auto;color:#00A8FF;text-decoration:none;border:1px solid #CCCCCC;background:#F2F2F2;" href="' + player3dvia.INSTALLER + '" OnClick="javascript:AutomaticReload();">';
					html += 'This experience requires the 3DVIA Player.<br/><strong>Click here to install it</strong>';
					html += '</a>';
				} else {
					html += '<a style="display:block;width:270px;height:150px;margin:5px auto 0 auto;padding:5px 15px 0px 15px;color:#00A8FF;text-decoration:none;border:1px solid #CCCCCC;background:#F2F2F2;" href="' + player3dvia.INSTALLER + '" OnClick="javascript:AutomaticReload();">';
					html += 'This experience requires the 3DVIA Player,<br/>';
					html += '<div style="width:112px;margin:10px auto 0 auto;background:white;">';
					html += '<div style="background:#00A8FF;color:white;">-&nbsp;&nbsp;&nbsp;Install Now&nbsp;&nbsp;&nbsp;-</div>';
					html += '<img src="' + this.CONTENT_ROOT + experienceId + '_M1.jpg?DefaultImage=splash_screen_default_M1.jpg" style="border:1px solid #00A8FF;width:110px;border-top:none;">';
					html += '</div></a>';
				}
			}
			else
			{
				html += '<a style="display:block;width:270px;height:64px;margin:20px auto 0 auto;padding:18px 15px 0px 15px;color:#00A8FF;text-decoration:none;border:1px solid #CCCCCC;background:#F2F2F2;" href="http://www.3dvia.com/studio/3dvia-player">';
				html += 'This experience is not yet available for your browser or Operating System.<br/><strong>Click here to know about the system requirements</strong>.';
				html += '</a>';
			}
			html += '</div>';
		}
		return html;
    },
	embedAt: function(wrapperId, width, height, experienceId, playerId, alwaysReplaceWrapper, params)
	{
		/* Arguments validation */
		if(arguments.length < 4) return;
		var playerId = arguments[4] || '3dvia-player';
		var alwaysReplaceWrapper = arguments[5] == null ? true : arguments[5]
		/* Writing to the DOM */
		if(alwaysReplaceWrapper || navigator.appName == 'Microsoft Internet Explorer' || player3dvia.isInstalled()) {
			var html = player3dvia.generateHTML(width, height, experienceId, playerId, params);
			document.getElementById(wrapperId).innerHTML = html;
		}
	},
	embedHere: function(width, height, experienceId, playerId, params)
	{
		/* Arguments validation */
		if(arguments.length < 3) return;
		var playerId = arguments[3] || '3dvia-player';
		/* Writing to the DOM */
		var html = player3dvia.generateHTML(width, height, experienceId, playerId, params);
		document.write(html);
	}
}
