var firstInboxPost = false;
function makeInboxPost (ary)
{
	var array = ary;
	
	var post = $('#templates  .inboxPostLinkWrapRow').clone(true);
	
	var thisPostID = array['postID'];
	post.attr('postid',thisPostID);
	if (lastPostID == thisPostID)
	{
		post.addClass('lastClickedClass');
	}
	var inboxRowLink = post.find('.inboxRowLink');
	
	post.click(function(){
	
	var postJustClicked = $(this);
	$('.prevFromClicked').removeClass('prevFromClicked');
	var clickedPostsId = postJustClicked.attr('postid');
	getTreeArray (clickedPostsId);
	$('.inboxHighlight').removeClass('inboxHighlight');
	postJustClicked.addClass('inboxHighlight');
	var prevPost = postJustClicked.prev();
	prevPost.addClass('prevFromClicked');
	});
	
	if (array['unread'] == 1) {post.addClass('unread');}
	else {post.addClass('read');}
	
	var attachments = array['attachments'];
	test = attachments;
	var attachmentCell = post.find('.inboxPostAttachmentCell');
	if (attachments.length == 0)
	{
		attachmentCell.hide();
	} else {
		var typePriority = new Array();
		typePriority['none'] = 0;
		typePriority['link'] = 1;
		typePriority['imageLink'] = 2;
		typePriority['imageSrc'] = 3;
		typePriority['video'] = 4;
		
		var attachmentType = 'none';
		var attachmentToUse = -1;
		for (var i=0;i<attachments.length;i++)
		{
			if (typePriority[attachmentType] < typePriority[attachments[i][0]])
			{
				attachmentToUse = i;
				attachmentType = attachments[i][0];
			}
		}
		
		attachmentCell.find('.inboxAttachmentImage').attr('src', attachments[attachmentToUse][2]);
		attachmentCell.find('.inboxAttachmentImage').attr('title', attachments.length + ((attachments.length > 1) ? ' attachments' : ' attachment'));
		if (attachmentType == 'video')
		{
			attachmentCellDiv = attachmentCell.find('.attachmentCellInboxDiv');
			attachmentCellDiv.append($('<img src="pics/iconPlay.png" class="inboxPlayIcon">'));
			attachmentCellDiv.find('.inboxPlayIcon').attr('title', attachments.length + ((attachments.length > 1) ? ' attachments' : ' attachment'));
		}
	}
	post.find ('.inboxPostText').html(array['text']);
	var receiveMethod = array['receiveMethod'];
	var methodSpan = post.find ('.receiveMethod');
	
	/******** NEW STUFF ************/
	if (array['depth'] != '0') // != '1'
	{
		methodSpan.append('<span>RE:</span>');
	}
	else
	{
		//methodSpan.next().remove(); //removing the spacer
		methodSpan.remove();
	}	
	var clicks = array['clicks'];
	var clickSpan = post.find('.inboxNumber');
	clickSpan.html(clicks);
	var lang = array['postLanguage'];
	post.attr('ln',lang);
	if (checkDir(lang) == 'ltr')
	{
		post.find('.transDirDiv').css('direction','ltr');
	} else {
		post.find('.transDirDiv').css('direction','rtl');
	}
	
	if (lang != ln)
	{
		transTipShow = true;
	}
	
	if (firstInboxPost)
	{
		var borderBottomDiv = post.find('.inboxPostDiv');
		borderBottomDiv.attr('style','border-bottom:none;')
		firstInboxPost = false;
	}
	/*****************************/
	
	return post;
}



