//$Id: feed.js 4728 2011-03-10 20:39:00Z ssundheim $

/* initial vars */
var activefeedfilterlink;
var loadingMessage;
var grupName;
var activeTag;

/**
  * This function is used to fetch a feed to be displayed.
  * 
  * @param string feedType - the type of feed to load
  * @param int tab - the source of the click 1 = a tab, 2 = a dropdown
  * @param int grupId - (optional) the grupId you wish to load
  * @param string tag - (optional) the tag you wish to load the feed for
  * @param string page - (optional) the page of results you wish to load
  */
function fetchFeed(feedType, tab, grupId, tag, page) {
	// See if we're reloading the same feed (ie, for list style change)
	if (feedType == 'tag') {
		var tagLink = document.getElementById('taglink'+tag);
		if (activeTag != null && tag == activeTag && !page) {
			// They are disabling the tag
			tag = false;
			activeTag = false;
			if (tagLink != null) {
				tagLink.className = '';
			}
			if (grupId && activeFeed != 'results') {
				feedType = 'grup';
			} else if (activeFeed == 'results') {
				feedType = 'results';
			} else {
				feedType = 'active';
			}
		} else {
			if (tagLink != null) {
				tagLink.className = 'selectedtag';
			}
			
			// If there was one previously selected, de-select it
			if (activeTag != null && tag != activeTag) {
				var activeTagLink = document.getElementById('taglink'+activeTag);
				if (activeTagLink != null) {
					activeTagLink.className = '';
				}
			}

			if (activeFeed == 'results') {
				feedType = 'results';
			}
		} 
	}

	if (feedType != 'tag' && feedType != 'reload' && (activeTag != null && activeTag.length)) {
		var tagLink = document.getElementById('taglink'+activeTag);
		if (tagLink != null) {
			tagLink.className = '';
			activeTag = false;
		}
	}

	if (feedType == 'reload') {
		feedType = activeFeed;
		if (feedType == 'tag') {
			tag = activeTag;
		}
		feedReload = true;
	} else { 
		feedReload = false;
	}

	if (!grupId) {
		grupId = 0;
	}

	if (!page) {
		page = 1;
	}

	/**
	  * Handle the changing of the tab displays, if necessary. If they didn't pass along anything for the tab argument
	  * then they are just changing the list display type so changing the display of the tabs isn't necessary.
	  */
	if (activefeedfilterlink == null) {
		activefeedfilterlink = document.getElementById('feedlink_' + defaultFeed);
	}
	if (tab > 0) {
		if (tab == 1 || feedType == activeFeed) {
			// They clicked one of the main feed tabs
			activefeedfilterlinkid = 'feedlink_' + feedType;

			// Let's make sure our dropdown selection gets reset
			if (document.getElementById('feedsearch') != null && document.getElementById('feedsearch').value != feedType) {
				document.getElementById('feedsearch').value = 'none';
			}
		} else {
			// They selected one from the list of dropdowns
			activefeedfilterlinkid = 'feedlink_dropdown';
		}
		oldactivefeedfilterlink = activefeedfilterlink;
		activefeedfilterlink = document.getElementById(activefeedfilterlinkid);
		if (oldactivefeedfilterlink != null && !feedReload) {
			oldactivefeedfilterlink.className = "";
		}
		if (activefeedfilterlink != null) {
			activefeedfilterlink.className = "filtercell_on";		  
		}
		activeFeed = feedType; 
	} else  {
	  	if (activefeedfilterlink == null) {
			activefeedfilterlink = document.getElementById('feedlink_' + defaultFeed);
		}
		if (!feedReload) {
			activefeedfilterlink.className = "";
		}
	}
	
	$('#feed_table').addClass('faded');
	
	//We used to allow people to change the feed format to "normal" so that it was more compact and did not show images
	
	var postString = 'feedtype='+feedType+'&grupid='+grupId+'&page='+page;
	if (tag != undefined && tag.length > 0) {
		activeTag = tag;
		postString += '&tag='+tag;
	}

	var postURI = '/ajaxserver/loadfeed.php5';
	loadXMLDocPost(postURI, postString);
}

