In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: FlickrDanboLoader [Myriad]

/**
 * Copyright xiaoming ( http://wonderfl.net/user/xiaoming )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/eiAr
 */

// forked from ProjectNya's FlickrDanboLoader [Myriad]
////////////////////////////////////////////////////////////////////////////////
// FlickrDanboLoader [Myriad]
//
// [AS3.0] FlickrMultiLoaderクラスだ! (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1033
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.AntiAliasType;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    [SWF(backgroundColor="#333333", width="465", height="465", frameRate="30")]

    public class Main extends Sprite {
        private var fontloader:FontLoader;
        //private static var basePath:String = "";
        private static var basePath:String = "http://www.project-nya.jp/images/flash/";
        private static var fontPath:String = "fonts/MyriadProSemibold.swf";
        private static var className:String = "MyriadProSemibold";
        //
        private var provider:DataProvider;
        private var loader:FlickrDanboLoader;
        private var photos:Array;
        private var holder:Sprite;
        private var panel:Sprite;
        private var fullscreenBtn:Btn;
        private var normalBtn:Btn;
        private var tags:TextField;
        private static var fontType:String = "Myriad Pro Semibold";
        private var searchBtn:Btn;

        public function Main() {
            Wonderfl.capture_delay(10);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            init();
        }

        private function init():void {
            graphics.beginFill(0x333333);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            fontloader = new FontLoader();
            fontloader.addEventListener(FontLoader.COMPLETE, loaded, false, 0, true);
            fontloader.load(basePath + fontPath, className);
        }
        private function loaded(evt:Event):void {
            fontloader.removeEventListener(FontLoader.COMPLETE, loaded);
            setup();
        }
        private function setup():void {
            panel = new Sprite();
            addChild(panel);
            panel.x = 232;
            panel.y = 40;
            fullscreenBtn = new Btn();
            panel.addChild(fullscreenBtn);
            fullscreenBtn.x = -110;
            fullscreenBtn.init({label: "full screen", type: 2, width: 100});
            fullscreenBtn.addEventListener(MouseEvent.CLICK, fullscreen, false, 0, true);
            normalBtn = new Btn();
            panel.addChild(normalBtn);
            normalBtn.x = -110;
            normalBtn.init({label: "normal", type: 2, width: 100});
            normalBtn.addEventListener(MouseEvent.CLICK, normal, false, 0, true);
            normalBtn.visible = false;
            var base:Shape = new Shape();
            panel.addChild(base);
            base.graphics.beginFill(0x000000);
            base.graphics.drawRoundRect(-40, -10, 140, 20, 10);
            base.graphics.endFill();
            tags = new TextField();
            panel.addChild(tags);
            tags.x = -30;
            tags.y = -8;
            tags.width = 120;
            tags.height = 19;
            tags.type = TextFieldType.DYNAMIC;
            tags.selectable = false;
            tags.embedFonts = true;
            tags.antiAliasType = AntiAliasType.ADVANCED;
            var tf:TextFormat = new TextFormat();
            tf.font = fontType;
            tf.size = 14;
            tf.align = TextFormatAlign.LEFT;
            tags.defaultTextFormat = tf;
            tags.textColor = 0xCCCCCC;
            tags.text = "danbo";
            searchBtn = new Btn();
            panel.addChild(searchBtn);
            searchBtn.x = 130;
            searchBtn.init({label: "search", type: 2});
            searchBtn.addEventListener(MouseEvent.CLICK, search, false, 0, true);
            provider = new DataProvider();
            provider.addEventListener(DataProvider.COMPLETE, complete, false, 0, true);
            loader = new FlickrDanboLoader();
            addChild(loader);
            loader.size(75, 6, 35, 16, 240, 500);
            var colors:Array = [0xCC0000, 0xFF9900, 0xFFCC00, 0x669900, 0x3399CC, 0x9966CC];
            loader.color = colors;
            loader.addEventListener(FlickrDanboLoader.ALL_COMPLETE, allComplete, false, 0, true);
            loader.addEventListener(FlickrDanboLoader.ALL_FADE_OUT, allFadeOut, false, 0, true);
            loader.addEventListener(FlickrDanboLoader.UPDATE, update, false, 0, true);
            loader.addEventListener(FlickrDanboLoader.FULL_SCREEN, screenMode, false, 0, true);
            loader.addEventListener(FlickrDanboLoader.NORMAL, screenMode, false, 0, true);
        }
        private function search(evt:MouseEvent):void {
            searchBtn.clicked = true;
            loader.reset();
            provider.search(tags.text, 200);
        }
        private function complete(evt:Event):void {
            var data:Object = evt.target.data;
            loader.dataProvider(data.s, data.m, data.p, data.i);
            loader.start();
        }
        private function allComplete(evt:Event):void {
            trace("allComplete");
        }
        private function allFadeOut(evt:Event):void {
            searchBtn.clicked = false;
        }
        private function update(evt:Event = null):void {
            panel.x = stage.stageWidth*0.5;
        }
        private function screenMode(evt:Event = null):void {
            switch (evt.type) {
                case FlickrDanboLoader.FULL_SCREEN :
                    fullscreenBtn.visible = false;
                    normalBtn.visible = true;
                    break;
                case FlickrDanboLoader.NORMAL :
                    fullscreenBtn.visible = true;
                    normalBtn.visible = false;
                    break;
            }
        }
        private function fullscreen(evt:MouseEvent):void {
            loader.screenMode(true);
        }
        private function normal(evt:MouseEvent):void {
            loader.screenMode(false);
        }

    }

}


//////////////////////////////////////////////////
// FlickrDanboLoaderクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageDisplayState;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.filters.BlurFilter;

class FlickrDanboLoader extends Sprite {
    private var bWidth:uint;
    private var cols:uint;
    private var xPos:uint;
    private var distance:uint;
    private var pWidth:uint;
    private var xOffset:uint;
    private var mw:uint;
    private var pw:uint;
    private var photos:Array;
    private var orders:Array;
    private var mlist:Array;
    private var plist:Array;
    private var infos:Array;
    private var photo:Sprite;
    private var container:Sprite;
    private var base:Shape;
    private var loader:ProgressLoader;
    private var holders:Array;
    private var loaders:Array;
    private var loaded:uint;
    private var completed:uint;
    private var colors:Array;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private var shade:DropShadowFilter;
    private var enabled:Boolean = true;
    private var nextID:uint;
    private static var deceleration:Number = 0.5;
    private var fullscreen:Boolean = false;
    private var flickr:FlickrBar;
    private var help:HelpPanel;
    public static const ALL_COMPLETE:String = "allcomplete";
    public static const ALL_FADE_OUT:String = "allfadeout";
    public static const UPDATE:String = "update";
    public static const FULL_SCREEN:String = "fullscreen";
    public static const NORMAL:String = "normal";

