﻿/*--
Copyright © 1998, 2009, Oracle and/or its affiliates.  All rights reserved.
--*/

var WMP_REQUIREDVERSION = "6,4,0,0";
var bWMPIsAvailable=false;

var b_asxPlaying=false;
var b_asxSuccessfull=false;

function CompareVersions(s1,s2)	// return: s1<s2
{
	a1=s1.split(",");
	while (a1.length<3)
		a1[a1.length]="000";
	a2=s2.split(",");
	while (a1.length<3)
		a1[a1.length]="000";
	for (var i=0;i<3;i++)
	{
		while (a1[i].length<3)
			a1[i]="0"+a1[i];
		while (a2[i].length<3)
			a2[i]="0"+a2[i];
		if (a1[i]<a2[i])
			return true;
		if (a1[i]>a2[i])
			return false;
	}
	return false;
}

try {
	var ver = "";
	if (!ver)
		ver = oClientCaps.getComponentVersion("{22D6F312-B0F6-11D0-94AB-0080C74C7E95}","ComponentID"); 
	if (!ver)
		ver = oClientCaps.getComponentVersion("{6BF52A52-394A-11D3-B153-00C04F79FAA6}","ComponentID"); 
	if (CompareVersions(ver,WMP_REQUIREDVERSION))
	{
		bWMPIsAvailable=false;
		if (UserPrefs.PlayAudio!="none")
		{
			var s=R_WMP_bad_version1+WMP_REQUIREDVERSION+R_WMP_bad_version2+ver+R_WMP_bad_version3;
		};
	}
	else
	{
		bWMPIsAvailable=true;
		document.writeln('<OBJECT id=SoundPlayer type=application/x-oleobject');
		document.writeln('classid=CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95 width=0 height=0>');
		document.writeln('<PARAM NAME=AnimationAtStart VALUE="0"></PARAM>');
		document.writeln('</OBJECT>');
		document.writeln('<SCRIPT FOR="SoundPlayer" EVENT="EndOfStream(lResult)" LANGUAGE=javascript>');
		document.writeln('	if (window.OnEndPlaySoundLocal)');
		document.writeln('		OnEndPlaySoundLocal(lResult);');
		document.writeln('</SCRIPT>');
		document.writeln('<SCRIPT FOR="SoundPlayer" EVENT="Error()" LANGUAGE="javaScript">');
		document.writeln('	if (window.OnErrorPlaySoundLocal)');
		document.writeln('		OnErrorPlaySoundLocal(SoundPlayer.ErrorCode,SoundPlayer.ErrorDescription);');
		document.writeln('</SCRIPT>');
	};
} catch(e) {};

function SoundPlayerClass()
{
	this.TopicPath=unescape(window.location.href);
	while (this.TopicPath.indexOf('\\')!=-1)
		this.TopicPath=this.TopicPath.replace('\\','/');
	var i=this.TopicPath.lastIndexOf("/");
	this.TopicPath=this.TopicPath.substr(0,i+1);
		
	this.RootPath=this.TopicPath;
	for (var j=0;j<=3;j++)
	{
		i=this.RootPath.lastIndexOf("/");
		if (i!=-1)
			this.RootPath=this.RootPath.substr(0,i);
	};
	this.RootPath=this.RootPath+'/';
	this.SoundPath=this.TopicPath;
	this.AudioPath=this.TopicPath+"../../audio/";	
	this.TemplatePath=this.RootPath+"TEMPLATE/Default/";	
}

SoundPlayerClass.prototype.Play = function(sndfile,audiofile,templatefile,asyncron)
{
	if (!asyncron)
		asyncron=false;
	this.asyncron=asyncron;
	
	if (sndfile.length==0)
	{
		if (window.OnErrorPlaySoundLocal)
			OnErrorPlaySoundLocal(0x8000EEEE,R_WMP_not_installed);
		return;
	}
		
	if (sndfile.substr(sndfile.length-4)==".ASX")
	{
		this.StartAsx(sndfile);
		return;
	};
	if (!templatefile)
	{
		fname=(audiofile ? this.AudioPath+sndfile : this.SoundPath+sndfile);
	}
	else
	{
		fname=this.TemplatePath+sndfile;
	};
	if (bWMPIsAvailable)
	{
		if (SoundPlayer)
		{
			SoundPlayer.Stop();
			setTimeout("SoundPlayer.Open(\""+fname+"\")",20);
		};
	}
	else if (window.OnErrorPlaySoundLocal)
		OnErrorPlaySoundLocal(0x8000EEEE,R_WMP_not_installed);
}

