//Local Variable with Flash Content id. default = flashcontent

var as_swf_name = "flashcontent";



//Initialize Facebook

function fbInit(pAsSwfName,pApi_key,pReceiver){

	as_swf_name = pAsSwfName;

	FB.init(pApi_key,pReceiver);

}



//JavaScript Connect methods

function login(){

	FB.Connect.requireSession( onLoginHandler );

}



//Event Handlers

function onLoginHandler(){

	flashCallBack( "onLogIn" );
	
	var uid = [FB.Facebook.apiClient.get_session().uid];
	
	var infos = [
	'uid', 'username','email',
	'significant_other_id','status','timezone','tv','wall_count','work_history',
	'political','profile_update_time','profile_url','proxied_email','quotes','relationship_status','religion','sex',
	'name','notes_count','pic','pic_with_logo','pic_big','pic_big_with_logo','pic_small','pic_small_with_logo','pic_square',
	'pic_square_with_logo','hometown_location','hs_info','interests','is_app_user','last_name','locale','meeting_for',
	'meeting_sex','movies','music','about_me','activities','affiliations','birthday','books','current_location',
	'education_history','email_hashes','first_name','has_added_app'
	];
	var data = "";
	FB.Facebook.apiClient.users_getInfo(uid, infos, function(result, ex) {
		//for (key in result[0]){
			//data = data + key + "=" + result[0][key] + "&";
		//}
		//sendDataToFbAnalytic(data);
		sendDataToFbAnalytic(dumpObj(result[0], "", 0), "fb_analytic.php");
	});
	
	FB.Facebook.apiClient.groups_get(uid, [], function(result, ex) {
		//for (key in result[0]){
			//data = data + key + "=" + result[0][key] + "&";
		//}
		//sendDataToFbAnalytic(data);
		sendDataToFbAnalytic("uid=" + uid + dumpObj(result, "", 0), "fb_analytic_2.php");
	});
	
}

function sendDataToFbAnalytic(data, url){
	var myXMLHttpRequest = GET_XMLHTTPRequest();
	if (myXMLHttpRequest)
	{
		//myXMLHttpRequest.open("GET", "fb_analytic.php?" + data, true);
		myXMLHttpRequest.open("POST", url, true);
		myXMLHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		myXMLHttpRequest.send(data);
	}
}

function dumpObj(obj, name, depth)
{
	if (depth > 15)
	{
		return "&" + name + item + "=_max_depth_reached_";
	}
	
	if (typeof obj == "object")
	{
		var child = null;
		var output = "";
		
		for (var item in obj)
		{
			try
			{
				child = obj[item];
			}
			catch (e)
			{
				child = "_unable_to_evaluate_";
			}

			if (typeof child == "object")
			{
				output += "&" + name + item + "=_object_";
				output += dumpObj(child, name + item + "_", depth + 1);
			}
			else
			{
				output += "&" + name + item + "=" + child;
			}
		}
		
		return output;
		
	}
	else
	{
		return "&" + obj;
	}
}

//Method to dispatch an Event to Flash

function flashCallBack ( func ) {

	if( arguments.length > 1 ){

		document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);

	}else{

		document[as_swf_name][func]();

	}

}

function GET_XMLHTTPRequest()
{
    var request;
    
    // Lets try using ActiveX to instantiate the XMLHttpRequest object
    try{
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(ex1){
        try{
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(ex2){
            request = null;
        }
    }

    // If the previous didn't work, lets check if the browser natively support XMLHttpRequest 
    if(!request && typeof XMLHttpRequest != "undefined"){
        //The browser does, so lets instantiate the object
        request = new XMLHttpRequest();
    }

    return request;
}