    public function FlickrDanboLoader() {
        photo = new Sprite();
        loader = new ProgressLoader();
        loader.prog(ProgressBar);
        loader.photo = 16;
        container = loader.container;
        loader.addEventListener(ProgressLoader.INIT, initializePhoto, false, 0, true);
        loader.addEventListener(ProgressLoader.IO_ERROR, ioerrorPhoto, false, 0, true);
        loader.addEventListener(ProgressLoader.COMPLETE, completePhoto, false, 0, true);
        loader.addEventListener(ProgressLoader.FADE_OUT, fadeOutPhoto, false, 0, true);
        loader.addEventListener(ProgressLoader.BAR_FADE_OUT, barFadeOutPhoto, false, 0, true);
        photo.addChild(loader);
        flickr = new FlickrBar();
        if (stage) resize();
        else addEventListener(Event.ADDED_TO_STAGE, resize, false, 0, true);
    }

    private function resize(evt:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, resize);
        stage.addEventListener(Event.RESIZE, update, false, 0, true);
    }
    public function size(w:uint, c:uint, x:uint, d:uint, m:uint, p:uint):void {
        bWidth = w;
        cols = c;
        xPos = x + w*0.5;
        distance = d;
        mw = m;
        pw = p;
        pWidth = mw;
        xOffset = pw;
    }
    public function set color(param:Array):void {
        colors = param;
    }
    public function dataProvider(pList:Array, m:Array, p:Array, i:Array):void {
        photos = pList;
        mlist = m;
        plist = p;
        orders = mlist;
        infos = i;
        init();
    }
    private function init():void {
        shade = new DropShadowFilter(1, 90, sColor, 0.5, 4, 4, 3, 3, false, false);
        holders = new Array();
        loaders = new Array();
        for (var n:uint = 0; n < photos.length; n++) {
            var holder:Sprite = new Sprite();
            holder.name = String(n);
            addChild(holder);
            holders.push(holder);
            holder.mouseChildren = false;
            var loader:ProgressLoader = new ProgressLoader();
            loader.prog(ProgressArc);
            loader.photo = 4;
            loader.addEventListener(ProgressLoader.INIT, initialize, false, 0, true);
            loader.addEventListener(ProgressLoader.IO_ERROR, ioerror, false, 0, true);
            loader.addEventListener(ProgressLoader.COMPLETE, complete, false, 0, true);
            loader.addEventListener(ProgressLoader.FADE_OUT, fadeOut, false, 0, true);
            loader.addEventListener(ProgressLoader.BAR_FADE_OUT, barFadeOut, false, 0, true);
            holder.addChild(loader);
            loaders.push(loader);
            var progArc:ProgressArc = ProgressArc(loader.progressBar);
            progArc.color(colors[n%colors.length]);
        }
        addChild(photo);
        photo.visible = false;
        photo.mouseChildren = false;
        flickr.y = 155;
        addChild(flickr);
        update();
        help = new HelpPanel();
        addChild(help);
        help.init({width: 120, height: 60, type: 2});
    }
    public function start():void {
        loaded = 0;
        flickr.init();
        completed = 0;
        load(loaded);
    }
    private function load(id:uint):void {
        var loader:ProgressLoader = loaders[id];
        loader.load(photos[id]);
    }
    private function next():void {
        loaded++;
        flickr.progress(loaded, photos.length);
        if (loaded < photos.length) {
            load(loaded);
        } else {
            dispatchEvent(new Event(FlickrDanboLoader.ALL_COMPLETE));
        }
    }
    public function reset():void {
        if (completed == 0) return;
        for (var n:uint = 0; n < photos.length; n++) {
            var loader:ProgressLoader = loaders[n];
            loader.unload();
        }
    }
    private function initialize(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.INIT, initialize);
    }
    private function ioerror(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.IO_ERROR, ioerror);
        loadComplete();
        next();
    }
    private function complete(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.COMPLETE, complete);
    }
    private function fadeOut(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.FADE_OUT, fadeOut);
        var loader:ProgressLoader = ProgressLoader(evt.target);
        var container:Sprite = loader.container;
        var holder:Sprite = Sprite(loader.parent);
        container.visible = true;
        holder.buttonMode = true;
        holder.mouseEnabled = true;
        holder.useHandCursor = true;
        holder.addEventListener(MouseEvent.CLICK, click, false, 0, true);
        holder.addEventListener(MouseEvent.MOUSE_OVER, showHelp, false, 0, true);
        holder.addEventListener(MouseEvent.MOUSE_OUT, hideHelp, false, 0, true);
        next();
    }
    private function barFadeOut(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.BAR_FADE_OUT, barFadeOut);
        loadComplete();
    }
    private function loadComplete():void {
        completed++;
        if (completed >= photos.length) {
            dispatchEvent(new Event(FlickrDanboLoader.ALL_FADE_OUT));
        }
    }
    private function click(evt:MouseEvent):void {
        if (!enabled) return;
        var id:uint = uint(evt.target.name);
        nextID = id;
        slideOut();
    }
    private function loadPhoto(id:uint):void {
        loader.unload();
        if (fullscreen) {
            orders = plist;
        } else {
            orders = mlist;
        }
        loader.load(orders[id]);
        photo.visible = true;
        enable(false);
    }
    private function clickOutSide():void {
        stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
    }
    private function clearOutSide():void {
        stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    }
    private function mouseDown(evt:MouseEvent):void {
        if (!enabled) return;
        if (!photo.hitTestPoint(stage.mouseX, stage.mouseY, true)) {
            clearOutSide();
            nextID = photos.length;
            slideOut();
        }
    }
    private function enable(flag:Boolean):void {
        enabled = flag;
        for (var n:uint = 0; n < photos.length; n++) {
            var holder:Sprite = holders[n];
            holder.mouseEnabled= enabled;
            holder.useHandCursor = enabled;
        }
        stage.mouseChildren= enabled;
    }
    private function initializePhoto(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.INIT, initializePhoto);
        photo.visible = true;
    }
    private function ioerrorPhoto(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.IO_ERROR, ioerrorPhoto);
        photo.visible = true;
        enable(true);
        clickOutSide();
    }
    private function completePhoto(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.COMPLETE, completePhoto);
    }
    private function fadeOutPhoto(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.FADE_OUT, fadeOutPhoto);
        container.visible = true;
        var targetPos:int = stage.stageWidth*0.5 + xOffset;
        container.x = - targetPos;
        slideIn();
    }
    private function barFadeOutPhoto(evt:Event):void {
        //evt.target.removeEventListener(ProgressLoader.BAR_FADE_OUT, barFadeOutPhoto);
        enable(true);
        clickOutSide();
    }
    private function slideOut():void {
        if (loader.content) {
            stage.addEventListener(Event.ENTER_FRAME, slideOutPhoto, false, 0, true);
        } else {
            nextPhoto();
        }
    }
    private function slideIn():void {
        stage.addEventListener(Event.ENTER_FRAME, slideInPhoto, false, 0, true);
    }
    private function slideOutPhoto(evt:Event):void {
        var targetPos:int = stage.stageWidth*0.5 + xOffset;
        container.x += (targetPos - container.x)*deceleration;
        var percent:Number = Math.abs(targetPos - container.x)/stage.stageWidth*2;
        blur(percent);
        if (Math.abs(targetPos - container.x) < 0.5) {
            container.x = - targetPos;
            blur(0);
            stage.removeEventListener(Event.ENTER_FRAME, slideOutPhoto);
            nextPhoto();
        }
    }
    private function slideInPhoto(evt:Event):void {
        var targetPos:int = 0;
        container.x += (targetPos - container.x)*deceleration;
        var percent:Number = Math.abs(targetPos - container.x)/stage.stageWidth*2;
        blur(percent);
        if (Math.abs(targetPos - container.x) < 0.5) {
            container.x = targetPos;
            blur(0);
            stage.removeEventListener(Event.ENTER_FRAME, slideInPhoto);
        }
    }
    private function blur(percent:Number):void {
        if (percent > 0) {
            var bx:uint = Math.floor(64*percent);
            var blurX:BlurFilter = new BlurFilter(bx, 0, 3);
            container.filters = [blurX];
        } else {
            container.filters = [];
        }
    }
    private function nextPhoto():void {
        if (nextID < photos.length) {
            loadPhoto(nextID);
        } else {
            photo.visible = false;
            clearOutSide();
        }
    }
    private function showHelp(evt:MouseEvent):void {
        var id:uint = uint(evt.target.name);
        var title:String = "[" + (id+1) + "] "+ infos[id].title;
        var type:String;
        if (id%cols == 0) {
            type = "left";
        } else if (id%cols == cols-1) {
            type = "right";
        } else {
            type = "center";
        }
        help.show(title, type);
    }
    private function hideHelp(evt:MouseEvent):void {
        help.hide();
    }
    private function update(evt:Event = null):void {
        dispatchEvent(new Event(FlickrDanboLoader.UPDATE));
        if (stage.displayState == StageDisplayState.FULL_SCREEN) {
            fullscreen = true;
            dispatchEvent(new Event(FlickrDanboLoader.FULL_SCREEN));
            pWidth = mw;
        } else {
            fullscreen = false;
            dispatchEvent(new Event(FlickrDanboLoader.NORMAL));
            pWidth = pw;
        }
        if (!contains(photo)) return;
        var sw:uint = stage.stageWidth;
        var sh:uint = stage.stageHeight;
        cols = Math.floor((sw - distance)/(bWidth + distance));
        xPos = Math.floor((sw - bWidth*cols - distance*(cols-1))*0.5) + bWidth*0.5;
        for (var n:uint = 0; n < photos.length; n++) {
            var holder:Sprite = holders[n];
            holder.x = xPos + (bWidth+distance)*(n%cols);
            holder.y = Math.floor(70 + bWidth*0.5) + (bWidth+distance)*Math.floor(n/cols);
        }
        photo.x = sw*0.5;
        photo.y = (sh - 50)*0.5 + 50;
        flickr.x = sw*0.5;
    }
    public function screenMode(mode:Boolean):void {
        if (stage.displayState == StageDisplayState.NORMAL) {
            fullscreen = true;
            stage.displayState = StageDisplayState.FULL_SCREEN;
            dispatchEvent(new Event(FlickrDanboLoader.FULL_SCREEN));
            pWidth = pw;
        } else {
            fullscreen = false;
            stage.displayState = StageDisplayState.NORMAL;
            dispatchEvent(new Event(FlickrDanboLoader.NORMAL));
            pWidth = mw;
        }
    }

}


