/**
* Bubble Info Window
* Creates an info bubble that pops up when you click on a pin or a sidebar item.
* A new bubble window is made every time you click on a pin
*
* Called By: MapView.as showAgentBubble() L107
*/

package interfaces {
    import com.greensock.*;
    import com.adobe.crypto.*;
    
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.LoaderContext;
    import flash.text.*;
    
    import libs.*;
    import templates.ComponentBase;
    import vo.Agent;

    public class AgentBubbleInfo extends ComponentBase {
        public var background:Sprite;
        private var image:Loader;
        private var agentName:TextField;
        private var address:TextField;
        private var phone:TextField;
        private var agentSite:TextField;
        private var agentSiteSp:Sprite;
        private var agentFacebook:TextField;
        private var agentFacebookSp:Sprite;
        private var getDirections:TextField;
        private var getDirectionsSp:Sprite;
        private var a:Agent;
        private var facebookId:String;
        
        public function AgentBubbleInfo(a:Agent) {
            super();
            this.a = a;
            initUI();
        }
        
        /**
        * UI
        */
        
        private function initUI():void {
            background = new Sprite();
            image = new Loader();
            agentName = new TextField();
            address = new TextField();
            phone = new TextField();
            agentSite = new TextField();
            agentSiteSp = new Sprite();
            agentFacebook = new TextField();
            agentFacebookSp = new Sprite();
            
            image = new Loader();
            var context:LoaderContext = new LoaderContext();
            context.checkPolicyFile = true;
            image.load(new URLRequest(a.pictureUrl),context);
            image.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void
            {
                var w:Number = image.width;
                var h:Number = image.height;
                var ratio:Number = h/w;
                image.width = 64;
                image.height = 64*ratio;
                var bmp:Bitmap = image.content as Bitmap;
                bmp.smoothing = true;
            });
            image.x = 10;
            var mask:Shape = new Shape();
            mask.x= 10;
            mask.graphics.beginFill(0);
            mask.graphics.drawRect(0,0,64,69)
            addChild(image);
            addChild(mask);
            image.mask = mask;
            
            agentName.defaultTextFormat = new TextFormat('Arial', 14, 0xFFFFFF,true);
            agentName.text = a.name;
            agentName.autoSize = TextFieldAutoSize.LEFT;
            agentName.width = 140;
            agentName.multiline = agentName.wordWrap = true;
            agentName.selectable = false;
            agentName.x = 80; agentName.y = 0;
            addChild(agentName);
            
            address.defaultTextFormat = new TextFormat('Arial', 12, 0xFFFFFF);
            address.width = 140;
            address.autoSize = TextFieldAutoSize.LEFT;
            address.wordWrap = address.multiline = true;
            address.x = 80; 
            address.y = agentName.y + agentName.height;
            address.text = a.address;
            address.selectable = false;
            addChild(address);
            
            phone.defaultTextFormat = new TextFormat('Arial', 12, 0xFFFFFF, true);
            phone.width = 140;
            var backupNumber:String = "";
            for(var i:String in a.phones) {
                if(a.phones[i].type == 'OFFICE') {
                    phone.text = a.phones[i].number;
                    break;
                } else if(a.phones[i].type == 'MOBILE') {
                    phone.text = a.phones[i].number;
                }
            }
            phone.x = 80;
            phone.height = 18;
            phone.selectable = false;
            phone.y = address.y + address.height;
            addChild(phone);
            
            agentSite.defaultTextFormat = new TextFormat('Arial', 12, 0x00abd8);
            agentSite.selectable = false;
            agentSite.width = 140;
            agentSite.autoSize = TextFieldAutoSize.LEFT;
            agentSite.text = 'Website';
            agentSite.mouseEnabled = false;
            
            agentSiteSp.x = 80;
            agentSiteSp.buttonMode = true;
            agentSiteSp.mouseChildren = false;
            agentSiteSp.y = phone.y + phone.height;
            agentSiteSp.addEventListener(MouseEvent.MOUSE_OVER, linkOver);
            agentSiteSp.addEventListener(MouseEvent.MOUSE_OUT, linkOut);
            agentSiteSp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
                navigateToURL(new URLRequest(a.website+'&sourceid=sm58'),'_blank');
            });
            
            agentSiteSp.addChild(agentSite);
            var overlaysp:Shape = new Shape();
            overlaysp.graphics.beginFill(0x0,0);
            overlaysp.graphics.drawRect(0,0,agentSiteSp.width, agentSiteSp.height);
            agentSiteSp.addChild(overlaysp);
            addChild(agentSiteSp);
            
            getDirections = new TextField();
            getDirectionsSp = new Sprite();
            getDirections.defaultTextFormat = new TextFormat('Arial', 12, 0x00abd8);
            getDirections.selectable = false;
            getDirections.width = 140;
            getDirections.autoSize = TextFieldAutoSize.LEFT;
            getDirections.text = 'Get Directions';
            getDirections.mouseEnabled = false;
            
            getDirectionsSp.x = 80;
            getDirectionsSp.y = agentSiteSp.y + agentSiteSp.height;
            getDirectionsSp.mouseEnabled = true;
            getDirectionsSp.buttonMode = true;
            getDirectionsSp.mouseChildren = false;
            getDirectionsSp.addEventListener(MouseEvent.MOUSE_OVER, linkOver);
            getDirectionsSp.addEventListener(MouseEvent.MOUSE_OUT, linkOut);
            getDirectionsSp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
                navigateToURL(new URLRequest('http://maps.google.com/maps?f=d&daddr='+a.address),'_blank');
            });
            
            getDirectionsSp.addChild(getDirections);
            addChild(getDirectionsSp);
            
            // Facebook
            agentFacebook.defaultTextFormat = new TextFormat('Arial', 12, 0x00abd8);
            agentFacebook.selectable = false;
            agentFacebook.width = 140;
            agentFacebook.autoSize = TextFieldAutoSize.LEFT;
            agentFacebook.text = 'Facebook';
            agentFacebook.mouseEnabled = false;
            
            agentFacebookSp.x = 80;
            agentFacebookSp.buttonMode = true;
            agentFacebookSp.mouseChildren = false;
            agentFacebookSp.y = getDirectionsSp.y + getDirectionsSp.height;
            agentFacebookSp.addEventListener(MouseEvent.MOUSE_OVER, linkOver);
            agentFacebookSp.addEventListener(MouseEvent.MOUSE_OUT, linkOut);
            agentFacebookSp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
                navigateToURL(new URLRequest('http://www.facebook.com/profile.php?id='+a.facebookId),'_blank');
            });
            
            agentFacebookSp.addChild(agentFacebook);
            var facebookOverlaysp:Shape = new Shape();
            facebookOverlaysp.graphics.beginFill(0x0,0);
            facebookOverlaysp.graphics.drawRect(0,0,agentFacebookSp.width, agentFacebookSp.height);
            agentFacebookSp.addChild(facebookOverlaysp);
            addChild(agentFacebookSp);
            
            if (a.facebookId == '0' || a.facebookId == 'email paramater required' || a.facebookId.replace(/^\s+|\s+$/g, "").length <= 0) {
                agentFacebookSp.visible = agentFacebook.visible = false;
            }
            
            var backgroundHeight:Number = agentName.textHeight+address.textHeight+phone.textHeight+agentSiteSp.height+getDirectionsSp.height+20;
            if (agentFacebookSp.visible) backgroundHeight += agentFacebookSp.height;
            
            background = fy.config(background, {
                parent: this, width: 140,
                height: backgroundHeight,
                backgroundColor: 0xFF0000, alpha:0
            });
            background.mouseChildren = false;
            this.setChildIndex(agentSiteSp, numChildren-1);
            this.setChildIndex(getDirectionsSp, numChildren-2);
            this.setChildIndex(agentFacebookSp, numChildren-2);
        }
        
        /**
        * Events
        */
        
        // Emulate browser link hover
        private function linkOver(e:MouseEvent):void {
            TweenMax.to( e.target, .5, {tint:0xFFFFFF});
        }
        private function linkOut(e:MouseEvent):void{
            TweenMax.to( e.target, .5, {removeTint:true});
        }
        
    }
}