<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="vertical"
    verticalAlign="middle" 
    verticalGap="2"
    backgroundColor="white" 
    creationComplete="initApp()" viewSourceURL="srcview/index.html">
    
    <!-- 
    //
    // Written by:
    // Paul Grudnitski
    // paul.grudnitski@gmail.com
    // www.eduviews.com
    //
    // YouTube Video URL Protocol Example
    //
    // Note: Some Video Display code borrowed from the excellent website blog.flexexamples.com and peterd
    //
    //
    // Permission is hereby granted, free of charge, to any person
    // obtaining a copy of this code and associated documentation
    // files (the "Code"), to deal in the Code without
    // restriction, including without limitation the rights to use,
    // copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the
    // Code is furnished to do so, subject to the following
    // conditions:
    //
    // The above copyright notice and this permission notice shall be
    // included in all copies or substantial portions of the Code.
    //
    // THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    // OTHER DEALINGS IN THE SOFTWARE.
    //    
    -->

    <mx:Script>
        <![CDATA[
        
        import mx.events.VideoEvent;
        import mx.utils.StringUtil;
        import ev.utils.VideoResolver;
        import mx.rpc.events.FaultEvent;
              
        private var youTubeVideo:String = "http://www.youtube.com/watch?v=_barTKxikvA";
        private var referralUrl:String = "http%3A%2F%2Feduviews%2Ecom%2Fportal%2Fnode%2F74";
        private var youTubeInitResponse:Object;
        private var vr:VideoResolver;
        
        private function initApp():void
        {
            // init the resolver
            vr = new VideoResolver();
            vr.addEventListener(DataEvent.DATA, handleResolveVideoComplete);
            vr.addEventListener(FaultEvent.FAULT, handleResolveVideoFault);
        }               
        
        private function loadBtn_click(evt:MouseEvent):void 
        {
            videoTitle.text = "Loading...";
            statusDisplay.text = "Initializing Player with ["+youTubeVideo+"]";
            vr.resolveVideoURL(youTubeVideo,referralUrl);
        }
        
        private function handleResolveVideoComplete(event:DataEvent):void
        {
            statusDisplay.text += "\n\nURL Complete: " + event.data.toString();
            videoTitle.text = "VIDEO TITLE: " + vr.youTubeInitResponse.title;
            videoDisplay.source = event.data.toString();
            progressBar.label = "connected";
            loadBtn.buttonMode = loadBtn.enabled = false;
            playBtn.buttonMode = playBtn.enabled = true;            
        }
        
        private function handleResolveVideoFault(event:FaultEvent):void
        {
            statusDisplay.text += "\n\nFAULT: " + event.fault.faultString;            
        }        

        private function playBtn_click(evt:MouseEvent):void 
        {
            playBtn.buttonMode = playBtn.enabled = false;
            pauseBtn.buttonMode = pauseBtn.enabled = true;            
            videoDisplay.play();
        }
        
        private function pauseBtn_click(evt:MouseEvent):void 
        {
            playBtn.buttonMode = playBtn.enabled = true;
            pauseBtn.buttonMode = pauseBtn.enabled = false;
            videoDisplay.pause();
        }        

        private function videoDisplay_playheadUpdate(evt:VideoEvent):void 
        {
            var playTime:Number = videoDisplay.playheadTime;
            var totTime:Number = videoDisplay.totalTime;
            progressBar.setProgress(playTime, totTime);
            progressBar.label = StringUtil.substitute("{0} of {1}",
                                                playTime.toFixed(3),
                                                totTime.toFixed(3));
        }
        
            
        ]]>
    </mx:Script>
    
    <mx:ApplicationControlBar dock="true">
        <mx:Button id="loadBtn"
                label="Load"
                buttonMode="true"
                enabled="true"
                click="loadBtn_click(event);" />
        <mx:Button id="playBtn"
                label="Play"
                buttonMode="false"
                enabled="false"
                click="playBtn_click(event);" />
        <mx:Button id="pauseBtn"
                label="Pause"
                buttonMode="false"
                enabled="false"
                click="pauseBtn_click(event);" />                

    </mx:ApplicationControlBar>

    <mx:Label id="videoTitle"
        text="No Video Loaded...Click Load Button"
        width="320"
        height="18"/>
        
    <mx:VideoDisplay id="videoDisplay"
        autoPlay="false"
        width="320"
        height="240" 
        playheadUpdate="videoDisplay_playheadUpdate(event);" />

    <mx:ProgressBar id="progressBar"
        mode="manual"
        label="{videoDisplay.state}"
        labelPlacement="center"
        width="{videoDisplay.width}" />
            
    <mx:TextArea id="statusDisplay" 
        width="320"
        height="80" 
        verticalScrollPolicy="auto"
        editable="false"
        text="Click the Load button to initialize the YouTube protocol"/>    
    
</mx:Application>