function zTweet( io_data ) {
  this.mo_data = io_data;
  /*
    created_at = "Fri..."
    from_user = tweet from "username"
    from_user_id = tweet from user id
    id = tweet id
    iso_language_code = "en"
    profile_image_url = "url"
    source = "Tweetie with < > " etc"
    text = "text of the tweet"
    to_user = @ "username"
    to_user_id = @ user id
  */
}
zTweet.create = function( io_data ) { return new zTweet( io_data ); };
zTweet.prototype.equals = function( io_tweet ) {
  return ( this.mo_data.id == io_tweet.mo_data.id );
};

zTweet.prototype.getId = function() { return this.mo_data.id; }
zTweet.prototype.getFromUserName = function() { return this.mo_data.from_user; }
zTweet.prototype.getFromUserId = function() { return this.mo_data.from_user_id; }
zTweet.prototype.getToUserName = function() { return this.mo_data.to_user; }
zTweet.prototype.getToUserId = function() { return this.mo_data.to_user_id; }
zTweet.prototype.getText = function() { return this.mo_data.text; }
zTweet.prototype.getProfileImageURL = function() { return this.mo_data.profile_image_url; }
zTweet.prototype.getFromUserImageURL = function() { return this.mo_data.profile_image_url; }
zTweet.prototype.getFromUserFeedURL = function() { return 'http://twitter.com/' + this.getFromUserName(); }
zTweet.prototype.getToUserFeedURL = function() { return 'http://twitter.com/' + this.getToUserName(); }

zTweet.prototype.getCreatedDate = function() { return new Date( this.mo_data.created_at ); };  
zTweet.prototype.getTimeSinceCreated = function() {
  
  var lv_difference = (new Date()).getTime() - this.getCreatedDate().getTime();
  var lv_days = Math.floor(lv_difference/1000/60/60/24);
  lv_difference -= lv_days * 1000 * 60 * 60 * 24;
  var lv_hours = Math.floor(lv_difference/1000/60/60);
  lv_difference -= lv_hours * 1000 * 60 * 60;
  var lv_minutes = Math.floor(lv_difference/1000/60);
  lv_difference -= lv_minutes*1000*60
  var lv_seconds = Math.floor(lv_difference/1000);
  
  return { days: lv_days, hours: lv_hours, minutes: lv_minutes, seconds: lv_seconds };
};
zTweet.prototype.getTimeSinceText = function() {
  var lo_timeSince = this.getTimeSinceCreated();
  var ls_time = '';
  if( lo_timeSince.days == 1 ) { ls_time += lo_timeSince.days + ' day '; }
  else if( lo_timeSince.days > 1 ) { ls_time += lo_timeSince.days + ' days '; }
  
  if( lo_timeSince.hours == 1 ) { ls_time += lo_timeSince.hours + ' hour '; }
  else if( lo_timeSince.hours > 1 ) { ls_time += lo_timeSince.hours + ' hours '; }
  else if( lo_timeSince.hours < 1 ) {
    if( lo_timeSince.minutes > 0 ) { ls_time += lo_timeSince.minutes + ' minutes '; }
    if( lo_timeSince.minutes < 1 && lo_timeSince.seconds > 0 ) { ls_time += lo_timeSince.seconds + ' seconds '; }
  }

  ls_time += 'ago';
  return ls_time;
};


function zTwitterSearcher( ) { }

zTwitterSearcher.ms_twitterSearchURL = 'http://search.twitter.com/search.json';

zTwitterSearcher.searchCallBack = function( ia_searchResults, if_callback ) {
  var la_tweets = new Array();
  var lo_tweet = null;
  var ls_lastId = null;
  
  for( var i = 0; i < ia_searchResults.length; i++ ) {
    lo_tweet = new zTweet.create( ia_searchResults[i] );
    la_tweets.push( lo_tweet );
    if( ls_lastId == null ) { ls_lastId = lo_tweet.getId(); }
  }
  
  if_callback( { results: la_tweets, 
                 lastId: ls_lastId } );    
};

zTwitterSearcher.search = function( io_args, if_callback ) {
  // ( { query, maxResults, sinceId } )

  var ls_url = zTwitterSearcher.ms_twitterSearchURL + '?callback=?';
  
  for( var arg in io_args ) {
    if( io_args[arg] == null ) { continue; }
    switch( arg ) {
      case 'maxResults': ls_url += '&rpp=' + io_args[arg]; break;
      case 'sinceID': ls_url += '&since_id=' + io_args[arg]; break;
      case 'query': ls_url += '&q=' + io_args[arg]; break;
    }
  }
  
  var lf_callback = function( io_data ) {
    zTwitterSearcher.searchCallBack( io_data.results, if_callback );
  }
  
	jQuery.getJSON(	ls_url, lf_callback );	
}


function zTwitterSearchFeed( is_query ) {
  this.ms_query = is_query;
  this.mo_tweets = new zArray();
  this.NewTweet = new zEvent();
  this.SearchFinished = new zEvent();
  this.ms_lastId = null;
  this.ms_maxResults = 20;
  this.mn_intervalHandle = null;
  this.mn_intervalTime = null;
  this.mb_searchOnInterval = false;
  this.SearchFinished.subscribe( this.onSearchFinished, this );  
}

zTwitterSearchFeed.prototype.startSearch = function( in_milliseconds, ib_timerCall ){
  var lo_this = this;
  this.mb_searchOnInterval = true;
  this.mn_intervalTime = in_milliseconds;
  if( ib_timerCall == null ) { lo_this.search(); }
  this.mn_intervalHandle = setTimeout( function() { lo_this.search(); }, in_milliseconds );
};

zTwitterSearchFeed.prototype.stopSearch = function() {
  this.mb_searchOnInterval = false;
  this.mn_intervalHandle = stopTimeout( this.mn_intervalHandle );
};

zTwitterSearchFeed.prototype.onSearchFinished = function( io_args, io_sender ) {
  if( this.mb_searchOnInterval ) {
    this.startSearch( this.mn_intervalTime, true );
  }
};

zTwitterSearchFeed.prototype.search = function( if_callback ) {

  var lo_this = this;
  var lf_callback = function( io_searchResults ) {
    
    if( io_searchResults.results == null ) { alert('Outta tweets'); return; }
    
    var la_resultTweets = io_searchResults.results;
    lo_this.ms_lastId = io_searchResults.lastId;
    
    for( var i = la_resultTweets.length - 1; i >= 0; i-- ) {
      lo_this.addTweet( la_resultTweets[i] );
    }
    
    lo_this.SearchFinished.fire();
    
  }
  
  zTwitterSearcher.search( { query: this.ms_query,
                             maxResults: this.mn_maxResults,
                             sinceId: this.ms_lastId },
                             lf_callback );
}

zTwitterSearchFeed.prototype.addTweet = function( io_tweet ) {
  if( io_tweet != null && !this.mo_tweets.contains( io_tweet ) ) {
    this.mo_tweets.push( io_tweet );
    this.NewTweet.fire( { tweet: io_tweet } );
  }
}


function zTwitterFormatter( io_formatter ) {
  this.mo_formatter = io_formatter;
}
zTwitterFormatter.prototype.format = function( is_string ) {
  is_string = is_string.replace(/@(\w+)/gi, '@<a class="tweetMention" href="http://twitter.com/$1">$1</a>');
  is_string = is_string.replace(/#(\w+)/gi, '<a class="tweetHashtag" href="http://search.twitter.com/search?q=&tag=$1">#$1</a>');

  if( this.mo_formatter != null ) {
    return this.mo_formatter.format(is_string);	
  } else { 
    return is_string;
  }
}

