<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical"
    creationComplete="init();">

    <mx:HTTPService id="webaroo"
        result="{fetchResult(event);}"
        fault="{handleFault(event);}"
        resultFormat="e4x"/>

    <mx:Panel width="250" height="200" layout="absolute"
            title="Webaroo" cornerRadius="20" alpha="1.0">

            <mx:Text id="microblog" text="Loading..." width="210" height="100"
                horizontalCenter="0" verticalCenter="-20"/>
            <mx:Button id="previousButton" label="Previous" click="stepToPost(-1);"
                horizontalCenter="-67" verticalCenter="59" width="76"/>
            <mx:Button id="nextButton" label="Next" click="stepToPost(+1);"
                horizontalCenter="67" verticalCenter="59" width="76"/>

    </mx:Panel>

    <mx:Script>
        <![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            private var posts:Array = [];
            private var currentPostIndex:int = -1;

            public function init():void
            {
                var webaroo_user:String = Application.application.parameters.name;
                if (webaroo_user == null)
                {
                    microblog.text = "User name not specified.";
                    return;
                }

                webaroo.url = 'http://www.swaroopch.info/files/flex/xml_proxy.php'
                            + '?url=http://sms.webaroo.com/feeds/'
                            + webaroo_user;

                webaroo.send();
            }

            public function fetchResult(event:ResultEvent):void
            {
                for each (var item:XML in event.result.channel.item)
                    posts.unshift(item);

                if (posts.length > 0)
                    stepToPost(posts.length);
            }

            public function handleFault(event:FaultEvent):void
            {
                microblog.text = "Error: " + event.fault.faultString + "\n"
                                           + event.fault.faultDetail;
            }

            public function stepToPost(step:int):void
            {
                if (currentPostIndex+step >= 0 && currentPostIndex+step <= posts.length-1)
                {
                    currentPostIndex += step;
                    microblog.text = getText(posts[currentPostIndex]);
                }
            }

            public function getText(post:XML):String
            {
                return post.title
                       + "\n\n\n"
                       + post.description
                       ;
            }

        ]]>
    </mx:Script>

</mx:Application>
