/*
PureMVC AS3 MultiCore Pipes Demo – Loadup for Flash Demo
Copyright (c) 2009 Frederic Saunier <frederic.saunier@puremvc.org>

Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package
{    
    import flash.display.InteractiveObject;
    import flash.display.MovieClip;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.ContextMenuEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    
    import org.puremvc.as3.demos.flash.loadupforflash.view.components.MainComponent;

    /**
     * Document class for this Application.
     */
    public final class PureMVC_LoadupForFlashDemo
        extends MovieClip
    {
        /**
         * Constructor.
         */
        public function PureMVC_LoadupForFlashDemo()
        {
            //Add support for view-source here
            addMenuItem(this, "./srcview/index.html");
            
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            var mainComponent:MainComponent = new MainComponent();
            addChild( mainComponent );
        }

        /**
         * Add "View Source" right-click menu support when compiled with Flex
         * Builder.
         * 
         * @param obj
         *         Target on which the ContextMenuEvent must be listened.
         * 
         * @param url
         *         URL of the view source folder (relative to the web page that
         *         embeds the animation).
         * 
         * @param hideBuiltIns
         *         Hides all built-in menu items (except Settings) in the
         *         ContextMenu that appears on right-click.
         */
        private static function addMenuItem( target:InteractiveObject, url:String, hideBuiltIns:Boolean=true ):void
        {
            if (target.contextMenu == null)
            {
                target.contextMenu = new ContextMenu();
                if (hideBuiltIns)
                    target.contextMenu.hideBuiltInItems();
            }
            
            var item:ContextMenuItem = new ContextMenuItem("View Source");
            
            item.addEventListener
            (
                ContextMenuEvent.MENU_ITEM_SELECT, 
                function(event:ContextMenuEvent):void
                {
                    if (event.target == item)
                        navigateToURL(new URLRequest(url), "_blank");
                }
            );
            
            target.contextMenu.customItems.push(item);
        }
    }
}