/**
 * By ikhuerta@gmail.com
 * More info @ blog.ikhuerta.com
 */

// DEFINE YOUR SETTINGS

$.facebook = {

	// change this for your facebook app api key
	apikey : fbApiId,

	// change this for the absolute uri of your xd_receiver.html document
	channelpath : "/xd_receiver.htm",

	// change this for your locale settings
	locale : "en_US",

	// change to false if you don't want use FBML and XFBML labels
	useFBML : true,

	// !!! don't change before this line
	onloadFunctions : [],
	ready : function (thefunction) {
		this.onloadFunctions[this.onloadFunctions.length] = thefunction;
	}
};

/// LOADING FACEBOOK JAVASCRIPT API...

document.write('<scr'+'ipt src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/' + $.facebook.locale + '" type="text/javascript"></sc'+'ript>');

var currentUrl = window.location.href.split("/");
currentUrl.shift();
currentUrl.shift();
currentUrl.shift();
if ( currentUrl[currentUrl.length-1].indexOf(".") )
	currentUrl.pop();
var channelpath = $.facebook.channelpath.split("/");
channelpath.shift();
for(var i=0;currentUrl[i] && channelpath[i] && currentUrl[i] == channelpath[i] ;i++)
{
	currentUrl.shift();
	channelpath.shift();
}
channelpath = channelpath.join("/");
for(var i=0;i<currentUrl.length;i++)
{
	channelpath = "../" + channelpath;
}
$.facebook.channelpath = channelpath;

$(document).ready(function() {
	//console.log(FB);
	if($.facebook) {
		FB.init($.facebook.apikey, $.facebook.channelpath);
		
		// alert("api inited");
		FB_RequireFeatures(["Api"], function(){
			FB.Facebook.init($.facebook.apikey, $.facebook.channelpath);
		    FB.ensureInit(function() {
		    	$.fb = FB.Facebook.apiClient;
		    	FB.Connect.ifUserConnected(function() {
		// FB.Connect.showPermissionDialog("offline_access",function(x) { alert(x)});	
		    	for(var i=0;i<$.facebook.onloadFunctions.length;i++)
		    	{
		    		$.facebook.onloadFunctions[i]();
		    	}
		    	});
		    });
		});
		/* FB_RequireFeatures(["XFBML"], function() { 
			// app id / api key 
			// Add XFBML elements through JavaScript codes 
			FB.XFBML.Host.addElement(new FB.XFBML.LoginButton($("login_button"))); 
		}); */
	}
});

function facebook_onlogin() {
	
	var api = FB.Facebook.apiClient; 
	var fbuid = api.get_session().uid;
	var timeStamp = Date.parse(new Date());
	var ajaxUrl = '/facebook/ajax/do/checkFacebookUser/fbid/'+ fbuid + '?' + timeStamp;

	$.getJSON(ajaxUrl, {}, function(data){
		if(data.status=="success") {
			window.location = '/facebook/login/';
		} else { 
			if( data.status == "alreadyused" )
				tb_show('Facebook Account Connected','/facebook/connected?keepThis=true&TB_iframe=true&height=250&width=400');
			else
				tb_show('Sign Up','/facebook/login?keepThis=true&TB_iframe=true&height=450&width=530',null,'facebook_closewin');
		}
	});
}

function facebook_onquicklogin() {
	
	var api = FB.Facebook.apiClient; 
	var fbuid = api.get_session().uid;
	var timeStamp = Date.parse(new Date());
	var ajaxUrl = '/facebook/ajax/do/checkFacebookUser/fbid/'+ fbuid + '?' + timeStamp;
	
	busidText = $("#businessid").val();
	recommendationText = $("#fb-recommendation").val();
	if(busidText) {
		name = "qckspRec";
		value= busidText+"-"+recommendationText;
		document.cookie = name + '=' + value + ";path=/;";
	}
	
	$.getJSON(ajaxUrl, {}, function(data){
		if(data.status=="success") {
			if(busidText) {
				window.location = '/facebook/quick-login?qckspRec=1';
			} else {
				window.location = '/facebook/quick-login/';
			}
		} else {
			if( data.status == "alreadyused" )
				window.location = '/facebook/connected?keepThis=true&TB_iframe=true&height=250&width=400';
			else
				window.location = '/facebook/quick-login?keepThis=true&TB_iframe=true&height=450&width=530';
		}
	});
}

function facebook_closewin() {
	window.location = '/user/home/?get_perms';
}

function show_fb_perms_dialog( permParam ) {
	var permStr = '';
	switch( permParam )
	{
		case 1:
			permStr = 'offline_access';
		break;
		case 2:
			permStr = 'publish_stream';
		break;
		case 4:
			permStr = 'offline_access,publish_stream';
		break;
		default:
			return;
		break;
	}
//	alert("show dialog:"+permStr);
	FB.Connect.showPermissionDialog(permStr,function(m) { if(m) displayAlert("Facebook Permission Granted!");});
	/*FB.Connect.showPermissionDialog("publish_stream,offline_access",function(perms) { 
		if(perms) { 
			displayAlert("Facebook Permission Granted!");
			save_session();
		} else {
			continue_without_permission();
		}
	});*/
//	FB.Connect.showPermissionDialog("offline_access",function(x) { alert(x)});
}
