// facebook javascript file

/*  +-------------------------------+
    |  FACEBOOK SCRIPTS             |
    |  dependency: facebook API     |
	|  dependency: jQuery           |
	|  dependency: jQuery fancybox  |
	+-------------------------------+  */

/**
 * @abstract depricated; 
 * api now asynchronously initialized from header
 * separated logout from facebook and logout from site
 * */
function InitializeFacebookConnect(){
	// facebook api is loaded asynchronously in the header

	// setup logout button for facebook users
	if(false && $(".nav_logout").length > 0){ // removed; don't logout user from facebook
		if(FB.getLoginStatus()){
			$('.nav_logout').removeAttr('href').attr('href','#');
			$('.nav_logout').attr('onclick','facebook_logout_button_onclick();');
		}
	}
}

/**
 * @abstract display the facebook profile pane in the header
 * @param int fb_uid the facebook uid of the current facebook user
 * */
function show_fb_profile_pane(fb_uid){
	if($("#facebook_profile_pane").length > 0){
		// only show facebook profile if user has linked it to the logged-in account
		$.ajax({
			type: 'POST',
			url: '/facebook/index/facebooklinked/uid/'+fb_uid,
			dataType: 'json',
			success: function(data){
				if(data.msg == 'profile_is_linked'){
					if($("#facebook_profile_pane").length > 0){
						$("#facebook_profile_pane").css('display','block');
					}
					if($("#facebook_linking").length > 0){
						$("#facebook_linking").css('display','block');
					}
				}
			}
		});
	}
}

/**
 * @abstract trigger the php function that will record login history
 * */
function update_facebook_login_history(){
	$.ajax({
		type: 'POST',
		url: '/facebook/index/facebookhistory',
		dataType: 'json',
		success: function(data){
			// do nothing
		}
	});
}

function goToIndex(){
	parent.window.top.location.href = '/';
}

/**
 * @abstract open url in fancybox window
 * @param string url
 * @param string title
 * @param boolean use_iframe
 * @param int width (only used when use_iframe == true)
 * */
function buildFancybox(url, title, use_iframe, width) {
	if(use_iframe == true){
		parent.window.top.$.fancybox({
			'type':'iframe',
			'width':width,
			'href':url,
			'title':title,
			'titleShow':true,
			'hideOnContentClick':false,
			'onComplete':function(){
				$.fancybox.resize(); // adjust height to fit content
			}
		});
	}
	else{
		parent.window.top.$.fancybox({
			'href':url,
			'title':title,
			'titleShow':true,
			'hideOnContentClick':false,
			'onComplete':function(){
				$.fancybox.resize(); // adjust height to fit content
			}
		});
	}
}

function buildSignup(url,title){
	buildFancybox(url,title,true,650);
}

function buildLogin(url,title){
	buildFancybox(url,title,false,null);
}

function not_a_member_click(){
	url = '/service/subscribe/signupnam?uri='+window.location.href;
	title = 'Sign Up';
	buildFancybox(url,title,true,800);
}

function link_this_profile(){
	url = '/facebook/index/facebookassociate';
	parent.window.top.location = url;
}

/**
 * @add association between facebook account and site profile
 * */
function link_account_click(){
	FB.getLoginStatus(
		function(response){
			if (response.authResponse && response.authResponse.userID.length > 0) {
				$.ajax({
					type: 'POST',
					url: '/facebook/index/facebooklink',
					data: ({ facebook_id : response.authResponse.userID }),
					dataType: 'json',
					success: function(data){
						if($("#facebook_link_pane").length > 0){
							$("#facebook_link_pane").html(data.msg);
						}
					}
				});
			}
		}
	);
}

/**
 * @abstract remove the association between facebook account and site profile
 * */
function unlink_account_click(){
	FB.getLoginStatus(
		function(response){
			if (response.authResponse && response.authResponse.userID.length > 0) {
				$.ajax({
					type: 'POST',
					url: '/facebook/index/unlinkfacebook',
					data: ({ facebook_id : response.authResponse.userID }),
					dataType: 'json',
					success: function(data){
						if(data.msg == 'profile_unlinked'){
							if($("#facebook_profile_pane").length > 0){
								$("#facebook_profile_pane").css('display','none');
							}
							if($("#facebook_linking").length > 0){
								$("#facebook_linking").css('display','none');
							}
						}
					}
				});
			}
		}
	);
}

/**
 * @abstract handle the facebook login callback
 * */
function fb_login_callback(){
	FB.getLoginStatus(
		function(response){
			if (response.authResponse && response.authResponse.userID.length > 0) {
				$.ajax({
					type: 'POST',
					url: '/facebook/index/signin',
					data: ({ uid : response.authResponse.userID }),
					dataType: 'json',
					success: function(data){
						switch(data.msg){
							case 'signin_successfully' :
								update_facebook_login_history(); // record login event in db 
								parent.window.top.location.href += '?ref=2'; // refresh page
								break;
							case 'profile_not_exist' :
								if(document.location.href != '/service/subscribe/signup'){
									parent.window.top.location.href = '/service/subscribe/signup'; // refresh page
									// buildFancybox('/service/subscribe/signup','Login Using Facebook',true,650); // send user to subscription page
								}
								break;
							case 'invalid_uid' :
							default : // do nothing
						}
					}
				});
			}
		}
	);
}



/**
 * @abstract
 * */
function facebook_event_auth(){
	FB.getLoginStatus(
		function(response){
			if (response.authResponse && response.authResponse.userID.length > 0) {
				$.ajax({
					type: 'POST',
					url: '/facebook/event/auth',
					data: ({ uid : response.authResponse.userID }),
					dataType: 'json',
					success: function(data){
						auth_code = data.auth_code;
					}
				});
			}
		}
	);
}


/**
 * @abstract facebook event: create
 * @param string name the title of the event
 * @param string description
 * @param int start_time (unix_timestamp)
 * @param int end_time (unix_timestamp)
 * */
function facebook_event_create(name,description,start_time,end_time){
	$.ajax({
		type: 'POST',
		url: '/facebook/event/create',
		data: ({ 'name' : name , 'description' : description , 'start_time' : start_time , 'end_time' : end_time }),
		dataType: 'json',
		success: function(data){
			alert('facebook_event_create result: '+data);
		}
	});
}

/**
 * @abstract facebook event: delete
 * @param int id the id of the event
 * */
function facebook_event_delete(id){
	$.ajax({
		type: 'POST',
		url: '/facebook/event/delete',
		data: ({ 'id' : id }),
		dataType: 'json',
		success: function(data){
			alert('facebook_event_delete result: '+data);
		}
	});
}








