//
// isound.js - General configuration, plugin detection and player integration
// Version 1.0.0
// Author Michael Pelny
// (c) Audiantis GmbH, 2007
// http://www.audiantis.com/
//

// Declaration
var g_isoundPath2Sounds;
var g_isoundiSoundHost;
var g_isoundStreamID;
var g_isoundStreamingURL;
var g_isoundFlash;
var g_isoundJava;
var g_isoundBrowser;
var g_isoundPath2iSoundFiles;

void (isoundInit());

function isoundInit()
{
	g_isoundPath2iSoundFiles = isoundGetPath();
	g_isoundBrowser = isoundGetBrowser();
	void (isoundSetSettings());
	void (isoundDetectPlugin());

	void(isoundWaitForBody());
	
	return true;
}

function isoundWaitForBody()
{
	var timer_id;
	
	if (document.body)
	{	stream_openStream(g_isoundStreamID, 128000,2);
		void (isoundLoadPlayer());
		window.clearTimeout(timer_id);
	}
	else
	{
		timer_id = window.setTimeout("isoundWaitForBody()", 200);
	}
	
	return true;
}

function isoundSetSettings()
{
	// Path to sounds
	g_isoundPath2Sounds = "sounds/multicult/";
	// iSound Hostname
	g_isoundiSoundHost = "85.214.131.229";
	// Don't change. Stream ID is generated by API
	g_isoundStreamID = stream_init(g_isoundiSoundHost, "", "", "", "", "");
	// Complete streaming URL for Player 
	g_isoundStreamingURL = stream_getURL();
	
	return true;
}

function isoundDetectPlugin()
{
	g_isoundFlash = isoundDetectFlash();
	g_isoundJava = isoundDetectJava();
	
	return true;
}

function isoundDetectFlash()
{
	var objActiveX;
	var e;
	var ret;
	var i;
	var anzPlugIns;
	var anzMimeTypes;
	
	objActiveX = "";
	e = "";
	ret = false;
	i = 0;
	anzPlugIns = 0;
	anzMimeTypes = 0;
	
	if (g_isoundBrowser == "ie")
	{
		try
		{
			objActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			ret = true;	
		}
		catch (e)
		{
			ret = false;
		}
	}
	else
	{
		anzPlugIns = navigator.plugins.length;
		if ((navigator.plugins) && (anzPlugIns > 0))
		{
			for (i; i < anzPlugIns; i++)
			{
				// Since Flash Version 7(?) or 8, realtime streaming is available
				if (navigator.plugins[i].description.indexOf("Flash 9"))
				{
					ret = true;
					break;
				}
			}
		}
		else
		{
			anzMimeTypes = navigator.mimeTypes.length;
			if ((navigator.mimeTypes) && (anzMimeTypes > 0))
			{
				for (i; i < anzMimeTypes; i++)
				{
					if (navigator.mimeTypes[i].type.indexOf("application/x-shockwave-flash") >= 0)
					ret = true;
					break;
				}
			}
		}
	}
	
	return ret;
}

function isoundDetectJava()
{
	var anzMimeTypes;
	var anzPlugIns;
	var i;
	var objActiveX;
	var ret;
	
	anzMimeTypes = 0;
	anzPlugIns = 0;
	i = 0;
	objActiveX = "";
	ret = false;
	
	if (g_isoundBrowser == "ie")
	{
		try
		{
			// Any java version...
			objActiveX = new ActiveXObject("JavaPlugin.FamilyVersionSupport");
			// ... specific version, i.e. 1.6
			//objActiveX = new ActiveXObject("JavaPlugin.160_01");
			ret = true;	
		}
		catch (e)
		{
			ret = false;
		}
	}
	else
	{
		if (navigator.plugins && navigator.plugins.length)
		{
			anzPlugIns = navigator.plugins.length;
			for (i; i < anzPlugIns; i++)
			{
				if (navigator.plugins[i].description.indexOf("Java") != -1)
				{
					ret = true;
					break;
				}
			}
		}
		
		if (navigator.mimeTypes && navigator.mimeTypes.length)
		{
			anzMimeTypes = navigator.mimeTypes.length;
			for (i; i < anzMimeTypes; i++)
			{
				if ((navigator.mimeTypes[i].type != null) && (navigator.mimeTypes[i].type.indexOf("application/x-java-applet") != -1 ))
				{
					ret = true;
					break;
				}
			}
		}
		else
		{
			//ret = false;
		}
	}
	return ret;
}

