<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright 2010 Chet Haase
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    width="500" height="298"
    viewSourceURL="srcview/index.html"
    creationComplete="creationComplete()">
    
    <fx:Script>
        [Embed("images/GoldenGate.jpg")]
        [Bindable]private var GoldenGate:Class;
        private var goldenGateBD:BitmapData;
        
        [Embed("images/Harbor.jpg")]
        [Bindable]private var Harbor:Class;
        private var harborBD:BitmapData;
        
        private function creationComplete():void
        {
            goldenGateBD = (new GoldenGate()).bitmapData;
            harborBD = (new Harbor()).bitmapData;
        }
        
        private var newImageSource:Class;
        
        private function clickHandler():void
        {
            var bd0:BitmapData;
            var bd1:BitmapData;
            
            if (img.source == GoldenGate)
            {
                newImageSource = Harbor;
                bd0 = goldenGateBD;
                bd1 = harborBD;
            }    
            else
            {
                newImageSource = GoldenGate;
                bd0 = harborBD;
                bd1 = goldenGateBD;
            }
            crossfadeFilter.shader.data.endImage.input = bd1;
            crossfader.play();  
        }
        
        private function effectEndHandler():void
        {
            img.source = newImageSource;
        }
    </fx:Script>
    <fx:Declarations>
        <s:ShaderFilter id="crossfadeFilter" shader="@Embed(source='shaders/crossfade.pbj')"/>
        <s:AnimateFilter id="crossfader" target="{img}" 
            bitmapFilter="{crossfadeFilter}" effectEnd="effectEndHandler()">
            <s:SimpleMotionPath property="progress" valueFrom="0" valueTo="1"/>
        </s:AnimateFilter>
    </fx:Declarations>
    <mx:Image id="img" source="{GoldenGate}" click="clickHandler()"/>
</s:Application>