var fbcSignInButtons = [];

var FbcSignIn = new Class({

    /**
     * Creates the popover with the given inner HTML and displays it.
     */
    initialize: function(prefix, omnitureTag) {
		this.prefix = prefix;
		this.omnitureTag = omnitureTag;
		fbcSignInButtons.push(this);
	},

	showButton: function() {
		if (this.isDisplayed) return;

		$(this.prefix + '-fb-reg-signin').setStyle('display', '');
		this.isDisplayed = true;

		var tag = this.omnitureTag;
		// add omniture tagging as an on click event
		$(this.prefix + '-fb-login-button').addEvent('click', function() {
		   	if(typeof OmnitureCustomLink == 'function')
			{
		    	OmnitureCustomLink(tag);
		    }
		});

		// just one backdrop
		if (!$('fb-reg-signin-backdrop')) {
			var backdrop = new Element('div', {
				'id' : 'fb-reg-signin-backdrop',
				'styles': {
		        	'display': 'none',
		        	'position': 'fixed',
		        	'_position' : 'absolute',
		        	'top': '0',
		        	'left' : '0',
		        	'width' : '100%',
		        	'height' : '100%',
		        	'background-color' : 'black',
		        	'z-index' : '3001',
		        	'-moz-opacity' : '0.7',
		        	'opacity' : '.70',
		        	'filter' : 'alpha(opacity=70)'
		    	}
			});
			MootoolsUtils.inject(backdrop, $(document.body));
		}
	}
});

FbcSignIn.signUserInWithFbc = function (returnType, returnValue) {
	FB.Connect.requireSession(function () {
		$('fb-reg-signin-backdrop').setStyle('display', '');
		// make an ajax call to attempt to authenticate the user, if account
		// is not linked, redirect to another page
		MootoolsUtils.request('/fbconnect/reg/signin.do?returnType=' + returnType + '&returnValue=' + returnValue, {
			method : 'get',
			onSuccess: function(responseText) {
				var forwardUrl = responseText;
				window.location = forwardUrl; 
			},
			onFailure: function () {
				alert('Could not log you in');
				MootoolsUtils.dispose($('fb-reg-signin-backdrop'));
			}
		});
	});	
}

FbcSignIn.showAllButtons = function () {
	for (var i = 0; i < fbcSignInButtons.length; i++) {
		fbcSignInButtons[i].showButton();
    }
}


