// Add view to the application.
window.application.addView((function( $, application ){
    
	// ViewProfile view class
	function HomePage(){
		this.REGEX_LOOSE_URL = null;
		this.REGEX_TWITTER_USERNAME = null;
		this.REGEX_TWITTER_HASHTAG = null;
		this.loadURL = null;
		this.content = null;
		this.container = null;
	}
	
	// intialize the view with the init prototype
	HomePage.prototype.init = function(){
		// initialize properties
		this.REGEX_LOOSE_URL = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g;
		this.REGEX_TWITTER_USERNAME = /[@]+[A-Za-z0-9-_]+/g;
		this.REGEX_TWITTER_HASHTAG = /[#]+[A-Za-z0-9-_]+/g;
		this.loadURL = 'index.htm #content';
		this.content = $('#contentWrapper');
		this.container = '#twitterFeed .list';
	};

	// show view
	HomePage.prototype.showView = function(){
		var self = this;
		// make sure we have the correct content
		self.content.load(self.loadURL, function(){
			application.getModel('TwitterFeed').getFeed(function(data){
				self.parseTwitter(data);
				//alert(self.container.html());
			});
		}).show();
	};
	
	// populate data
	HomePage.prototype.parseTwitter = function(json){
		var self = this;
		var htm = "<ul>";
		for (var i=0;i<json.length;i++){
			var ent = json[i].text;
			ent = ent.replace(self.REGEX_LOOSE_URL, function(url){
            	return url.link(url);
        	});
			ent = ent.replace(self.REGEX_TWITTER_USERNAME, function(name){
            	return name.link('http://twitter.com/'+name);
        	});
			ent = ent.replace(self.REGEX_TWITTER_HASHTAG, function(hash){
            	return hash.link('http://search.twitter.com/search?q='+hash);
        	});
			htm += "\n<li><span>"+ent+"</span></li>";
		}
		htm += "\n</ul>";
		$(self.container).html(htm);
		//alert(self.container.html());
	};
	
	// hide view
	HomePage.prototype.hideView = function(){
		//this.content.hide();
	};
	
	// return a new view class
	return (new HomePage());
	
})( jQuery, window.application ));
