<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
                layout="absolute"
                backgroundColor="0x888888"
                creationComplete="onCreationComplete()" 
                viewSourceURL="srcview/index.html"
                mouseDown="onMouseDown()"
                mouseMove="onMouseMove()"
                mouseUp="onMouseUp()">
    
    <mx:Script>
        <![CDATA[
            [Bindable][Embed(source="assets/gene_Marc.jpg")]public var photoClass1:Class;
            [Bindable][Embed(source="assets/bill.jpg")]public var photoClass2:Class;
            [Bindable][Embed(source="assets/rogueGray.png")]public var photoClass3:Class;
            [Bindable]protected var list:Array;
            private var bmd:BitmapData;

            [Embed(source="assets/AdjustCIELab.pbj", mimeType="application/octet-stream")]
            protected var PBClass:Class;
            private var shader:Shader;
            private var filter:ShaderFilter;
            protected var sound:Sound;
            protected var audio:SoundChannel;
            protected var bDown:Boolean;
            protected var isPlayin:Boolean;
            
            protected function onCreationComplete():void {
                init();
            }
            
            protected function empty():void {
                if(uicImage.numChildren) {
                    var bmp:Bitmap = uicImage.getChildAt(0) as Bitmap;
                    uicImage.removeChildAt(0);
                    bmp.bitmapData.dispose();
                    bmp = null;
                }
            }
            
            protected function init():void {
                list = ["Gene and Marc","Bill","Rogue"];
                onChangeCombo();
                shader=new Shader()
                shader.byteCode=new PBClass();
                sound = new Sound(new URLRequest("http://www.ctyeung.com/flex/AdjustCIELab/assets/weewoo.mp3"));
            }
            
            protected function onChangeCombo():void {
                if(uicImage.numChildren)
                    uicImage.removeChildAt(0);
                switch(cmb.selectedIndex) {
                    case 0:
                        bmd = new photoClass1().bitmapData;
                        break;
                        
                    case 1:
                        bmd = new photoClass2().bitmapData;
                        break;
                        
                    case 2:
                        bmd = new photoClass3().bitmapData;
                        break;
                }
                uicImage.addChild(new Bitmap(bmd));
                uicImage.width =bmd.width;
                uicImage.height = bmd.height;
            }
            
            protected function onSlideL():void {
                shader.data.L.value = [slideL.value];
                filter = new ShaderFilter(shader);
                uicImage.filters = [filter];
            }
            
            protected function onSlidea():void {
                shader.data.a.value = [slidea.value];
                filter = new ShaderFilter(shader);
                uicImage.filters = [filter];
            }
            
            protected function onSlideb():void {
                shader.data.b.value = [slideb.value];
                filter = new ShaderFilter(shader);
                uicImage.filters = [filter];
            }
            
            protected function playSound():void {
                if(!isPlayin) {
                    audio = sound.play();
                    isPlayin=true;
                }
            }
            
            protected function onMouseDown():void {
                bDown = true;    
            }
            
            protected function onMouseMove():void {
                if(bDown)
                    playSound();
            }
            
            protected function onMouseUp():void {
                if(audio) 
                    audio.stop();
                    
                this.isPlayin = false;
                bDown = false;
            }
        ]]>
    </mx:Script>
    
    <mx:VBox horizontalAlign="center" width="100%" height="100%">
        <mx:ComboBox id="cmb" change="onChangeCombo()" dataProvider="{list}"/>
        <mx:HBox>
            <mx:UIComponent id="uicImage"/>
            <mx:VSlider id="slideL" labels="L*" change="onSlideL()" minimum="-100" maximum="100" value="0"/>
            <mx:VSlider id="slidea" labels="a*" change="onSlidea()" minimum="-127" maximum="127" value="0"/>
            <mx:VSlider id="slideb" labels="b*" change="onSlideb()" minimum="-127" maximum="127" value="0"/>        
        </mx:HBox>
    </mx:VBox>
</mx:Application>