//////////////////////////////////////////////////
// ProgressLoaderクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.filters.DropShadowFilter;

class ProgressLoader extends Sprite {
    private var loader:Loader;
    private var info:LoaderInfo;
    public var container:Sprite;
    public var content:*;
    private var base:Shape;
    private var corner:uint;
    private var smoothing:Boolean;
    private var ProgressBar:Class;
    public var progressBar:*;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private var shade:DropShadowFilter;
    public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
    public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
    public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
    public static const INIT:String = Event.INIT;
    public static const COMPLETE:String = Event.COMPLETE;
    //public static const FADE_OUT:String = ProgressBar.FADE_OUT;
    //public static const BAR_FADE_OUT:String = ProgressBar.BAR_FADE_OUT;
    public static const FADE_OUT:String = "fadeOut";
    public static const BAR_FADE_OUT:String = "barFadeOut";

    public function ProgressLoader() {
        container = new Sprite();
        addChild(container);
        base = new Shape();
        container.addChild(base);
        shade = new DropShadowFilter(1, 90, sColor, 0.5, 4, 4, 3, 3, false, false);
        base.filters = [shade];
        loader = new Loader();
        container.addChild(loader);
        info = loader.contentLoaderInfo;
    }

    public function prog(Bar:Class):void {
        ProgressBar = Bar;
        progressBar = new ProgressBar();
    }
    public function init(x:int = 0, y:int = 0):void {
        progressBar.x = x;
        progressBar.y = y;
    }
    public function set photo(c:uint):void {
        corner = c;
    }
    public function load(file:String, s:Boolean = false):void {
        smoothing = s;
        info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
        info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        info.addEventListener(Event.INIT, initialize, false, 0, true);
        info.addEventListener(Event.COMPLETE, complete, false, 0, true);
        progressBar.addEventListener(ProgressBar.FADE_OUT, fadeOut, false, 0, true);
        progressBar.addEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut, false, 0, true);
        addChild(progressBar);
        try {
            loader.load(new URLRequest(file));
            progressBar.init();
        } catch (err:Error) {
            progressBar.error(err.message);
        }
    }
    public function unload():void {
        loader.unload();
        if (contains(progressBar)) removeChild(progressBar);
        base.graphics.clear();
    }
    private function progress(evt:ProgressEvent):void {
        progressBar.progress(evt.bytesLoaded, evt.bytesTotal);
    }
    private function ioerror(evt:IOErrorEvent):void {
        loader.unload();
        progressBar.error(evt.type);
        dispatchEvent(new Event(ProgressLoader.IO_ERROR));
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        dispatchEvent(new Event(ProgressLoader.HTTP_STATUS));
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        dispatchEvent(new Event(ProgressLoader.SECURITY_ERROR));
    }
    private function initialize(evt:Event):void {
        if (smoothing) {
            content = Bitmap(info.content);
            content.smoothing = true;
        } else {
            content = info.content;
        }
        centerize();
        container.visible = false;
        dispatchEvent(new Event(ProgressLoader.INIT));
    }
    private function complete(evt:Event):void {
        info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        info.removeEventListener(Event.INIT, initialize);
        info.removeEventListener(Event.COMPLETE, complete);
        dispatchEvent(new Event(ProgressLoader.COMPLETE));
    }
    private function fadeOut(evt:Event):void {
        evt.target.removeEventListener(ProgressBar.FADE_OUT, fadeOut);
        dispatchEvent(new Event(ProgressBar.FADE_OUT));
    }
    private function barFadeOut(evt:Event):void {
        evt.target.removeEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut);
        removeChild(progressBar);
        dispatchEvent(new Event(ProgressBar.BAR_FADE_OUT));
    }
    private function centerize():void {
        var w:uint = content.width;
        var h:uint = content.height;
        var cx:int = -uint(w*0.5);
        var cy:int = -uint(h*0.5);
        content.x = cx;
        content.y = cy;
        base.graphics.beginFill(bColor);
        base.graphics.drawRect(cx-corner, cy-corner, w+corner*2, h+corner*2);
        base.graphics.endFill();
    }

}


