		//Hide the button globally after a user connects their TWITTER account
		function twitterConnectHideButton(){
			//Hide the buttons on the top right homepage
			$('.twitter-connect-unregistered').hide();
			$('.twitter-connect-registered').hide();

			//Hide the buttons on the registration modal
			$('.twitter-connect').hide();
		}

		//Hide the button globally after a user connects their FACEBOOK account
		function facebookConnectHideButton(){
			//Hide the buttons on the top right homepage
			$('.facebook-connect-unregistered').hide();
			$('.facebook-connect-registered').hide();

			//Hide the buttons on the registration modal
			$('.facebook-connect').hide();
		}

		//Fill out the register and edit profile page via Facebook with this function
	 	function fillFbData()
    	{
    		FB.api('/me', function(response) {
    			$('#firstName').val(response.first_name);
    			$('#lastName').val(response.last_name);
    			$('#email').val(response.email);
    			$('#fbID').val(response.id);

    			//Add the Faceboook image
    			$('#avatar-preview img').attr('src','http://graph.facebook.com/' + response.id + '/picture?type=large');

				//Delete the custom avatar if it has one. if it fails, whatever, no big deal.
				$.post(BASE_URL + '/account/delete-avatar/');

				//Add the facebook image label
    			append_facebookAvatarNotice();
    		});
    	}


		$(document).ready(function() {

			//Facebook Connect Button when NOT signed in
			$('.facebook-connect-unregistered').click(function(){
				FB.login(function(response) {

					$.post(BASE_URL + '/account/fb-check/',
						{fbid:response.session.uid},
						function(data){

							if (data=='1')	//login user
							{
								window.location = BASE_URL + "/account/fb-login";
								// window.location = BASE_URL + "/account/fb-login?ref=<?=(isset($this->ref))?$this->ref:$_SERVER['REQUEST_URI'] ?>";
							}
							else 	//Customize the global fancybox settings for this instance
							{
								var thisFancySettings = {href: BASE_URL + '/account/register/fbconnect/'+response.session.uid};
								$.extend(thisFancySettings,fancySettings);	//combine
								$.fancybox(thisFancySettings);
							}
						}
					);

				},{perms:'offline_access,publish_stream,email'});

			});

			//Facebook Connect Button when they ARE signed in
			$('.facebook-connect-registered').live('click',function(){
				FB.login(
					function(response) {
						$.post(BASE_URL + '/account/fb-check/',
	                        {fbid:response.session.uid},
	                        function(data){
		                        if (data=='1')
		                        {
			                        //login user
		                        	window.location = BASE_URL + "/account/fb-login";
		                        }
		                        else
			                    {
			                        //associate account with facebook through ajax
									/*
		                        	$.post(
	        							BASE_URL + '/account/facebook-quick/',
	        							{fbid:response.session.uid, uid:'<?=!empty($this->currentUser->getUser()->userID) ? $this->currentUser->getUser()->userID : '';?>'},
	        							function(data){
	        								if (data == '1')	//Hide buttons, it worked
		        							{
	        									facebookConnectHideButton();
	        								}
	        								else	//Someone already is using this account
	        								{
		        								alert('Error: this Facebook account is already associated with a different Fantasy Throwdown account.');
	        								}
	        							}
	        						);
	        						*/
		                        }
	                    	}
	                	);
					},
					{perms:'offline_access,publish_stream,email'}
				);
			});

			//Facebook Connect Button when the register/edit profile box is opened
		    $('.facebook-connect').live('click',function(){
                FB.login(function(response) {
                    $.post(BASE_URL + '/account/fb-check/',
                        {fbid:response.session.uid},
                        function(data){
	                        if (data=='1')
	                        {
		                        //login user
	                        	window.location = BASE_URL + "/account/fb-login";
	                        }
	                        else {
	                        	fillFbData();
	                        	facebookConnectHideButton();
	                        }
                    	}
                	);
                },{perms:'offline_access,publish_stream,email'});
            });


			//Twitter Connect Button when NOT signed in
			$('.twitter-connect-unregistered').click(function(){
				window.open(BASE_URL + '/account/twitter-verify');
			});

			//Twitter Connect Button when they ARE signed in
			//This function is used for both the top right button and the edit profile buttons
			$('.twitter-connect-registered').live('click',function(){
				window.open(BASE_URL + '/account/twitter-quick');
			});
			
			
		});
