YUI().use("gallery-yql", "node", "widget", function(Y) {

    var TweetList = function(config) {
        TweetList.superclass.constructor.apply(this, arguments);
    }
    TweetList.NAME = "tweetlist";
    TweetList.ATTRS = {
        _tweets: {
            value: []
        },
        twit: {
            value: null
        }
    };
    Y.extend(TweetList, Y.Widget, {
        formatTweet: function(tweet, xtraClass) {
            var html = [];

            tweet.text = tweet.text.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1" target="_blank">$1<\/a>');
            tweet.text = tweet.text.replace(/@([a-zA-Z0-9_]+)/gi,'<a href="http://twitter.com/$1" target="_blank" class="username">@$1<\/a>');
            tweet.text = tweet.text.replace(/#([a-zA-Z0-9_]+)/gi,'<a href="http://search.twitter.com/search?q=%23$1" target="_blank" class="hashtag">#$1<\/a>');

            html.push('<div class="tweet ' + xtraClass + '">');
            html.push('<a class="twitter-avatar"><img src="');
            html.push(tweet.profile_image_url);
            html.push('" /></a>');
            html.push(tweet.text);
            html.push('</div>');
            return html.join("");
        },
        initializer: function() {
            this.publish("newTweets", {
                defaultFn: Y.bind(this.showTweets, this)
            });
        },
        renderUI: function() {
            var query = new Y.yql(
                "select * from twitter.search where q='from:" + this.get("twit") + "'");
            query.on("query", Y.bind(this.setTweets, this));
        },
        setTweets: function(query) {
            if (query.results) {
                this.set("_tweets", query.results.results);
            }
            this.fire("newTweets");
        },
        showTweets: function() {
            var html = [];
            var tweets = this.get("_tweets");
            var count = tweets.length;

            for (var i=0; i < count; i++) {
                var tweet = tweets[i];
                if (i % 2) {
                    html.push(this.formatTweet(tweet, "even"));
                } else {
                    html.push(this.formatTweet(tweet, "odd"));
                }
            }
            this.get("contentBox").set("innerHTML", html.join(""));
        }

    });
    var FlickrGroup = function(config) {
        FlickrGroup.superclass.constructor.apply(this, arguments);
    }
    FlickrGroup.NAME = "flickrgroup";
    FlickrGroup.ATTRS = {
        _photos: {
            value: []
        }
    };
    Y.extend(FlickrGroup, Y.Widget, {
        initializer: function() {
            this.publish("newPhotos", {
                defaultFn: Y.bind(this.showPhotos, this)
            });
        },
        renderUI: function() {
            var query = new Y.yql(
                "select * from flickr.people.publicphotos(0,5) where " +
                "user_id='33878669@N06' and extras='url_s'");
            query.on("query", Y.bind(this.setPhotos, this));
        },
        setPhotos: function(query) {
            if (query.results) {
                this.set("_photos", query.results.photo);
            }
            this.fire("newPhotos");
        },
        showPhotos: function() {
            var photos = this.get("_photos");
            var count = photos.length;

            var container = Y.Node.create('<ul></ul>');

            for (var i = 0; i < 4; i++) {
                var photo = photos[i];
                var url = photo.url_s;
                var width = photo.width_s / 2;
                var height = photo.height_s / 2;
                container.insert(
                    Y.Node.create(
                        '<li>' +
                        '<img src="' + url + '" width="' + width +
                        '"  height="' + height + '" /></li>'
                    ));
            }
            this.get("contentBox").insert(container);
        }
    });

    new TweetList({
        "contentBox": "#tweets",
        "twit": "rockstar_"
    }).render();
    new FlickrGroup({
        "contentBox": "#flickr"
    }).render();

    /*new Y.yql(
        "SELECT * FROM lastfm.recenttracks WHERE user='rockstar__' AND " +
        "api_key='1b21ef07e3ef3a12197c1d7b7b41b227'").on(
            "query", function(query) {
                var tracks = query.results.lfm.recenttracks.track;
                var count = tracks.length;
                var container = Y.Node.create("<ul></ul>");

                for (var i=0; i < count; i++) {
                    var track = tracks[i];
                    var cover = track.image[1].content;
                    if (cover !== undefined) {
                        container.insert(Y.Node.create(
                            '<li><a href="' + track.url + '"><img src="' +
                            cover + '" />' + track.artist.content + ' - ' +
                            track.name + '</a></li>'));
                    }
                }
                Y.one("#lastfm").insert(container);
            });*/


    Y.one("#posts").get("children").setStyle("display", "none");
    Y.one("#posts").one(".post").setStyle("display", "block");
    var more = Y.Node.create('<div id="more">See more posts &raquo;</div>');
    more.on("click", function(e) {
        Y.one("#posts").get("children").setStyle("display", "block");
        e.currentTarget.setStyle("display", "none");
    });
    Y.one("#posts").appendChild(more);
});

