/* who-knows-who.js */
$(document).ready(function(){
	$("#who-knows-who-more-connections-button").click(function(){
		$("#who-knows-who-module .module-content").fadeOut("fast", function(){
			//add loader
			$(this).after('<div class="loading" style="height:' + $(this).height() + 'px;"></div>');

			var module = $("#who-knows-who-module");
			$.get('/connections/findConnections/results?offset=' + module.attr("offset") + '&connectionsNeeded=' + module.attr("connectionsNeeded") +'&startId=' + module.attr("startId"), function(data) {
				data = $.parseJSON(data);
				if(data.error){
					$("#who-knows-who-module .loading").after('<p class="error">' + data.error + '</p>').remove();					
				} else {
					var people = $("#who-knows-who-module .module-content .people");
					people.html('');
					for(var i = 0; i < data.people.length; i++){
						/* build Universal Rollover */
						people.append('<li>'
									+ '<a href="' + data.people[i].url + '">'
										+ '<h4><span class="dot">' + data.people[i].name + '</span></h4>'
										+ '<div class="image-wrapper"><img src="' + data.people[i].image + '"/></div>'
									+ '</a>'
									+ buildConnectionsRollover(data.people[i])
								+'</li>');
					}
		
					var relationships = $("#who-knows-who-module .module-content .relationships");
					relationships.html('');
					for(var i = 0; i < data.people.length - 1; i++){
						relationships.append('<li>' + data.people[i].relationship + '</li>');
					}
					$("#who-knows-who-module .loading").remove();
					$("#who-knows-who-module .module-content").fadeIn('fast');
					module.attr("offset", parseInt(module.attr("offset")) + 1);
					$("#who-knows-who-module .universal-rollover").universal_rollover();	
				}
			});
		});			
	}).trigger('click');
});

function buildConnectionsRollover(profile){
	var universal_rollover = '<div class="universal-rollover rollover-with-image clearfix profile-layout">'
		+ '<div class="universal-rollover-top"></div>'
		+ '<div class="universal-rollover-middle clearfix">'
			+ '<div class="right-column">'
				+ '<img src="' + profile.image + '"/>';
				if(profile.inTheseGroups.length > 0){
					universal_rollover = universal_rollover + '<ul><li class="bold">In these groups:</li>';
					for(var i = 0; i < profile.inTheseGroups.length; i++){
						universal_rollover = universal_rollover + '<li><a href="' + profile.inTheseGroups[i].link + '">' + profile.inTheseGroups[i].linkName + '</a></li>';
					}
					universal_rollover = universal_rollover + '</ul>';
				}
				universal_rollover = universal_rollover + '</div>'
				+ '<div class = "left-column">'
				+ '<h3 class="dot"><a href="' + profile.url + '">' + profile.name + '</a></h3>'
				+ '<h4>' + profile.rolloverSubtitle + '</h4>' 
				+ '<p class="description">' + profile.rolloverText + '</p>' 
				+ '<p class="links">'
					+ '<a href="' + profile.url + '" class="red small-person-icon">See full bio</a>';
					if(profile.videoLink){
						universal_rollover = universal_rollover + '| <a href="' + profile.videoLink + '" class="red small-camera-icon">Watch video</a>';
					}
				universal_rollover = universal_rollover + '</p>'	
				/* START BIOPULSE */
				/*
				+ '<div class = "green-tabs-container">';
					if(parseInt(profile.biopulseChange) > 0){
						universal_rollover = universal_rollover + '<a href="' + profile.url + '/biopulse" class="green-tab-button-left green-tab-button"><span class="up">' + profile.biopulseChange + '%</span></a>';
					} else {
						universal_rollover = universal_rollover + '<a href="' + profile.url + '/biopulse" class="green-tab-button-left green-tab-button"><span class="down">' + profile.biopulseChange + '%</span></a>';					
					}
					universal_rollover = universal_rollover + '<a href="' + profile.url + '/biopulse" class="green-tab-button-right green-tab-button">SEE MORE BIOPULSE</a>'
				+ '</div>'
				*/
				/* END BIOPULSE */
			+ '</div>'
		+ '</div>'
		+ '<div class="universal-rollover-bottom"></div>'
	+ '</div>';
	
	return universal_rollover;
}
