var EmbedTweet =
{
  // CONFIGURATION
  enableDebugging: false,
  automaticEmbedding: true, // set to false to disable automatic embedding and use embedClass to manually embed

  // DONT CHANGE THIS
  objRegex: /http(s)?:\/\/twitter\.com\/([a-z0-9]+)\/status(es)?\/([0-9]+)/i, // Regex to find twitter status links
  doEmbedClass: 'EmbedTweet',
  dontEmbedClass: 'dontEmbedTweet',
  
  // FUNCTION - debug
  // Outputs debugging messages
  debug: function(msg)
  {
    if(this.enableDebugging) console.log(msg);
  },
  
  // FUNCTION - handleTweet
  // Every anchor that should me transformed into an embedded Tweet is passed to this function
  handleTweet: function(link)
  {
    if(this.objRegex.test(link.href))
  	{
      // @todo: Transfer classes on the anchor to the generated embedtweet div

    	// get the tweet status id used by Twitter to identify tweets
    	temp = link.href.split(/(\/)/);
    	id = temp[ temp.length - 1 ];

    	// include the embedtweet javascript file
    	// which will call contentLoad to embed itself
    	this.includeScript('http://embedtweet.com/embed/' + id);

    	// give the anchor an id, so we'll know where to place the tweet div
    	// @todo: If the anchor already has an ID it is replaced. Perhaps using a classname is more flexible.
    	link.id	= "embedtweet_link_" + id;

    	// hide original link
    	link.hide();
    }
  },

  // FUNCTION - includeScript
  // Includes a javascript file by creating a script-tag.
  includeScript: function(url)
  {
  	var head			= document.getElementsByTagName('head')[0];
  	var script		= document.createElement('script');
  	script.type		= 'text/javascript';
  	script.src		= url;
  	head.appendChild(script);
  },

  // FUNCTION - includeStylesheet
  // Includes a stylesheet by creating a link-tag.
  includeStylesheet: function(url, media)
  {
  	var head		= document.getElementsByTagName('head')[0];
  	var link		= document.createElement('link');
  	link.type		= 'text/css';
  	link.href		= url;
  	link.media	= media;
  	link.rel		= 'stylesheet';
  	head.appendChild(link);
  },

  // FUNCTION - contentLoaded
  // This function is called by the embedtweet js file.
  // It inserts the generated html where the anchor used to be.
  contentLoaded: function(id, tweet_html)
  {
  	Element.insert($("embedtweet_link_" + id), { after: tweet_html });
  },
  
  // FUNCTION - init
  // Sets everything up
  init: function()
  {
    EmbedTweet.debug('EmbedTweet javascript library loaded.');

    // Check for Prototype
    if(window.Prototype && window.Prototype.Version)
    {
      EmbedTweet.debug('Prototype loaded!');

    	// Include stylesheet
    	// @todo: Include print stylesheet
    	EmbedTweet.includeStylesheet('http://embedtweet.com/stylesheets/embed_v1.css', 'screen');

    	// When the page is loaded, start
    	Event.observe(window, 'load', function() { EmbedTweet.start(); } );
    }
    else
    {
      EmbedTweet.debug('ERROR: Prototype not loaded!');
    }
  },
  
  // FUNCTION - start
  // Loops through all the anchors, checks if it should be transformed and does if necessary
  start: function()
  {
    // Converts all anchors linking to tweets to 'embedded tweets'
    // Links with the dontEmbedTweet are skipped
    EmbedTweet.debug('Initializing EmbedTweet');

    var links = new Array();

    if(this.automaticEmbedding)
    {
      // Find all anchors, except with the ones with dontEmbedClass
      links = $$('a:not(.'+this.dontEmbedClass+')');
    }
    else
    {
      // No automatic embedding, only embed the ones with doEmbedClass
      links = $$('a.'+this.doEmbedClass);
    }            
    // For each anchor check if it links to a tweet and handle it
    //links.invoke.call('EmbedTweet', 'handleTweet');
    for(var i=0; i < links.length; i++)
    {
      EmbedTweet.handleTweet(links[i]);
    }
  }

};

// initiate EmbedTweet
EmbedTweet.init();