<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    backgroundColor="#FFFFFF" 
    backgroundGradientAlphas="1,1" 
    xmlns:services="com.mariosh.services.*"
    creationComplete="init()" xmlns:comps="comps.*" viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import flash.net.navigateToURL;
            import com.mariosh.services.amazons3.S3ProgressEvent;
            private function init():void
            {
                btn.addEventListener(MouseEvent.CLICK, browseClick);
            }
            
            private function browseClick(event:MouseEvent):void
            {
                amazonS3.addEventListener(Event.SELECT, onSelect);
                amazonS3.browseLocalFiles();
            }
            
            private function onSelect(event:Event):void
            {
                fname.text = amazonS3.file.name;
                fsize.text = formatFileSize(amazonS3.file.size);
                
                btn.removeEventListener(MouseEvent.CLICK, browseClick);
                btn.label = "Upload";
                btn.addEventListener(MouseEvent.CLICK, uploadClick);
                
                lnk.visible = false;
            }
            
            private function uploadClick(event:MouseEvent):void
            {
                amazonS3.addEventListener(S3ProgressEvent.PROGRESS, onS3Progress);
                amazonS3.addEventListener(Event.COMPLETE, onS3Complete);
                amazonS3.upload();
            }
            
            private function onS3Progress(event:S3ProgressEvent):void
            {
                progressBox.percentage = event.percentage;
            }
            
            private function onS3Complete(event:Event):void
            {
                btn.removeEventListener(MouseEvent.CLICK, uploadClick);
                btn.label = "Browse";
                btn.addEventListener(MouseEvent.CLICK, browseClick);
                progressBox.reset();
                lnk.visible = true;
            }
            
            private function formatFileSize(numSize:Number):String
            {
                var strReturn:String;
                numSize = Number(numSize / 1000);
                strReturn = String(numSize.toFixed(1) + " KB");
                if (numSize > 1000) {
                    numSize = numSize / 1000;
                    strReturn = String(numSize.toFixed(1) + " MB");
                    if (numSize > 1000) {
                        numSize = numSize / 1000;
                        strReturn = String(numSize.toFixed(1) + " GB");
                    }
                }                
                return strReturn;
            }

        ]]>
    </mx:Script>
    
    <mx:Style source="styles.css"/>
    
    <services:AmazonS3 id="amazonS3"
        accessKey="[YOUR ACCESS KEY]"
        secretKey="[YOUR SECRET KEY]"
        bucket="[BUCKET]"
        useHTTPS="true"/>
    
    <mx:Image 
        x="10" y="10" 
        source="assets/grid.jpg"/>
    <mx:ApplicationControlBar
        x="94" y="65" 
        width="555" height="46" 
        verticalAlign="middle" 
        paddingLeft="10" 
        paddingRight="10" 
        fontSize="11" 
        color="#DADADA" 
        fontWeight="bold">
        <mx:filters>
            <mx:GlowFilter color="0x666666" blurX="8" blurY="8" quality="3"/>
        </mx:filters>
        <mx:Label text="Select File:"/>
        <comps:ProgressBox id="progressBox" 
            cornerRadius="4" borderStyle="solid" 
            borderColor="#2A2B2B" width="100%" height="34" 
            fontWeight="normal" color="#2AA1CD" 
            paddingTop="7" paddingLeft="5" paddingRight="5">
            <mx:Label id="fname" width="205" truncateToFit="true"/>
            <mx:Label id="fsize" width="60"/>
            <mx:VRule strokeWidth="1" strokeColor="#666666" height="18"/>
            <mx:LinkButton id="lnk" 
                label="Link" paddingTop="0" paddingBottom="0"
                click="navigateToURL(new URLRequest('http://mariosh.s3.amazonaws.com/'+amazonS3.file.name))" 
                visible="false" selectionColor="#000000" 
                rollOverColor="#000000" textRollOverColor="#FFFFFF" 
                textSelectedColor="#FFFFFF"/>
        </comps:ProgressBox>
        <mx:Button id="btn" 
            label="Browse" 
            height="34" width="79" 
            cornerRadius="2" borderColor="#1A1A1A" 
            themeColor="#000000" 
            fillAlphas="[0.56, 0.73, 0.56, 0.56]" 
            fillColors="[#80BEF2, #3B98E7]" 
            color="#FFFFFF" fontWeight="bold" 
            textRollOverColor="#000000"/>
    </mx:ApplicationControlBar>
    
</mx:Application>