SoundPlayerClass.prototype.Stop = function()
{
	if (b_asxPlaying)
	{
		this.StopAsx();
		return;
	};
	if (bWMPIsAvailable)
	{
		if (SoundPlayer)
			setTimeout("SoundPlayer.Stop()",10);
	};
}

SoundPlayerClass.prototype.IsAvailable = function()
{
	return bWMPIsAvailable;
};

SoundPlayerClass.prototype.SetTemplateRootPath = function(s)
{
	if (s.length>0)
		this.TemplatePath=s;
};

SoundPlayerClass.prototype.SetTemplateSet = function(s)
{
	if (s.length>0)
		this.TemplatePath=this.RootPath+"TEMPLATE/"+s+"/";	
};

SoundPlayerClass.prototype.SetSoundPath = function(s)
{
	if (s.length>0)
		this.SoundPath=s;
};

SoundPlayerClass.prototype.StartAsx = function (fname)
{
	this.mysound=sounds[fname];
	if (!this.mysound)
	{
		if (OnErrorPlaySound)
			OnErrorPlaySound();
		return;
	};
	if (!this.mysound.fct)
	{
		if (OnErrorPlaySound)
			OnErrorPlaySound();
		return;
	};
	if (this.mysound.fct==0)
	{
		if (OnErrorPlaySound)
			OnErrorPlaySound();
		return;
	};
	this.asxindex=0;
	b_asxSuccessfull=false;
	if (bWMPIsAvailable)
	{
		if (SoundPlayer)
		{
			b_asxPlaying=true;
			s=this.mysound.flist[this.asxindex];
			if (s.substr(0,5)=="TEMP:")
				this.Play(s.substr(5),false,true)
			else
				this.Play(s);
		};
	}
	else if (window.OnErrorPlaySound)
		OnErrorPlaySound(0x8000EEEE,R_WMP_not_installed);
};

SoundPlayerClass.prototype.StopAsx = function ()
{
	if (bWMPIsAvailable)
	{
		SoundPlayer.Stop();
	}
	b_asxPlaying=false;
};

SoundPlayerClass.prototype.AsxNext = function ()
{
	if (b_asxPlaying)
	{
		this.asxindex++;
		if (!this.mysound)
		{
			return;
		};
		if (!this.mysound.fct)
		{
			return;
		};
		if (this.asxindex<this.mysound.fct)
		{
			s=this.mysound.flist[this.asxindex];
			if (s.substr(0,5)=="TEMP:")
				this.Play(s.substr(5),false,true)
			else
				this.Play(s);
		}
		else
		{
			b_asxPlaying=false;
			if (b_asxSuccessfull)
			{
				if (OnEndPlaySound)
					OnEndPlaySound();
			}
			else
			{
				if (OnErrorPlaySound)
					OnErrorPlaySound();
			};
		};
	};
};

function OnEndPlaySoundLocal(v1)
{
	if (SoundPlayerObj.asyncron==true)
		return;
	if (!b_asxPlaying)
	{
		OnEndPlaySound(v1);
		return;
	};
	b_asxSuccessfull=true;
	setTimeout("SoundPlayerObj.AsxNext()",10);
};

function OnErrorPlaySoundLocal(v1,v2)
{
	if (SoundPlayerObj.asyncron==true)
		return;
	if (v1==-2147467259)
		return;
	if (!b_asxPlaying)
	{
		OnErrorPlaySound(v1,v2);
		return;
	};
	setTimeout("SoundPlayerObj.AsxNext()",10);
};

var SoundPlayerObj = new SoundPlayerClass();

try
{
	SoundPlayer.Stop();
}
catch(e)
{
	bWMPIsAvailable=false;
}
