var fbConnectionsLoaded = false;


function SMConnection (type) {
	this.type = type;
}

SMConnection.prototype.render = function() {
	$friendContainer = $("<div>");
	
	$friendContainer.data("friend", this);
	
	$friendContainer.addClass('friend-container');
	$friendContainer.click(smConnectionSelect);
	
	$checkbox = $('<input type="checkbox" />');
	
	$friendSelect = $('<div class="friend-select"></div>');
	$checkbox.appendTo($friendSelect);
	$friendSelect.appendTo($friendContainer);
	$('<div class="friend-pic"><img src="https://graph.facebook.com/' + this.id + '/picture?type=square" width="50" height="50" style="float:left;"></div>').appendTo($friendContainer);
	$('<div class="friend-info"><p>' + this.name + '</p><p><span class="facebook-type">Facebook</span></p></div>').appendTo($friendContainer);
	
	return $friendContainer;
};

function smConnectionSelect() {
	$friendContainer = $(this).closest('.friend-container');
	
	if ($friendContainer) {
		if ($friendContainer.hasClass('friend-container-selected')) {
			$friendContainer.removeClass('friend-container-selected');
			$friendContainer.find('input[type=checkbox]').removeAttr('checked');
		} else {
			$friendContainer.addClass('friend-container-selected');
			$friendContainer.find('input[type=checkbox]').attr('checked', true);
		}
	}
}

function smConnectionsFilter() {
	var filterValue = $(this).val().toLowerCase();
	
	$('.friend-container').each(function() {
	
		friend = $(this).data('friend');
		
		if (friend.nameLower.indexOf(filterValue) >= 0) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}

function smDiscountLink() {
	FB.getLoginStatus(function(response){
		if (response.session){
			FB.api('/me/feed', 'post', {message: smBoughtTicektsText.replace('%eventName%', eventName), name:event.name, link:eventLink}, function(response) {
				// the post worked
				var fbPost = response;
			});
		}
		else {
			FB.login(function(response) {
				if (response.authResponse) {						
					FB.api('/me/feed', 'post', {message: smBoughtTicektsText.replace('%eventName%', eventName), name:event.name, link:eventLink}, function(response) {
						// the post worked
						var fbPost = response;
					});
				}
			}, {scope: appPermissions});
		}
	});
}


function smSendInvites() {
	eventIndex = $("#event-select").val();
	
	if (eventIndex == "none") {
		// replace this with something pretty
		alert("Please select an event");
	} else {	
		eventId = events[eventIndex]["eventId"];
		eventName = events[eventIndex]["eventName"];
		eventLink = events[eventIndex]["eventLink"];
		
		FB.getLoginStatus(function(response){
			if (response.session){
				$('.friend-container-selected').each(function() {
					friend = $(this).data('friend');
					
					FB.api('/' + friend.id + '/feed', 'post', {message: smInviteFriendText, name:event.name, link:eventLink}, function(response) {
						// the post worked
						var fbPost = response;
					});
				});
			}
			else {
				FB.login(function(response) {
					if (response.authResponse) {						
						$('.friend-container-selected').each(function() {
							friend = $(this).data('friend');
							
							FB.api('/' + friend.id + '/feed', 'post', {message: smInviteFriendText, name:event.name, link:eventLink}, function(response) {
								// the post worked
								var fbPost = response;
							});
						});
					}
				}, {scope: appPermissions});
			}
		});
	}
}

function showConnections() {
	$("#friends-box").html("");
	
	for (var count = 0; count < smConnections.length; count++) {
		smConnections[count].render().appendTo("#friends-box");
	}
	
	$('.friend-info').each(function(index) {
		var objHeight = $(this).height();
		var parentHeight = $(this).parent().height();
		
		$(this).css('margin-top', ((parentHeight - objHeight) / 2) + "px");
	});
}

function showLoader(show) {
	if (show)
		$("#loader").show();
	else
		$("#loader").hide();
}

function fb_loadFriends() {
	$("#fb-select-radio").attr("checked", true);
	
	if (!fbConnectionsLoaded) {

		FB.getLoginStatus(function(response){
			if (response.session){
				showLoader(true);
			
				fbConnectionsLoaded = true;

				FB.api('/me/friends', function(response) {
					var friends = response.data;
	
					$('#facebook-button-text').text(" ("+friends.length+")");
	
					for (var count = 0; count < friends.length; count++) {
						
						var connection = new SMConnection("facebook");
						
						connection.id = friends[count].id;
						connection.name = friends[count].name;
						connection.nameLower = friends[count].name.toLowerCase();
						
						smConnections[smConnections.length] = connection;
						
					}

					showConnections();
					
					showLoader(false);
				});
			}
			else {
				FB.login(function(response) {
					if (response.authResponse) {						
						showLoader(true);
					
						fbConnectionsLoaded = true;
		
						FB.api('/me/friends', function(response) {
							var friends = response.data;
			
							$('#facebook-button-text').text(" ("+friends.length+")");
			
							for (var count = 0; count < friends.length; count++) {
								
								var connection = new SMConnection("facebook");
								
								connection.id = friends[count].id;
								connection.name = friends[count].name;
								connection.nameLower = friends[count].name.toLowerCase();
								
								smConnections[smConnections.length] = connection;
								
							}
		
							showConnections();
							
							showLoader(false);
						});
					}
				}, {scope: appPermissions});
			}
		});
	} else {
		showConnections();
	}
}

function fb_attendingEvent(fEventId, fEventLink) {
	eventId = fEventId;
	eventLink = fEventLink;

	FB.getLoginStatus(function(response){
		if (response.session){
			FB.api('/'+eventId, function(response) {
				fb_postAttendingInfo(response);
			});
		}
		else {
			FB.login(function(response) {
				if (response.authResponse) {						
					FB.api('/'+eventId, function(response) {
						fb_postAttendingInfo(response);
					});
				}
			}, {scope: appPermissions});
		}
	});
}

function fb_postAttendingInfo(fbEvent) {
	FB.getLoginStatus(function(response){
		if (response.session){
			FB.api('/'+fbEvent.id+'/attending', 'post', function(response) {
				if (!response || response.error) {
						console.log('error, msg: ' + response.error.message);
				} else {
					// we sucessfully rsvp'd attending to the event
				}
			});
			
			FB.api('/me/feed', 'post', {message: smIAmGoingText.replace("%eventName%", eventName), name:event.name, link:eventLink}, function(response) {
				// the post worked
				var fbPost = response;
			});
		}
		else {
			FB.login(function(response) {
				if (response.authResponse) {						
					FB.api('/'+fbEvent.id+'/attending', 'post', function(response) {
						if (!response || response.error) {
								console.log('error, msg: ' + response.error.message);
						} else {
							// we sucessfully rsvp'd attending to the event
						}
					});
					
					FB.api('/me/feed', 'post', {message: smIAmGoingText.replace("%eventName%", eventName), name:event.name, link:eventLink}, function(response) {
						// the post worked
						var fbPost = response;
					});
				}
			}, {scope: appPermissions});
		}
	});
}
