/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

(function() {

var initializing = false,
    fnTest = /xyz/.test(function(){xyz;}) ? /\b__super\b/ : /.*/,
    Class = function(){},
    window = this;

Class.extend = function(prop) {
    var __super = this.prototype;
    initializing = true;
    var proto = new this();
    initializing = false;
    for (var name in prop) {
        if (name) {
            proto[name] = typeof prop[name] == "function" &&
                typeof __super[name] == "function" && fnTest.test(prop[name]) ?
                (function(name, fn) {
                    return function() {
                        var tmp = this.__super;
                        this.__super = __super[name];
                        var ret = fn.apply(this, arguments);
                        this.__super = tmp;
                        return ret;
                    };
                })(name, prop[name]) : prop[name];
        }
    }

    function Class() {
        if ( !initializing && this.__constructor ) {
            this.__constructor.apply(this, arguments);
        }
    }
    Class.prototype = proto;
    Class.constructor = Class;
    Class.extend = arguments.callee;
    return Class;
};

var G = window.TweeterReader = Class.extend({
    __constructor : function(options) {
        this.options = this.mix({
            username: null,
            target: null,
            count: 10,
            cssClass: 'tweetClass',
            profile_image: false,
            ticker: false,
            effect_opt: "fade"

        }, options);        
    },
    getTweets : function() {        
         var url = "http://twitter.com/status/user_timeline/" + this.options.username + ".json?count=" + this.options.count + "&callback=?"
         var t = this.options.target;
         var css = this.options.cssClass;
         var profileimage = this.options.profile_image;
         var tic = this.options.ticker;
         var effect_opt = this.options.effect_opt;
         var style = 'style="display:block;"';


         $("#" + t).hide();
         jQuery.getJSON(url, function(data) {                        
			$(data).each(function(index){

                             //alert(data[index].text);
                             var textUrl = data[index].text.replace(/(.*)(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/g, '<a href=$2>$1</a>');
                             //var textUrl = data[index].text.replace(/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/g, '');
                             var html = '<div class="' + css + '" ' + style + '>';
                             if(profileimage)
                                 html = html + '<img alt="profilo" src="'+ data[index].user.profile_image_url+'"/>'
                            $('#'+t).append(html + textUrl +'</div>')                           
                            if(tic && effect_opt != 'slide')
                                style = 'style="display:none;"';
                        })
                        if(tic)
                          $('#' + t).newsTicker({childDivClass: css, effect: effect_opt});

		});
                $("#" + t).show();
    },
    mix : function( obj, ext ) {
        return jQuery.extend(obj, ext);
    }
});

})();