//////////////////////////////////////////////////
// FontLoaderクラス
//////////////////////////////////////////////////

import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.Font;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;

class FontLoader extends EventDispatcher {
    public var id:uint;
    private var loader:Loader;
    private var info:LoaderInfo;
    private var _className:String;
    private var _font:Font;
    private var _fontName:String;
    private var embeded:Boolean = false;
    public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
    public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
    public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
    public static const INIT:String = Event.INIT;
    public static const COMPLETE:String = Event.COMPLETE;

    public function FontLoader() {
        loader = new Loader();
        info = loader.contentLoaderInfo;
    }

    public function load(file:String, name:String, e:Boolean = false):void {
        _className = name;
        embeded = e;
        info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
        info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        info.addEventListener(Event.INIT, initialize, false, 0, true);
        info.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            var context:LoaderContext = new LoaderContext();
            context.applicationDomain = ApplicationDomain.currentDomain;
            context.securityDomain = SecurityDomain.currentDomain;
            loader.load(new URLRequest(file), context);
        } catch (err:Error) {
            trace(err.message);
        }
    }
    public function unload():void {
        loader.unload();
    }
    private function progress(evt:ProgressEvent):void {
        dispatchEvent(evt);
    }
    private function ioerror(evt:IOErrorEvent):void {
        loader.unload();
        dispatchEvent(new Event(FontLoader.IO_ERROR));
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        dispatchEvent(new Event(FontLoader.HTTP_STATUS));
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        dispatchEvent(new Event(FontLoader.SECURITY_ERROR));
    }
    private function initialize(evt:Event):void {
        dispatchEvent(new Event(FontLoader.INIT));
    }
    private function complete(evt:Event):void {
        info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        info.removeEventListener(Event.INIT, initialize);
        info.removeEventListener(Event.COMPLETE, complete);
        var FontClass:Class = Class(ApplicationDomain.currentDomain.getDefinition(className));
        if (!embeded) {
            Font.registerFont(FontClass);
            _font = Font(new FontClass());
        } else {
            var document:Object = new FontClass();
            _font = document.font;
        }
        _fontName = _font.fontName;
        dispatchEvent(new Event(FontLoader.COMPLETE));
    }
    public function get className():String {
        return _className;
    }
    public function get font():Font {
        return _font;
    }
    public function get fontName():String {
        return _fontName;
    }

}


//////////////////////////////////////////////////
// ProgressBarクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

class ProgressBar extends Sprite {
    private var base:Shape;
    private var bar:Sprite;
    private var stripe:Shape;
    private var prog:Shape;
    private var inner:Shape;
    private var title:TextField;
    private var txt:TextField;
    private var total:TextField;
    private static var label:String = "loading...";
    private static var fontType:String = "Myriad Pro Semibold";
    private static var _width:uint = 220;
    private static var _height:uint = 40;
    private static var bHeight:uint = 10;
    private static var tWidth:uint = 55;
    private static var tHeight:uint = 20;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var tColor:uint = 0x999999;
    private static var s1Color:uint = 0xABCDFB;
    private static var s2Color:uint = 0x71A8F1;
    private static var e1Color:uint = 0xFFFF00;
    private static var e2Color:uint = 0x333333;
    private var baseShade:DropShadowFilter;
    private var barShade:DropShadowFilter;
    private var _percent:Number = 0;
    private var targetPercent:Number;
    private var basePos:Number;
    private static var interval:uint = 500;
    private static var deceleration:Number = 0.4;
    private var timer:Timer;
    public static const FADE_OUT:String = "fadeOut";
    public static const BAR_FADE_OUT:String = "barFadeOut";

    public function ProgressBar() {
        draw();
        percent = 0;
        visible = false;
    }

    private function draw():void {
        createChildren();
        createText();
    }
    private function createChildren():void {
        baseShade = new DropShadowFilter(0, 90, sColor, 0.5, 4, 4, 1.5, 2, false, false);
        barShade = new DropShadowFilter(0, 90, sColor, 0.3, 4, 4, 1.5, 2, true, false);
        base = new Shape();
        bar = new Sprite();
        inner = new Shape();
        stripe = new Shape();
        prog = new Shape();
        addChild(base);
        addChild(bar);
        bar.addChild(inner);
        bar.addChild(stripe);
        bar.addChild(prog);
        createBase(base, -_width*0.5, -_height*0.5, _width, _height, bColor);
        base.filters = [baseShade];
        bar.x = -_width*0.5 + 10;
        bar.y = -2;
        bar.filters = [barShade];
        createBase(inner, 0, 0, _width-20, bHeight, bColor);
        createStripe(stripe, _width-20, bHeight, [s1Color, s2Color]);
        createBase(prog, 0, 0, _width-20, bHeight, sColor);
        stripe.mask = prog;
    }
    private function createText():void {
        title = new TextField();
        txt = new TextField();
        total = new TextField();
        addChild(title);
        addChild(txt);
        addChild(total);
        title.x = -tWidth;
        title.y = -_height*0.5 + 2;
        title.width = tWidth;
        title.height = tHeight - 2;
        title.type = TextFieldType.DYNAMIC;
        title.selectable = false;
        title.embedFonts = true;
        title.antiAliasType = AntiAliasType.ADVANCED;
        var tfl:TextFormat = new TextFormat();
        tfl.font = fontType;
        tfl.size = 12;
        tfl.align = TextFormatAlign.LEFT;
        title.defaultTextFormat = tfl;
        title.text = label;
        txt.x = 0;
        txt.y = -_height*0.5 + 2;
        txt.width = tWidth;
        txt.height = tHeight - 2;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        txt.embedFonts = true;
        txt.antiAliasType = AntiAliasType.ADVANCED;
        var tfr:TextFormat = new TextFormat();
        tfr.font = fontType;
        tfr.size = 12;
        tfr.align = TextFormatAlign.RIGHT;
        txt.defaultTextFormat = tfr;
        total.x = -tWidth;
        total.y = 8;
        total.width = tWidth*2;
        total.height = 12;
        total.type = TextFieldType.DYNAMIC;
        total.selectable = false;
        total.embedFonts = true;
        total.antiAliasType = AntiAliasType.ADVANCED;
        var tfc:TextFormat = new TextFormat();
        tfc.font = fontType;
        tfc.size = 10;
        tfc.align = TextFormatAlign.CENTER;
        total.defaultTextFormat = tfc;
        total.textColor = tColor;
    }
    public function init():void {
        basePos = y;
        alpha = 1;
        total.text = "0KB";
        createStripe(stripe, _width-20, bHeight, [s1Color, s2Color]);
        percent = 0;
        visible = true;
    }
    public function error(e:String):void {
        alpha = 1;
        txt.text = "0%";
        total.text = e;
        createStripe(stripe, _width-20, bHeight, [e1Color, e2Color]);
        prog.scaleX = 1;
        visible = true;
    }
    public function progress(l:Number, t:Number):void {
        total.text = Math.ceil(t/1024) + "KB";
        targetPercent = Math.round(l/t*100);
        addEventListener(Event.ENTER_FRAME, progressTo, false, 0, true);
    }
    private function progressTo(evt:Event):void {
        percent += (targetPercent - percent)*deceleration;
        if (Math.abs(targetPercent - percent) < 0.5) {
            percent = targetPercent;
            removeEventListener(Event.ENTER_FRAME, progressTo);
        }
        if (percent >= 100) {
            dispatchEvent(new Event(ProgressBar.FADE_OUT));
            timer = new Timer(interval, 1);
            timer.addEventListener(TimerEvent.TIMER, fadeOut, false, 0, true);
            timer.start();
        }
    }
    private function fadeOut(evt:TimerEvent):void {
        timer.removeEventListener(TimerEvent.TIMER, fadeOut);
        addEventListener(Event.ENTER_FRAME, barFadeOut, false, 0, true);
    }
    private function barFadeOut(evt:Event):void {
        var speed:Number = 0.05;
        y += 20*speed;
        alpha -= speed;
        if (alpha <= 0) {
            removeEventListener(Event.ENTER_FRAME, barFadeOut);
            y = basePos;
            alpha = 0;
            visible = false;
            dispatchEvent(new Event(ProgressBar.BAR_FADE_OUT));
        }
    }
    public function get percent():Number {
        return _percent;
    }
    public function set percent(param:Number):void {
        _percent = param;
        manage(_percent);
    }
    private function manage(p:Number):void {
        txt.text = String(Math.round(p)) + "%";
        prog.scaleX = p/100;
    }
    private function createBase(target:Shape, x:int, y:int, w:uint, h:uint, color:uint):void {
        target.graphics.beginFill(color);
        target.graphics.drawRect(x, y, w, h);
        target.graphics.endFill();
    }
    private function createStripe(target:Shape, w:uint, h:uint, colors:Array):void {
        target.graphics.clear();
        for (var n:uint = 0; n <= Math.ceil(w/h); n++) {
            target.graphics.beginFill(colors[n%2]);
            target.graphics.moveTo(h*(n-1), 0);
            target.graphics.lineTo(h*n, h);
            target.graphics.lineTo(h*(n+1), h);
            target.graphics.lineTo(h*n, 0);
            target.graphics.lineTo(h*(n-1), 0);
            target.graphics.endFill();
        }
    }

}