function isoundLoadPlayer()
{
	var objBody;
	var objDiv;
	var player;
	var ret;

	player = "";
	ret = false;

	objBody = document.getElementsByTagName("body")[0];
	objDiv = document.createElement("div");
	objDiv.id = "Audiantis_iSound_Player";
	objBody.appendChild(objDiv);	
	
	if (g_isoundFlash)
	{
		player = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='1' height='1' id='iSoundPlayer' align='middle'>"
		   + "<param name='allowScriptAccess' value='sameDomain' />"
		   + "<param name='flashvars' value='sound=" + g_isoundStreamingURL + "' />"
		   + "<param name='movie' value='" + g_isoundPath2iSoundFiles + "iSoundPlayer.swf' />"
		   + "<param name='quality' value='high' />"
		   + "<param name='bgcolor' value='#FFFFFF' />"
		   + "<embed src='" + g_isoundPath2iSoundFiles + "iSoundPlayer.swf' id='iSoundPlayer' flashvars='sound=" + g_isoundStreamingURL + "' swLiveConnect='true' quality='high' bgcolor='#FFFFFF' width='1' height='1' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />"
		   + "</object>";
	}
	else if (g_isoundJava)
	{
		player = "<APPLET code='javazoom.jl.player.PlayerApplet' archive='http://" + g_isoundiSoundHost  + "/java/isound1.1.jar'"
			+ "width='8' height='8' name='iSoundPlayer'>"
			+ "<param name='audioURL' value='" + g_isoundStreamingURL +  "'>"
			+ "<param name='scriptable' value='false'>"
			+ "Your browser does not support Java applets.</APPLET>";
	}
	else
	{
		player = "No player loaded.";
	}

	try
	{
		objDiv.innerHTML = player;
		ret = true;
	}
	catch (e)
	{
		alert("Error in Line 237 of 'isound.js'\n" + err.name + " " + err.message);
		ret = false;
	}
	
	return ret;
}

function isoundGetBrowser()
{
	var appVer;
	var usrAge;
	var isIE;
	var isOP;
	var isFF;
	var browserInitial;
	var ret;
	
	// appVersion (browser version)
	appVer = navigator.appVersion.toLowerCase();
	// userAgent (HTTP-Identification of browsers)
	usrAge = navigator.userAgent.toLowerCase();
	
	// Search for keywords in strings and safe position (if found, otherwise -1)
	isIE = (appVer.indexOf("msie")    != -1) ? true : false;
	isOP = (usrAge.indexOf("opera")   != -1) ? true : false;
	isFF = (usrAge.indexOf("firefox") != -1) ? true : false;

	browserInitial = "";
	browserInitial = (isIE) ? "ie" : browserInitial;
	browserInitial = (isOP) ? "op" : browserInitial;
	browserInitial = (isFF) ? "ff" : browserInitial;
	
	ret = browserInitial;
	
	return ret;
}

function isoundGetPath()
{
	var ret;
	var objScript;
	var pos;
	var len;
	var src;
	var path;
	
	ret = "";
	objScript = "";
	pos = 0;
	len = 0;
	src = "";
	path = "";
	
	len = document.getElementsByTagName("script").length;
	len--;
	objScript = document.getElementsByTagName("script")[len];
	src = objScript.getAttribute("src");
	pos = src.indexOf("isound.js");
	if (pos == -1)
	{
		pos = src.indexOf("isound-api.js");
	}
	path = (pos == 0) ? "" : src.substr(0, pos);
	ret = path;

	return ret;
}

