// JavaScript Document

var fb_session = false, // store the data that will be posted to facebook, for access or modification
		timer = false;
		
function postIt(session)
{
	var $wait = jQuery('#please-wait'),
			url = 'http://'+window.location.hostname;
	
	$wait.dialog('open');
	pleaseWaitSetup();
	
	jQuery.post(url+'/sign-up-fb.php',session,function(data)
	{
		var action = data.action,
				error = data.error,
				message = data.message;
		
		$wait.dialog('close');	
		clearTimeout(timer);
		
		if(error)
		{
			alert(message);
		}
		else
		{	
			switch(action)
			{
				case 'force-login': /* send the user to the dashboard */
					window.location = message;
					break;
				case 'prompt-password': /* the email address is on file, but we need to ask for the password */
					promptPassword();
					break;
				case 'next-step': /* send the user to the next step in the sign-up flow */
				case 'redirect': /* we must redirect the user to another page */
					window.location = message;
					//alert(message);
					break;
				default:
					alert(message);
					break;
			}
		}
	},'json');
}

/* sets up a dialog  */
function promptPassword()
{
	$p = jQuery('#prompt-password');
	
	$p.find('.dialog-content').html('<div><input type="password" id="user-password" /></div>');
	$p.dialog('option','title','Please enter your WebStarts Password').dialog('open');
}

/* creates the content of the dialog box, optionally pass a string of text, which will also enable the ability to close the dialog */
function pleaseWaitSetup(text)
{
	var $this = jQuery('#please-wait'),
			$content = $this.find('.dialog-content');
	
	if(text == 'undefined' || !text || text == null)
	{
		$content.html('<img src="_img/ajax-loader.gif" alt="please wait..." />');
		impatient();
	}
	else
	{
		$content.html(text);
		$this.dialog('option','buttons',[
		{
			'Ok' : function()
			{
				jQuery(this).dialog('close');
			}
		}]);
	}
}

function impatient()
{
	timer = setTimeout(function()
	{
		pleaseWaitSetup('It seems to be taking too long for our servers to communicate with facebook. You may want to refresh the page, try again, or use our regular methods of login/sign up. Pleae contact customer service if you have any questions.');
		
	},10000);
}

jQuery(document).ready(function()
{
	jQuery('#please-wait').dialog({
		autoOpen : false,
		draggable : false,
		resizable : false,
		modal : true,
		title : 'Please Wait...'
	});
	
	jQuery('#prompt-password').dialog({
		autoOpen : false,
		draggable : false,
		resizable : false,
		width : 400,
		modal : true,
		buttons : {
			'Submit' : function()
			{
				var new_pass = jQuery('#user-password').val();
				
				fb_session.password = new_pass;
				
				postIt(fb_session);
				jQuery(this).dialog('close');
				jQuery('#please-wait').dialog('open');
			}
		}
	});
	
	jQuery('#facebook-login-button,#facebook-login-button-2').click(function()
	{
		var html = '';
		
		FB.login(function(response) {
			console.log(response);
			if (response.authResponse) {	
				//if (response.perms) {
					// user is logged in and granted some permissions.
					//for(var i in response.session)
					//{
						//html += i+':'+response.session[i]+', \n';
					//}
					
					//response.session['perms'] = response.perms;
					//fb_session = response.session;
					postIt(response.authResponse);
					//window.location = 'sign-up-fb.php?via=fb';
				/*} else {
					// user is logged in, but did not grant any permissions
					alert('Please grant the requested permissions');
				}*/
			} else {
				// user is not logged in
				alert('Please login to your facebook account to continue');
			}
			
		}, {scope:'email,publish_stream'});
		
		return this;
	});
});

/*
session_key:2.ggDLGe8uJ9NdQJjey_s_jw__.3600.1301936400-79103694, 
uid:79103694, 
expires:1301936400, 
secret:SmHa4CfAbPvwzgVJUJDKMA__, 
base_domain:webstarts.com, 
access_token:202003823152954|2.ggDLGe8uJ9NdQJjey_s_jw__.3600.1301936400-79103694|nonHSas7_T6krae1SnAviFou_P4, 
sig:4c041db88332767e8d683dd861e069b0
*/
