var songTimeOne;

document.write('<DIV ID="asadalSoundPlayer" style="position:absolute;left:-1000;top:-1000"></DIV>');

var asadalSound = function ()
{
	this.imgPlayS	= "/image/design/list/button/btn_play.gif";
	this.imgPlayB	= "/image/design/list/button/btn_play2.gif";
	this.imgStopS	= "/image/design/list/button/btn_stop.gif";
	this.imgStopB	= "/image/design/list/button/btn_stop2.gif";
	this.playObj	= document.getElementById('asadalSoundPlayer');
	this.playBtnList= [];
	this.rePlay		= false;
	this.browsers	= '';
	this.songFile	= '';
	this.objNames	= '';
	this.controlObj	= '';
	this.objs		= '';
}

asadalSound.prototype.getLoad	= function ()
{
	if (this.browsers == "MSIE" || this.browsers == "Netscape")
	{ 
		this.playObj.innerHTML	= '<object id="objListTypeSoundPlayer" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft?Windows?Media Player components..." type="application/x-oleobject" width="0" height="0" style="border: 0px solid #000000; padding: 0px; margin: 0px; background-color: #000000">'
								+ '	<param name="url" value="' + this.songFile + '">'
								+ '	<param name="volume" value="100">'
								+ '	<embed id="sound" type="application/x-mplayer2" src="' + this.songFile + '" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2" url="' + this.songFile + '" volume="100" width="0" height="0"></embed>'
								+ '</object>';
	}
	else
	{
		this.playObj.innerHTML	= '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="0" HEIGHT="0" ID="objListTypeSoundPlayer" style="border: 0px solid #000000; padding: 0px; margin: 0px; background-color: #000000" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
								+ '	<PARAM name="SRC" VALUE="' + this.songFile + '">'
								+ '	<PARAM name="AUTOPLAY" VALUE="true">'
								+ '	<PARAM name="CONTROLLER" VALUE="false">'
								+ '	<PARAM name="VOLUME" VALUE="100">'
								+ '	<PARAM name="ENABLEJAVASCRIPT" VALUE="true">'
								+ '	<PARAM name="TYPE" VALUE="audio/wav">'
								+ '	<embed classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" name="objListTypeSoundPlayer" id="objListTypeSoundPlayer" src="' + this.songFile + '" pluginspage="http://www.apple.com/quicktime/download/" volume="100" enablejavascript="true" type="audio/wav" height="0" width="0" style="border: 0px solid #000000; padding: 0px; margin: 0px; background-color: #000000" autostart="true"></embed>'
								+ '</OBJECT>';
	}
}

asadalSound.prototype.setConfig = function ()
{
	this.controlObj	= document.getElementById(this.objNames);
	this.browsers	= this.browserType();
}

asadalSound.prototype.playBtnControl = function ()
{
	for (key in this.playBtnList)
	{
		if (this.objNames == key)
		{
			document.getElementById(key).src	= (this.playBtnList[key] == '') ? this.imgStopS : this.imgStopB;
		}
		else
		{
			document.getElementById(key).src	= (this.playBtnList[key] == '') ? this.imgPlayS : this.imgPlayB;
		}
	}
}

asadalSound.prototype.listSoundPlayer = function (objName, file, btn)
{
	this.playBtnList[objName]	= (btn) ? btn : '';
	this.objNames				= objName;
	this.playBtnControl();

	// 다른 음악 파일을 재생을 할때
	if (this.songFile != '')
	{
		this.songStop();
	}

	// 같은 음악 파일을 눌렀을 때는 정지로 간주
	if (this.songFile == file)
	{
		this.songStop();
		if (this.rePlay == false)
		{
			this.controlObj.src	= (this.playBtnList[this.objNames] == '') ? this.imgPlayS : this.imgPlayB;
			this.rePlay			= true;
			return;
		}
	}
	this.rePlay	= false;
	this.isTypeSoundBannerStop();

	// 현 재생 파일과 브라우저 정보를 획득한다
	this.setConfig();
	this.songFile		= file;
	this.getLoad();
	setTimeout('displayTime();', 1000);
}

