var snd1;
var snd2;
var snd3;
var snd4;
var channel3:SoundChannel=new SoundChannel();
var channel4:SoundChannel=new SoundChannel();
var channel1:SoundChannel=new SoundChannel();
var channel2:SoundChannel=new SoundChannel();
var snd;
var channel:SoundChannel=new SoundChannel();

var CurrentAudioPosition:Number=0;
var CurrentAudioPosition1:Number=0;
var CurrentAudioPosition2:Number=0;
var CurrentAudioPosition3:Number=0;
var CurrentAudioPosition4:Number=0;

var adjustVolume:Number=.5;
var adjustEffectVolume:Number=.5;
var tempAdjustVol,tempEffectVol;
var muteFlag:Boolean = false;

function playSoundFromEngine(str:String)
{  
	if(str == "click_on_scene")
	{
		snd=getDefinitionByName(str);
		snd=new snd();
		try
		{
		channel=snd.play();
		channel.soundTransform=soundTransform2;
		}
		catch(e){channel=null}
	}
	else
	{
		/*if(str == "puzzleComplete")
		{
			if 	(puzzleSkipped==true)
			{
				return;
			}
		}*/
		if(muteFlag)
			return;
	   // muteflag=true;
		trace("inside playSound");
		snd=getDefinitionByName(str);
		snd=new snd();
		try
		{
			channel=snd.play();
			channel.soundTransform=soundTransform2;
		}
		catch(e){channel=null}
	}
}
function playSound1(str:String)
{  
   // muteflag=true;
	if(muteFlag)
		return;

	var snd31:Class=objectLoader.levelLoader.contentLoaderInfo.applicationDomain.getDefinition(str) as Class
	snd3=new snd31();
	try
	{
		channel3=snd3.play();
		channel3.soundTransform=soundTransform2;
	}
	catch(e){channel3=null}
}
function playSoundLoop(str:String)
{  
   // muteflag=true;
   if(muteFlag)
		return;
   trace("inside playSound");
	var snd11:Class=objectLoader.levelLoader.contentLoaderInfo.applicationDomain.getDefinition(str) as Class
	//snd1=getDefinitionByName(str);
	try
	{
		channel1.stop();
	}
	catch(e)
	{
	}	
	snd1=new snd11();
	try
	{
		channel1=snd1.play(0,int.MAX_VALUE);
		channel1.soundTransform=soundTransform2;
	}
	catch(e){channel1=null}
}

function playSoundLoop1(str:String)
{  
   // muteflag=true;
   if(muteFlag)
		return;
   trace("inside playSound");
   var snd21:Class=objectLoader.levelLoader.contentLoaderInfo.applicationDomain.getDefinition(str) as Class
	//snd2=getDefinitionByName(str);
	try
	{
		channel2.stop();
	}
	catch(e)
	{
	}
	snd2=new snd21();
	try
	{
		channel2=snd2.play(0,int.MAX_VALUE);
		channel2.soundTransform=soundTransform2;
	}
	catch(e){channel2=null}
}
function playSoundLoop2(str:String)
{  
   // muteflag=true;
   if(muteFlag)
		return;
   trace("inside playSound");
   var snd41:Class=objectLoader.levelLoader.contentLoaderInfo.applicationDomain.getDefinition(str) as Class
	//snd2=getDefinitionByName(str);
	try
	{
		channel4.stop();
	}
	catch(e)
	{
	}	
	
	snd4=new snd41();
	try
	{
	channel4=snd4.play(0,int.MAX_VALUE);
	channel4.soundTransform=soundTransform2;
	}
	catch(e){channel4=null}
}

function StopAllChannels()
{
	/*try
	{
		if (channel!=null)
		{
			channel.stop();
		}
	}
	catch(e)
	{
		channel=null;
	}	*/
	try
	{
		if (channel1!=null)
		{
			channel1.stop();
		}
	}
	catch(e)
	{
		channel1=null;
	}
	try
	{
		if (channel2!=null)
		{
			channel2.stop();
		}
	}
	catch(e)
	{
		channel2=null;
	}
	try
	{
		if (channel3!=null)
		{
			channel3.stop();
		}
	}
	catch(e)
	{
		channel3=null;
	}		
	try
	{
		if (channel4!=null)
		{
			channel4.stop();
		}
	}
	catch(e)
	{
		channel4=null;
	}		
}

