$.noConflict();
FB.init({
    appId  : '150229911731316',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    channelURL : 'http://www.collegefoodspot.com/channel.php', // channel.html file
    oauth  : true, // enable OAuth 2.0
});
jQuery(document).ready(function(){
	jQuery('a','#login-form').bind('click',function(e){
		FB.login(function(response) {
			//console.log("userID from login: " + response.authResponse.userID);
			//console.log("accessToken from login: " + response.authResponse.accessToken);
			//console.log("expiresIn from login: " + response.authResponse.expiresIn);
			if (response.authResponse) {
				jQuery.ajax({
					url: "/facebook/handle_login.php",
					data: "userID="+response.authResponse.userID+"&accessToken="+response.authResponse.accessToken+"&expiresIn="+response.authResponse.expiresIn,
					dataType:"json",
					success: function(data){
						jQuery('<img>').attr('src',data['profile-photo']).appendTo('#profile-photo');
						jQuery('#profile-username').html(data['profile-username']);
						jQuery('#profile-message').html(data['profile-welcome']);
						jQuery('#login-form').css('display','none');
						jQuery('#logout-form').css('display','block');
					}
				});
				showFriends();
			}
 		}, {scope: 'user_birthday,user_education_history,user_hometown,user_location,friends_birthday,friends_education_history,friends_work_history'});
	e.preventDefault();
	});
	jQuery('a','#logout-form').bind('click',function(e){
		FB.logout(function(response) {
			jQuery('#login-form').css('display','block');
			jQuery('#logout-form').css('display','none');
			jQuery('#profile-photo').empty();
			jQuery('#profile-username').empty();
			jQuery('#profile-message').empty();
			jQuery('#facebook-friends').empty();
			jQuery.ajax({
				url: "/facebook/kill_session.php",
				dataType:"json",
				success: function(data){
					//console.log('data');
					//console.log(data);
				}
			});
		});
	e.preventDefault();
	});
	FB.getLoginStatus(function(response) {
		if (response.authResponse) {
			showFriends();
		}
	});
	//listen for likes/recommendations
	FB.Event.subscribe('edge.create',
		function(response) {
			console.log(response);
			/*
			jQuery.ajax({
				url: "/facebook/save_user_action.php",
				data: "user_action=like&action_response="+encodeURIComponent(response)
			});*/
		}
	);
	//listen for unlinks/recommendations
	FB.Event.subscribe('edge.remove',
		function(response) {
			jQuery.ajax({
				url: "/facebook/save_user_action.php",
				data: "user_action=unlike&action_response="+encodeURIComponent(response)
			});
		}
	);
	//listen for new comment
	FB.Event.subscribe('comment.create',
		function(response) {
			jQuery.ajax({
				url: "/facebook/save_user_action.php",
				data: "user_action=comment&action_response="+encodeURIComponent(response.href)+"&commentID="+response.commentID,
				success: function(data){
					//do nothing
					//console.log(data);
				}
			});
		}
	);
	//listen for delete comment
	FB.Event.subscribe('comment.remove',
		function(response) {
			jQuery.ajax({
				url: "/facebook/save_user_action.php",
				data: "user_action=uncomment&action_response="+encodeURIComponent(response.href)+"&commentID="+response.commentID,
				success: function(data){
					//do nothing
					//console.log(data);
				}
			});
		}
	);
});	
function showFriends(){
	//TODO: would be good to show a "loading friends..." place holder (searching for friends, etc.)
	//TODO: if the user logs in, out, and back in, the "tell your friends" label appears from the previous log in (before friends have loaded) that could be cleaned up
	jQuery.ajax({
		url: "/facebook/get_friends.php",
		dataType:"json",
		success: function(data){
			if (data.length>0){
				var invities = data;
				var $invite_friends = jQuery('<a>').attr('href','#').text('Tell Your Facebook Friends').bind('click',function(e){
					FB.ui({
						method: 'send',
						to: invities,
						link: 'http://www.collegefooddish.com',
						picture: 'http://collegefoodspot.com/templates/cfd/images/CFD_logo_205pix.png',
						description: 'CFD is the on-line destination for anyone interested in discussing, exploring, complaining about and - hopefully - improving the college dining experience! Our motto is "We dish college food," so come prepared to spill the beans and put it all on the table! Our mission is to provide every college with the forum where current and prospective college students, their parents, college administration, food service vendors, nutrition experts and any other stakeholders can share their perceptions of dining on and off campus. CFD is your source to learn about college dining options, both on and off campus. Find out where to eat on campus and discover great places around town. Visit our forums and talk turkey, or chicken, or pizza, or whatever!'
			  			},
			  			function(response) {
							if (response){ //if there is a response (meaning people were invited) show the thank you modal
								var dialogOptions = {
									buttons: [
							    		{
							        		text: "Ok",
							        		click: function() { jQuery(this).dialog("close"); }
							    		}
									],
									modal:true,
									resizable:false
								}
								jQuery("#dialog").dialog(dialogOptions);
							}
						}
					);
					e.preventDefault();
				});
				jQuery('#invite-friends').html($invite_friends);
			}
			jQuery.each(data,function(b){
				jQuery('<img>').attr('src','http://graph.facebook.com/'+data[b]+'/picture').appendTo('#facebook-friends');
				if (b>=8){ //out at 9
					return false; //break the jQuery loop
				}
			});
		}
	});
}

