import flash.events.Event;
import flash.events.MouseEvent;
import fl.events.SliderEvent;

//Kayleigh Smith
//Balloon Example
//30-01-2012

//Libraries

//Declaration

var inflation:Number = new Number;
var filling:Boolean = new Boolean;
var emptying:Boolean = new Boolean;
var fillRate:Number = new Number;
var myY:Number = new Number;
var myX:Number = new Number;
var firstrun:Boolean = new Boolean;
var takeoff:Boolean = new Boolean;
var myOrigX:Number = new Number;
var myOrigY:Number = new Number;
var s1:Sound = new Sound();
var ctrl1:SoundChannel = new SoundChannel;
var trans1:SoundTransform = new SoundTransform;
var req1:URLRequest = new URLRequest("balloonpop.mp3");
var myResult:Number = new Number;
var myTotal:Number = new Number;
var jitter:Number = new Number;


//Initialisation

inflation = 0.1;
fillRate = 0.1;
filling = false;
emptying=false;
takeoff=false;
myY= balloon2_mc.y;
myX= balloon2_mc.x;
myOrigX = myX;
myOrigY = myY;
s1.load(req1);
myResult = 0;
myTotal = 0;
myScore.text = "Score: " + myResult + " / " + myTotal;
jitter = 10;

//Event Listeners

s1.addEventListener(Event.COMPLETE, soundloaded1);
fill_btn.addEventListener(MouseEvent.CLICK, inflate);
empty_btn.addEventListener(MouseEvent.CLICK, deflate);
stop_btn.addEventListener(MouseEvent.CLICK, stopFill);
fly_btn.addEventListener(MouseEvent.CLICK, fly);
newBtn.addEventListener(MouseEvent.CLICK, newBal);
slider_mc.addEventListener(SliderEvent.CHANGE,pressureChange);
balloon2_mc.addEventListener(MouseEvent.CLICK, explode);
this.addEventListener(Event.ENTER_FRAME, fillBalloon);

//Functions

function inflate(event:MouseEvent):void{
trace("fill button pressed");


filling = true;
emptying=false;
}

function deflate(event:MouseEvent):void{
trace("deflate button pressed");

filling = false;
emptying = true;

}

function stopFill(event:MouseEvent):void{
trace("stopFill button pressed");

filling = false; emptying=false;
}

function fly(event:MouseEvent):void{
trace("fly button pressed");
takeoff=true;
myTotal += 1;
myScore.text = "Score: " + myResult + " / " + myTotal;
}

function fillBalloon(event:Event):void{
if(firstrun==true)
{
balloon2_mc.height = inflation * 300;
balloon2_mc.width = inflation * 220;
}
if(filling == true){

if(inflation < 1){ inflation += (fillRate * 0.1); balloon2_mc.height =
inflation * 300; balloon2_mc.width = inflation * 220; firstrun==true; } } if(emptying==true){ if(inflation > 0.1){
inflation -= (fillRate * 0.1);
balloon2_mc.height = inflation * 300;
balloon2_mc.width = inflation * 220;
}
}

if(takeoff == true)
{
if(myY > -25)
balloon2_mc.y = myY;
balloon2_mc.x = myX;
this.nozzle_mc.y=myY;
myX += jitter * (Math.random() - 0.5);
myY -=5;
}
}
function pressureChange(e:SliderEvent):void{
fillRate = e.target.value / 200;
}

function explode(Event:MouseEvent):void
{
Object(this).balloon2_mc.visible=false;
Object(this).nozzle_mc.visible=false;
}

function newBal(Event:MouseEvent):void
{
balloon2_mc.visible = true;
this.nozzle_mc.visible = true;
filling = false;
emptying = false;
takeoff = false;
inflation = 0.1;
balloon2_mc.height = inflation * 300;
balloon2_mc.width = inflation * 220;
myY = myOrigY;
myX = myOrigX;
balloon2_mc.x = myX;
balloon2_mc.y = myY;
nozzle_mc.x = myX;
nozzle_mc.y = myY;
}
function soundloaded1(event:Event):void
{
ctrl1 = s1.play();
ctrl1.stop();
ctrl1.soundTransform = trans1;
trans1.volume = 1;
}