function loadMoreFBReviews (reviews) {
    var div = document.getElementById('fbreviewsdiv');
    if (div.hasChildNodes()) {
    	div.removeChild(div.firstChild);
    }
    div.appendChild(populateFacebookReviews(reviews, reviews.allReviews.length));
}  
  
function createFBReviewsBox (reviews) {
	var reviewBox = document.getElementById( 'facebookReviews' );
	var divHeading = document.createElement ('div');
	divHeading.setAttribute('class', 'fbrevbg');
	divHeading.setAttribute('className', 'fbrevbg');
	var tableHeading = document.createElement( 'table' );
	  tableHeading.setAttribute( 'cellPadding', '0' );
	  tableHeading.setAttribute( 'cellSpacing', '0' );
	  tableHeading.setAttribute( 'border', '0' );
	  tableHeading.setAttribute( 'width', '100%');

	  var row = tableHeading.insertRow( -1 );
	  var cell = row.insertCell( -1 );
	  cell.setAttribute( 'align', 'left' );
	  cell.setAttribute( 'vAlign', 'middle' );
	  cell.setAttribute( 'class', 'graybox3' );
	  cell.setAttribute( 'className', 'graybox3' );
	  var img = document.createElement( 'img' );
	  img.setAttribute( 'class', 'fbrevlogo' );
	  img.setAttribute( 'className', 'fbrevlogo' );
	  img.setAttribute( 'src', '/img/facebook/flogo.gif' );
	  
	  var cellRight = row.insertCell( -1 );
	  cellRight.setAttribute( 'align', 'left' );
	  cellRight.setAttribute( 'vAlign', 'middle' );
	  cellRight.setAttribute( 'width', '100%' );
	  cellRight.setAttribute( 'class', 'graybox4' );
	  cellRight.setAttribute( 'className', 'graybox4' );
	  var div = document.createElement( 'div' );
	  div.setAttribute( 'class', 'facebookFriendsHeading' );
	  div.setAttribute( 'className', 'facebookFriendsHeading' );
	  
	  if( reviews.friendReviews || ( reviews.allReviews.length === 0 ) ) {
		  div.innerHTML = 'Reviews from your friends';
	  } else {
	  	  div.innerHTML = 'Reviews from Facebook users';
	  }
	    	
	  cell.appendChild( img );
	  cellRight.appendChild( div );

	divHeading.appendChild (tableHeading);
	reviewBox.appendChild( divHeading );
	
	div = document.createElement( 'div' );
	div.setAttribute( 'id', 'fbreviewsdiv' );
	
  	div.appendChild( populateFacebookReviews( reviews, 2 ));
  	reviewBox.appendChild(div);
	  var footer=document.createElement( 'div' );
	  footer.setAttribute( 'class', 'fbrevbottom' );
  	  footer.setAttribute( 'className', 'fbrevbottom' );
  	  if( reviews.friendReviews ) {
		  footer.innerHTML="Want to see more reviews from your friends?  <a href=\"/facebook/invite.do\">Invite them to sign in with Facebook Connect!</a>";
	  } else {
		  footer.innerHTML="Want to see your friends' recommendations?  <a href=\"/facebook/invite.do\">Invite them to sign in with Facebook Connect!</a>";
	  }
  	  reviewBox.appendChild( footer );
}
		
function populateFacebookReviews( reviews, maxReviews ) {
	var friends = reviews.friends;
	var table = document.createElement( 'table' );
	  table.setAttribute( 'id', 'fbreviewstable' );
	  table.setAttribute( 'cellPadding', '0' );
	  table.setAttribute( 'cellSpacing', '12' );
	  table.setAttribute( 'border', '0' );
	  table.setAttribute( 'width', '100%' );

	if (reviews.allReviews.length === 0) {
	  	  var row = table.insertRow( -1 );
	  	  	var maxFriends = friends.length;
	  	  	if (friends.length > 4) {
	  	  		maxFriends = 4;
	  	  	}  
	  	   var rotatePics = friends.length * Math.random ();	
	  	   for( var counter=0; counter < maxFriends; counter++ ) {
	  	      var cell = row.insertCell (-1);
	  	      var img = document.createElement( 'fb:profile-pic' );
	  	      img.setAttribute( 'uid', friends[(Math.floor((rotatePics + counter) % friends.length))] );
			  img.setAttribute( 'facebook-logo', 'false' );
			  img.setAttribute( 'linked', 'false' );
			  img.setAttribute( 'width', '50' );
			  img.setAttribute( 'height', '50' );
			  img.setAttribute( 'size', 'square' );
		      cell.setAttribute( 'align', 'center' );
		      cell.setAttribute( 'vAlign', 'middle' );
			  cell.appendChild( img );
	  	   }
	}
	else {
	 var reviewNum = reviews.allReviews.length;
	 if (reviewNum > maxReviews) {
	 	reviewNum = maxReviews;
	 }
	 for( var counter=0; counter < reviewNum; counter++ ) {
  	  var row = table.insertRow( -1 );
	  var cellImage  = row.insertCell( -1 );
	  var cellText = row.insertCell( -1 );
		
	  var img = document.createElement( 'img' );
	  img.setAttribute( 'src', reviews.allReviews[counter].userProfileImage);
	  img.setAttribute( 'width', '50' );
	  img.setAttribute( 'height', '50' );
	  cellImage.appendChild( img );
	  cellImage.setAttribute( 'vAlign', 'top' );	

	  cellText.setAttribute( 'align', 'left' );	  
	  cellText.setAttribute( 'vAlign', 'top' );	
	  cellText.setAttribute( 'width', '100%' );
	  var firstSection = document.createElement ('div');
	  firstSection.setAttribute('class', 'fbfirstsection');
	  firstSection.setAttribute('className', 'fbfirstsection');
	  var consumerhref = document.createElement( 'a' );
	  consumerhref.setAttribute( 'href', '/consumerReviewsHistory.do?userId=' + 
	  	reviews.allReviews[counter].userID );
		  consumerhref.appendChild(document.createTextNode(reviews.allReviews[counter].userName));
	  var secondSection = document.createElement('div');
	  var reviewhref = document.createElement( 'a' );
	  reviewhref.setAttribute( 'href', '/r/' + reviews.allReviews[counter].reviewID );
	  reviewhref.appendChild(document.createTextNode('"' + reviews.allReviews[counter].title + '"'));
  	  var stars = document.createElement( 'img' );
	  stars.setAttribute( 'src', '/img/stars-' + reviews.allReviews[counter].rating.toFixed(1) + '.gif' );
	  stars.setAttribute( 'class', 'fbstars');
	  stars.setAttribute( 'className', 'fbstars');

	  secondSection.appendChild(reviewhref);
	  secondSection.appendChild(document.createElement( 'br' ));
	  secondSection.appendChild(document.createTextNode( reviews.allReviews[counter].companyName ));
	  secondSection.appendChild(document.createElement( 'br' ));
		
	  firstSection.appendChild(document.createTextNode( 'posted by ' ));
	  firstSection.appendChild( consumerhref );	    


	  cellText.appendChild( secondSection );
	  cellText.appendChild( firstSection );
	  
	  }
	  	if (reviews.allReviews.length > maxReviews) {
		  var row = table.insertRow( -1 );
		  var cell = row.insertCell( -1 );
		  cell.setAttribute('colSpan','2');
		  cell.innerHTML = "<div id='clickForMore' style='text-align:right'><a href='#' onclick='getMoreFBReviews();return false;'>Show more &raquo;</a></div>";
		}
	}
	return table;
  }




