/*-------------------------------------------------------------------------*
|	FileName : SuperMusicPlayer.js                                         |
|   Copyright(c) June 1999, SUPERBOARD Technology, Inc. All right reserved |
*--------------------------------------------------------------------------*/

/**************************************************************************
 * Function Name : function PlayList(name, url, flag)                     *
 * Function Description                                                   *
 *          : À½¾Ç ÇÃ·¹ÀÌ¾î ½ÇÇàÇÑ´Ù.                                     *
 **************************************************************************/
function PlayList(name, url, flag)
{
	this.name = name;
	this.url = url;
	this.flag = flag;
}

/**************************************************************************
 * Function Name : function nowList(name, url, flag)                      *
 * Function Description                                                   *
 *          : ¸®½ºÆ®¿¡ ÀÖ´Â ÇöÀç°îÀ» ¼³Á¤ÇÑ´Ù.                            *
 **************************************************************************/
function nowList(name, url, flag)
{
	this.m_num = 0;
	this.m_plist[this.m_num++] = new PlayList(name, url, flag);
}

/**************************************************************************
 * Function Name : function AddList(name, url, flag)                      *
 * Function Description                                                   *
 *          : ¸®½ºÆ®¿¡ Ç×¸ñÀ» Ãß°¡ÇÑ´Ù.                                   *
 **************************************************************************/
function AddList(name, url, flag)
{
	this.m_plist[this.m_num++] = new PlayList(name, url, flag);
}

/**************************************************************************
 * Function Name : function GetPercent()                                  *
 * Function Description                                                   *
 *          : ÁøÇà ÆÛ¼¾Æ®¸¦ ¸®ÅÏÇÑ´Ù.                                     *
 **************************************************************************/
function GetPercent()
{
	return Math.round(this.m_player.CurrentPosition * 100 / this.m_player.SelectionEnd);
} 

/**************************************************************************
 * Function Name : function NextMusic()                                   *
 * Function Description                                                   *
 *          : ÇÃ·¹ÀÌ¾î ½ÃÀÛ ¹× ´ÙÀ½°î.                                    *
 **************************************************************************/
function NextMusic() // ½ÃÀÛ/´ÙÀ½°î
{
	if(this.m_num <= 0)
	{
		this.m_text.value = "µî·ÏµÈ À½¾ÇÀÌ ¾ø½À´Ï´Ù";
		return;
	}
	itmp = this.m_now;
	do
	{
		if (++this.m_now >= this.m_num)
			this.m_now = 0;
	} while (((itmp != this.m_now) && !this.m_plist[this.m_now].flag))	// flag °¡ 0ÀÎ°ÍÀº ÇÃ·¹ÀÌ ÇÏÁö ¾Ê´Â´Ù.

	this.Play_now();

}

/**************************************************************************
 * Function Name : function AutoNext()                                    *
 * Function Description                                                   *
 *          : ÀüÃ¼ Repeat ½ÇÇà.                                           *
 **************************************************************************/
function AutoNext()
{
	if(this.m_num <= 0)
	{
		this.m_text.value = "µî·ÏµÈ À½¾ÇÀÌ ¾ø½À´Ï´Ù";
		return;
	}
	if (!this.m_brepeat) // ¹Ýº¹ÀÌ ¾Æ´Ò¶§
	{
		itmp = this.m_now;
		do
		{
			if (++this.m_now >= this.m_num)
			{
				this.m_now = 0;
				if (!this.m_brewind)
				{
					this.Stop();
					return;
				}
			}

		} while(((itmp != this.m_now) && !this.m_plist[this.m_now].flag))	// flag °¡ 0ÀÎ°ÍÀº ÇÃ·¹ÀÌ ÇÏÁö ¾Ê´Â´Ù.
	}

	this.Play_now();
}

/**************************************************************************
 * Function Name : function PrevMusic()                                   *
 * Function Description                                                   *
 *          : ÀÌÀü°î ÇÃ·¹ÀÌ.                                              *
 **************************************************************************/
function PrevMusic()
{
	if(this.m_num <= 0)
	{
		this.m_text.value = "µî·ÏµÈ À½¾ÇÀÌ ¾ø½À´Ï´Ù";
		return;
	}
	do
	{
		if (--this.m_now < 0)
			this.m_now = this.m_num-1;
	} while (!this.m_plist[this.m_now].flag);	// flag °¡ 0ÀÎ°ÍÀº ÇÃ·¹ÀÌ ÇÏÁö ¾Ê´Â´Ù.

	this.Play_now();
}

