
/*
 * ----------   s1 Facebook jQuery Library Functions   ----------
 */


/* FBML init function */

$(document).ready( function () {
		       jQuery.fn.facebookMLinit = function(appId) {
			   
			   var function_list = '<scr' + 'ipt>' +
			       'window.fbAsyncInit = function() {FB.init({appId: "' + appId + '", status: true, cookie: true, xfbml: true});};' +
			       init_func() +
			       '</scr' + 'ipt>';

			   $(this).html(function_list);
		       };
		   });

/*
 * asynchronous load of connect.facebook.net/en_US/all.js
 * 
 * http://www.hyperarts.com/blog/how-to-add-facebook-like-button-social-plugins-to-wordpress-posts/
 * 
 */

function init_func(){

    var func_text = "(function() {var e = document.createElement('script'); e.async = true; e.src = " +
	"document.location.protocol + '//connect.facebook.net/en_US/all.js'; " +
	"document.getElementById('fb-root').appendChild(e);}());";

    return func_text;
}

/*
 * check the Facebook cookie for functions that require login
 *
 */
function readCookie(name){
    name += '=';
    var parts = document.cookie.split(/;\s*/);
    for (var i = 0; i < parts.length; i++){
        var part = parts[i];
        if (part.indexOf(name) == 0)
        return part.substring(name.length)
    }
    return null;
}

/*
 * show the user the login button if they need to login to use the FB functionality
 *
 */
function show_login_button(){
    var btn;
    btn = 'You must login to your Facebook account to use this feature:<br /><fb:login-button onlogin="window.location.reload();"></fb:login-button>';
    return btn;
}

/*
 * like function, add a 'Like' button to the page
 * 
 */

$(document).ready( function () {
    jQuery.fn.facebookLike = function (passedOpts) {
	
	var options = {
	    'iframe': true,
	    'domain':   document.location.href,	    
	    'showfaces': 'true',
	    'width':    400,
	    'height':   80,
	    'action': 'like',
	    'colorscheme': 'light'
	};
	
	jQuery.extend(options, passedOpts);
	
	$.each(this, function() {
	    if(options.iframe == false){
		$(this).html('<fb:like action="'			+ escape(options.action)	+
			     '" colorscheme="'				+ escape(options.colorscheme)	+
			     '" layout="standard" show_faces="'		+ escape(options.showfaces)	+
			     '" width="'				+ escape(options.width)		+
			     '" height="'				+ escape(options.height)	+
			     '" ></fb:like>');
	    }
	    else{
		$(this).html('<iframe src="http://www.facebook.com/plugins/like.php?href='							+ escape(options.domain) 	+
		      '&amp;layout=standard&amp;show_faces='											+ escape(options.showfaces)	+
		      '&amp;width='        													+ escape(options.width)		+
		      '&amp;action='														+ escape(options.action)	+
		      '&amp;colorscheme='													+ escape(options.colorscheme)	+
		      '" scrolling="no" frameborder="0" allowTransparency="true" style="border-width:0px; border:none; overflow:hidden; width:'	+ options.width			+
		      'px; height:'														+ options.height		+
		      'px"></iframe>');
	    }
	});
    };
});

/*
 * recommended function - display articles which have been recommmended by other facebook users
 * 
 */
$(document).ready( function () {
    jQuery.fn.facebookRecommended = function (passedOpts) {
	
	var options = {
	    'iframe': true,
	    'domain':   document.domain,
	    'width':    200,
	    'height':   200,
	    'colorscheme': 'light',
	    'header': 'true'
	};
	
	jQuery.extend(options, passedOpts);
	
	$.each(this, function() {
	    if(options.iframe == false){
		$(this).html('<fb:recommendations width="'	+ escape(options.width)		+
			     '" height="'			+ escape(options.height)	+
			     '" header="'			+ escape(options.header)	+
			     '" colorscheme="'			+ escape(options.colorscheme)	+
			     '" site="'				+ escape(options.domain)	+
			     '"></fb:recommendations>');
	    }
	    else{
		$(this).html('<iframe src="http://www.facebook.com/plugins/recommendations.php?site='	+ escape(options.domain)	+
		      '&amp;width='        								+ escape(options.width)		+
		      '&amp;height='       								+ escape(options.height)	+
		      '&amp;header='       								+ escape(options.header)	+
		      '&amp;colorscheme='  								+ escape(options.colorscheme)	+
		      '&amp;font=arial&amp;border_color" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'
		      + options.width + 'px; height:' + options.height + 'px;" allowTransparency="true"></iframe>');
	    }
	});
    };
});

/*
 * recent activity function - display general recent activity by friends related to this site
 * 
 * 
 */

