<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  preinitialize="preInit()"
     xmlns:containers="flexlib.containers.*" height="100%" width="100%" minWidth="75" minHeight="75"
      horizontalScrollPolicy="off" verticalScrollPolicy="off" borderStyle="none"
      backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #FFFFFF]" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.effects.Resize;
            import mx.controls.Alert;
            import mx.collections.Sort;
            import mx.controls.LinkButton;
            import mx.collections.SortField;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            import flash.external.ExternalInterface;
            import mx.events.StyleEvent;
            
            private var tag:LinkButton;
            private var company:String = "";
            private var link:String = "";
            
            private function preInit():void
            {
                var sStyleLoc:String = "";
                var styleEvent:IEventDispatcher;
                if(this.parameters.sStyleLoc != null && this.parameters.sStyleLoc != '')
                {
                    styleEvent = StyleManager.loadStyleDeclarations(sStyleLoc);
                }
                else
                {
                    // Add an IEventDispatcher object and assign it the StyleManager.loadStyleDeclaration()
                    styleEvent = StyleManager.loadStyleDeclarations("styles.swf");
                }
                styleEvent.addEventListener(StyleEvent.COMPLETE, completeHandler);
                styleEvent.addEventListener(StyleEvent.ERROR, faultHandler);
            }            
            private function faultHandler(event:StyleEvent):void
            {
                Alert.show("Error loading style: "+event.errorText, "Style Load Error");
            }    
            private function completeHandler(event:StyleEvent):void
            {
                initApp();
            }
            private function initApp():void
            {
                var cm:ContextMenu = new ContextMenu();
                cm.hideBuiltInItems();
                
                var companyItem:ContextMenuItem = new ContextMenuItem(company);
                companyItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleContextItemClick);                
                cm.customItems.push(companyItem);
                application.contextMenu = cm;
                
                // This allows us to resize the flex container from the javascript
                // @params sHeight: controls the height of the flex app
                // @params sWidth: controls the width of the flex app
                if(this.parameters.sHeight != null && this.parameters.sHeight!='')
                {
                    this.height = this.parameters.sHeight;
                }
                if(this.parameters.sWidth != null && this.parameters.sWidth!='')
                {
                    this.width = this.parameters.sWidth;
                }
                // Set the location of the external xml tag list document
                // @params sXMLLoc: string location of the xml document
                if(this.parameters.sXMLLoc != null && this.parameters.sXMLLoc != '')
                {
                    service.url = this.parameters.sXMLLoc;
                    service.send();
                }
                else
                {
                    // load the test xml file
                    service.send();
                }                
            }
            private function handleContextItemClick(event:ContextMenuEvent):void
            {
                navigateToURL(new URLRequest(link));                
            }
            
            private function httpResultHandler(event:ResultEvent):void
            {
                buildTagCloud(new XML(event.result));
            }
            
            private function httpFaultHandler(event:FaultEvent):void
            {
                Alert.show("Error occurred: "+event.fault.message, "XML Transmission Error");
            }
            
            private function buildTagCloud(xml:XML):void
            {
                var x:XMLList = xml.children();
                var a:ArrayCollection = new ArrayCollection();
                var sort:Sort = new Sort();
                sort.fields=[new SortField("name", true)];
                
                for each(var item:Object in x)
                {
                    var obj:Object = new Object();
                    obj.name = item..@name;
                    obj.weight = item..@weight;
                    obj.loc = item..@loc;
                    obj.font = item..@font;
                    obj.color = item..@color;
                    a.addItem(obj);
                }                
                a.sort = sort;
                a.refresh();                
                for(var i:Number = 0; i<a.length;i++)
                {
                    if(tagContainer.measuredHeight < this.height)
                    {
                        tagContainer.addChild(createLinkButton(a[i].name,a[i].loc, a[i].weight, a[i].color, a[i].font));
                    }
                }    
            }
            
            private function createLinkButton(label:String,url:String, size:Number, tagColor:String, font:String):LinkButton
            {                        
                tag = new LinkButton();
                tag.label = label;    
                
                tag.setStyle("fontSize", size);    
                                        
                tag.setStyle("color", StyleManager.getStyleDeclaration(".Tag"+tagColor).getStyle("color")); 
                tag.setStyle("textRollOverColor", StyleManager.getStyleDeclaration(".Tag"+tagColor).getStyle("textRollOverColor"));
                tag.setStyle("fontFamily", StyleManager.getStyleDeclaration(".Tag"+font).getStyle("fontFamily"));
                tag.toolTip = url;
                tag.addEventListener(MouseEvent.CLICK, tagClickHandler);                
                tag.addEventListener(MouseEvent.MOUSE_OVER, tagOverHandler);
                tag.addEventListener(MouseEvent.MOUSE_OUT, tagOutHandler);
                return tag;
            }
            
            public function tagClickHandler(event:MouseEvent):void
            {
                // Logic to fire the location click event in the external api                
                /* ExternalInterface.call("LoadExtLink", event.currentTarget.toolTip.toString()); */
                navigateToURL(new URLRequest(event.currentTarget.toolTip.toString()));
            }
            private function tagOverHandler(event:MouseEvent):void
            {
                effGlowIn.play([event.currentTarget]);                
            }
            private function tagOutHandler(event:MouseEvent):void
            {
                effGlowOut.play([event.currentTarget]);                
            }
        ]]>
    </mx:Script>
    <mx:Glow id="effGlowIn" duration="150" color="0x0066FF" alphaFrom="0" alphaTo=".25" blurXFrom="0" blurXTo="5" blurYFrom="0" blurYTo="5"/>
    <mx:Glow id="effGlowOut" duration="150" color="0x0066FF" alphaFrom=".25" alphaTo="0" blurXFrom="5" blurXTo="0" blurYFrom="5" blurYTo="0"/>
    
    
    <mx:HTTPService id="service" url="tagTest.xml" resultFormat="e4x" result="httpResultHandler(event)" fault="httpFaultHandler(event)"/>
    <containers:FlowBox id="tagContainer" horizontalScrollPolicy="off" verticalScrollPolicy="off" verticalAlign="bottom"
         horizontalAlign="center" horizontalGap="2" borderStyle="none" verticalGap="0" themeColor="#B6F998" left="0" right="0" top="0" bottom="0"/>
</mx:Application>