/**
 * A JavaScript to detect if an user has a Cult3D viewer installed.
 * It also (in most cases) tells you what version of the plugin
 *
 * This script has been tested with and should work with:
 * Windows Netscape
 * Windows Internet Explorer (Cult3D plugin 5.?.? or later)
 * Macintosh Netscape
 * Macintosh Internet Explorer (IE 5.0 or later) (No version info)
 * Linux/Unix Netscape
*/


//------ VERSION 3.0, DATE October 24 2001 ------------
//------ AUTHOR  Jonatan Andersson (Cycore)---------
//------ developed under MS Windows NT 4.0(v 0.0..2.0) and Win2000 (3.0..?)

var vbScriptFoundCult3D;
var vbCult3DVersion;

/**
 * This method should be called to detect if there is a
 * Cult3D viewer installed on the users system.
 *
 * @return true if the user has a Cult3D Viewer installed, otherwise false.
*/
function checkForCult3D()
{
	vbScriptFoundCult3D = false;

	if (navigator == null)
	{
		// The webbrowser does not support the navigator object
		return false;
	}

	if (navigator.plugins == null)
	{
		// The webbrowser does not support the plugins property.
		return false;
	}

	if (navigator.plugins.length == null)
	{
		// The webbrowser does not support the length property.
		return false;
	}

	if (navigator.plugins.length == 0)
	{
		// The plugin array is empty, if this is running under windows IE,
		// Cult3D can still be installed, let us check
		if (isWindows() && isInternetExplorer())
		{
		 	makeAndExecuteVBScript();
			return vbScriptFoundCult3D;
		}
		else
		{
			// Nope, this is not windows and IE, No Cult3D found
			return false;
		}
	}

	if (parsePluginArrayForCult3D() == true)
	{
		// A Cult3D Plugin was found in the existing plugin array
		return true;
	}

	// Passed all combinations, no Cult3D found
	return false;
}

/**
 * This method should be called to get the version of a Cult3D Plug-in.
 *
 * Plattforms: Windows       Internet Explorer (Tested, 5.0 and later)
 *             Windows       Netscape          (Tested, 4.0, 5.0, 5.2, 5.3)
 *  		   Macintosh     Netscape          (Tested, 5.0, 5.2, 5.3)
 *             Solarix/Linux Netscape          (Tested, 5.2 B1)
 *
 * Please note that Netscape Cult3D version are sensitive in version layout,
 * it parses a text with version info.
 *
 * @return Return the version number of the Cult3D plug-in,
 *         if the script was unable to find any plug-in it returns an
 *         empty string
 */
function getCult3DVersion()
{
    // Make sure there is a Cult3D plug-in installed
    if (!checkForCult3D())
    {
    	return "";
    }

    if (isWindows() && isNetscape())
    {
    	return getWinNSCult3DVersion();
    }

    if (isMacintosh() && isNetscape())
    {
    	return getMacNSCult3DVersion();
    }

    if (isUnix() && isNetscape())
    {
    	return getUnixNSCult3DVersion();
    }

    if (isWindows() && isInternetExplorer())
    {
    	return getWinIECult3DVersion();
    }

    if (isMacintosh() && isInternetExplorer())
    {
    	return getMacIECult3DVersion();
    }

    return "";
}


/**
 *
 * @param cult3dStringSource A string from getCult3DVersion
 * @param cult3dStringTarget A string from getCult3DVersion
 *
 * @return Returns an integer value.
 *         The value is less than zero if source is less than target,
 *         the value is zero if source and target are equal,
 *         the value is greater than zero if source is greater than target.
 *
 */
function compareCult3DVersions(cult3dString1, cult3dString2)
{
	return "Not yet implemented";
}

