<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preinitialize="preInit()"   initialize="init()" creationComplete="created()"  applicationComplete="appComplete()"  xmlns:ns3="com.blitzagency.xray.logger.*" horizontalScrollPolicy="off" verticalScrollPolicy="off" backgroundGradientAlphas="[1.0, 1.0]"  backgroundGradientColors="[#BEBEBE, #595959]" xmlns:ns4="com.rictus.reflector.*" xmlns:degrafa="http://www.degrafa.com/2007" xmlns:ns1="com.*" xmlns:ns2="com.brightPoint.controls.*"  viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import flash.net.navigateToURL;
            import Mapping.MapType;
            import mx.effects.Glow;
            import mx.events.EffectEvent;
            import mx.effects.Parallel;
            import mx.effects.AnimateProperty;
            //import CustomYahooMap.MapType;
            import mx.core.UIComponent;
            import CustomPV3D.Camera.CameraTilt;
            import CustomPV3D.Camera.FauxCameraRotate;
            import CustomPV3D.Main;
            import com.yahoo.maps.api.core.location.LatLon;
            
            import com.yahoo.maps.api.YahooMapEvent;
            import com.yahoo.maps.api.YahooMap;
//-------------------------------------------------------------------------------------------------------------------------            
            public var threeD:Main;
            public var yahooMap:YahooMap;
            public var zoomRate:Number=15;
            public var cameraRotation:FauxCameraRotate;
            public var cameraTilting:CameraTilt;
            public var stopMovingMap:Boolean=false;
            [Bindable] public var clickedLat:Number=0;
            [Bindable] public var clickedLong:Number=0;
            [Bindable] public var clickedY:Number=0;
            [Bindable] public var clickedX:Number=0;
            public var isAnythingBeingDragged:Boolean=false; 
            
              
//-------------------------------------------------------------------------------------------------------------------------                    
            private function preInit():void
            {
                this.width = Capabilities.screenResolutionX;
                trace("this.width "+this.width )
                this.height = Capabilities.screenResolutionY
                trace("this.height "+this.height )
            
            }            
//-------------------------------------------------------------------------------------------------------------------------            
            private function created():void
            {
                yahooMap=new YahooMap();
                yahooMap.addEventListener(YahooMapEvent.MAP_INITIALIZE,setInitialMapPosition)
                //Replace the YahooMap Key with your own which you can get free at http://developer.yahoo.com/maps/flash/asGettingStarted.html -> get an application ID here
                //chances are I will delete this one soon
                yahooMap.init("ypqDucbV34EgiV2gh2VcafVoqAvecyIHrbPe9VgatmvZVZyDBK7kJRSjTQxra5JZKUNuFUIH_CSyG7yR", 800, 600);    

                //yahooMap.addScaleBar()
                yahooMap.addPanControl()
                yahooMap.addZoomWidget()
                //yahooMap.addCrosshair()
                //yahooMap.addTypeWidget()
                
            }
//-------------------------------------------------------------------------------------------------------------------------            
            private function appComplete():void
            {
                //mouse scrolling zoom 
                stage.addEventListener(MouseEvent.MOUSE_WHEEL,scrollZoomMap);
                //hook for key+mouse interaciton
                stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeys)
                stage.addEventListener(KeyboardEvent.KEY_UP,checkKeys)
            }
//-------------------------------------------------------------------------------------------------------------------------            
            private function setInitialMapPosition(e:YahooMapEvent):void
            {
                
                yahooMap.removeEventListener(YahooMapEvent.MAP_INITIALIZE,setInitialMapPosition)
                //set LatLon for initial map position (eye-ball)
                var p:LatLon=new LatLon(40,-95)
                //set map position
                yahooMap.centerLatLon=p;
                yahooMap.zoomLevel=14;
                yahooMap.centerOnDoubleClick=false;
                //adding all kinds of events related to map
                yahooMap.addEventListener(YahooMapEvent.MAP_MOUSE_DOWN,mapIsDown);
                yahooMap.addEventListener(YahooMapEvent.MAP_MOUSE_UP,mapIsUp);
                yahooMap.addEventListener(YahooMapEvent.MAP_MOUSE_CLICK, handleMapClick);
                yahooMap.addEventListener(YahooMapEvent.MAP_MOUSE_DOUBLE_CLICK, handleMapDoubleClick);
                yahooMap.addEventListener(YahooMapEvent.MAP_ZOOM, handleMapZoom);
                
                yahooMap.addEventListener(YahooMapEvent.MAP_DRAG_START, startDragingMap);
                yahooMap.addEventListener(YahooMapEvent.MAP_DRAG_STOP, stopDragingMap);
                yahooMap.addEventListener(YahooMapEvent.MAP_DRAG, dragingMap);
                
                //initiate PV3D
                start3d();
                //set initial camera position
                setupCamera();
            }