/**************************************************************************
 * Function Name : function Play_now()                                    *
 * Function Description                                                   *
 *          : ÇöÀç°î ÇÃ·¹ÀÌ.                                              *
 **************************************************************************/
function Play_now()
{
	this.m_delay = 0;
	this.m_state = 1;	
	this.m_old = 100;
	this.m_player.Open(this.m_plist[this.m_now].url);	
	this.m_text.value = this.m_plist[this.m_now].name	
	this.SetInterVal(true);
}

/**************************************************************************
 * Function Name : function Play()                                        *
 * Function Description                                                   *
 *          : ¹ÂÁ÷ ÇÃ·¹ÀÌ¾î ½ÃÀÛ.                                         *
 **************************************************************************/
function Play()
{
	if(this.m_num <= 0)
	{
		this.m_text.value = "µî·ÏµÈ À½¾ÇÀÌ ¾ø½À´Ï´Ù";
		return;
	}

	if (this.m_state == 2)		// ÀÏ½ÃÁ¤Áö »óÅÂÀÌ¸é
	{
		this.m_player.play();	// ½ÇÇà
	}
	else 
	{
		if(this.m_now == -1)
			this.NextMusic();
		else
			this.Play_now();
	}
	this.m_state = 1;
}

/**************************************************************************
 * Function Name : function Pause()                                       *
 * Function Description                                                   *
 *          : ¹ÂÁ÷ ÇÃ·¹ÀÌ¾î ÀÏ½ÃÁ¤Áö.                                     *
 **************************************************************************/
function Pause()
{
	this.m_state = 2;
	this.m_player.pause();	// ½ÇÇà
}

/**************************************************************************
 * Function Name : function State()                                       *
 * Function Description                                                   *
 *          : ÇöÀç Ãâ·Â ÆÛ¼¾Å×ÀÌÁö.                                       *
 **************************************************************************/
function State()
{
	if (this.m_state == 1)
	{
		var i = this.m_player.CurrentPosition;
		if (i < 1)
			i = 0;
		else
			i = Math.round(i * 100 / this.m_player.SelectionEnd);
		if (!(this.m_old == 100 && i == 100))
		{
			if (!i)
			{
				this.m_delay += this.time;
				if(this.m_blisten && (this.m_delay / 1000 > 20)) // ¹Ì¸®µè±âÀÌ°í 20ÃÊ¸¦ ÃÊ°úÇßÀ»¶§ ½ÇÆÐÇß´Ù´Â ÇÔ¼öÈ£Ãâ
				{
					this.PreListenRtn(false);
					this.Stop();
					return;
				}			
				if (this.m_delay / 1000 > this.timeout)	// 20ÃÊº¸´Ù Å©°Ô µô·¹ÀÌµÇ¸é
				{
					this.AutoNext();
					return;
				}	
				this.m_text.value = this.m_plist[this.m_now].name + " (loading...)"
			}
			else
			{
				if(this.m_blisten) // ¹Ì¸®µè±âÀÏ¶§ ¼º°øÇß´Ù´Â ÇÔ¼öÈ£ÃâÇØÁØ´Ù
				{
					this.PreListenRtn(true);
					this.m_blisten = false;

					return;
				}
				this.m_delay = 0;
				this.m_text.value = this.m_plist[this.m_now].name + " ("+ i +" %)"
				if (i == 100)
					this.AutoNext();
			}
		}
		this.m_old = i;
	}
}

/**************************************************************************
 * Function Name : function Mute()                                        *
 * Function Description                                                   *
 *          : Mute ±â´É.                                                  *
 **************************************************************************/
function Mute()
{
	this.m_player.Mute = this.m_bmute;
} 

/**************************************************************************
 * Function Name : function Stop()                                        *
 * Function Description                                                   *
 *          : ÇÃ·¹ÀÌ¾î Á¤Áö ±â´É.                                         *
 **************************************************************************/
function Stop()	// Á¤Áö
{	
	if (this.m_state == 0)	// Á¤ÁöÁßÀÌ¸é
		return;

	this.SetInterVal(false);
	this.m_player.stop();
	this.m_now--;
	this.m_state = 0;
	this.m_text.value = "Á¤Áö";
}

/**************************************************************************
 * Function Name : function Pause()                                       *
 * Function Description                                                   *
 *          : Pause ±â´É.                                                 *
 **************************************************************************/