//////////////////////////////////////////////////
// ProgressArcクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.display.Graphics;

class ProgressArc extends Sprite {
    private var base:Shape;
    private var prog:Shape;
    private var txt:TextField;
    private static var fontType:String = "Myriad Pro Semibold";
    private static var radius:uint = 20;
    private static var thickness:uint = 10;
    private static var outer:uint;
    private static var inner:uint;
    private static var tWidth:uint = 80;
    private var aColor:uint;
    private static var eColor:Number = 0x666666;
    private static var tColor:Number = 0xFFFFFF;
    private static var sColor:Number = 0x000000;
    private var shade:DropShadowFilter;
    private var _percent:Number = 0;
    private var targetPercent:Number = 0;
    private static var interval:uint = 500;
    private static var deceleration:Number = 0.4;
    private var timer:Timer;
    public static const FADE_OUT:String = "fadeOut";
    public static const BAR_FADE_OUT:String = "barFadeOut";

    public function ProgressArc() {
        outer = radius + thickness/2;
        inner = radius - thickness/2;
        draw();
        percent = 0;
        visible = false;
    }

    private function draw():void {
        createChildren();
        createText();
    }
    private function createChildren():void {
        shade = new DropShadowFilter(1, 90, sColor, 0.3, 4, 4, 2, 3, false, false);
        base = new Shape();
        prog = new Shape();
        addChild(base);
        addChild(prog);
        prog.filters = [shade];
    }
    private function createText():void {
        txt = new TextField();
        addChild(txt);
        txt.x = -tWidth/2;
        txt.y = -6;
        txt.width = tWidth;
        txt.height = 14;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        txt.embedFonts = true;
        txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = 12;
        tf.align = TextFormatAlign.CENTER;
        txt.textColor = tColor;
        txt.defaultTextFormat = tf;
    }
    public function init():void {
        alpha = 1;
        percent = 0;
        visible = true;
    }
    public function error(e:String):void {
        alpha = 1;
        txt.text = e;
        createArc(prog, 1, eColor);
        visible = true;
    }
    public function progress(l:Number, t:Number):void {
        targetPercent = Math.round(l/t*100);
        addEventListener(Event.ENTER_FRAME, progressTo, false, 0, true);
    }
    private function progressTo(evt:Event):void {
        percent += (targetPercent - percent)*deceleration;
        if (Math.abs(targetPercent - percent) < 0.5) {
            percent = targetPercent;
            removeEventListener(Event.ENTER_FRAME, progressTo);
        }
        if (percent >= 100) {
            dispatchEvent(new Event(ProgressArc.FADE_OUT));
            timer = new Timer(interval, 1);
            timer.addEventListener(TimerEvent.TIMER, fadeOut, false, 0, true);
            timer.start();
        }
    }
    private function fadeOut(evt:TimerEvent):void {
        timer.removeEventListener(TimerEvent.TIMER, fadeOut);
        addEventListener(Event.ENTER_FRAME, barFadeOut, false, 0, true);
    }
    private function barFadeOut(evt:Event):void {
        var speed:Number = 0.05;
        alpha -= speed;
        if (alpha <= 0) {
            removeEventListener(Event.ENTER_FRAME, barFadeOut);
            alpha = 0;
            visible = false;
            dispatchEvent(new Event(ProgressArc.BAR_FADE_OUT));
        }
    }
    public function get percent():Number {
        return _percent;
    }
    public function set percent(param:Number):void {
        _percent = param;
        manage(_percent);
    }
    private function manage(p:Number):void {
        txt.text = String(Math.round(p)) + "%";
        createArc(prog, p/100, aColor);
    }
    //円弧(ドーナツ型)を描く
    private function createArc(target:Shape, percent:Number, color:uint):void {
        target.graphics.clear();
        target.graphics.beginFill(color);
        drawRing(target, 0, 0, outer, inner, 360*percent, -90);
        target.graphics.endFill();
    }
    private function drawRing(target:Shape, x:Number, y:Number, outerRadius:Number, innerRadius:Number, degree:Number=360, fromDegree:Number=0, split:Number=36):void {
        var points:Array = getRingPoints(x, y, outerRadius, innerRadius, degree, fromDegree, split);
        drawLines(target, points, true);
    }
    private function getRingPoints(x:Number, y:Number, outerRadius:Number, innerRadius:Number, degree:Number=360, fromDegree:Number=0, split:Number=36):Array {
        var fromRad:Number = fromDegree*Math.PI/180;
        var dr:Number = (degree* Math.PI/180)/split;
        var pt:Point;
        var imax:int = split +1;
        var rad:Number;
        var points:Array = new Array();
        for(var i:int=0; i < imax; i++) {
            pt = new Point();
            rad = fromRad + dr*i;
            pt.x = Math.cos(rad)*outerRadius+x;
            pt.y = Math.sin(rad)*outerRadius+y;
            points.push(pt);
        }
        var points2:Array = [];
        for(i=0; i<imax; i++){
            pt = new Point();
            rad = fromRad + dr*i;
            pt.x = Math.cos(rad)*innerRadius+x;
            pt.y = Math.sin(rad)*innerRadius+y;
            points2.push(pt);
        }
        points2.reverse();
        points = points.concat(points2);
        return points;
    }
    private function drawLines(target:Shape, points:Array, close:Boolean=false):void {
        var imax:Number = points.length;
        target.graphics.moveTo(points[0].x, points[0].y);
        for(var i:Number=1; i<imax; i++) {
            target.graphics.lineTo(points[i].x, points[i].y);
        }
        if(close) target.graphics.lineTo(points[0].x, points[0].y);
    }
    public function color(param:uint):void {
        aColor = param;
    }

}


