<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"  
    paddingBottom="0"
    paddingLeft="0"
    paddingRight="0"
    paddingTop="0" creationComplete="init()" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import configure;
            import progress;
            
            public var so:SharedObject = SharedObject.getLocal("user_options");
            
            import com.adobe.air.notification.*;
            
            private var purr:Purr = new Purr(1);
            
            static private var filesUploading:Number = 0;
            
            public namespace atom = "http://www.w3.org/2005/Atom";
            use namespace atom;
                        
            public function init():void {
                // Force the extensions to be registered to this application
                NativeApplication.nativeApplication.setAsDefaultApplication('doc');
                NativeApplication.nativeApplication.setAsDefaultApplication('xls');
                NativeApplication.nativeApplication.setAsDefaultApplication('ppt');
                
                // Invoke is called when you double click on a Airdoc file
                NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, invoked);
                if(!so.data.auth){
                    configure.open();
                }
            }
            
            public function invoked(e:InvokeEvent):void {
                if(e.arguments && so.data.auth){
                    for(var i:int=0; i < e.arguments.length; i++){
                        uploadFile(e.arguments[i]);
                    }
                }
            }
            
            public function exitUnlessFilesUploading():void {
                if(filesUploading == 0) {
                    // We wait a sec - because sometimes it takes a while for the browse to open
                    setInterval(NativeApplication.nativeApplication.exit, 5)
                }
            }
            
            public function uploadFile(path:String):void {
                
                filesUploading += 1;
                                
                var file:File = new File();
                file.nativePath = path;
                
                // The Growl like notification
                this.purr.addTextNotificationByParams('Airdoc', 'Uploading ' + truncate(file.name, 25));
                this.purr.alert(NotificationType.INFORMATIONAL, null);
                
                file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, function(e:DataEvent):void{
                    // Google docs returns an Atom feed which we have to parse to find the doc's url
                    var data:XML = new XML(e.data);
                    navigateToURL(new URLRequest(data.atom::link.(@rel=='alternate').@href));
                    filesUploading -= 1;

                    exitUnlessFilesUploading();
                });
                
                // For some reason file.type doesn't always return a value - so we need to hard code them
                var ct:String = file.type;
                switch(file.extension){
                    case 'doc':
                        ct = 'application/msword';
                        break;
                    case 'xls':
                        ct = 'application/vnd.ms-excel';
                        break;
                    case 'ppt':
                        ct = 'application/vnd.ms-powerpoint';
                        break;
                }
                
                var upR:URLRequest = new URLRequest('http://docs.google.com/feeds/documents/private/full')
                
                var header1:URLRequestHeader = new URLRequestHeader('Authorization', 'GoogleLogin auth=' + this.so.data.auth);
                var header2:URLRequestHeader = new URLRequestHeader('Slug', file.name);
                var header3:URLRequestHeader = new URLRequestHeader('Content-Type', ct);
                upR.requestHeaders = [header1, header2, header3];
                upR.followRedirects = true;
                
                // We need to set this, otherwise uploadUnencoded throws a whoopsy!
                upR.method = URLRequestMethod.POST;
                
                file.uploadUnencoded(upR);
            }
            
            // The file names get too long for Growl notification
            private function truncate(txt:String, len:Number):String {
                if(txt.length >  len){
                    return txt.substr(0, len - 3) + '...'
                } else {
                    return txt
                }
            }
            
        ]]>
    </mx:Script>
</mx:Application>