$(document).ready( function () {
    
    jQuery.fn.facebookRecent = function (passedOpts) {
	
	var options = {
	    'iframe': true,
	    'domain':   document.domain,
	    'width':    200,
	    'height':   200,
	    'colorscheme': 'light',
	    'header': 'true'
	};

	jQuery.extend(options, passedOpts);

	$.each(this, function() {
	    if(options.iframe == false){
		$(this).html('<fb:activity width="'	+ escape(options.width)		+
			     '" height="'		+ escape(options.height)	+
			     '" header="'		+ escape(options.header)	+
			     '" colorscheme="'		+ escape(options.colorscheme)	+
			     '" site="'			+ escape(options.domain)	+
			     '"></fb:activity>');
	    }
	    else{
		$(this).html('<iframe src="http://www.facebook.com/plugins/activity.php?site=' 	+ escape(options.domain)	+
		      '&amp;width='        							+ escape(options.width)		+
		      '&amp;height='       							+ escape(options.height)	+
		      '&amp;header='       							+ escape(options.header)	+
		      '&amp;colorscheme='  							+ escape(options.colorscheme)	+
		      '&amp;font=arial&amp;border_color" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'
		      + options.width + 'px; height:' + options.height + 'px;" allowTransparency="true"></iframe>');
	    }
	});
    };
});

/*
 * comments function - make a comment on a page through facebook
 * 
 * http://developers.facebook.com/docs/reference/fbml/comments
 */

$(document).ready( function () {
    
    jQuery.fn.facebookComments = function (passedOpts) {
	var options = {
	    'width':    500,
	    'height':   200,
	    'numposts': 0,
	    'title': 'Leave a comment using your Facebook Account',
	    'simple': 1,
	    'publish_feed': 0,
	    'reverse': 0
	};

	jQuery.extend(options, passedOpts);

	$.each(this, function() {
	    $(this).html('<fb:comments numposts="'	+ escape(options.numposts)	+
			 '" title="'			+ escape(options.title)		+
			 '" simple="'			+ escape(options.simple)	+
			 '" width="'			+ escape(options.width)		+
			 '" height="'			+ escape(options.height)	+
			 '"></fb:comments>');
	});
    };
});

/*
 * invite function - invite your facebook friends to use this site
 */

$(document).ready( function () {
    
    jQuery.fn.facebookInvite = function (passedOpts) {
	
	var options = {
	    'app_id':0,
	    'main_wrap_width': 500,
	    'action': document.location.href,
	    'method': 'POST',
	    'type': 's1',
	    'content': 'Join me on this s1 site.<fb:req-choice url=http://' + document.domain + ' label=\'Join Us!\' />',
	    'f_selector_border': false,
	    'f_selector_actiontext': 'Invite your friends to join you on this s1 site.',
	    'f_selector_width': 500,
	    'f_selector_height': 400,
	    'f_selector_cols':3,
	    'f_selector_condensed': false
	};

	jQuery.extend(options, passedOpts);
	var cookie_check = readCookie('fbs_' + options.app_id);

	$.each(this, function() {
	    
	    if(cookie_check){
	    var fb_invite;
	    var f_selector_condensed_req = '';
	    if(options.f_selector_condensed == true){
		f_selector_condensed_req = '<fb:request-form-submit />';
	    }
	    fb_invite = '<fb:serverfbml width="'				+ escape(options.main_wrap_width) +
			    '"><scr' + 'ipt type="text/fbml">' +
				'<fb:fbml>' +
				'<fb:request-form action="'			+ options.action +
				'" method="'					+ escape(options.method) +
				'" invite="true" type="'			+ options.type + '"' +
				'content="'					+ options.content +
				'" ><fb:multi-friend-selector showborder="'	+ escape(options.f_selector_border) +
				'" actiontext="'				+ options.f_selector_actiontext +
				'" width="'					+ escape(options.f_selector_width) +
				'" cols="'					+ escape(options.f_selector_cols) +
				'" condensed="'					+ escape(options.f_selector_condensed) +
				'" email_invite="false" />'			+ f_selector_condensed_req +
				'</fb:request-form>' +
				'</fb:fbml>' +
			    '</scr' + 'ipt>' +
			    '</fb:serverfbml>';
	    }
	    else{
		fb_invite = show_login_button();
	    }
	    document.getElementById(this.id).innerHTML = fb_invite;
	});
    };
});

/*
 * ----------------------------------------------------------------------------------------------------
 * ----------------------------------------------------------------------------------------------------
 * ----------------------------------------------------------------------------------------------------
 * 
 */

/*
 * true
 */
true;