//////////////////////////////////////////////////
// FlickrBarクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

class FlickrBar extends Sprite {
    private var base:Shape;
    private var bar:Sprite;
    private var stripe:Shape;
    private var prog:Shape;
    private var inner:Shape;
    private var title:TextField;
    private var txt:TextField;
    private var total:TextField;
    private static var label:String = "loading...";
    private static var fontType:String = "Myriad Pro Semibold";
    private static var _width:uint = 220;
    private static var _height:uint = 40;
    private static var bHeight:uint = 10;
    private static var tWidth:uint = 55;
    private static var tHeight:uint = 20;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var tColor:uint = 0x999999;
    private static var s1Color:Number = 0xCCE6FF;
    private static var s2Color:Number = 0x0280FD;
    private static var s3Color:Number = 0xFD3EB0;
    private static var e1Color:Number = 0xFFFF00;
    private static var e2Color:Number = 0x333333;
    private static var e3Color:Number = 0x333333;
    private var baseShade:DropShadowFilter;
    private var barShade:DropShadowFilter;
    private var _percent:Number = 0;
    private var targetPercent:Number;
    private var basePos:Number;
    private static var interval:uint = 500;
    private static var deceleration:Number = 0.4;
    private var timer:Timer;
    public static const FADE_OUT:String = "fadeOut";
    public static const BAR_FADE_OUT:String = "barFadeOut";

    public function FlickrBar() {
        draw();
        percent = 0;
        visible = false;
    }

    private function draw():void {
        createChildren();
        createText();
    }
    private function createChildren():void {
        baseShade = new DropShadowFilter(0, 90, sColor, 0.5, 4, 4, 1.5, 2, false, false);
        barShade = new DropShadowFilter(0, 90, sColor, 0.3, 4, 4, 1.5, 2, true, false);
        base = new Shape();
        bar = new Sprite();
        inner = new Shape();
        stripe = new Shape();
        prog = new Shape();
        addChild(base);
        addChild(bar);
        bar.addChild(inner);
        bar.addChild(stripe);
        bar.addChild(prog);
        createBase(base, -_width*0.5, -_height*0.5, _width, _height, bColor);
        base.filters = [baseShade];
        bar.x = -_width*0.5 + 10;
        bar.y = -2;
        bar.filters = [barShade];
        createBase(inner, 0, 0, _width-20, bHeight, bColor);
        createStripe(stripe, _width-20, bHeight, [s1Color, s2Color, s3Color]);
        createBase(prog, 0, 0, _width-20, bHeight, sColor);
        stripe.mask = prog;
    }
    private function createText():void {
        title = new TextField();
        txt = new TextField();
        total = new TextField();
        addChild(title);
        addChild(txt);
        addChild(total);
        title.x = -tWidth;
        title.y = -_height*0.5 + 2;
        title.width = tWidth;
        title.height = tHeight - 2;
        title.type = TextFieldType.DYNAMIC;
        title.selectable = false;
        title.embedFonts = true;
        title.antiAliasType = AntiAliasType.ADVANCED;
        var tfl:TextFormat = new TextFormat();
        tfl.font = fontType;
        tfl.size = 12;
        tfl.align = TextFormatAlign.LEFT;
        title.defaultTextFormat = tfl;
        title.text = label;
        txt.x = 0;
        txt.y = -_height*0.5 + 2;
        txt.width = tWidth;
        txt.height = tHeight - 2;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        txt.embedFonts = true;
        txt.antiAliasType = AntiAliasType.ADVANCED;
        var tfr:TextFormat = new TextFormat();
        tfr.font = fontType;
        tfr.size = 12;
        tfr.align = TextFormatAlign.RIGHT;
        txt.defaultTextFormat = tfr;
        total.x = -tWidth;
        total.y = 8;
        total.width = tWidth*2;
        total.height = 14;
        total.type = TextFieldType.DYNAMIC;
        total.selectable = false;
        total.embedFonts = true;
        total.antiAliasType = AntiAliasType.ADVANCED;
        var tfc:TextFormat = new TextFormat();
        tfc.font = fontType;
        tfc.size = 10;
        tfc.align = TextFormatAlign.CENTER;
        total.defaultTextFormat = tfc;
        total.textColor = tColor;
        total.text = "photos";
    }
    public function init():void {
        basePos = y;
        alpha = 1;
        createStripe(stripe, _width-20, bHeight, [s1Color, s2Color, s3Color]);
        percent = 0;
        visible = true;
    }
    public function error(e:String):void {
        alpha = 1;
        txt.text = "0%";
        createStripe(stripe, _width-20, bHeight, [e1Color, e2Color, e3Color]);
        prog.scaleX = 1;
        visible = true;
    }
    public function progress(l:Number, t:Number):void {
        targetPercent = Math.round(l/t*100);
        addEventListener(Event.ENTER_FRAME, progressTo, false, 0, true);
    }
    private function progressTo(evt:Event):void {
        percent += (targetPercent - percent)*deceleration;
        if (Math.abs(targetPercent - percent) < 0.5) {
            percent = targetPercent;
            removeEventListener(Event.ENTER_FRAME, progressTo);
        }
        if (percent >= 100) {
            dispatchEvent(new Event(FlickrBar.FADE_OUT));
            timer = new Timer(interval, 1);
            timer.addEventListener(TimerEvent.TIMER, fadeOut, false, 0, true);
            timer.start();
        }
    }
    private function fadeOut(evt:TimerEvent):void {
        timer.removeEventListener(TimerEvent.TIMER, fadeOut);
        addEventListener(Event.ENTER_FRAME, barFadeOut, false, 0, true);
    }
    private function barFadeOut(evt:Event):void {
        var speed:Number = 0.05;
        y += 20*speed;
        alpha -= speed;
        if (alpha <= 0) {
            removeEventListener(Event.ENTER_FRAME, barFadeOut);
            y = basePos;
            alpha = 0;
            visible = false;
            dispatchEvent(new Event(FlickrBar.BAR_FADE_OUT));
        }
    }
    public function get percent():Number {
        return _percent;
    }
    public function set percent(param:Number):void {
        _percent = param;
        manage(_percent);
    }
    private function manage(p:Number):void {
        txt.text = String(Math.round(p)) + "%";
        prog.scaleX = p/100;
    }
    private function createBase(target:Shape, x:int, y:int, w:uint, h:uint, color:uint):void {
        target.graphics.beginFill(color);
        target.graphics.drawRect(x, y, w, h);
        target.graphics.endFill();
    }
    private function createStripe(target:Shape, w:uint, h:uint, colors:Array):void {
        target.graphics.clear();
        for (var n:uint = 0; n <= Math.ceil(w/h); n++) {
            if (n == Math.ceil(w/h) - 1) {
                target.graphics.beginFill(colors[2]);
            } else {
                target.graphics.beginFill(colors[n%2]);
            }
            target.graphics.moveTo(h*(n-1), 0);
            target.graphics.lineTo(h*n, h);
            target.graphics.lineTo(h*(n+1), h);
            target.graphics.lineTo(h*n, 0);
            target.graphics.lineTo(h*(n-1), 0);
            target.graphics.endFill();
        }
    }

}