//-------------------------------------------------------------------------------------------------------------------------    
            private function checkKeys(k:KeyboardEvent):void
            {
                //hook for key+mouse interaction
            }
//---------------------------------------------------------------------------------------------------------------------------------            
            private function scrollZoomMap(e:MouseEvent):void 
            {
                var n:Number=(e.delta/3)*zoomRate;
                if(e.delta>0 && threeD.default_camera.distanceTo(threeD.sceneManipulationContainer)>50)
                {
                    threeD.default_camera.moveForward(n);
                }
                else if(e.delta<0 && threeD.default_camera.distanceTo(threeD.sceneManipulationContainer)<1500)
                {
                    threeD.default_camera.moveBackward(-n);
                }
            }
//---------------------------------------------------------------------------------------------------------------------------------            
            private function start3d():void
            {
                trace("start3d")
                threeD=new Main;
                var threeDcontainer:UIComponent=new UIComponent;
                threeDcontainer.width=800;
                threeDcontainer.height=600;
                threeDcontainer.setStyle("top",0);
                threeDcontainer.setStyle("left",0);
                threeDcontainer.setStyle("right",0);
                threeDcontainer.setStyle("bottom",0);
                threeDcontainer.addChild(threeD);
                
                pv3dContainer.addChild(threeDcontainer);
            }
//---------------------------------------------------------------------------------------------------------------------------------            
            private function setupCamera():void
            {
                cameraRotation=new FauxCameraRotate(threeD.default_camera,threeD.cube);
                //so why threeD.sceneManipulationContainer?
                //because that container is not really moving anywhere so we can count on it for distance to camera
                cameraTilting=new CameraTilt(threeD.default_camera,threeD.sceneManipulationContainer);
            }                             
//---------------------------------------------------------------------------------------------------------------------------------            
            private function rotateCamera(a:Number):void
            {
                cameraRotation.rotateCamera(a);
            }   
//---------------------------------------------------------------------------------------------------------------------------------            
            private function tiltCamera(angle:Number):void
            {
                cameraTilting.tiltCamera(angle);
            }
//---------------------------------------------------------------------------------------------------------------------------------                            
            public function stopFlickering(e:MouseEvent):void
            {
                
                if(e.type==MouseEvent.MOUSE_DOWN)
                {
                    stopMovingMap=true
                }
                else if(e.type==MouseEvent.MOUSE_UP)
                {
                    stopMovingMap=false
                }    
            }
//---------------------------------------------------------------------------------------------------------------------------------            
            //this is where you can map GPS to local space and play with 3D
            private function getAllCoordinates(e:YahooMapEvent):void 
            {
                clickedLong=Number(e.data.latlon.lon);
                clickedLat=Number(e.data.latlon.lat);
                
                clickedX=Number(e.data.point.x);
                clickedY=Number(e.data.point.y);
            } 
//---------------------------------------------------------------------------------------------------------------------------------                
            private function moveMe(e:MouseEvent):void 
            {
                var nll:LatLon;
                var yTo:Number=0
                var xTo:Number=0
                
                var moveBy:Number=100;
                switch(e.currentTarget.name)
                {
                    case "goUp":
                        nll=yahooMap.getXYToLatLon(new Point(0,moveBy));
                        xTo=0;
                        yTo= moveBy;
                    break;
                    case "goDown":
                        nll=yahooMap.getXYToLatLon(new Point(0,-moveBy));
                        xTo=0;
                        yTo= -moveBy;
                    break;
                    case "goLeft":
                        nll=yahooMap.getXYToLatLon(new Point(moveBy,0));
                        xTo= moveBy;
                        yTo=0;
                    break;
                    case "goRight":
                        nll=yahooMap.getXYToLatLon(new Point(-moveBy,0));
                        xTo= -moveBy;
                        yTo=0;
                    break;
                }
                yahooMap.panToLatLon(nll);
            }
