var swirlBpiCab = 'http://www.pinecoast.com/SwirlBpi.cab';
var swirlBpiCab_ver = 'http://www.pinecoast.com/SwirlBpi.cab#version=1,6,0,0';
var versionString = 'Version 1.6.0.0';
var ver0 = 1060000;

function get_os()
{
    var os = '';
    if (typeof(navigator.platform) != 'undefined') {
	    var platform;
        platform = navigator.platform.toLowerCase();
        if (platform.indexOf('win') != -1) {
            os = 'win';
        }
        else if (platform.indexOf('mac') != -1) {
            os = 'mac';
        }
        else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1) {
            os = 'unix';
        }
    }
    return os;
}

function get_browser()
{
    var browser = '';
    var browserversion = 0;
    var ua = window.navigator.userAgent.toLowerCase();
    if ((i = ua.indexOf('opera')) != -1) {
        browser = 'opera';
        browserversion  = parseFloat('0' + ua.substr(i+6), 10);
    }
    else if ((i = ua.indexOf('msie')) != -1) {
        browser = 'ie';
        browserversion  = parseFloat('0' + ua.substr(i+5), 10);
    }
    else if (ua.indexOf('gecko') != -1) {
        if (ua.indexOf('netscape') != -1)
            browser = 'netscape';
        else if (ua.indexOf('compuserve') != -1)
            browser = 'compuserve';
        else
            browser = 'mozilla';
    }
    else if ((ua.indexOf('mozilla') !=-1) && (ua.indexOf('spoofer')==-1) && (ua.indexOf('compatible') == -1) &&
             (ua.indexOf('opera')==-1)&& (ua.indexOf('webtv')==-1) && (ua.indexOf('hotjava')==-1)) {
        browser = 'netscape';
    }
    else if ((i = ua.indexOf('aol')) != -1 ) {
        browser  = 'aol';
    }
    else if ((i = ua.indexOf('hotjava')) != -1 ) {
        browser  = 'hotjava';
    }
    return browser;
}

function ie_version()
{
    var ua = window.navigator.userAgent.toLowerCase();
    var msie = ua.indexOf ("msie");
    if (msie > 0) {
        var version = parseInt (ua.substring (msie+5, ua.indexOf (".", msie)));
        return version;
    }
    return 0;
}

function select_plugin()
{
    var plugin = 'none';
    if (typeof(navigator.platform) != 'undefined') {
        var platform = navigator.platform.toLowerCase();
        var ua = window.navigator.userAgent.toLowerCase();
        var browser = get_browser();
        if (platform.indexOf('win') != -1) {
		    if(window.ActiveXObject) {
		        plugin = 'ie';
		    }
		    else if (browser == 'opera') {
		        plugin = 'mozilla';
		    }
		    else if (ua.indexOf('gecko') != -1) {
			    if (ua.indexOf('netscape') != -1) {
			    }
			    else if (ua.indexOf('compuserve') != -1) {
			    }
			    else {
			        plugin = 'mozilla';
			    }
		    }
		    else {
		    }
        }
        else if (platform.indexOf('mac') != -1) {
        }
        else if (platform.indexOf('unix') != -1 ||
                 platform.indexOf('linux') != -1 ||
                 platform.indexOf('sun') != -1) {
        }
    }
    return plugin;
}

function decideDoInstall()
{
	var os2 = get_os();
	var browser2 = get_browser();
	var isIE = window.ActiveXObject ? true : false; // ActiveX is only used in Internet Explorer

	if ( (os2 == 'win') && (browser2 != 'ie') && (!isIE) ) {
		var found = false;
		if( navigator.plugins && navigator.plugins.length > 0) {
			var n = navigator.plugins.length;
			var i = 0;
			for (i=0; i < n; i++ ) {
				var name = navigator.plugins[i].name;
				if(name == 'SwirlX3D 3D Plug-in') {
					var desc = navigator.plugins[i].description;
					var ind = desc.indexOf('; v.');
					if(ind > 0) {
						var ver = desc.substring(ind + 4);
						if(ver >= ver0) {
							found = true;
						}
					}
				}
			}
		}
		if(!found) {
			return true;
		}
    }
    return false;
}

function hasNpPlugin()
{
	var os2 = get_os();
	var browser2 = get_browser();
	var isIE = window.ActiveXObject ? true : false; // ActiveX is only used in Internet Explorer

	if ( (os2 == 'win') && (browser2 != 'ie') && (!isIE) ) {
		if( navigator.plugins && navigator.plugins.length > 0) {
			var n = navigator.plugins.length;
			var i = 0;
			for (i=0; i < n; i++ ) {
				var name = navigator.plugins[i].name;
				if(name == 'SwirlX3D 3D Plug-in') {
					return true;
				}
			}
		}
    }
    return false;
}

function display_sw3d(w, h, x3dfilename, imagefilename, imagealt)
{
    var plugin = select_plugin();
	var browser2 = get_browser();
    if(plugin == 'ie') {
        var ie_ver = ie_version();
	    document.writeln('<OBJECT WIDTH=' + w + ' HEIGHT=' + h);
	    if(ie_ver < 7) {
			document.writeln(' CODEBASE=' + swirlBpiCab);
		}
	    else {
			document.writeln(' CODEBASE=' + swirlBpiCab_ver);
		}
	    document.writeln(' CLASSID="CLSID:0BD834DB-1118-4207-82A8-B398B7A49A21" VIEWASTEXT>');
	    document.writeln('    <PARAM NAME="Url" VALUE="' + x3dfilename + '">');
	    document.writeln('<IMG src="' + imagefilename + '" border=0 alt="' + imagealt + '" width=' + w + ' height=' + h + '/>');
	    document.writeln('</' + 'OBJECT>');
    }
    else if(plugin == 'mozilla') {
		var doInstall = decideDoInstall();
		var hasNp = hasNpPlugin();
		if(doInstall) {
		    if(hasNp) {
			    document.writeln('<OBJECT type=model/x-sw3d height=' + h + ' width=' + w + ' data="' + x3dfilename + '" VIEWASTEXT>');
			    document.writeln('<IMG src="' + imagefilename + '" border=0 alt="' + imagealt + '" width=' + w + ' height=' + h + '/>');
				document.writeln('</' + 'OBJECT>');
		    }
		    else {
		        document.writeln('<IMG src="' + imagefilename + '" border=0 alt="' + imagealt + '" width=' + w + ' height=' + h + '/>');
		    }
		    document.writeln('<BR>The latest version of the plug-in is required. It can be installed by pressing the INSTALL link below.');
			document.writeln('<BR><BR>');
			document.writeln('<TABLE><TR><TD>');
			document.writeln('<A HREF="http://www.pinecoast.com/downloadnp.htm">');
		    document.writeln('<IMG src="images/down_install.jpg" alt="INSTALL" border=0></A>');
			document.writeln('</TD><TD class="pg">');
		    document.writeln('<B> SwirlX3D Mozilla plug-in, ' + versionString + '.</B>');
			document.writeln('</TD></TR></TABLE>');
		}
		else {
		    document.writeln('<OBJECT type=model/x-sw3d height=' + h + ' width=' + w + ' data="' + x3dfilename + '" VIEWASTEXT>');
		    document.writeln('<IMG src="' + imagefilename + '" border=0 alt="' + imagealt + '" width=' + w + ' height=' + h + '/>');
			document.writeln('</' + 'OBJECT>');
		}
    }
    else {
	    document.writeln('<IMG src="' + imagefilename + '" border=0 alt="' + imagealt + '" width=' + w + ' height=' + h + '/>');
    }
}