function setAllVolume()
{
	soundTransform1.volume=musicVolume;
	soundTransform2.volume=soundVolume;
	setVolume();

	try
	{
		channel.soundTransform=soundTransform2;
	}
	catch(e)
	{
	}
	
	try
	{
		channel1.soundTransform=soundTransform2;
	}
	catch(e)
	{
	}
	try
	{
		channel2.soundTransform=soundTransform2;
	}
	catch(e)
	{
	}
	try
	{
		channel3.soundTransform=soundTransform2;
	}
	catch(e)
	{
	}
	try
	{
		channel4.soundTransform=soundTransform2;
	}
	catch(e)
	{
	}
}

function setVolume()
{
	var soundList:Array = new Array('Haunted_Cottage','Ground_Keepers_Cottage','Hamlet_of_Brenn','Tower_Of_Secrets','Squirrel_Headquarters','Gypsys_Tale_Theme','Village6');
	for(var i = 0;  i < soundList.length; i++)
	{
		try
		{
			sound[soundList[i]]._sound_channel.soundTransform=soundTransform1;
		}
		catch(e)
		{
		}
	}
}
function StopAllMusics()
{
	trace("inside stopallmusic");
	muteFlag = true;
	try
	{
		/*if(channel!=null)
		{
			CurrentAudioPosition=channel.position;
			channel.soundTransform=soundTransform2;
		}*/
		
		//Stopping Engine side sounds
		if(channel1!=null)
		{
			CurrentAudioPosition1=channel1.position;
			channel1.stop();
		}
	}
	catch(e)
	{
	}
	try
	{
		//Stopping Engine side sounds
		if(channel2!=null)
		{
			CurrentAudioPosition2=channel2.position;
			channel2.stop();
		}
	}
	catch(e)
	{
	}
	try
	{
		//Stopping Engine side sounds
		if(channel3!=null)
		{
			CurrentAudioPosition3=channel3.position;
			channel3.stop();
		}
	}
	catch(e)
	{
	}
	try
	{
		//Stopping Engine side sounds
		if(channel4!=null)
		{
			CurrentAudioPosition4=channel4.position;
			channel4.stop();
		}
	}
	catch(e)
	{
	}
}

function ResumeMusics()
{
		//channel1.addEventListener(Event.SOUND_COMPLETE,soundLoop);
		//musicVolume=tempAdjustVol;
		//soundVolume=tempEffectVol;	
	
	
	    //DebugManager.Debug(musicVolume);
		
		muteFlag = false;
		/*try
		{
			//Engine side sounds restarting
			if(channel!=null)
			{
				//channel1.soundTransform=soundTransform2;
				channel= snd.play(CurrentAudioPosition,int.MAX_VALUE);/////
				channel.soundTransform=soundTransform2;
			}
		}
		catch(e)
		{
		}		*/
	
		try
		{
			//Engine side sounds restarting
			if(channel1!=null)
			{
				//channel1.soundTransform=soundTransform2;
				channel1= snd1.play(0,int.MAX_VALUE);/////
				channel1.soundTransform=soundTransform2;
			}
		}
		catch(e)
		{
		}		
		
		try
		{
			//Engine side sounds restarting
			if(channel2!=null)
			{
				channel2= snd2.play(0,int.MAX_VALUE);/////
				channel2.soundTransform=soundTransform2;
			}
		}
		catch(e)
		{
		}	
		/*
		try
		{
			//Engine side sounds restarting
			if(channel3!=null)
			{
				channel3= snd3.play(CurrentAudioPosition2);/////
				channel3.soundTransform=soundTransform2;
			}
		}
		catch(e)
		{
		}*/			
		try
		{
			//Engine side sounds restarting
			if(channel4!=null)
			{
				channel4= snd4.play(0,int.MAX_VALUE);/////
				channel4.soundTransform=soundTransform2;
			}
		}
		catch(e)
		{
		}
}
