var s_id;
function gotocoursepage() {
	section_id = s_id;
	document.location.href="/www/?m=learning&c=course_page&i=" + section_id;
}

function onLogin(section_id) {
	s_id = section_id;
	jQuery.ajax({
		type: 'GET',
		url:  '?c=ajaxlogin',
		dataType: 'json',
		success: function(json){
			if(json.logged_in){		
				if(section_id){
					setTimeout("gotocoursepage()",1);
				}else{
					document.location.href="?m=dashboard&locale=" + json['locale'];
				}
			} else {
				document.location.href="?c=login_register&fb=1" + (section_id ? '&i='+section_id:'');
			}
		}
	});
}

function onLoginJoinSection(path, bad_login_path) {
	jQuery.ajax({
		type: 'GET',
		url:  '?c=ajaxlogin',
		dataType: 'json',
		success: function(json){
			if(json.logged_in){		
				document.location.href = path + json['locale'];
			} else {
				document.location.href = bad_login_path;
			}
		}
	});
}

function associate_fb_user() {
	jQuery.ajax({
		type: 'GET',
		url:  '?c=ajax_associate',
		dataType: 'json',
		success: function(json){
			if(json.logged_in){		
				update_user_box();
			}
		}
	});
}

function update_user_box() {
	var user_box = document.getElementById('facebook_user');
	if(user_box) {
		user_box.style.marginTop = '-39px';
		user_box.innerHTML = '';
		FB.XFBML.Host.parseDomTree();
	}
}
var web_hostfb = '';
function logoutFB(web_host) {
	web_hostfb = web_host;
	FB.Connect.logout(logoutRedirect);
}

function logoutRedirect() {
	document.location = web_hostfb + '/?c=logout';
} 

function publishToWall(title, text) {
	var attachment = {'name':title,
			'href':'http://www.coursefeed.com',
			'description':text };
			//'media': [{'type': 'image', 'src': pictureData[0].pic, 'href':'http://apps.facebook.com/'+appName+'/'}] };
	
	FB.Connect.streamPublish('', attachment);
}

function inviteFriend(web_host, id)
{
	var attachment = {'media': [{'type':'image',
	                             'src': web_host + '/www/fw/themes/leso2/images/logo.png',
	                             'href':web_host}]};
	                             
	var actionLinks = [{ "text": "CourseFeed", "href": web_host}];

	FB.Connect.streamPublish('I\'d like to invite you to use CourseFeed - a new online social learning platform', attachment, actionLinks, id, 'Invite to use CourseFeed');

}

function publishNewCourseMember(fbId, web_host, courseName, institutionName, courseId)
{
	var attachment = {'media': [{'type':'image',
								 'src': web_host + '/www/fw/themes/leso2/images/logo.png',
								 'href':web_host + '/www/?m=learning&c=browse_section&i=' + courseId}]};

	var actionLinks = [{ "text": courseName, "href": web_host + '/www/?m=learning&c=browse_section&i=' + courseId}];
	
	FB.Connect.streamPublish('I just joined the ' + courseName + ' course at ' + institutionName + ' on CourseFeed.' , attachment, actionLinks, null, 'CourseFeed');
}