//////////////////////////////////////////////////
// HelpPanelクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
import flash.events.MouseEvent;

class HelpPanel extends Sprite {
    private var base:Sprite;
    private var panel:Shape;
    private var arrow:Shape;
    private var board:Shape;
    private var txt:TextField;
    private static var fontType:String = "Myriad Pro Semibold";
    private var px:uint;
    private var py:uint;
    private var _width:uint = 100;
    private var _height:uint = 60;
    private var type:uint = 1;
    private static var xOffset:uint = 15;
    private static var yOffset:uint = 10;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var cColor:uint = 0xEEEEEE;
    private static var tColor:uint = 0x000000;
    private var shade:DropShadowFilter;
    private static var typeL:String = "left";
    private static var typeC:String = "center";
    private static var typeR:String = "right";
    private var _type:String = typeC;
    private var deceleration:Number = 0.25;
    private var step:uint = 10;
    private var _visible:Boolean = false;

    public function HelpPanel() {
        if (stage) initialize();
        else addEventListener(Event.ADDED_TO_STAGE, initialize, false, 0, true);
    }

    public function init(option:Object):void {
        if (option.width != undefined) _width = option.width;
        if (option.height != undefined) _height = option.height;
        if (option.type != undefined) type = option.type;
        if (option.dec) deceleration = option.dec;
        if (option.step) step = option.step;
        draw();
    }
    private function draw():void {
        switch (type) {
            case 1 :
                bColor = 0xFFFFFF;
                cColor = 0xEEEEEE;
                tColor = 0x000000;
                break;
            case 2 :
                bColor = 0x000000;
                cColor = 0x333333;
                tColor = 0xFFFFFF;
                break;
        }
        shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 1.5, 2, false, false);
        base = new Sprite();
        panel = new Shape();
        arrow = new Shape();
        board = new Shape();
        txt = new TextField();
        addChild(base);
        base.addChild(panel);
        base.addChild(arrow);
        base.addChild(board);
        base.addChild(txt);
        createBox(panel, -_width*0.5, -_height, _width, _height, 4);
        createArrow(arrow, 6, 8);
        createBase(board, -_width*0.5+5, -_height+5, _width-10, _height-10);
        base.y = - yOffset;
        txt.x = -_width*0.5 + 8;
        txt.y = -_height + 8;
        txt.width = _width - 16;
        txt.height = _height - 16;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        txt.multiline = true;
        txt.wordWrap = true;
        txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = 12;
        tf.align = TextFormatAlign.LEFT;
        txt.defaultTextFormat = tf;
        txt.textColor = tColor;
        filters = [shade];
    }
    private function initialize(evt:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, initialize);
        visible = false;
        x = stage.stageWidth*0.5;
        y = stage.stageHeight*0.5;
        stage.addEventListener(Event.ENTER_FRAME, move, false, 0, true);
        mouseChildren = false;
        mouseEnabled = false;
    }
    private function update(t:String):void {
        switch (t) {
            case typeL :
                base.x = _width*0.5 - xOffset;
                arrow.x = - _width*0.5 + xOffset;
                break;
            case typeC :
                base.x = 0;
                arrow.x = 0;
                break;
            case typeR :
                base.x = - _width*0.5 + xOffset;
                arrow.x = _width*0.5 - xOffset;
                break;
        }
    }
    private function move(evt:Event):void {
        var xMouse:uint = stage.mouseX;
        var yMouse:uint = stage.mouseY;
        if (xMouse >= 0 && xMouse <= stage.stageWidth) {
            px += (xMouse - px)*deceleration;
            py += (yMouse - py)*deceleration;
        }
        x = Math.round(px);
        y = Math.round(py);
        showPanel();
    }
    private function showPanel():void {
        var a:Number = 1/step;
        if (_visible) {
            visible = true;
            alpha += a;
            if (alpha >= 1) alpha = 1;
            update(_type);
        } else {
            alpha -= a;
            if (alpha <= 0) {
                alpha = 0;
                visible = false;
            }
        }
    }
    public function show(t:String, tp:String):void {
        txt.text = t;
        _visible = true;
        setType(tp);
    }
    public function hide():void {
        _visible = false;
    }
    private function setType(param:String):void {
        switch (param) {
            case typeL :
            case typeC :
            case typeR :
                _type = param;
                break;
            default :
                _type = typeC;
                break;
        }
    }
    private function createBox(target:Shape, x:int, y:int, w:uint, h:uint, c:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.drawRoundRect(x, y, w, h, c*2);
        target.graphics.endFill();
    }
    private function createArrow(target:Shape, w:uint, h:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.moveTo(0, 0);
        target.graphics.lineTo(-w*0.5, 0);
        target.graphics.lineTo(0, h);
        target.graphics.lineTo(w*0.5, 0);
        target.graphics.lineTo(0, 0);
        target.graphics.endFill();
    }
    private function createBase(target:Shape, x:int, y:int, w:uint, h:uint):void {
        target.graphics.beginFill(cColor);
        target.graphics.drawRect(x, y, w, h);
        target.graphics.endFill();
    }

}


//////////////////////////////////////////////////
// DataProviderクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.system.Security;
import flash.events.Event;

class DataProvider extends Sprite {
    private var loader:TextLoader;
    private var dataXML:XML;
    public var data:Object;
    private var sList:Array;
    private var tList:Array;
    private var mList:Array;
    private var pList:Array;
    private var oList:Array;
    private var iList:Array;
    private static var reqURL:String = "http://api.flickr.com/services/rest/";
    private static var method:String = "method=flickr.photos.search";
    private static var key:String = "7a4e79f1dbe398cd714ecd6dbe6a9cbb";
    private var flickrURL:String;
    public static const COMPLETE:String = Event.COMPLETE;
    public static const ERROR:String = "error";

    public function DataProvider() {
        Security.loadPolicyFile("http://api.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm1.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm4.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm5.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm6.static.flickr.com/crossdomain.xml");
        Security.loadPolicyFile("http://farm7.static.flickr.com/crossdomain.xml");
        init();
    }