asadalSound.prototype.songStop = function ()
{
	if (!this.browsers) return;

	(this.browsers == "MSIE" || this.browsers == "Netscape") ? this.objs.controls.Stop() : this.objs.Stop();

	clearTimeout(songTimeOne);
}

asadalSound.prototype.displayTime = function ()
{
	var times			= '';
	var duration		= '';
	var songState		= '';
	var bufferStatus	= '';
	var mins			= '';
	var secs			= '';
	var scale			= '';

	// IE, Netscape 일때 재생 정보
	if (this.browsers == "MSIE" || this.browsers == "Netscape")
	{
		this.objs		= document.objListTypeSoundPlayer;
		if (!this.objs.settings)
		{
			alert('재생할 음악이 없습니다.'); return;
		}

		songState		= this.objs.playState;
		bufferStatus	= this.objs.network.bufferingProgress;
		if (bufferStatus < 100)
		{
			songTimeOne	= setTimeout('displayTime();', 20);
			return;
		}
		times		= this.formatTime(Math.round(this.objs.controls.currentposition));
		duration	= this.formatTime(this.objs.currentMedia.duration);
		if (songState == 1)
		{
			this.controlObj.src	= (this.playBtnList[this.objNames] == '') ? this.imgPlayS : this.imgPlayB;
			return;
		}
	}
	else	// 그외 일때 재생 정보
	{
		if (typeof document.objListTypeSoundPlayer.GetVolume != 'function')
		{
			alert('재생할 음악이 없습니다.'); return;
		}
		this.objs		= document.embeds['objListTypeSoundPlayer'];
		try { songState = this.objs.GetPluginStatus(); }
		catch (e) { songTimeOne = setTimeout('displayTime();', 20); }

		if (songState.toLowerCase() != 'playable' && songState.toLowerCase() != 'complete')
		{
			songTimeOne = setTimeout('displayTime();', 20);
			return;
		}
		times		= this.objs.GetTime();
		duration	= this.objs.GetDuration();
		scale		= this.objs.GetTimeScale();
		times		= this.formatTime(Math.floor(times*(1/scale)));
		duration	= this.formatTime(Math.floor(duration*(1/scale)));
		if (times == duration)
		{
			this.controlObj.src	= (this.playBtnList[this.objNames] == '') ? this.imgPlayS : this.imgPlayB;
			return;
		}
	}
	songTimeOne = setTimeout('displayTime();', 250);
}

asadalSound.prototype.formatTime = function (total)
{
	var second = parseInt(total) % 60;
	var minute = parseInt(total / 60);
	return ((minute < 10) ? "0" : "") + minute + ":" + ((second < 10) ? "0" : "") + second;
}

asadalSound.prototype.browserType = function ()
{
	var type	= ['Opera', 'Firefox', 'MSIE', 'Netscape', 'Safari'];
	var browser	= navigator.userAgent;
	var cnt		= type.length;

	for (var i=0; i<cnt; i++)
	{
		if (browser.indexOf(type[i])!= -1)
		{
			return type[i];
		}
	}
}

/* 배너 재생 중지 기능 */
asadalSound.prototype.isTypeSoundBannerStop = function ()
{
	var soundBanner	= document.getElementById('objSoundBanner');

	if (soundBanner)
	{
		soundBanner.TGotoFrame('/sound', 2);
		soundBanner.TCallLabel('/sound', 'f2');
	}
}

var soundControl		= new asadalSound();

listSoundPlayer			= function (objName, file, btn)	{ soundControl.listSoundPlayer(objName, file, btn);	}
displayTime				= function ()					{ soundControl.displayTime();						}
listTypeSoundPlayerStop = function ()					{ isTypeSoundBannerStop(); soundControl.songStop();	}
isTypeSoundBannerStop	= function ()					{ soundControl.isTypeSoundBannerStop();				}
