<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	title="Browse Example"
	backgroundColor="0x222222">
	
	<mx:Script>
		<![CDATA[
			import mx.events.IndexChangedEvent;
			import mx.events.DragEvent;
			import mx.containers.Canvas;
			import mx.effects.effectClasses.BlurInstance;
			import flash.filesystem.*;
			import com.adobe.images.*;

			// File object for saving / loading
			private var file:File;
			
			
			// callback when slider is changed
			public function blurImg():void {
				// get the value from the slider
				var blurVal:Number = slider.value;
				
				// use the slider value to construct a blurring filter
				var filter:BlurFilter = new BlurFilter(blurVal, blurVal, 3);
				
				// apply the blur filter to the image
				img.filters = [filter];
			}
			
			public function saveImg():void {
				// start at desktop directory
				file = File.desktopDirectory; 
				
				// listen for when the user has selected a location to save
				file.addEventListener(Event.SELECT, onFileSave);
				
				// request a download a bogus file, to get the Browse dialog to pop up
				file.download(new URLRequest("http://foo/foo"), "blurred.jpg");
			}
				
			public function onFileSave(e:Event):void {
				
				// cancel the file download, since it was bogus anyway
				// now, 'file' now points at where they want to save
				file.cancel();

				// grab the pixel data from the image
				var bmpData:BitmapData = new BitmapData(img.width, img.height);
				bmpData.draw(img);
				
				// encode the pixel data into a JPEG
				var jpgEncoder:JPGEncoder = new JPGEncoder();
				var jpgBytes:ByteArray = jpgEncoder.encode(bmpData);

				// open a filestream from the file
				var stream:FileStream = new FileStream();
				stream.open(file, FileMode.WRITE);
				
				// write the jpeg data to the file
				stream.writeBytes(jpgBytes, 0, jpgBytes.length);
				
				// close the stream. the file is now saved.
				stream.close();				
				
			}
			
			public function loadImg():void {
				
				// start at the desktop directory
				file = File.desktopDirectory; 
				
				// listen for select event
				file.addEventListener(Event.SELECT, onFileLoad); 
				
				// open browse dialog to pick an image file
				file.browse([new FileFilter("Images", "*.jpg;*.png")]);
			}

			// handler for when user selects a file			
			private function onFileLoad(e:Event):void {
				
				// 'file' now contains a url pointing at the file they selected.
				// in a flash movie, you would not have access to the full path,
				// but in apollo you do.
				trace(file.url);
				
				// load the image that they picked.
				// In Apollo, you can load from file://... URLs
				img.load(file.url);
			}
			

		]]>
	</mx:Script>
	<mx:VBox left="5" top="5" bottom="5" right="5" horizontalAlign="center">
		<mx:HBox width="100%"  horizontalAlign="center">
			<mx:Button label="Load..." click="loadImg()"  />
			<mx:Button label="Save as JPG..." click="saveImg()"  />			
		</mx:HBox>
		<mx:HSlider id="slider" change="blurImg()"  minimum="1" maximum="25" snapInterval="0.2" width="90%"/>
		<mx:Image id="img" />
	</mx:VBox>
	

</mx:ApolloApplication>