    private function init():void {
        sList = new Array();
        tList = new Array();
        mList = new Array();
        pList = new Array();
        oList = new Array();
        iList = new Array();
        loader = new TextLoader();
        loader.addEventListener(TextLoader.COMPLETE, complete, false, 0, true);
    }
    public function search(tags:String, n:uint):void {
        var option:String = "&license=1,2,3,4,5,6&sort=interestingness-desc&per_page=" + n;
        flickrURL = reqURL + "?" + method +"&api_key=" + key + "&tags=" + tags + option;
        try {
            loader.load(flickrURL);
        } catch (err:Error) {
            dispatchEvent(new Event(DataProvider.ERROR));
        }
    }
    private function complete(evt:Event):void {
        var xml:XML = new XML(evt.target.data);
        xml.ignoreWhite = true;
        for (var n:uint = 0; n < xml.photos.photo.length(); n++) {
            var element:XML = xml.photos.photo[n];
            var farm:String = element.@farm;
            var server:String = element.@server;
            var id:String = element.@id;
            var secret:String = element.@secret;
            var title:String = element.@title;
            var owner:String = element.@owner;
            var basePath:String = "http://farm" + farm + ".static.flickr.com/" + server +"/" + id + "_" + secret;
            sList[n] = basePath + "_s.jpg";
            tList[n] = basePath + "_t.jpg";
            mList[n] = basePath + "_m.jpg";
            pList[n] = basePath + ".jpg";
            oList[n] = basePath + "_o.jpg";
            iList[n] = {title: title, owner: owner};
        }
        data = {s: sList, t: tList, m: mList, p: pList, o: oList, i:iList};
        dispatchEvent(new Event(DataProvider.COMPLETE));
    }

}


//////////////////////////////////////////////////
// TextLoaderクラス
//////////////////////////////////////////////////

import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;

class TextLoader extends EventDispatcher {
    private var loader:URLLoader;
    private var _data:*;
    public static const TEXT:String = URLLoaderDataFormat.TEXT;
    public static const BINARY:String = URLLoaderDataFormat.BINARY;
    public static const COMPLETE:String = "complete";

    public function TextLoader() {
        loader = new URLLoader();
    }

    public function load(file:String, format:String = TextLoader.TEXT):void {
        loader.dataFormat = format;
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            loader.load(new URLRequest(file));
        } catch (err:Error) {
            trace(err.message);
        }
    }
    private function ioerror(evt:IOErrorEvent):void {
        trace(evt.text);
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        trace(evt.status);
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        trace(evt.text);
    }
    private function complete(evt:Event):void {
        loader.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        loader.removeEventListener(Event.COMPLETE, complete);
        _data = evt.target.data;
        dispatchEvent(new Event(TextLoader.COMPLETE));
    }
    public function get data():* {
        return _data;
    }

}


//////////////////////////////////////////////////
// Btnクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;

class Btn extends Sprite {
    public var id:uint;
    private var shade:Shape;
    private var bottom:Shape;
    private var light:Shape;
    private var base:Shape;
    private var txt:TextField;
    private var label:String = "";
    private static var fontType:String = "Myriad Pro Semibold";
    private var _width:uint = 60;
    private static var _height:uint = 19;
    private static var corner:uint = 5;
    private var type:uint = 1;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var upColor:uint = 0x666666;
    private static var overColor:uint = 0x333333;
    private static var offColor:uint = 0x999999;
    private static var gColor:uint = 0x0099FF;
    private var blueGlow:GlowFilter;
    private var shadeGlow:GlowFilter;
    private var _clicked:Boolean = false;
    private var _enabled:Boolean = true;

    public function Btn() {
    }

    public function init(option:Object):void {
        if (option.id != undefined) id = option.id;
        if (option.label != undefined) label = option.label;
        if (option.width != undefined) _width = option.width;
        if (option.type != undefined) type = option.type;
        draw();
    }
    private function draw():void {
        switch (type) {
        case 1 :
            bColor = 0xFFFFFF;
            sColor = 0x000000;
            upColor = 0x666666;
            overColor = 0x333333;
            offColor = 0x999999;
            break;
        case 2 :
            bColor = 0x000000;
            sColor = 0xFFFFFF;
            upColor = 0x666666;
            overColor = 0x999999;
            offColor = 0x333333;
            break;
        }
        blueGlow = new GlowFilter(gColor, 0.6, 5, 5, 2, 3, false, true);
        shadeGlow = new GlowFilter(sColor, 0.3, 4, 4, 2, 3, false, true);
        shade = new Shape();
        bottom = new Shape();
        light = new Shape();
        base = new Shape();
        txt = new TextField();
        addChild(shade);
        addChild(bottom);
        addChild(light);
        addChild(base);
        addChild(txt);
        createBase(shade, _width, _height, corner, sColor);
        shade.filters = [shadeGlow];
        createBase(bottom, _width, _height, corner, sColor, 0.3);
        createBase(light, _width, _height, corner, gColor);
        light.filters = [blueGlow];
        createBase(base, _width, _height, corner, bColor);
        txt.x = -_width*0.5;
        txt.y = -_height*0.5;
        txt.width = _width;
        txt.height = _height - 1;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        txt.embedFonts = true;
        txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = 14;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
        txt.text = label;
        enabled = true;
        mouseChildren = false;
    }
    private function rollOver(evt:MouseEvent):void {
        _over();
    }
    private function rollOut(evt:MouseEvent):void {
        _up();
    }
    private function press(evt:MouseEvent):void {
        _down();
    }
    private function release(evt:MouseEvent):void {
        _up();
    }
    private function click(evt:MouseEvent):void {
    }
    private function _up():void {
        txt.y = -_height*0.5;
        txt.textColor = upColor;
        base.y = -1;
        light.visible = false;
        light.y = -1;
    }
    private function _over():void {
        txt.y = -_height*0.5;
        txt.textColor = overColor;
        base.y = -1;
        light.visible = true;
        light.y = -1;
    }
    private function _down():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = overColor;
        base.y = 0;
        light.visible = true;
        light.y = 0;
    }
    private function _off():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = offColor;
        base.y = 0;
        light.visible = false;
        light.y = 0;
    }
    public function get clicked():Boolean {
        return _clicked;
    }
    public function set clicked(param:Boolean):void {
        _clicked = param;
        enabled = !_clicked;
        if (_clicked) {
            _down();
        } else {
            _up();
        }
    }
    public function get enabled():Boolean {
        return _enabled;
    }
    public function set enabled(param:Boolean):void {
        _enabled = param;
        buttonMode = _enabled;
        mouseEnabled = _enabled;
        useHandCursor = _enabled;
        if (_enabled) {
            _up();
            addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
            addEventListener(MouseEvent.CLICK, click, false, 0, true);
        } else {
            _off();
            removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            removeEventListener(MouseEvent.MOUSE_DOWN, press);
            removeEventListener(MouseEvent.MOUSE_UP, release);
            removeEventListener(MouseEvent.CLICK, click);
        }
    }
    private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
        target.graphics.beginFill(color, alpha);
        target.graphics.drawRoundRect(-w*0.5, -h*0.5, w, h, c*2);
        target.graphics.endFill();
    }

}