function Pause()
{	
	if (this.m_state != 1)	// ÇÃ·¹ÀÌ »óÅÂ°¡ ¾Æ´Ï¸é
		return;

	this.m_player.pause();
	this.m_state = 2;
	this.m_text.value = "ÀÏ½ÃÁ¤Áö";
}

/**************************************************************************
 * Function Name : function SetList()                                     *
 * Function Description                                                   *
 *          : À½¾Ç ¸®½ºÆ®¸¦ Ãâ·ÂÇÔ.                                       *
 **************************************************************************/
function SetList() 
{	
	if (this.popid)
	{
		try
		{
			this.popid.focus();
			return;
		}
		catch(e)
		{
			this.popid = null;
		}
	}

	this.popid = window.open("", "", 'width=246, height=321, scrollbars=yes, status=no');
	
	this.popid.document.writeln("<html><head><title>À½¾Ç ¸®½ºÆ®</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=euc-kr\"></head>");
	this.popid.document.writeln("<style>");
	this.popid.document.writeln("body { SCROLLBAR-FACE-COLOR: 333333; SCROLLBAR-HIGHLIGHT-COLOR: 222222; SCROLLBAR-SHADOW-COLOR: #222222; SCROLLBAR-3DLIGHT-COLOR: 222222; SCROLLBAR-ARROW-COLOR: #333333; SCROLLBAR-TRACK-COLOR: 333333; SCROLLBAR-DARKSHADOW-COLOR: 222222; }");
	this.popid.document.writeln("tr,{ font-size:9pt; color:#2F2F2F; font-family:±¼¸²; line-height:130%;}");
	this.popid.document.writeln(".small {font-size:8pt;}");
	this.popid.document.writeln("</style>");

	this.popid.document.writeln("<body leftmargin=\"0\" topmargin=\"0\" bgcolor=\"222222\">");
	this.popid.document.writeln("<table width=\"230\" border=\"0\" cellspacing=\"0\" cellpadding=\"7\">");
	this.popid.document.writeln("<tr>");
	this.popid.document.writeln("	<td align=\"center\" bgcolor=\"222222\"><strong><img src=\"http://image.superboard.com/link/music/list.gif\" width=\"97\" height=\"16\"></strong></td>");
	this.popid.document.writeln("</tr>");
	this.popid.document.writeln("<tr>");
	this.popid.document.writeln("	<td align=\"center\" background=\"http://image.superboard.com/link/music/sunflower.jpg\">");
	this.popid.document.writeln("		<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">");

	for (i = 0; i < this.m_num; i++)
	{
		input_str  = "		<tr>";
		input_str += "			<td align=\"right\">"+ (i + 1) +". </td>";
		input_str += "			<td><input type=\"checkbox\" name=\"list"+ i +"\"";
		
		if (this.m_plist[i].flag)
			input_str += " checked>";
		else
			input_str += ">";

		input_str += this.m_plist[i].name + "</td>";

		input_str += "		</tr>";
		input_str += "		<tr>";
		input_str += "			<td height=\"1\" colspan=\"2\" background=\"http://image.superboard.com/link/music/dot.gif\"></td>";
		input_str += "		</tr>";

		this.popid.document.write(input_str);
	}

	this.popid.document.writeln("		</table>");
	this.popid.document.writeln("	</td>");
	this.popid.document.writeln("</tr>");
	this.popid.document.writeln("<tr>");
	this.popid.document.writeln("	<td align=\"right\" bgcolor=\"222222\"><a href=\"#\" onclick=\"opener."+ this.id +".SaveList();\"><img src=\"http://image.superboard.com/link/music/completion.gif\" width=\"53\" height=\"17\" border=\"0\"></a></td>");
	this.popid.document.writeln("</tr>");
	this.popid.document.writeln("</table>");
	this.popid.document.writeln("</body>");
}

/**************************************************************************
 * Function Name : function SaveList()                                    *
 * Function Description                                                   *
 *          : Ç×¸ñ ¸®½ºÆ®¸¦ ÀúÀåÇÑ´Ù.                                     *
 **************************************************************************/
function SaveList()
{	
	if (!this.popid)
		return;

	for(i = 0; i < this.m_num; i++)
	{
		input_str = eval("this.popid.list"+ i);

		if (input_str.checked)
			this.m_plist[i].flag = 1;
		else
			this.m_plist[i].flag = 0;
	}

	this.popid.self.close();
	this.popid = null;
}

/**************************************************************************
 * Function Name : function SetInterVal(flag)                             *
 * Function Description                                                   *
 *          : Interval ¼³Á¤.                                              *
 **************************************************************************/
function SetInterVal(flag) 
{	
	if (flag)
	{
		if (this.timerid)
			return;

		this.timerid = setInterval(this.id + ".State()", this.time);
	}
	else
	{
		if (!this.timerid)
			return;

		clearInterval(this.timerid);
		this.timerid = null;
	}
}

/**************************************************************************
 * Function Name : SuperPlayer(IDSTR, MediaID, TextID, time)              *
 * Function Description                                                   *
 *          : ÇÃ·¹ÀÌ¾î ÃÊ±âÈ­.                                            *
 **************************************************************************/
function SuperPlayer(IDSTR, MediaID, TextID, time) //constructor
{
	this.id = IDSTR;
	this.m_player = MediaID;			// ÇÃ·¹ÀÌ¾î ¾ÆÀÌµð
	this.m_text = TextID;				// ÅØ½ºÆ® ¾ÆÀÌµð
	this.m_plist = new Array();			// À½¾Ç ¸®½ºÆ®°¡ µé¾î°¥ ¹öÆÛ
	this.m_num = 0;						// À½¾Ç ¸®½ºÆ®¿¡ µé¾îÀÖ´Â °¹¼ö
	this.m_now = -1;					// ÇöÀç ¼±ÅÃµÇ¾îÀÖ´Â ¸®½ºÆ®
	this.m_state = 0;					// 0: Á¤Áö 1: ½ÇÇàÁß 2:ÀÏ½ÃÁ¤ÁöÁß
	this.m_brepeat = false;				// ¹Ýº¹ ÇÃ·¡±×
	this.m_brewind = false;				// ¸¶Áö¸· °î ÈÄ Ã³À½ºÎÅÍ ´Ù½Ã ÇÃ·¹ÀÌ
	this.m_bmute = false;				// À½¼Ò°Å
	this.m_old = 100;					// ÀÌÀü ÁøÇà ÆÛ¼¾Æ®
	this.time = time;					// Term
	this.m_delay = 0;					// Delay Time
	this.m_timeout = 30;				// Å¸ÀÓ¾Æ¿ô ½Ã°£
	this.popid = null;					// ¸®½ºÆ® ÆË¾÷ ¾ÆÀÌµð
	this.timerid = null;				// Å¸ÀÌ¸Ó¾ÆÀÌµð
	this.m_blisten = false;				// ¹Ì¸®µè±â

	// method
	this.AddList = AddList;				// À½¾ÇÃß°¡
	this.nowList = nowList;				// ÇöÀç°î ¼³Á¤
	this.Play = Play;					// Play
	this.Play_now = Play_now;			// ÇöÀç À½¾ÇÀ» Ã³À½ºÎÅÍ ÇÃ·¹ÀÌÇÑ´Ù
	this.Pause = Pause;					// Pause
	this.NextMusic = NextMusic;			// Next
	this.PrevMusic = PrevMusic;			// Previous
	this.Stop = Stop;					// Stop
	this.AutoNext = AutoNext;			// À½¾ÇÀÌ ³¡³ª¸é ÀÚµ¿À¸·Î ´ÙÀ½ À½¾ÇÀ» µé·ÁÁØ´Ù
	this.Mute = Mute;					// À½¼Ò°Å
	this.State = State;					// setInterval ·Î ÀÌÇÔ¼ö¸¦ ÁöÁ¤ÇØÁÖ¸é ÇØ´ç ÃÊ¸¶´Ù ÇöÀç »óÅÂ¸¦ text·Î ³ªÅ¸³½´Ù.
	this.SetList = SetList;				// ÇÃ·¹ÀÌÇÒ À½¾ÇÀ» ¼±ÅÃÇÑ´Ù.
	this.SaveList = SaveList;			// ¼³Á¤³»¿ëÀ» ÀúÀåÇÑ´Ù
	this.SetInterVal = SetInterVal;		// Å¸ÀÌ¸Ó¸¦ ÀÛµ¿½ÃÅ°°Å³ª Á¤Áö½ÃÅ²´Ù.
	this.PreListenRtn = PreListenRtn;	// ¹Ì¸®µè±â¿¡¼­ È£ÃâÇÏ´ÂÇÔ¼ö
}