//---------------------------------------------------------------------------------------------------------------------------------                            
//---------------------------------------------------------------------------------------------------------------------------------    
            public function mapIsDown(e:YahooMapEvent):void
            {
                getAllCoordinates(e)        
            }
//---------------------------------------------------------------------------------------------------------------------------------                            
            
            public function mapIsUp(e:YahooMapEvent):void
            {
                getAllCoordinates(e)
            }                    
//---------------------------------------------------------------------------------------------------------------------------------                            
            
            public function handleMapClick(e:YahooMapEvent):void
            {
                getAllCoordinates(e)
            }
//---------------------------------------------------------------------------------------------------------------------------------        
            public function startDragingMap(e:YahooMapEvent):void
            {
                
            }    
//---------------------------------------------------------------------------------------------------------------------------------        
            private function stopDragingMap(e:YahooMapEvent):void
            {

            }    
//---------------------------------------------------------------------------------------------------------------------------------        
            private function dragingMap(e:YahooMapEvent):void
            {    
                getAllCoordinates(e)        
            }
//---------------------------------------------------------------------------------------------------------------------------------                
            private function handleMapDoubleClick(e:YahooMapEvent):void 
            {
                getAllCoordinates(e)
            }
//---------------------------------------------------------------------------------------------------------------------------------                        
            private function handleMapZoom(event:YahooMapEvent):void 
            {

            }
//---------------------------------------------------------------------------------------------------------------------------------                        
            private function getPathf():void 
            {
                navigateToURL(new URLRequest("http://www.pathf.com"),"_blank");
            }            