function displayFeedItems(input, results, success) {
	if (results[0].getElementsByTagName('feedresults')[0] != null) {
		var resultsHTML = results[0].getElementsByTagName('feedresults')[0].firstChild.data;
		document.getElementById('feed_table').innerHTML = resultsHTML;
	} else {
		var errorMessage = results[0].getElementsByTagName('errormessage')[0].firstChild.data;
		var feedItems = results[0].getElementsByTagName('feeditem');
		var feedListDisplay = results[0].getElementsByTagName('feedlistdisplay')[0].firstChild.data;
		if (grupName != undefined) {
			feedListDisplay += ' in the '+grupName;
		}
		var feedPaginationHtml = results[0].getElementsByTagName('feedpaginationhtml')[0].firstChild.data;
		var feedPaginationDiv = document.getElementById('paginationcontainer');
		if (feedPaginationHtml != 'none') {
			feedPaginationDiv.innerHTML = feedPaginationHtml;
		} else {
			if (feedPaginationDiv.hasChildNodes()) {
				while (feedPaginationDiv.hasChildNodes()) {
					feedPaginationDiv.removeChild(feedPaginationDiv.firstChild);
				}
			}
		}

		// Clear out the table of any loading messages or errors or something
		var feedTable = document.getElementById('feed_table');
		if (feedTable.hasChildNodes()) {
			while (feedTable.childNodes.length >= 1) {
				feedTable.removeChild(feedTable.firstChild);
			}
		}

		var tableBody = document.createElement('tbody');

		// Check to make sure there isn't an error to display to them
		if (errorMessage != null && errorMessage != 'none')	{
			// Then there was an error so we'll show it to 'em
			var errorRow = document.createElement('tr');
			var errorCell = document.createElement('td');
			errorCell.className = ('feederror');
			errorP = document.createElement('p');
			errorP.appendChild(document.createTextNode(errorMessage));
			errorCell.appendChild(errorP);
			errorRow.appendChild(errorCell);

			var tableBody = document.createElement('tbody');
			tableBody.appendChild(errorRow);

			feedTable.appendChild(tableBody);
		} else {
			// There's feed items to go through and display
			for (ii=0; ii<feedItems.length; ii++) {
				var thisFeedItem = feedItems[ii];

				// Grab our 
				var thisFeedType = thisFeedItem.getAttribute('feeditemtype');
				var thisFeedContentID = thisFeedItem.getAttribute('contentid');
				var thisFeedUserID = thisFeedItem.getAttribute('userid');
				var thisFeedUserAvatar = thisFeedItem.getAttribute('useravatar');
				var thisFeedUserNickname = htmlEntityDecode(thisFeedItem.getAttribute('usernickname'));
				var thisFeedUserNicknameLink = thisFeedItem.getAttribute('usernicknamelink');
				var thisFeedTopicLinkText = thisFeedItem.getAttribute('topiclinktext');
				var thisFeedTopicTitle = htmlEntityDecode(thisFeedItem.getAttribute('topictitle'));
				var thisFeedAnswerLinkText = thisFeedItem.getAttribute('answerlinktext');
				var thisFeedAnswerTitle = htmlEntityDecode(thisFeedItem.getAttribute('answertitle'));
				var thisFeedAnnouncementTitle = htmlEntityDecode(thisFeedItem.getAttribute('announcementtitle'));
				var thisFeedProfileTitle = htmlEntityDecode(thisFeedItem.getAttribute('profiletitle'));
				var thisFeedContentMetaID = thisFeedItem.getAttribute('metaid');
				var thisFeedContentMetaID2 = thisFeedItem.getAttribute('metaid2');
				var thisFeedContentTopicImage = thisFeedItem.getAttribute('topicimage');
				var thisGroupName = htmlEntityDecode(thisFeedItem.getAttribute('groupname'));
				var thisGroupId = htmlEntityDecode(thisFeedItem.getAttribute('groupid'));
				var thisAuthorsPick = htmlEntityDecode(thisFeedItem.getAttribute('authorspick'));
				var thisTopicPrivacy = htmlEntityDecode(thisFeedItem.getAttribute('topicprivacy'));

				var newFeedItemRow = document.createElement('tr');
				var newFeedItemCell = document.createElement('td');
				newFeedItemCell.className = 'topiclist_title';


				// First we'll create our user image link
				var newFeedItemUserImageLink = document.createElement('a');
				newFeedItemUserImageLink.className = 'membericon_link';
				newFeedItemUserImageLink.href = '/profile/'+thisFeedUserNicknameLink;
				var newFeedItemUserImage = document.createElement('img');
				newFeedItemUserImage.className = 'membericon_sm';
				newFeedItemUserImage.src = thisFeedUserAvatar;
				newFeedItemUserImageLink.appendChild(newFeedItemUserImage);

				// Now we'll create our user name and link
				var newFeedItemUserNicknameLink = document.createElement('a');
				newFeedItemUserNicknameLink.href = '/profile/'+thisFeedUserNicknameLink;
				newFeedItemUserNicknameLink.appendChild(document.createTextNode(thisFeedUserNickname));
				
				
				
				newFeedItemGroupLabel =  document.createTextNode('')
				var groupText_privacy_label = document.createTextNode('');
				
				if(thisGroupId >= 1) {
				//Create and append the "in group.." label
					
					newFeedItemGroupLabel = document.createElement('p');
					newFeedItemGroupLabel.className='group_label tiny';
					
					groupText_privacy_label = document.createElement('div');
					
					if(thisTopicPrivacy == 'private') {	
					    groupText_privacy_label.className = 'privacylabel private_lbl';
					    groupText_privacy_label.title = 'Only you and other members of the group can see this topic.';    
					    var groupText_privacy_text = document.createTextNode(thisTopicPrivacy);
					    groupText_privacy_label.appendChild(groupText_privacy_text);
					} else if(thisTopicPrivacy == 'panel'){
					    groupText_privacy_label.className = 'privacylabel panel_lbl';
					    groupText_privacy_label.title = 'Only members of the group can vote, but anyone can comment and submit answers.';    
					    var groupText_privacy_text = document.createTextNode(thisTopicPrivacy);
					    groupText_privacy_label.appendChild(groupText_privacy_text);
					} 

					
					var groupText_prefix = document.createTextNode('In ');
					
					
					var groupText = document.createTextNode(thisGroupName);
					
					newFeedItemGroupLink = document.createElement('a');	
					newFeedItemGroupLink.href = '/group/' + thisGroupId;			
					newFeedItemGroupLink.appendChild(groupText); 
					
					//newFeedItemGroupLabel.appendChild(groupText_privacy_label);
					newFeedItemGroupLabel.appendChild(groupText_prefix);
					newFeedItemGroupLabel.appendChild(newFeedItemGroupLink);			
				}
				
				//If this is authors pick, create a label
				QALabel = document.createTextNode('');
				if(thisAuthorsPick == 1) {
				   var QAText = document.createTextNode('Q&A');
				   var QALabel = document.createElement('div');
				   QALabel.title = 'The author of this question is looking for one "right" answer or solution.';
				   QALabel.className = 'qalabel';
				   QALabel.appendChild(QAText);
				}

				// Now, depending on the type of feedType, we'll render the row to be added accordingly
				switch (thisFeedType) {
					// New topic
					case '1':
						if (thisFeedTopicLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {					
							var actionText = document.createTextNode(' posted this new topic! ');					
							topicLink = '/topic/'+thisFeedContentID+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemFirstLink = document.createElement('a');
							newFeedItemFirstLink.href = topicLink;
							newFeedItemFirstLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemFirstLink);
						}
					break;

					// New answer
					case '2':
						if (thisFeedTopicLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {
							if (thisFeedAnswerLinkText == 'false') {
								answerLink = '/answer/'+thisFeedContentID+'/'+thisFeedAnswerLinkText;
								var newFeedItemSubject = document.createTextNode('an answer which has since been removed.');						
							} else {
								answerLink = '/answer/'+thisFeedContentID+'/'+thisFeedAnswerLinkText;
								var newFeedItemSubject = document.createElement('a');
								newFeedItemSubject.href = answerLink;
								newFeedItemSubject.className = 'feed_answertitle';
								thisAnswerLinkTextForLink = '"'+thisFeedAnswerTitle+'"';
								newFeedItemSubject.appendChild(document.createTextNode(thisAnswerLinkTextForLink));
								
							}
							
							var actionText = document.createTextNode(' added ');
							topicLink = '/topic/'+thisFeedContentMetaID+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemTopicTitleLink = document.createElement('a');
							newFeedItemTopicTitle.className = 'appletest';
							newFeedItemTopicTitleLink.href = topicLink;
							newFeedItemTopicTitleLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemTopicTitleLink);
						}
					break;

					// Topic comment
					case '3':
						if (thisFeedTopicLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {
							var actionText = document.createTextNode(' added a comment. ');

							topicLink = '/topic/'+thisFeedContentMetaID+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemFirstLink = document.createElement('a');
							newFeedItemFirstLink.href = topicLink;
							newFeedItemFirstLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemFirstLink);

							commentLink = '/comment/'+thisFeedContentID;
							newFeedItemSecondLink = document.createElement('a');
							newFeedItemSecondLink.href = commentLink;
							newFeedItemSecondLink.appendChild(document.createTextNode('(See comment...)'));
						}
					break;

					// Answer comment
					case '4':
						if (thisFeedTopicLinkText == 'false' || thisFeedAnswerLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {
							var actionText = document.createTextNode(' commented on ');
							
							answerLink = '/answer/'+thisFeedContentMetaID+'/'+thisFeedAnswerLinkText;
							var newFeedItemSubject = document.createElement('a');
							newFeedItemSubject.href = answerLink;
							newFeedItemSubject.className = 'feed_answertitle';
							thisAnswerLinkTextForLink = '"'+thisFeedAnswerTitle+'"';
							newFeedItemSubject.appendChild(document.createTextNode(thisAnswerLinkTextForLink));

							topicLink = '/topic/'+thisFeedContentMetaID2+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemSecondLink = document.createElement('a');
							newFeedItemSecondLink.href = topicLink;
							newFeedItemSecondLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemSecondLink);

							commentLink = '/comment/'+thisFeedContentID;
							newFeedItemThirdLink = document.createElement('a');
							newFeedItemThirdLink.href = commentLink;
							newFeedItemThirdLink.appendChild(document.createTextNode('(See comment...)'));
						}
					break;

					// Profile comment
					case '5':
						var actionText = document.createTextNode(' commented on ');
						secondaryText = document.createTextNode('. ');
						
						thisProfileLinkTextForLink = thisFeedProfileTitle;
						profileLink = '/profile/'+thisProfileLinkTextForLink;
						newFeedItemFirstLink = document.createElement('a');
						newFeedItemFirstLink.href = profileLink;
						newFeedItemFirstLink.appendChild(document.createTextNode('profile'));

						commentLink = '/comment/'+thisFeedContentID;
						var newFeedItemSubject = document.createElement('a');
						newFeedItemSubject.href = commentLink;
						newFeedItemSubject.appendChild(document.createTextNode(' '+thisFeedProfileTitle+'\'s profile'));
					break;

					// Announcement comment
					case '6':
						if (thisFeedAnnouncementTitle == 'false') {
							actionText = document.createTextNode(' (This content is no longer active)');
						} else {
							var actionText = document.createTextNode(' has commented on "');
							secondaryText = document.createTextNode('"');

							announcementLink = '/announcement/'+thisFeedContentMetaID;
							newFeedItemFirstLink = document.createElement('a');
							newFeedItemFirstLink.href = announcementLink;
							thisAnnouncementLinkTextForLink = thisFeedAnnouncementTitle;
							newFeedItemFirstLink.appendChild(document.createTextNode(thisAnnouncementLinkTextForLink));
						}
					break;

					// Topic edit
					case '8':
						if (thisFeedTopicLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {
							actionText = document.createTextNode(' edited this topic. ');
							topicLink = '/topic/'+thisFeedContentID+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemFirstLink = document.createElement('a');
							newFeedItemFirstLink.href = topicLink;
							newFeedItemFirstLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemFirstLink);
						}
					break;

					// Answer edit				
					case '9':
						if (thisFeedTopicLinkText == 'false' || thisFeedAnswerLinkText == 'false') {
							var actionText = document.createTextNode(' '+thisFeedTopicTitle);
						} else {
							actionText = document.createTextNode(' edited the answer ');
							secondaryText = document.createTextNode(' in ');
							answerLink = '/answer/'+thisFeedContentID+'/'+thisFeedAnswerLinkText;
							topicLink = '/topic/'+thisFeedContentMetaID+'/'+thisFeedTopicLinkText;
							var newFeedItemTopicTitle = document.createElement('h3');
							newFeedItemFirstLink = document.createElement('a');
							newFeedItemFirstLink.className = 'feed_answertitle';
							newFeedItemFirstLink.href = answerLink;
							thisAnswerLinkTextForLink = '"'+thisFeedAnswerTitle+'"';
							newFeedItemFirstLink.appendChild(document.createTextNode(thisAnswerLinkTextForLink));
							

							newFeedItemSecondLink = document.createElement('a');
							newFeedItemSecondLink.href = topicLink;
							newFeedItemSecondLink.appendChild(document.createTextNode(thisFeedTopicTitle));	
							newFeedItemTopicTitle.appendChild(groupText_privacy_label);
							newFeedItemTopicTitle.appendChild(QALabel);
							newFeedItemTopicTitle.appendChild(newFeedItemSecondLink);
						}
					break;
				}
				
				var newFeedItemTopicImageCell = document.createElement('td');
				newFeedItemTopicImageCell.className = 'topicthumb';
				if (thisFeedContentTopicImage != 'false') {
					var newFeedItemTopicImageLink = document.createElement('a');
					newFeedItemTopicImageLink.className = 'framed';
					newFeedItemTopicImageLink.href = topicLink;
					var newFeedItemTopicImage = document.createElement('img');
					newFeedItemTopicImage.src = thisFeedContentTopicImage;
					var newFeedItemTopicImageDiv = document.createElement('div');
					newFeedItemTopicImageDiv.style.backgroundImage = 'url('+thisFeedContentTopicImage+')';
					newFeedItemTopicImageDiv.appendChild(newFeedItemTopicImage);
					newFeedItemTopicImageLink.appendChild(newFeedItemTopicImageDiv);
					newFeedItemTopicImageCell.appendChild(newFeedItemTopicImageLink);
				} else {
					newFeedItemTopicImageCell.appendChild(document.createTextNode(' '));
				}

				// Now that we have our information, we can go ahead and append to our cell as necessary
				/* Print the topic title */
				if (newFeedItemTopicTitle != null) {
					newFeedItemCell.appendChild(newFeedItemTopicTitle);
					newFeedItemCell.appendChild(newFeedItemGroupLabel);
				}
				
				/* Show the activity description for the topic ("user did ______") */
				var newFeedItemDescription = document.createElement('span');
				newFeedItemDescription .className = 'desc';
								
					

					newFeedItemDescription.appendChild(newFeedItemUserImageLink); 
					
				/* Show the member link */
				newFeedItemDescription.appendChild(newFeedItemUserNicknameLink);
				
				/* Show what they did */
				if (actionText != null) {
					newFeedItemDescription.appendChild(actionText);
				}
				
				/* Show where they did it (not always used) */
				if (newFeedItemSubject != null) {
					newFeedItemDescription.appendChild(newFeedItemSubject);
				}			
				
				/* Print the activity description */
				newFeedItemCell.appendChild(newFeedItemDescription);
				
				newFeedItemRow.appendChild(newFeedItemCell);

				newFeedItemRow.appendChild(newFeedItemTopicImageCell);	

				tableBody.appendChild(newFeedItemRow);

				newFeedItemTopicTitle = null;
				
				newFeedItemGroupLabel = null;
				actionText = null;
				newFeedItemSubject = null;
			} // end loop of feed items 
		} // end case there wasn't an error
		feedTable.appendChild(tableBody);
		
		
		// Let's scroll the window, if needed, eg , if they clicked on of the page links at the bottom
		feedposition = $("#feed_table").offset().top - 45;
		if($(window).scrollTop() > feedposition + 50){
		    $(window).scrollTop(feedposition)
		}
		$('#feed_table').removeClass('faded');
	}
	
	// Last, we'll enable the selection box for them again
	if (document.getElementById('feedsearch') != null) {
		document.getElementById('feedsearch').disabled = '';
	}
}

