//-------------------------------
// Class
//-------------------------------

function RecentActivityView() {
	
	var _self = this;

	//-------------------------------
	// Constants
	//-------------------------------
	
	RecentActivityView.prototype.CONTROLS_CLICK = "refresh_feed";
	
	//-------------------------------
	// Properties
	//-------------------------------
	
	var _view = $j("<ol class='recentActivityView'></ol>");
	var _controls = $j("<ul class='controls'></ul>");
	
	var _position = 0;
	var _urlExp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	var _htmlExp = /<(?:.|\s)*?>/g;
	var _current = "all";
	var _currentIndex = 0;
	
	//-------------------------------
	// Constructor
	//-------------------------------
	
	$j(function() {
		_controls.append("<h3 class='title'>Recent Activity Feed - Powered By: Wells Fargo</h3>");
	});
	
	//-------------------------------
	// Public Methods
	//-------------------------------
	
	_self.controls = function(data) {
		var all = $j("<li class='btnall hover selected'>All</li>");
		_controls.append(all);
		
		$j(all).click(function() {
			_current = "all";
			
			$j("li", _controls).removeClass("selected");
			$j(this).addClass("selected");
			$j("li", _view).hide().fadeIn();
		});
	
		$j(data).each(function(i, item) {
			var li = $j("<li class='btn" + item.com + " hover'>" + item.source + "</li>");
			$j(li).click(function() {
				_current = item.com;
				$j("li", _controls).removeClass("selected");
				$j(this).addClass("selected");
				
				$j(".container", _view).fadeOut(300, function() {
					$j("li", _view).hide().filter(function(index) {
						return $j(this).attr("type") == i;
					}).show();
				}).fadeIn(300);
			});
			
			_controls.append(li);
		});
		
		_controls.append("<div class='clear'></div>");
	}
	
	_self.deploy = function(data) {
		var content	= "<div class='container'>";
		$j(data).each(function(i, item) {
			content += activityContainer(item);
		});
		
		content	+= "</div>";
		$j(_view).append(content);
		
		$j("#recentActivityFeed").append(_controls);
		$j("#recentActivityFeed").append(_view);
		cbb.init();
	}
	
	_self.add = function(item) {
		var content = $j(activityContainer(item));
		$j(".container", _view).prepend(content.hide());
		if(_current == item.com || _current == "all") content.fadeIn(500);
		cbb.init();
	}
	
	_self.remove = function(data) {
		if(_currentIndex > 0) _currentIndex = 0;
		var current = (data.type == "news") ? _currentIndex + 1 : 0;
		_currentIndex++;
		
		
		$j("li[type='" + data.com + "']:last", _view).remove();
		$j("li[type='" + data.com + "']", _view).eq(current).hide().insertAfter(".container li:eq(7)", _view);

	}
	
	//-------------------------------
	// Private Methods
	//-------------------------------
	
	function activityContainer(item) {
		var date = new Date($j(item.content).find("pubDate").text());
		var description = $j(item.content).find("description").text();
		description = description.replace(_htmlExp, "").substring(0, 200);
		//<a href="$1" target="_blank">$1</a>
		
		var content	= "";
		content		+= "<li type='" + item.com + "'><a href='" + item.site + "'><img src='" + item.image + "'/></a><div class='cbb'>";
		content		+= "<em>From " + "<a href='" + item.site + "'>" + item.source + "</a> / " + (date.getMonth() + 1) + "." + date.getDate() + "." + date.getFullYear() + " " + ((date.getHours() > 12) ? date.getHours() - 12 : (date.getHours() == 0) ? 12 : date.getHours()) + ":" + ((date.getMinutes() < 10) ? "0" + date.getMinutes() : date.getMinutes()) + ((date.getHours() > 11) ? " PM" : " AM") + "</em><br/>";
		content		+= "<h2><a href='" + $j(item.content).find("link").text() + "'>" + ((item.source == "Twitter") ? "@pdxtrailblazers" : $j(item.content).find("title").text()) + "</a></h2>";
		content		+= "<p class='description'>" + description.replace(_urlExp, "<a href='$1' target='_blank'>$1</a>") + "..." + "</p>";
		content		+= "<p class='meta'><a href='" + $j(item.content).find("link").text() + "'>Continue Reading</a></p>";
		content		+=  "</div></li>";
		
		return content;
	}

	//-------------------------------
	// Listeners
	//-------------------------------



}
