/*
 * Flame java script code
 * (c) 2008 - Better.be Application Services
 * by Marc 'Foddex' Oude Kotte
 */

function show_error_div( name, msg ) {

	var d = document.getElementById( name );
	if (d!=null)
		d.style.display = 'block';
	else
		document.write( '<span>'+msg+'</span>' );
}

function flame_include(
	width,				/* width of the control */
	height,				/* height of the control */
	application,		/* name of the application clazz to instantiate */
	baseurl,			/* base url for flame data */
	minver_major,		/* minimum required major version */
	minver_minor,		/* minimum required minor version of major version */
	notInstalledMozDiv,	/* id of the DOM element to make visible when we're running in Mozilla (or other Gecko browser) and the plugin is not installed */
	notSupportedDiv,	/* id of the DOM element to make visible when we're running in a browser we don't support */
	extra_vars			/* must be array with variable names at even indices, and values at uneven. Variable names and values may NOT contain ", : and ; characters */
	) {

	/* init */
	var isIE = navigator.userAgent.indexOf('MSIE') !=-1;
	var isGecko = navigator.userAgent.indexOf('Gecko') !=-1;
	var isWin = navigator.appVersion.indexOf('Windows') != -1;

	/* when we're in Internet Explorer, simply output, let IE handle
	 * handle installing the plugin through the CAB.
	 */
	if (isIE && isWin) {
		/* create extra vars param */
		var extraVars = '';
		for (var x=0; x<extra_vars.length; x+=2)
			extraVars += extra_vars[x]+':'+extra_vars[x+1]+';';

		/* output */
		var s = '<object id="flamecontrol" classid="clsid:c4245f17-d5a0-4620-8443-6f2daa0ab1fc" width="' + width + '" height="' + height + '" ' +
				'codebase="http://flame.betterbe.com/bin/flame-0.10.cab#Version=0,10,0,0">' +
				'<param name="baseurl" value="' + baseurl + '" />' +
				'<param name="reqver_major" value="' + minver_major + '" />' +
				'<param name="reqver_minor" value="' + minver_minor + '" />' +
				'<param name="application" value="' + application + '" />' +
				'<param name="userdata" value="' + extraVars + '" />' +
				'</object>';
		document.write( s );
		return;
	}

	/* when we're not IE, check if the plugin is enabled, and output
	 * HTML based on that
	 */
	if (isGecko) {
		var installed = navigator.plugins['Flame Browser Plugin']!=null;
		if (installed) {
			document.write( '<embed type="application/flame-plugin" width="' + width + '" height="' + height + '" '+"\n" );
			document.write(     'flame_application="' + application + '" ' );
			document.write(     'flame_baseurl="' + baseurl + '" ' );
			document.write(     'flame_reqver_major="' + minver_major + '" ' );
			document.write(     'flame_reqver_minor="' + minver_minor + '" ' );
			for (var x=0; x<extra_vars.length; x+=2)
				document.write( extra_vars[x]+'="'+extra_vars[x+1]+'" ' );
			document.write(     '>' );
			document.write( '</embed>' );
		} else {
			var divName = notInstalledMozDiv + (isWin ? '_win' : '_lin' );
			show_error_div( divName, 'Plugin not installed (yet)...' );
		}
		return;
	}

	/* handle unsupported case */
	show_error_div( notSupportedDiv, 'Browser not supported (yet)...' );
}
