﻿/// <reference path="../../_Common/Include/js/jquery-1.6.2.debug.js" />
/// <reference path="../../_Common/Include/js/jquery-ui-1.8.2.custom.js" />
/// <reference path="../../_Common/Include/js/jquery.json-2.2.debug.js" />
/// <reference path="../../_Common/Include/js/swfobject.js" />


// login :: fb_login();

// setup
// set the permissions for the authentication dialog
var fb_permissions = 'email,publish_stream,publish_actions';
var fb_authorized;


function fb_log() {
	return;
	if (window.console) {
		if ($.browser.msie) {
			console.log('Hello IE! ' + fb_log.arguments.length); //fb_log.arguments.join(', '));
		} else {
			console.log.apply(console, arguments);
		}
	}
}

// init facebook session 
function fb_init(appId, channelUrl) {
	fb_log("--fb_init--");
	
	window.FB_appId = appId;
	window.FB_channelUrl = channelUrl;
	
	(function(d) {
		var js, id = 'facebook-jssdk';
		if (d.getElementById(id))
			return;
		js = d.createElement('script');
		js.id = id;
		js.async = true;
		js.src = "//connect.facebook.net/de_DE/all.js";
		d.getElementsByTagName('head')[0].appendChild(js);
	}(document));
}
	
window.fbAsyncInit = function() {
	
	var appId = window.FB_appId;
	var channelUrl = window.FB_channelUrl;
	FB.init({
		'appId'  : appId,
		'status' : true, // check login status
		'cookie' : true, // enable cookies to allow the server to access the session
		'xfbml'  : true, // parse XFBML
		'channelUrl' : channelUrl, // channel.html file
		'oauth'  : true // enable OAuth 2.0
	});
	
	// subscribe to auth.statusChange to get noticed if the user state changes
	FB.Event.subscribe('auth.statusChange', function (response) {
		fb_log("status: ", response, response.status);
		if (response.status == 'connected'){
			fb_state(true);
		} else {
			fb_state(false);
		}
	});
	
}


// switch between LOGGED IN and GUEST states  
function fb_state(state) {
	fb_log("--fb_state-- ", state);
	
	if (state == true) {
		fb_authorized = true;
		
		FB.api('/me', function(fb_response) {
			window.fb_username = fb_response.name;
			fb_log("--fb_state--", "response: ", fb_response);
			fb_user_image(fb_response.id);
		});
	
	} else if (state == false) {
		fb_authorized = false;
		window.fb_username = null;
	}
}


// function to return an users thumbnail image based on the users ID
function fb_user_image(uid) {
	fb_log("--fb_user_image--");
	
	FB.api({
			method: 'fql.query',
			query: 'SELECT name, pic_square FROM user WHERE uid=' + uid
		},
		function(response) {
			fb_log("--fb_user_image--", "response: ", response[0].pic_square);
			// '<img src="' + response[0].pic_square + '" />'
		}
	);
}


//function to log out the current user
function fb_logout() {
	fb_log("--fb_logout--");
	FB.logout(function(response) {
		fb_state(false); 
	});
}


// login the user. set the response on succesful login or do something if the user canceled the login
function fb_login(callback) {
	fb_log("--fb_login--");
	FB.login(function (response) {

	    var fb_debugMode = (window.location.href.indexOf('DebugId=') != -1);

	    fb_log("--fb_login--", "response", response);
	    if (fb_debugMode || response.authResponse) {
	        if (callback) callback(response);
	    } else {
	        if (callback) callback();
	    }
	}, { scope: fb_permissions });
}


function fb_post(content, direct_post) {
	fb_log("--fb_post--", content, direct_post);
	
	if (!window.fb_username)
		window.fb_username = '…';
	content.name = content.name.replace(/\{user\}/, window.fb_username);
	
	if (direct_post == true && fb_authorized != false) {
		FB.api(
			'/me/feed',
			'post',
			content,
			function(response) {
				fb_log('response ', response);
				if (response && response.id) {
					// alert('Post was published.');
				} else {
					// alert('Post was not published.');
				}
			}
		);
	} else {
		// if direct_post != true or the user is not authorized post via dialog
		content.method = 'feed';
		FB.ui(
			content,
			function(response) {
				fb_log('response ', response);
				if (response && response.post_id) {
					// alert('Post was published.');
				} else {
					// alert('Post was not published.');
				}
			}
		);
	}
}

