//
//	Sound Class
//

Sound = {
	'range': isBGM ? 'Doc' : 'Pages',
	'dir': soundDir,
	'bgm': typeof bgmSrc != 'undefined' ? bgmSrc : false,
	'sounds': sounds,
	'match' : 0,
	'embed' : 0
};

Sound.Render = function() {
	this.ClearPagesBGM();
	/*
	if(navigator.userAgent.indexOf('Safari') >= 0) {
		this.embed	= '<embed ';
		this.embed	+= 'id="Sound" ';
		this.embed	+= 'width="170" ';
		this.embed	+= 'height="45" ';
		this.embed	+= 'style="position: absolute; left: -9999px;" ';
		this.embed	+= 'autostart="true" ';
		this.embed	+= 'loop="true" ';
		this.embed	+= 'src="';
		this.embed	+= this.dir + this.bgm;
		this.embed	+= '">';
		this.embed	+= '</embed>';
		
		document.body.appendChild(NewEl('div', 'SoundBox')).innerHTML	= this.embed;
		this.embed	= $('Sound');
		return;
	}

	this.embed	= NewEl('embed', 'Sound');
	this.embed.setAttribute('width', 170);
	this.embed.setAttribute('height', 45);

	this.embed.style.position	= 'absolute';
	this.embed.style.left		= '-9999px';

	this.embed.setAttribute('autostart', 'true');
	this.embed.setAttribute('loop', 'true');

	document.body.appendChild(this.embed);
	*/
}

Sound.CreateEmbed = function(sound_file) {
	//alert("START : " + this.embed + " / " + this.embed.removed);
	if ( !this.embed || this.embed.removed == true) {
		this.embed = document.createElement("embed");
		this.embed.setAttribute("id", "Sound");
		this.embed.setAttribute("src", sound_file);
		this.embed.setAttribute("hidden", true);
		this.embed.setAttribute("autostart", true);
	} else this.Stop();

	this.embed.removed = false;
	document.body.appendChild(this.embed);
	this.embed = $('Sound');
}

Sound.Play = function() {
	$('imgSoundArea').turn(false);
	
	if(this.range == 'Doc')
		this.PlayDocBGM();
	else if(this.range == 'Pages')
		this.PlayPagesBGM();
}

Sound.Stop = function() {
	//this.embed.Stop();
	//alert("STOP : " + this.embed + " / " + this.embed.removed);
	if ( this.embed && !this.embed.removed ) {
		this.embed.Stop();
		this.ClearPagesBGM();
		document.body.removeChild(this.embed);
		this.embed.removed = true;
		
		if($('imgSoundArea').turned == true)
		{
			if($('imgPageSound'))
			{
				$('imgPageSound').turn(true);
			}
		}
	}
}

Sound.PlayDocBGM = function() {
	//alert('Sound Doc');
	//this.embed.setAttribute('src', this.dir + this.bgm);
	this.CreateEmbed(this.dir + this.bgm);
	this.embed.Play();
}

Sound.PlayPagesBGM = function() {
	if(spreadPage == 1)
	{
		sounds_key = parseInt(Book.page / 2);
	}
	else
	{
		sounds_key = Book.page - 1;
	}
	
	// ----------------------------------------------------------------
	// match º¯¼ö Àü¿ª ÁöÁ¤ (2007-02-01)
	// ¼öÁ¤ÀÚ : ¿À»ó¿î(Oh Sang-woon, osu30946@orange-design.co.kr)
	// var match	= this.sounds[Book.page].match(/([0-9]+)_([0-9]+).[a-z0-9]+/i);
	//this.match	= this.sounds[Book.page].match(/([0-9]+)_([0-9]+).[a-z0-9]+/i);
	this.match = this.sounds[sounds_key].match(/([0-9]+)_([0-9]+).[a-z0-9]+/i);
	// ----------------------------------------------------------------
	
	if(this.match) {
		//this.embed.setAttribute('src', this.dir + this.sounds[Book.page]);
		//this.embed.Play();
			
		//this.CreateEmbed(this.dir + this.sounds[Book.page]);
		this.CreateEmbed(this.dir + this.sounds[sounds_key]);
		
		if($('imgPageSound').turned == false)
		{
			this.SetPagesBGM();
		}
	}
}


Sound.SetPagesBGM = function() {
	// ----------------------------------------------------------------
	// match º¯¼ö Àü¿ª ÁöÁ¤ (2007-02-01)
	// ¼öÁ¤ÀÚ : ¿À»ó¿î(Oh Sang-woon, osu30946@orange-design.co.kr)
	// this.goNext	= setInterval('Book.Next()', parseInt(match[2]) * 1000);
	this.goNext	= setInterval('Book.Next()', parseInt(this.match[2]) * 1000);
	// ----------------------------------------------------------------
}

Sound.ClearPagesBGM = function() {
	clearInterval(this.goNext);
	this.goNext = false;
}