<?xml version="1.0" encoding="utf-8"?>

<!--

Copyright (c) 2008 Mrinal Wadhwa (http://www.mrinalwadhwa.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns="com.abdulqabiz.utils.*"
    layout="absolute"
    backgroundColor="#222222"
    backgroundGradientColors="[#222222,#222222]"
    creationComplete="onCreationComplete()" 
    viewSourceURL="srcview/index.html">


    <mx:Script>
        <![CDATA[
            import flash.net.navigateToURL;
            import mx.controls.Alert;
            import flash.external.ExternalInterface;
            
            
            /* called on creation complete of application */
            private function onCreationComplete():void{
                if(ExternalInterface.available) { // external interface is supported by the browser
                    
                    // register callback to recieve test results
                    ExternalInterface.addCallback("testForGears",testForGears); 
                    
                    // request a test for gears from the wrapper
                    ExternalInterface.call("isGearsInstalled");
                }else{
                    Alert.show("This example needs a browser that supports ExternalInterface");
                }
                
            }
            
            
            /*  called by wrapper to communicate gears test results */
            private function testForGears(passed:Boolean):void{
                if(passed){ // gears is installed
                    currentState = 'gearsPresent';
                }else{ //gears is not installed
                    currentState = 'gearsNotPresent';
                }
            }
            
            
            private function createShortcut():void{
                    
                    /* ask the wrapper to create a shortcut */
                    ExternalInterface.call ("createShortcut");
                    
                    /*I had to call this because FireFox 3 for some reason messes 
                    up the swf file's rendering after showing the gears conformation 
                    popup*/
                    invalidateDisplayList();
            }
            
            /* redirects to gears install page, comes back after instalation */
            private function redirectToInstallPage():void{
                navigateToURL(new URLRequest("http://gears.google.com/?action=install" + 
                        "&message=Install Gears to checkout Mrinal's Flex Desktop Shortcut example" + 
                        "&return=http://experiments.mrinalwadhwa.com/Gears/FlexDesktopShortcut/FlexGears.html"),"_self");
            }
            
            /* link to source */
            private function viewSource():void{
                navigateToURL(new URLRequest("srcview/index.html"));
            }
            
            /* link to blog post */
            private function moreInfo():void{
                navigateToURL(new URLRequest("http://weblog.mrinalwadhwa.com/2008/06/27/flex-and-gears/"),"_self");
            }            
        ]]>    
    </mx:Script>



    <!-- This JavaScript gets embedded in the HTML container 
         
         DO NOT FORGET to get gear_init.js from 
         http://code.google.com/apis/gears/tools.html#gears_init
         
         and include it into your projects index.template.html file 
         as follows
         
         <script type="text/javascript"  src="gears_init.js"></script>
    -->
    <JavaScript>
        <![CDATA[
        
            // tells the swf if gears is installed
            function isGearsInstalled() {
              if (window.google && google.gears) { //gears is installed
                    thisMovie("FlexGears").testForGears(true);    
                    return;
              }
              //gears is not installed
              thisMovie("FlexGears").testForGears(false);    
            }

            
            function thisMovie(movieName) {
                 if (navigator.appName.indexOf("Microsoft") != -1) {
                     return window[movieName];
                 } else {
                     return document[movieName];
                 }
             }
        
        
            // use gears to create a desktop shortcut
            function createShortcut() {
                 
                 var desktop = google.gears.factory.create("beta.desktop");
                 var description = "This shortcut launches Flex + Gears desktop shortcut example";
            
                 var icons = {
                    "16x16":    "assets/icon16.png",
                    "32x32":    "assets/icon32.png",
                    "48x48":    "assets/icon48.png",
                    "128x128":  "assets/icon128.png"
                 };
            
                 desktop.createShortcut("Flex & Gears desktop shortcut example",  // name
                                        "FlexGears.html",  // url
                                        icons,  // icons (must specify at least one)
                                        description);  // description (optional)
            }
        ]]>
    </JavaScript>

    <mx:Style source="styles.css"/>

    <mx:states>
        <mx:State name="gearsPresent"/>
        <mx:State name="gearsNotPresent">
            <mx:RemoveChild target="{createShortcutButton}"/>
            <mx:AddChild position="lastChild">
                <mx:Button label="Install Gears" styleName="install" 
                    horizontalCenter="0" top="150" click="redirectToInstallPage()"/>
            </mx:AddChild>
            <mx:SetProperty target="{message}" name="text" 
                value="This sample needs Google Gears to run."/>
        </mx:State>
    </mx:states>
    
    <mx:Button label="Create Desktop Shortcut" 
        horizontalCenter="0" top="150"
        click="createShortcut()" 
        id="createShortcutButton"
        styleName="shortcut"/>
    
    <mx:Label id="message"
        text="Google Gears is installed !"  
        horizontalCenter="0" top="100"/>
    <mx:LinkButton label="View Source" horizontalCenter="0" top="350" click="viewSource()"/>
    <mx:LinkButton label="More Info" horizontalCenter="0" top="380" click="moreInfo()"/>

</mx:Application>