/**
 * Recent Activity Feed
 * 
 * REQUIRE jquery.min.js v1.4.2+
 * REQUIRE debug.min.js v0.3+
 *
 * VERSION 0.0.0 by Matthew Toledo on 5/17/2010
 * o script created
 *
 */
 
 
 function activity(iStart,iNumItems) {
	
	// properties
	var sFeedUrl		= '/DC_Activity_Cron/fetch_JSON';
	var iImageIconWidth	= 100;
	var oFeed;
	var iStart;
	var iNumItems;
	
	// getters and setters
	this.setFeedUrl = function (sUrl) { sFeedUrl = sUrl; }
	this.getFeedUrl = function () { return sFeedUrl; }
	this.setStart = function (i) { 
		iStart = i; 
		iStart = parseInt(iStart);
		iStart = (isNaN(iStart) || iStart < 0) ? 0 : iStart;
	}
	this.getStart = function() { return iStart; }
	this.setNumItems = function (i) { 
		iNumItems = i; 
		iNumItems = parseInt(iNumItems);
		iNumItems = (isNaN(iNumItems) || iNumItems < 0) ? 10 : iNumItems;
	}
	this.getNumItems = function() { return iNumItems; }
	this.getFeed = function () { return oFeed;}
	
	// initialize the constructor args
	this.setStart(iStart);
	this.setNumItems(iNumItems);
	
	// convert month 0-11 into string
	this.month = function (iMonth) {
		var aMonth = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
		return aMonth[iMonth];
	}

	
	// The heart of the class. This displays the recent activity cache in a container
	this.displayFeed = function (sContainerSelector) {
		
		// make sure the container exists
		if (!$(sContainerSelector).length) {
			debug.error('No container matches selector "' + sContainerSelector + '"');
			return false;
		}
		
		// pass this.month along.
		var oMonth = this.month;

		// show the loader
		$('#activity-loading').show(); 
		
		// make the ajax call
		$.post(sFeedUrl, {"limit":iNumItems,"offset":iStart}, function(sData, sTextStatus, oXMLHttpRequest) {
			
			debug.log('Going to try it...');
			
			try {
				// oFeed = jQuery.parseJSON( sData ); // only need to parse if JSON headers are not returned by the remote script, otherwise it is automatically parsed.
				oFeed = sData;
				debug.log(oFeed);
			}
			catch(e) {
				// We report an error, and show the erronous JSON string (we replace all " by ', to prevent another error)
				debug.log(e);
				oFeed = jQuery.parseJSON( '{"code":"error","content":"' + e.replace(/"/g, "'") + '"}' );
			} 
			
			debug.log('oFeed: ',oFeed);

			// continue if no errors with the feed
			if (oFeed.code && oFeed.code == 'error') {
				debug.error('problem parsing feed json: ' + oFeed.content);
		        $(sContainerSelector).html('<div class="ajax-menu-header"><h3>DCMessageBoards is currently not available.</h3></div>');
				
				// hide the loader
				$('#activity-loading').hide(); 

				return false;
			}
		
			// track the number of recent event notes
			var i = 0;
			
		    // add the ul
		    $(sContainerSelector).html('<ul class="activity-feed"></ul>');
			
			// loop through the array of feed JSON objects		
			for (var sKey in oFeed) {
				if (oFeed.hasOwnProperty(sKey)) {
		
					i++;
					
					var o = oFeed[sKey];
					var s = sContainerSelector + ' ul.activity-feed';

					//debug.log(s);
					
					switch (o.s_type) {
						
						case 'event':
							// authors can sometimes be blank (guest)
							o.s_author = o.s_author ? o.s_author : 'Guest';
							
							// init a string that will hold mini event calendar icon's HTML
							var sCal = '';
							// split the date into bits
							var aDateBits = o.s_date.split(' ');
							// see if there are at least 3 bits
							if (aDateBits.length > 2) {
								var sDate = aDateBits[0] + ' ' + aDateBits[1] + aDateBits[2];
								var iEventDate = Date.parse(sDate);
								
								// make sure we can parse the date
								if (!isNaN(iEventDate)) {
									var oEventDate = new Date(iEventDate);
									var iMonth = oEventDate.getMonth()+1;
									var iDay = oEventDate.getDate();
									sCal = '<span class="cal m' + iMonth + ' d' + iDay + '"><span class="m">' + oMonth(iMonth) + '</span><span class="d">' + iDay + '</span></span>';
								}
							}
							
							$(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '">' + sCal + o.s_author + ' posted the event &quot;' + o.s_title + '&quot; occuring ' + o.s_date + ' ' + o.s_age + ' ago.</a></div></li>');
							break;
							
						case 'bbs':
						    $(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '">' + o.s_author + ' posted the forum topic &quot;' + o.s_title + '&quot; ' + o.s_age + ' ago.</a></div></li>');
							break;
						
						case 'gallery_comment':
							$(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '"><img width="' + iImageIconWidth + '" src="' + o.s_icon_url + '" alt="' + o.s_title + '" /> ' + o.s_author + ' commented on the image &quot;' + o.s_title + '&quot; ' + o.s_age + ' ago.<br class="clear-both" /></a></div></li>');
							break;
						
						case 'gallery_image':
							$(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '"><img width="' + iImageIconWidth + '" src="' + o.s_icon_url + '" alt="' + o.s_title + '" /> ' + o.s_author + ' posted the image &quot;' + o.s_title + '&quot; to the album &quot;' + o.s_album_title + '&quot; ' + o.s_age + ' ago.<br class="clear-both" /></a></div></li>');
							break;	
						
						case 'blog_comment':						
						    $(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '">' + o.s_author + ' commented on the blog entry &quot;' + o.s_title + '&quot; by ' + o.blog_author + ' ' + o.s_age + ' ago.</a></div></li>');
							break;
						
						case 'blog_entry':
							$(s).append('<li class="atom"><div class="' + o.s_type + '"><a id="atom-'+i+'" href="' + o.s_url + '">' + o.s_author + ' posted a new entry entitled &quot;' + o.s_title + '&quot; to the blog &quot;' + o.s_blog_name + '&quot; ' + o.s_age + ' ago.</a></div></li>');
							break;					
						default:
							debug.warn("Unknown type of feed item: '" + o.s_type + "'");
							break;
							
					}
					
				}
			}

			// hide the loader
			$('#activity-loading').hide(); 

		});
	}
	
 }
 

