<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    themeColor="#00FF00"
    backgroundColor="#FFFFFF" backgroundGradientColors="{ [0xFFFFFF, 0xFFFFFF] }"
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    frameRate="75"
    layout="absolute" 
    creationComplete="onCreationComplete()" 
    xmlns:comp="components.*" viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            
            [Bindable]
            private var sound : Sound;
            private var soundChannel : SoundChannel;
            private var file : URLRequest;
            private var playing : Boolean = false;
            
            [Bindable]
            private var position : Number = 0;
            
            private function onCreationComplete() : void
            {
                sound = new Sound();
                file = new URLRequest("http://www.cynergysystems.com/blogs/blogs/andrew.trice/christmas/assets/05%20Santa%20Claus%20Is%20Coming%20to%20Town.mp3");
                sound.load( file );
                
                sound.addEventListener( Event.COMPLETE, onSoundComplete );
                sound.addEventListener( Event.OPEN, onSoundOpen );
                sound.addEventListener( ProgressEvent.PROGRESS, onSoundProgress );
              
                  addEventListener( Event.ENTER_FRAME, onEnterFrame )
            }
            
            private function onSoundOpen( event : Event ) : void
            {
                progress.label = "opening media...";
            }
            
            private function onSoundProgress( event : ProgressEvent ) : void
            {
                progress.label = event.bytesLoaded.toString() + "/" + event.bytesTotal.toString();
                progress.setProgress( event.bytesLoaded, event.bytesTotal);
            }
             
            private function onSoundComplete( event : Event ) : void
            {
                progress.label = "media complete";
                slider.maximum = sound.length;
                progress.visible = false;
                player.visible = true;
                //play();    
            }
            
            private function onEnterFrame( event : Event ) : void
            {
                if ( soundChannel != null && playing )
                {
                    slider.value = soundChannel.position;
                }
                    
            }
            
            private function play() : void
            {
                if (position >= sound.length ) position = 0;
                if ( !playing )
                {
                    soundChannel = sound.play( position );
                    playing = true;
                }
                else
                {
                    position = soundChannel.position;
                    soundChannel.stop();
                    playing = false;
                }
                playButton.label = (playing ? "Pause" : "Play");
            }
            
            private function stop() : void
            {
                position = 0;
                soundChannel.stop();
                playing = false;
                slider.value = 0;
                playButton.label = "Play";
            }
            
        ]]>
    </mx:Script>
    
    <mx:ProgressBar 
        id="progress" mode="manual"   
        horizontalCenter="0" verticalCenter="0" 
        width="90%" visible="true"
        hideEffect="{ fadeOut }" />
    
    <mx:Panel 
        borderColor="#00FF00"
        layout="absolute" 


        visible="false" 
        id="player" 
        title="Santa Claus Is Coming To Town! (press play to begin)"   
        height="245" width="500" 
        horizontalCenter="0" 
        verticalCenter="0"
        showEffect="{ fadeIn }">
        
        <mx:Image source="@Embed('/assets/santa.png')" right="10" bottom="35"/>
        
        <mx:Button label="Play" click="play()" id="playButton"  width="65" bottom="10" left="10"/>
        <mx:Button label="Stop" click="stop()" id="stopButton"  width="65" bottom="10" left="83"/>
        
        <mx:HSlider minimum="0" id="slider" enabled="false" bottom="15" left="156" right="10"/>
        <comp:BasicVisualization top="10" bottom="40" left="10" right="159"/>
            
    </mx:Panel>
    
    <mx:Fade id="fadeOut" alphaFrom="1" alphaTo="0" duration="300" />
    <mx:Fade id="fadeIn" alphaFrom="0" alphaTo="1" duration="500" />
    
</mx:Application>