| 1 | package { |
|---|
| 2 | |
|---|
| 3 | import com.longtailvideo.jwplayer.utils.Stretcher; |
|---|
| 4 | |
|---|
| 5 | import flash.display.*; |
|---|
| 6 | import flash.events.*; |
|---|
| 7 | import flash.filters.DropShadowFilter; |
|---|
| 8 | import flash.net.*; |
|---|
| 9 | import flash.text.*; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** This plugin renders a single related thumb. **/ |
|---|
| 13 | public class RelatedThumb extends Sprite { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** Embedding the image assets. **/ |
|---|
| 17 | [Embed(source="../../assets/glow.png")] |
|---|
| 18 | private const GlowSheet:Class; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /** Background for the entire thumb. **/ |
|---|
| 22 | private var _back:Sprite; |
|---|
| 23 | /** The link to redirect to. **/ |
|---|
| 24 | private var _link:String; |
|---|
| 25 | /** Container that imports the preview thumbnail **/ |
|---|
| 26 | private var _loader:Loader; |
|---|
| 27 | /** Mask for the loader. **/ |
|---|
| 28 | private var _mask:Sprite; |
|---|
| 29 | /** Graphic over the image. **/ |
|---|
| 30 | private var _glow:DisplayObject; |
|---|
| 31 | /** Height of the thumb. **/ |
|---|
| 32 | private var _height:Number; |
|---|
| 33 | /** Overlay between the image and text. **/ |
|---|
| 34 | private var _overlay:Sprite; |
|---|
| 35 | /** Textfield that displays the title. **/ |
|---|
| 36 | private var _field:TextField; |
|---|
| 37 | /** TextField formatting. **/ |
|---|
| 38 | private var _format:TextFormat; |
|---|
| 39 | /** Width of the thumb. **/ |
|---|
| 40 | private var _width:Number; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** Constructor. **/ |
|---|
| 44 | public function RelatedThumb(width:Number,height:Number,image:String,link:String,title:String=null) { |
|---|
| 45 | _width = width; |
|---|
| 46 | _height = height; |
|---|
| 47 | _link = link; |
|---|
| 48 | _back = new Sprite(); |
|---|
| 49 | _back.graphics.beginFill(0,1); |
|---|
| 50 | _back.graphics.drawRect(0,0,width,height); |
|---|
| 51 | _back.filters = new Array(new DropShadowFilter(0)); |
|---|
| 52 | addChild(_back); |
|---|
| 53 | _loader = new Loader(); |
|---|
| 54 | _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,_loaderHandler); |
|---|
| 55 | _loader.load(new URLRequest(image)); |
|---|
| 56 | addChild(_loader); |
|---|
| 57 | _mask = new Sprite(); |
|---|
| 58 | _mask.graphics.beginFill(0,1); |
|---|
| 59 | _mask.graphics.drawRect(1,1,width-2,height-2); |
|---|
| 60 | addChild(_mask); |
|---|
| 61 | _loader.mask = _mask; |
|---|
| 62 | _glow = new GlowSheet(); |
|---|
| 63 | _glow.x = 1; |
|---|
| 64 | _glow.y = 1; |
|---|
| 65 | _glow.width = width-2; |
|---|
| 66 | _glow.scaleY = _glow.scaleX; |
|---|
| 67 | addChild(_glow); |
|---|
| 68 | if(title) { |
|---|
| 69 | _overlay = new Sprite(); |
|---|
| 70 | _overlay.graphics.beginFill(0,0.8); |
|---|
| 71 | _overlay.graphics.drawRect(1,height-27,width-2,26); |
|---|
| 72 | addChild(_overlay); |
|---|
| 73 | _field = new TextField(); |
|---|
| 74 | _field.height = 20; |
|---|
| 75 | _field.width = width-10; |
|---|
| 76 | _field.x = 5; |
|---|
| 77 | _field.y = height-22; |
|---|
| 78 | _format = new TextFormat('Arial', 12, 0xFFFFFF); |
|---|
| 79 | _format.align = 'center'; |
|---|
| 80 | _field.defaultTextFormat = _format; |
|---|
| 81 | _field.selectable = false; |
|---|
| 82 | _field.text = title; |
|---|
| 83 | addChild(_field); |
|---|
| 84 | } |
|---|
| 85 | buttonMode = true; |
|---|
| 86 | mouseChildren = false; |
|---|
| 87 | addEventListener(MouseEvent.CLICK,_clickHandler); |
|---|
| 88 | addEventListener(MouseEvent.MOUSE_OUT,_outHandler); |
|---|
| 89 | addEventListener(MouseEvent.MOUSE_OVER,_overHandler); |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** Redirect on thumb click. **/ |
|---|
| 94 | private function _clickHandler(event:MouseEvent):void { |
|---|
| 95 | navigateToURL(new URLRequest(_link),'_top'); |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Fade the image when loaded. **/ |
|---|
| 100 | private function _loaderHandler(event:Event):void { |
|---|
| 101 | try { |
|---|
| 102 | Bitmap(_loader.content).smoothing = true; |
|---|
| 103 | } catch(e:Error) {} |
|---|
| 104 | Stretcher.stretch(_loader,_width,_height,Stretcher.FILL); |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** Redirect on thumb click. **/ |
|---|
| 109 | private function _outHandler(event:MouseEvent):void { |
|---|
| 110 | _back.graphics.clear(); |
|---|
| 111 | _back.graphics.beginFill(0,1); |
|---|
| 112 | _back.graphics.drawRect(0,0,_width,_height); |
|---|
| 113 | _back.filters = new Array(new DropShadowFilter(0)); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Redirect on thumb click. **/ |
|---|
| 118 | private function _overHandler(event:MouseEvent):void { |
|---|
| 119 | _back.graphics.clear(); |
|---|
| 120 | _back.graphics.beginFill(0xFFFFFF,1); |
|---|
| 121 | _back.graphics.drawRect(0,0,_width,_height); |
|---|
| 122 | _back.filters = new Array(new DropShadowFilter(0,45,0xFFFFFF)); |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | } |
|---|