/**
 * This function tells if it is running on a MS Windows system
 *
 * @return If on MS Windows return boolean true, otherwise false
*/
function isWindows()
{
	agent = navigator.userAgent;
	agent = agent.toLowerCase();

	if (agent.indexOf("win") >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * This function tells if it running on a Macintosh system
 *
 * @return If on a macintosh return boolean true, otherwise false
 */
function isMacintosh()
{
	agent = navigator.userAgent;
	agent = agent.toLowerCase();

	if (agent.indexOf("mac") >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * This function tells if it running on a Unix/Linux system
 *
 * @return If on *nix return boolean true, otherwise false
 */
function isUnix()
{
	agent = navigator.userAgent;
	agent = agent.toLowerCase();

	if ((agent.indexOf("sunos") >= 0) || (agent.indexOf("linux") >= 0))
	{
		return true;
	}
	else
	{
		return false;
	}
}


/**
  * This function tells if it is running on a Netscape browser
  *
  * return If on Netscape return boolen true, otherwise false.
*/
function isNetscape()
{
	browser = navigator.appName;
	browser = browser.toLowerCase();

	if (browser.indexOf("netscape") >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
  * This function tells if it is running on a Internet Explorer browser
  *
  * return If on Netscape return boolen true, otherwise false.
*/
function isInternetExplorer()
{
	browser = navigator.appName;
	browser = browser.toLowerCase();

	if (browser.indexOf("internet explorer") >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

///////// PRIVATE FUNCTIONS USED INTERNALLY BY THE SCRIPT ////////////////

/**
  * This method is called from VBScript.
  * This method tells JavaScript that VBScript found a Cult3D Plugin
*/
function setVBCult3DFound()
{
	vbScriptFoundCult3D = true;
}

/**
  * This method is called from VBScript.
  * This method tells JavaScript that VBScript did not found a Cult3D Plugin.
*/
function setVBCul3DNotFound()
{
	vbScriptFoundCult3D = false;
}

function setVBCult3DVersion(version)
{
	vbCult3DVersion = version;
}

/**
  * This method writes a VBScript on the site for detecting a Cult3D Viewer on
  * Windows Internet Explorer, it also executes the written script.
  * This method will also get the version number of the plug-in
  *
  * This method will be called if the end user uses MS Windows and
  * Internet Explorer
*/
function makeAndExecuteVBScript()
{
	// Make the VBScript
	document.writeln("<SCRIPT LANGUAGE=\"vbscript\">");
	document.writeln("<!--");

	document.writeln("  sub checkForIEOnWindows()");
	document.writeln("    On Error Resume Next");
	document.writeln("    Dim foundCult3D");
	document.writeln("                ");
	document.writeln("    foundCult3D = getPluginInfo");
    document.writeln("    if (foundCult3D = True) then");
    document.writeln("      setVBCult3DFound");
    document.writeln("    else");
    document.writeln("      setVBCul3DNotFound");
    document.writeln("    end if");
	document.writeln("  end sub");

	document.writeln("  function getPluginInfo()");
	document.writeln("    On Error Resume Next");
	document.writeln("    Dim objCult3D");
	document.writeln("    Dim foundCult3D");
	document.writeln("                   ");
    document.writeln("    Set objCult3D = CreateObject(\"IECULT.Cult3DVersion\")");
   	document.writeln("    If (IsObject(objCult3D)) Then");
    document.writeln("      setVBCult3DVersion(objCult3D.Version)");
   	document.writeln("      foundCult3D = True");
   	document.writeln("    Else");
    document.writeln("      setVBCult3DVersion(\"\")");
    document.writeln("      foundCult3D = False");
 	document.writeln("    End If");
    document.writeln("                           ");
	document.writeln("    Set objCult3D = Nothing");
	document.writeln("    getPluginInfo  = foundCult3D");
	document.writeln("  end function");

	document.writeln("' -->");
	document.writeln("</SCRIPT>");

	// Execute it
	document.writeln("<SCRIPT LANGUAGE=\"JavaScript\">");
	document.writeln("<!--");
	document.writeln("  checkForIEOnWindows()");
	document.writeln("// -->");
	document.writeln("</SCRIPT>");
}

/**
  * This function tells if it can find the Cult3D plugin (ignores what version)
  * for Netscape and Netscape compatible browsers.
  *
  * return If it can find the Cult3D plugin return boolean true, otherwise false
*/
function parsePluginArrayForCult3D()
{
	// Get Cult3D's Plugin instance from the PluginArray
	var plugin = parseNetscapePluginArray();

	if (plugin == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * @Return Cult3D's plugin instance in Netscape Plugin Array
 */
function parseNetscapePluginArray()
{
	var numPlugins = navigator.plugins.length;
	var plugVersion = "";
	var plugName = "";
	var plugin;

	for (i = 0; i < numPlugins; i++)
	{
        plugin = navigator.plugins[i];
        plugName = plugin.name.toLowerCase();

        if (plugName.indexOf("cult3d") >= 0)
        {
        	// Cult3D was found
        	return plugin;
		}
	}

	return null;
}

function getWinNSCult3DVersion()
{
	var plugin = parseNetscapePluginArray();
	if (plugin == null)
	{
		return "";
	}

    // In this string the version number is located for
    // version 4.0, 5.0 and 5.2
    var pluginName = plugin.name.toLowerCase();

    if (pluginName.indexOf("cult3d") > -1)
    {
    	pluginName = deleteStringFromLeft(pluginName, "cult3d");
    }

    if (pluginName.indexOf("plugin") > -1)
    {
    	pluginName = deleteStringFromLeft(pluginName, "plugin");
    }

    if (pluginName.indexOf("plug-in") > -1)
    {
    	pluginName = deleteStringFromLeft(pluginName, "plug-in");
    }

    // Remove an eventual v before the version number ex. Cult3D Plugin v5.0
    var vIndex = pluginName.indexOf("v");
    if ((vIndex > -1) && (!isNaN(pluginName.charAt(vIndex+1))))
    {
    	pluginName = deleteStringFromLeft(pluginName, "v");
    }

	return trim(pluginName);
}

function getMacNSCult3DVersion()
{
	var plugin = parseNetscapePluginArray();
	if (plugin == null)
	{
		return "";
	}

    var c3dDesc = plugin.description.toLowerCase();
    if (c3dDesc.indexOf("cult3d") > -1)
    {
    	c3dDesc = deleteStringFromLeft(c3dDesc, "cult3d");
    }

    if (c3dDesc.indexOf("plugin") > -1)
    {
    	c3dDesc = deleteStringFromLeft(c3dDesc, "plugin");
    }

    if (c3dDesc.indexOf("plug-in") > -1)
    {
    	c3dDesc = deleteStringFromLeft(c3dDesc, "plug-in");
    }

	// Terminate the string at the first <br> sign
	if (c3dDesc.indexOf("<br>") > -1)
	{
		c3dDesc = c3dDesc.substring(0, c3dDesc.indexOf("<br>"));
	}

    // 5.2 has the company name along with the version number, clean vnr
    if (c3dDesc.indexOf("from") > -1)
    {
    	c3dDesc = c3dDesc.substring(0, c3dDesc.indexOf("from"));
    }

	return trim(c3dDesc);
}

function getUnixNSCult3DVersion()
{
	var plugin = parseNetscapePluginArray();
	if (plugin == null)
	{
		return "";
	}

    var c3dDesc = plugin.description.toLowerCase();

    // if the v and a number is next to each other, assume version number
    var vIndex = c3dDesc.indexOf("v");
    if ((vIndex > -1) && (!isNaN(c3dDesc.charAt(vIndex+1))))
    {
    	c3dDesc = c3dDesc.substring(vIndex, c3dDesc.length);
    	c3dDesc = c3dDesc.substring(0, c3dDesc.indexOf("<br>"));

    	if (c3dDesc.indexOf("v") > -1)
    	{
    		c3dDesc = deleteStringFromLeft(c3dDesc, "v");
    	}

    	// Remove any eventual comments about the version number (expire date, etc)
    	if (c3dDesc.indexOf("-") > -1)
    	{
    		c3dDesc = c3dDesc.substring(0, c3dDesc.indexOf("-"));
    	}

		return trim(c3dDesc);
    }
    else
    {
    	// Nope, Could'nt locate the place where the version number is written
    	return "";
    }
}

function getWinIECult3DVersion()
{
	return vbCult3DVersion;
}

function getMacIECult3DVersion()
{
	return "";
}

function deleteStringFromLeft(sourceString, stringToDelete)
{
	sourceString = sourceString.substring(sourceString.lastIndexOf(stringToDelete)+stringToDelete.length, sourceString.length);
	return sourceString;
}

function trim(str)
{
   return str.replace(/^\s*/, '').replace(/\s*$/, '');
}


