/**
 * **********************************************************************
 * **********************************************************************
 * Tweets import via Twitter Search API Method
 * search: Returns tweets that match a specified query.
 * http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search
 *
 *
 *
 * @author stefan kloiber, zero+ones gmbh
 * @date 2009-08
 *
 * @version 0.0.5
 *
 * @dependencies: jQuery 1.3.2
 *
 * @tested Firefox 3, IE 6|7|8, Chrome 2, Safari 4
 * **********************************************************************
 * **********************************************************************
 */


/* define namespace */
var zo; // declares a global symbol
if (!zo) zo = {};
    else if (typeof zo != 'object')
        throw new Error('zo exists, but is not an object!');


zo.twitterImport =
{
    conf: {
        // default values, do not change
        twFormat: 'JSON', // the response will use the JSONP format with a callback
        twUsername: '', // get tweets from a specified username
        twLang: '', // restricts tweets to the given language, given by an ISO 639-1 code
        twRpp: 0 // number of tweets to return per page
    },

    /**
     * initial call
     *
     * @attribute   (Array)     newsStream     Array with maps containing the ticker content
     */
    init: function(options)
    {
        var conf = this.conf;
        conf.twFormat = options.format;
        conf.twUsername = options.username;
        conf.twLang = options.lang;
        conf.twRpp = options.rpp;
        this.importTweets();
    },

    // import tweets using the JSONP format with an callback
    importTweets: function()
    {
        var conf = this.conf;
        if (conf.twFormat.toUpperCase() === "JSON") {
            jQuery.getJSON('http://search.twitter.com/search.json?q=from%3A' +conf.twUsername+ '&rpp=' +conf.twRpp+ '&callback=?',
                function(data) {
                    zo.twitterImport.buildTweetShow(data);
                }
            );
        }
    },
    
    buildTweetShow: function(data)
    {
        var conf = this.conf,
            url = /(\w+):\/\/([\w.]+)\/(\S*)/g;
        jQuery.each(data.results, function(i) {
            var twDate = new Date(this.created_at),
                twFullDate = twDate.toLocaleDateString(),
                twShowDate = twFullDate.substring(twFullDate.indexOf(',')+2),
                twText = this.text,
                twUrl = twText.match(url), // parse urls
                twLink
            ;
            if (twUrl !== null) {
                for (var i=0, l=twUrl.length; i<l; i++) {
                    // build anchor with the parsed url
                    twLink = " <a href=\"" +twUrl[i]+ "\" target=\"_blank\">" +twUrl[i]+ "</a>";
                    // replace the url-string with the built anchor
                    twText = twText.replace(twUrl[i], twLink);
                }
            }
            // create tweet container with date and text
            jQuery('<div style=font-size:11px;></div>')
                .addClass('zoTweet')
                .append(jQuery('<p></p>')
                    .addClass('twDate')
                    .text(twShowDate)
                )
                .append(jQuery('<p></p>')
                    .addClass('twText')
                    .html(twText)
                )
                .appendTo(jQuery('div#zoTweets'))
            ;
        });
    }
};