// jQuery plugin - Dolphin RSS Aggregator
(function($){
	$.fn.dolRSSFeed = function() {
		return this.each( function(){
			
			var $Cont = $(this);
			var iRSSID = parseInt( $Cont.attr( 'rssid' ) || 0 );
			if( !iRSSID )
				return false;
			
			var iMaxNum = parseInt( $Cont.attr( 'rssnum' ) || 0 );
			var iMemID  = parseInt( $Cont.attr( 'member' ) || 0 );
			
			$.getFeed( {
				url: 'get_rss_feed.php?ID=' + iRSSID + '&member=' + iMemID ,
				success: function(feed) {
					if( window.console ) console.log( feed );
					
					var sCode =
						'<div class="rss_feed_wrapper">';
					var iCount = 0;
					for( var iItemId = 0; iItemId < feed.items.length; iItemId ++ ) {
						var item = feed.items[iItemId];
						
						var oDate = new Date( item.updated );
						var sDate = oDate.toLocaleString();

						// Javed | Mantis 0000125 | next 4 lines
						var sLSN_firstSentenceEnd = "";
						sLSN_firstSentenceEnd = item.description.indexOf(".");	
						sLSN_firstSentence = item.description.slice(0, sLSN_firstSentenceEnd);
						item.description = sLSN_firstSentence + '...';
					        // sClockIcon = null;	

						sCode +=
							'<div class="rss_item_wrapper">' +
								'<div class="rss_item_header">' +
									'<a href="' + item.link + '" target="_blank">' + item.title + '</a>' +
								'</div>' +
								'<div class="rss_item_info">' +
									'<span>' +
										( sClockIcon != undefined ? ( '<img src="' + sClockIcon + '" /> ' ) : '' ) +
										sDate +
									'</span>' +
								'</div>' +
								'<div class="rss_item_desc">' + item.description + '</div>' +
							'</div>';
						
						iCount ++;
						if( iCount == iMaxNum )
							break;
					}
				// There is a bug in the default code. In profile view, it shows "Member_name's forum posts" but shows an incomplete link
				// Mantis 0000022 | 0000082 | Javed
				// This string is in 'feed.title' = "XYZ's forum posts" 	
				// So we extract that

				var sLSN_MemberName = feed.title;
				sLSN_MemberName = sLSN_MemberName.replace(/'s forum posts/, "");
				// no sMemberName should have just the author name, and we can craft our URL
				// Mantis 0000092 | Javed
				// On homepage, forum search link wasn't working.. bad link
				// Commented out next line and recrafted it
				// we will figure out if the browser is on the home page or the profile page

				var sLSN_Href = window.location.href;
				if(/\/lsn\/$/.test(window.location.href) || /index.php$/.test(window.location.href)){
					var sLSN_TargetUrl = feed.link+ '#action=goto&search_result=1&&tlts&0&&topics';
				} else {
					var sLSN_TargetUrl = feed.link+ '#action=goto&search_result=1&&msgs&0&' + sLSN_MemberName + '&topics';
					// var sLSN_TargetUrl = feed.link+ '#action=goto&search_result=1&&tlts&0&&topics';
				}
					sCode +=
							'<div class="rss_read_more">' +
								'<a href="' + sLSN_TargetUrl + '" target="_blank">' + feed.title + '</a>' +
							'</div>' +
						'</div><div class="clear_both"></div>';
					
					$Cont.html( sCode );
				}
			} );
			
		} );
	};
})(jQuery);