//---------------------------------------------------------------------------------------------------------------------------------                            
//---------------------------------------------------------------------------------------------------------------------------------    
            //effect for mouseOver for map type buttons that didn't work out
            private function highlightMapType(e:MouseEvent):void
            {
                /* trace(e.type)
                var pathfBlue:Glow=new Glow;
                pathfBlue.color=0x009dff;
                pathfBlue.alphaFrom=0
                pathfBlue.alphaTo=1
                pathfBlue.blurXFrom=0
                pathfBlue.blurYFrom=0
                pathfBlue.blurXTo=6
                pathfBlue.blurYTo=6

                if(e.type=="rollOver")
                {
                    pathfBlue.play([e.target])
                }
                else if(e.type=="rollOut")
                {
                    pathfBlue.play([e.target],true)
                }
                else if(e.type=="click")
                {
                    pathfBlue.play([e.target])
                } */
            }
                                           
        ]]>
    </mx:Script>
        <mx:Style>
        @font-face {
              src:url("Assets/MyriadPro-Bold.otf");
              fontFamily: "MyriadPro";
              fontWeight:bold;
           }
           
           @font-face {
              src:url("Assets/MyriadPro-Regular.otf");
              fontFamily: "MyriadPro";
              fontWeight:regular;
           }

       Application
       {
          /* font-size: 24px; */
          font-family: "MyriadPro";
       }
       
       .noPadding
       {
          paddingLeft: 0;
          paddingRight: 0;
          paddingTop: 0;
          paddingBottom: 0;
       }
       
       .mainLinks
       {
          fontSize:18;
       }
    </mx:Style>
    <mx:Canvas id="pv3dContainer" clipContent="true" left="0" right="0" top="0" bottom="0" horizontalScrollPolicy="off" verticalScrollPolicy="off">
        
    </mx:Canvas>
    <mx:Canvas id="mapNavigationPanel0" horizontalScrollPolicy="off" verticalScrollPolicy="off" backgroundColor="#1C1C1C" backgroundAlpha="0.7" cornerRadius="6" borderStyle="solid" top="10" left="10" right="111" height="36">
        <mx:filters>
            <mx:DropShadowFilter alpha="0.5" blurX="6" blurY="4" angle="90" distance="1" quality="2" />
        </mx:filters>
        <mx:Label y="9" text="Longitude:" paddingLeft="0" paddingRight="0" right="334.95" fontSize="12" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="9" text="Latitude:" paddingLeft="0" paddingRight="0" right="213.25" fontSize="12" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="8" text="{clickedLong}" width="70" paddingLeft="0" paddingRight="0" right="267.09998" fontSize="12" fontWeight="bold" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="8" text="{clickedLat}" width="70" paddingLeft="0" paddingRight="0" right="145.34998" fontSize="12" fontWeight="bold" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="9" text="X:" paddingLeft="0" paddingRight="0" right="128.5" fontSize="12" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="9" text="Y:" paddingLeft="0" paddingRight="0" right="57.849976" fontSize="12" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="8" text="{clickedX}" width="50" paddingLeft="0" paddingRight="0" right="73.75" fontSize="12" fontWeight="bold" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label y="8" text="{clickedY}" width="50" paddingLeft="0" paddingRight="0" right="10" fontSize="12" fontWeight="bold" styleName="noPadding" color="#FFFFFF"/>
        <mx:Label text="Interactive YahooMap in PV3D" fontSize="20" fontWeight="normal" id="demoTitle" top="3" left="53" height="26" color="#FFFFFF">
        </mx:Label>
        <mx:Image x="4" y="3" source="Assets/pathfLogo.png" toolTip="pathf.com" click="getPathf()" useHandCursor="true" buttonMode="true"/>
    </mx:Canvas>
    <mx:Canvas width="93" id="mapNavigationPanel" horizontalScrollPolicy="off" verticalScrollPolicy="off" right="10" top="10" backgroundColor="#1C1C1C" bottom="10" backgroundAlpha="0.7" cornerRadius="6" borderStyle="solid">
        <mx:filters>
            <mx:DropShadowFilter alpha="0.5" blurX="6" blurY="4" angle="90" distance="1" quality="2" />
        </mx:filters>
        <mx:Label x="3" y="10" text="Pan Map:" fontSize="12" color="#FFFFFF" textAlign="center"/>
        <mx:Label x="8" y="132" text="Map Type:" fontSize="12" color="#FFFFFF"/>
        <ns1:ArrowButton x="8" y="60" arrowPointsAngle="180" name="goLeft" click="moveMe(event)"/>
        <ns1:ArrowButton x="56" y="60" arrowPointsAngle="0" name="goRight" click="moveMe(event)"/>
        <ns1:ArrowButton x="32" y="36" arrowPointsAngle="270" name="goUp" click="moveMe(event)"/>
        <ns1:ArrowButton x="32" y="84" arrowPointsAngle="90" name="goDown" click="moveMe(event)"/>
        <ns1:RoundedImage width="70" height="70" y="158" click="{new MapType(yahooMap,'normal')}" x="10" source="Assets/NormalMap.png" id="normalMap" cornerRadius="6" useHandCursor="true" buttonMode="true"/>
        <ns1:RoundedImage width="70" height="70" source="Assets/SatelliteMap.png" y="238" click="{new MapType(yahooMap,'satellite')}" x="10" id="satelliteMap" cornerRadius="6" useHandCursor="true" buttonMode="true"/>
        <ns1:RoundedImage width="70" height="70" source="Assets/HybridMap.png" y="318" click="{new MapType(yahooMap,'hybrid')}" x="10" id="hybridMap" cornerRadius="6" useHandCursor="true" buttonMode="true"/>
        <ns1:SimpleDial x="10" y="408" width="70" height="70" angleChanged="{rotateCamera(angleDialer.currentAngle)}" id="angleDialer">
        </ns1:SimpleDial>
        <ns1:SimpleTilter x="10" y="496" angleChanged="tiltCamera(tilter.currentAngle)" id="tilter">
        </ns1:SimpleTilter>
    
    </mx:Canvas>
</mx:WindowedApplication>