function makePost (selectedPst, postPositn, typ)
{
	var selectedPost = selectedPst;
	var postPosition = postPositn;
	var type = typ;
	
	post = $('#templates  .post').clone(true);
	var postTopRight = post.find('.postTopRightDiv');
	/*
	var watch = selectedPost['watch'];
	var forward = selectedPost['forward'];
	
	postTopRight.find('.postTopRightWatch').bind('click',watchPost);
	postTopRight.find('.postTopRightForward').bind('click',forwardPost);
	if (forward == 1)
	{
		var span = postTopRight.find('.postTopRightForward');
		var icn = span.find('.postTopRightIcon');
		forwarded(span,icn);
	}
	if (watch == 1)
	{
		var span = postTopRight.find('.postTopRightWatch');
		var icn = span.find('.postTopRightIcon');
		watching(span,icn);
	}
	
	var receivedStat = selectedPost['receivedStat'];
	*/
	var clickedStat = selectedPost['clickedStat'];
	/*
	var watchingStat = selectedPost['watchingStat'];
	*/
	var forwardedStat = selectedPost['forwardedStat'];
	var forwardedAlready = selectedPost['forward'];
	
	post.find('.postVotesSpan').html(forwardedStat);
	
	if (forwardedAlready == 1)
	{
		var votingArrow = post.find('.votingArrowBefore');
		votingArrow.removeClass('votingArrowBefore');
		votingArrow.attr('src','./pics/upBlue.png');
	}
	
	var replyNumber = selectedPost['replies'].length;
	var repliedStat = replyNumber;
	post.attr('replynum',replyNumber);
	
	var infoDiv = postTopRight.find('.infoDiv');
	var infoArrow = postTopRight.find('.infoArrow');
	infoArrow.text(clickedStat);
	infoArrow.removeAttr('onclick');
	/*
	infoArrow.click(function (event){showInfo(event,this);});
	if (receivedStat == '0'){infoDiv.append('<span class="infoSpan statReceived">No one received this post to inbox yet</span>');}
	if (receivedStat == '1'){infoDiv.append('<span class="infoSpan statReceived">1 person received this post to their inbox</span>');}
	if ((receivedStat != '1') && (receivedStat != '0')){infoDiv.append('<span class="infoSpan statReceived">'+receivedStat+' people received this post to their inbox</span>');}
	infoDiv.append('<br>');
	if (clickedStat == '0'){infoDiv.append('<span class="infoSpan statReceived">No one opened it</span>');}
	if (clickedStat == '1'){infoDiv.append('<span class="infoSpan statReceived">1 person opened it</span>');}
	if ((clickedStat != '1') && (clickedStat != '0')){infoDiv.append('<span class="infoSpan statReceived">'+clickedStat+' people opened it</span>');}
	infoDiv.append('<br>');
	if (repliedStat == '0'){infoDiv.append('<span class="infoSpan statReceived statReplied">No one replied</span>');}
	if (repliedStat == '1'){infoDiv.append('<span class="infoSpan statReceived statReplied">1 person replied</span>');}
	if ((repliedStat != '1') && (repliedStat != '0')){infoDiv.append('<span class="infoSpan statReceived statReplied">'+repliedStat+' people replied</span>');}
	infoDiv.append('<br>');
	if (watchingStat == '0'){infoDiv.append('<span class="infoSpan statReceived">No one is watching it</span>');}
	if (watchingStat == '1'){infoDiv.append('<span class="infoSpan statReceived">1 person is watching</span>');}
	if ((watchingStat != '1') && (watchingStat != '0')){infoDiv.append('<span class="infoSpan statReceived">'+watchingStat+' people are watching it</span>');}
	*/
	//if (signInStatus == false)
	//{
		postTopRight.find('.postTopRightWatch').remove();
		postTopRight.find('.postTopRightForward').remove();
		postTopRight.find('.infoArrow').remove();
	//}
	var thisPostID = selectedPost['postID'];
	post.attr('postid', thisPostID);
	post.attr('postposition', postPosition);
	if (type == 0)
	{
		post.find('.showChainCell').hide();
	}
	else
	{
		post.addClass('replyPost');
	}
	//var author = selectedPost['username'];
	//var displayPicture = selectedPost['displayPicture'];
	//var authorID = selectedPost['userID'];
	var text = selectedPost['text'];
	/*
	var postTags = selectedPost['tags'];
	var postTagsSpan = post.find('.bottomTagsCell');
	for (var k in postTags)
	{
		tag = $('#templates  .postTagForConv').clone(true);
		var tagText = postTags[k]['tagText'];
		var tagID = postTags[k]['tagID'];
		tag.text(tagText);
		tag.attr('tagid', tagID);
		//tag.attr('href','index.php?tagID='+tagID+'&tagText='+tagText);
		var clippedTag = clipTag(tag);
		postTagsSpan.append(clippedTag);
	}
	
	var profileLink = post.find('.postProfileLink');
	//profileLink.attr('href','profile.php?profileID='+authorID);
	profileLink.attr('profileID',authorID);
	profileLink.click(showProfilePopup);
	profileLink.attr('title','see all posts by '+author);
	profileLink.attr('profilename',author);
	post.find('.username').html(author);
	post.find('.userPicture').attr('src', displayPicture);
	*/
	var postText = post.find('.postText');
	postText.html(text);
	var showRepliesButton = post.find('.showRepliesButton');
	showRepliesButton.click(showRepliesClicked);
	showRepliesButton.html(replyNumberMany+' ('+replyNumber+')');
	if (replyNumber >= 2)
	{
		$('.treeNavigationCell').show();
	}
	if (replyNumber <= 1)
	{
		if (replyNumber == 1) {showRepliesButton.html (replyNumberOne);}
		else {showRepliesButton.html (replyNumberNone);}
		showRepliesButton.removeAttr('onclick');
		showRepliesButton.removeClass('bottomReplyButton');
		showRepliesButton.addClass('notClickable');
	}
		
	var attachments = selectedPost['attachments'];
	var attachmentsTable = post.find('.postAttachmentsTable');
	if (attachments.length > 0)
	{		
		attachmentsTable.show();
	}
	for (var j = 0; j<attachments.length; j++)
	{		
		var currentAttachment = attachments[j];
		var attachmentType = currentAttachment[0];
		var attachmentLink = currentAttachment[1];
		var attachmentThumb = currentAttachment[2];
		var attachmentInfo = currentAttachment[3];
		
		var selectedRow;
		var selectedContentCell;
		var selectedTable;
		if (attachmentType == 'link')
		{
			selectedRow = attachmentsTable.find('.linksRow');
			selectedRow.show();
			selectedContentCell = selectedRow.find('.attachmentContentCell');
			var item = $('#templates  .itemLinkAttachment').clone(true);
			var links = item.find('.linkLinkAttachment');
			links.attr('href',attachmentLink);
			var spanLinkInfo = item.find('.spanLinkInfo');
			spanLinkInfo.html(attachmentInfo);
			selectedContentCell.append(item);
		} else if (attachmentType == 'imageSrc')
		{
			selectedRow = attachmentsTable.find('.imagesRow');
			selectedRow.show();
			selectedContentCell = selectedRow.find('.attachmentContentCell');
			var item = $('#templates  .attachmentImageThumnbNail').clone(true);
			item.attr('imagesrc',attachmentLink);
			item.click(loadImagePopup);
			
			item.attr('src', attachmentThumb);
			selectedContentCell.append(item);
		} else if (attachmentType == 'imageLink')
		{
			selectedRow = attachmentsTable.find('.imagesRow');
			selectedRow.show();
			selectedContentCell = selectedRow.find('.attachmentContentCell');
			var item = $('#templates  .linkImageAttachment').clone(true);
			item.attr('href',attachmentLink);
			item.find('.genericImage').attr('src', attachmentThumb);
			item.attr('title','external source: ' + attachmentLink);
			selectedContentCell.append(item);
		} else if (attachmentType == 'video')
		{
			selectedRow = attachmentsTable.find('.videosRow');
			selectedRow.show();
			cellAttachmentLabel = selectedRow.find('.cellAttachmentLabel');
			cellAttachmentLabel.remove();
			selectedContentCell = selectedRow.find('.attachmentContentCell');
			var VA = $('#templates  .divVideoAttachmentItem').clone(true);
			var imageVA = VA.find('.videoResultImage');
			var titleVA = VA.find('.videoResultSpan');
			imageVA.attr('src',attachmentThumb);
			titleVA.html(attachmentInfo);
			imageVA.attr('previewlink',attachmentLink);
			titleVA.attr('previewlink',attachmentLink);
			imageVA.click(openVideoAttachment);
			titleVA.click(openVideoAttachment);
			playIcon = VA.find('.iconPlay');
			playIcon.attr('previewlink',attachmentLink);
			playIcon.click(openVideoAttachment);
			selectedContentCell.append(VA);
		}
	}
	if ($.browser.msie)
	{
		post.css('border','2px solid #ccc');
	}
	/******** NEW STUFF ************/
	var lang = selectedPost['postLanguage'];
	post.attr('ln',lang);
	postText.attr('posttext',text);
	
	if (checkDir(lang) == 'ltr')
	{
		post.find('.transDirDiv').css('direction','ltr');
	} else {
		post.find('.transDirDiv').css('direction','rtl');
	}
	/*******************************/
	
	return post;
}

