var TwitterFeed_Library = new XUI_TwitterFeedLibrary();

function XUI_TwitterFeedLibrary()
{
	// Global/(Default) Settings/Method Definitions 
	this.RemoveTwitterFeed = RemoveTwitterFeed;
	
	// Library-Specific	
	this.library_name;
	this.twitter_feeds = [];
	this.twitter_feed_groups = [];
	
	// Methods
	function RemoveTwitterFeed(name)
	{
		// Remove A Twitter Feed From The Library
		this.twitter_feeds[name] = null;
	}		
	// End Methods
}

function XUI_TwitterFeedGroup(group_name, container, account_names, amount_posts)
{
	// Inherit From/Assign To XUI Framework
	XUI_Framework.call(this, name);
	
	// Feed Group-Specific
	this.name = group_name;
	this.container = container;
	this.accounts = account_names;
	this.amount_posts = amount_posts;
	
	// Add This XUI Object To The Global Framework Collection
	XUI.AddObject(this.name, this);
	
	// Add The Feed To The Feed Library
	TwitterFeed_Library.twitter_feed_groups.push(this);
	
	// Expand The Feed Group Object's Scope
	var self = this;
	
	for (i = 0; i < this.accounts.length; i++)
	{		
		var ihtml_output = new XUI_TwitterFeed(this.accounts[i], this.amount_posts, this.container, this.container);
		//console.log(ihtml_output);
	}
	
	$("#" + this.container).html(ihtml_output);
}

function XUI_TwitterFeed(account_name, amount_posts, container, group_name)
{		
	// Inherit From/Assign To XUI Framework
	XUI_Framework.call(this, account_name);
	
	// Feed-Specific
	this.name = account_name;
	this.container = container;
	this.account_name = account_name;
	this.amount_posts = amount_posts;
	this.group_name = group_name;
	this.html_output = "";
	
	// Add This XUI Object To The Global Framework Collection
	XUI.AddObject(this.name, this);
	
	// Add The Feed To The Feed Library
	TwitterFeed_Library.twitter_feeds.push(this);
	
	// Expand The Feed Object's Scope
	var self = this;
	
	$.jTwitter(self.account_name, self.amount_posts, function(posts)
		{
			//console.log(posts);
						
			var html_output = "";
			
			var session_date = new Date();
			var session_id = session_date.getTime();
			
			var amount_feeds = session_id; /* TwitterFeed_Library.twitter_feeds.length;*/
			var tweet_feed_wrapper_class = "tweet_feed_zero_wrapper";
			
			if(amount_feeds > 0)
			{
				var tweet_feed_wrapper_class = "tweet_feed_wrapper";
			}
						
			var html_output = '<div id="tweet_feed' + (amount_feeds + 1) + '" class="' + tweet_feed_wrapper_class + '">';
			html_output += '<div id="tweet_feed' + (amount_feeds + 1) + '_avatar" class="tweet_feed_avatar">';
            html_output += '<a href="http://www.twitter.com/' + posts[0].user.screen_name + '"><img src="' + posts[0].user.profile_image_url + '" /></a>';
            html_output += '</div>';
			html_output += '<div id="tweet_feed' + (amount_feeds + 1) + '_details" class="tweet_feed_details">';
			html_output += '<h3><a href="http://www.twitter.com/' + posts[0].user.screen_name + '">' + posts[0].user.screen_name + '</a></h3><p>' + posts[0].user.followers_count + ' follower(s)</p>';
			html_output += '</div>';
            html_output += '<div class="clear"></div>';
            html_output += '<div id="tweet_feed' + (amount_feeds + 1) + '_text" class="tweet_feed_text">';
                    				
			// Render Each Tweet
			$.each(posts, function(i, post)
				{
					html_output += "<p>" + post.text + "</p>";
				}
			);
			
			html_output += '</div>';
			html_output += '</div>';

			//if (self.group_name)
			//{
				//xhtml_output = '<div id="' + self.group_name + '">' + html_output + '</div>';
			//}		
				
			self.html_output = html_output;
			
			//if (self.group_name)
			//{
			//	return html_output;
			//}
			//else
			//{
			$("#" + self.container).html(html_output);
			//}
		}
	);
}
