import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.geom.Transform;
import flash.media.SoundTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.events.SliderEvent;

//Kayleigh Smith
//Lesson 2
//06/02/2012

//Declarations
var req1:URLRequest = new URLRequest(“clock.mp3″);
var s1:Sound = new Sound();
var c1:SoundChannel = new SoundChannel;
var trans1:SoundTransform = new SoundTransform;
var vol1:Number = new Number;
var loop1:Boolean = new Boolean;

var req2:URLRequest = new URLRequest(“DoorCreaking.mp3″);
var s2:Sound = new Sound();
var c2:SoundChannel = new SoundChannel;
var trans2:SoundTransform = new SoundTransform;
var vol2:Number = new Number;
var loop2:Boolean = new Boolean;

var req3:URLRequest = new URLRequest(“Waterfillingglassbottle.mp3″);
var s3:Sound = new Sound();
var c3:SoundChannel = new SoundChannel;
var trans3:SoundTransform = new SoundTransform;
var vol3:Number = new Number;
var loop3:Boolean = new Boolean;

//Initializations
vol1 = 0;
vol2=0;
vol3=0;
s1.load(req1);
s2.load(req2);
s3.load(req3);
loop1= false;
loop2= false;
loop3= false;

in1_mc.visible=false;
in2_mc.visible=false;
in3_mc.visible=false;
//EventListeners
s1.addEventListener(Event.COMPLETE, soundLoaded1);
pl1.addEventListener(MouseEvent.CLICK, playSound1);
st1.addEventListener(MouseEvent.CLICK, stopSound1);
level1.addEventListener(SliderEvent.CHANGE, change1);
le1.addEventListener(MouseEvent.CLICK, loopHandler1);

s2.addEventListener(Event.COMPLETE, soundLoaded2);
pl2.addEventListener(MouseEvent.CLICK, playSound2);
st2.addEventListener(MouseEvent.CLICK, stopSound2);
level2.addEventListener(SliderEvent.CHANGE, change2);
le2.addEventListener(MouseEvent.CLICK, loopHandler2);
s3.addEventListener(Event.COMPLETE, soundLoaded3);
pl3.addEventListener(MouseEvent.CLICK, playSound3);
st3.addEventListener(MouseEvent.CLICK, stopSound3);
level3.addEventListener(SliderEvent.CHANGE, change3);
le3.addEventListener(MouseEvent.CLICK, loopHandler3);

//functions
function soundLoaded1(event:Event):void
{
c1 = s1.play();
c1.stop();

c1.soundTransform = trans1;
trans1.volume = vol1;
}

function soundLoaded2(event:Event):void
{ c2 = s2.play();
c2.stop();
c2.soundTransform = trans2;
trans2.volume = vol2;
}

function soundLoaded3(event:Event):void
{
c3 = s3.play();
c3.stop();
c3.soundTransform = trans3;
trans3.volume = vol3;
}

function playSound1(event:MouseEvent):void
{
c1 = s1.play();
c1.soundTransform = trans1;
trans1.volume = vol1;
c1.addEventListener(Event.SOUND_COMPLETE, soundComp1);
in1_mc.visible=true;
}

function playSound2(event:MouseEvent):void
{
c2 = s2.play();
c2.soundTransform = trans2;
trans2.volume = vol2;
c2.addEventListener(Event.SOUND_COMPLETE, soundComp2);
in2_mc.visible=true;
}
function playSound3(event:MouseEvent):void
{
c3 = s3.play();
c3.soundTransform = trans3;
trans3.volume = vol3;
c3.addEventListener(Event.SOUND_COMPLETE, soundComp3);
in3_mc.visible=true;
}

function stopSound1(event:MouseEvent):void
{
c1.stop();
in1_mc.visible=false;
}

function stopSound2(event:MouseEvent):void
{

c2.stop();
in2_mc.visible=false;
}

function stopSound3(event:MouseEvent):void
{

c3.stop();
in3_mc.visible=false;

}

function change1 (e:SliderEvent):void
{
vol1=e.target.value / 100;
c1.soundTransform = trans1;
trans1.volume = vol1;
}

function change2 (e:SliderEvent):void
{ vol2=e.target.value / 100;
c2.soundTransform = trans2;
trans2.volume = vol2;
}
function change3 (e:SliderEvent):void
{
vol3=e.target.value / 100;
c3.soundTransform = trans3;
trans3.volume = vol3;
function loopHandler1(event:MouseEvent):void
{
if(le1.selected == false)
{
loop1=false;
}
else
{
loop1=true;
}
}

function loopHandler2(event:MouseEvent):void
{
if(le2.selected == false)
{
loop2=false;
}
else
{
loop2=true;
}
}

function loopHandler3(event:MouseEvent):void
{
if(le3.selected == false)
{
loop3=false;
}
else
{
loop3=true;
}
}

//IMPORTANT

//RECURSION function calling itself over and over (loop effect)
function soundComp1(e:Event):void
{
c1.soundTransform = trans1;
trans1.volume = vol1;
s1.play();
if(loop1==true){
in1_mc.visible=true;
c1.addEventListener(Event.SOUND_COMPLETE, soundComp1);
}
else{
in1_mc.visible=false;
c1.stop();
}
}

function soundComp2(e:Event):void
{
c2.soundTransform = trans2;
trans2.volume = vol2;
s2.play();
if(loop2==true){
in2_mc.visible=true;
c2.addEventListener(Event.SOUND_COMPLETE, soundComp2);
}
else{
in2_mc.visible=false;
c2.stop();
}
}

function soundComp3(e:Event):void
{
c3.soundTransform = trans3;
trans3.volume = vol3;
s3.play();
if(loop3==true){
in3_mc.visible=true;
c3.addEventListener(Event.SOUND_COMPLETE, soundComp3);
}
else{
in3_mc.visible=false;
c3.stop();
}
}