// http://ejohn.org/blog/simple-javascript-inheritance/
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  // The base Class implementation (does nothing)
  this.Class = function(){};
  
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
    
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
    
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" && 
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
            
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);        
            this._super = tmp;
            
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
    
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    
    // Populate our constructed prototype object
    Class.prototype = prototype;
    
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
    
    return Class;
  };
})();

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};
  
  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :
      
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
        
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
        
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
    
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();

google.load("feeds", "1");

function OnLoad() {
  // Create a feed control
  var feedControl = new google.feeds.FeedControl();

  // Add two feeds.
  feedControl.addFeed("http://feeds.feedburner.com/padicode");
  feedControl.setNumEntries(2);
  // Draw it.
  feedControl.draw(document.getElementById("feed"));
}

google.setOnLoadCallback(OnLoad);

function initialize() {
       
		
		var tweets = new google.feeds.Feed("http://twitter.com/statuses/user_timeline/85670540.rss");
        tweets.setNumEntries(3);
        tweets.load(function(result) {
            if (!result.error) {
                var container = $(".results");
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var entry = result.feed.entries[i];
                    entry.content = format(entry.content.replace(/^padicode:./, ""));
                    container.append($(tmpl("tweet_template", entry)));
                }
            }
        });
    }
 
    function format(str) {
        str = str.replace(/(\w+:\/\/\S+\w)/ig, '<a href="$1" target="_blank">$1</a>');
        str = str.replace(/(\W|^)@(\w+)(?=$|\s|\.|,|:|;|\(|\)|\+|\-|\!|\&|\?)/ig, '$1<a title="$2" href="http://twitter.com/$2" target="_blank">&#64;$2</a>');
        return str;
    }
    google.setOnLoadCallback(initialize);
 
    $(function() {
    $("#row1").find(".tab").click(function() {
  	  var tab = this
  	  $(".tab").removeClass("selected");
  	  $(tab).addClass("selected");
  	  
  	  $(".tab-content").removeClass("active");
  	  $(".tab-content").hide();
  	  
  	  var id = $(tab).attr("id");
  	  $("#"+id+"-content").addClass("active");
  	  $("#"+id+"-content").show();
  	  $("#"+id+"-content .tab-content_body").fadeIn("slow");
  	  
  	  return false;
	 });
  });