function FocusButtonLogin(e, prefix) 
{ 
    var keynum  
    var keychar  
    var numcheck 

    if(window.event) // IE  
    { 
        keynum = e.keyCode  
    } 
    else if(e.which) // Netscape/Firefox/Opera  
    { 
        keynum = e.which  
    } 
    
    keychar = String.fromCharCode(keynum) 
    if (keynum == 13) {
    	
        if(document.getElementById(prefix + '_username').value != '' && document.getElementById(prefix + '_password').value != '') {
            document.getElementById(prefix + '_login_button').click();
        }
        return false;  
    } else {
        return true; 
    }
} 

function checkSessionBlockFB() {
    FB.getLoginStatus(function(response) {
    	if (response.session) {
			jQuery.get(ff.site_path + "/services/fb_auth"
		        , "frmAction=login&token=" + response.session.access_token
		        , function(data) {
		            if(data.length > 0) {
            			jQuery(".login-box .fb-loading").hide();
						removeBlockFB(data);
					} else {
						window.location.reload();
					}
			});
			showBlockFB(response.session.uid, true);
		}
	});
}

function removeBlockFB(strError) {
    jQuery(".login-box .standard-login").fadeIn();
    jQuery(".login-box .social-login .fb-data").html("");
    jQuery(".login-box .social-login .fb-data").hide();
    
    if(strError) {
	    jQuery(".login-box .social-login .fb-error").html(strError);
		jQuery(".login-box .social-login .fb-error").fadeIn();
        jQuery(".login-box .social-login .fb-content").hide();
	} else {
	    jQuery(".login-box .social-login .fb-error").html("");
		jQuery(".login-box .social-login .fb-error").hide();
        jQuery(".login-box .social-login .fb-content").show();
	}

	if(jQuery.isFunction('removeFB'))
		removeFB(strError);
}
function showBlockFB(userID, showWaiting){
	jQuery(".login-box .standard-login").hide();
	jQuery(".login-box .social-login .fb-content").hide();
	jQuery(".login-box .std-error, .login-box .fb-error").hide();

	if(showWaiting) {
        jQuery(".login-box .fb-loading").show();
	} else {
        jQuery(".login-box .fb-loading").hide();
	}
	
    FB.api('/' + userID, function(response) {
		jQuery(".login-box .social-login").fadeIn();
        jQuery(".login-box .social-login .fb-data").html('<img class="fb-image-profile" src="https://graph.facebook.com/' + userID + '/picture" /><span>' + response.name + '</span>');
        jQuery(".login-box .social-login .fb-data").fadeIn();
        jQuery(".login-box .logout-field .fb-data").html('<img class="fb-image-profile" src="https://graph.facebook.com/' + userID + '/picture" /><span>' + response.name + '</span>');
        jQuery(".login-box .logout-field .fb-data").fadeIn();

    });

	if(jQuery.isFunction('showFB'))
		showFB(userID);
}

jQuery(function() {
	if(jQuery(".login-box").attr("class") !== undefined) {
		var dialogWidth = 300;

		//jQuery(".login-box").hide();
		if(jQuery(".social-login.facebook").attr("fbappid") !== undefined) {
			if(dialogWidth < 600)
				dialogWidth = 600; 

			if(jQuery("#fb-root").attr("id") === undefined)
				jQuery(".login-box").prepend('<div id="fb-root"></div>');
			
			var strFBLang = 'en_US';
			if(ff.language !== undefined && ff.language.length > 0) {
				strFBLang = ff.language.substr(0, 2).toLowerCase() + "_" + ff.language.substr(0, 2).toUpperCase();
			}	
			ff.pluginLoad("FB", "http://connect.facebook.net/" + strFBLang + "/all.js", function() {
				FB.init({
						appId  : jQuery(".social-login.facebook").attr("fbappid"),
						status : true, // check login status
						cookie : true, // enable cookies to allow the server to access the session
						xfbml  : true  // parse XFBML
					});
				FB.Event.subscribe('auth.login', function(response) {
					checkSessionBlockFB();
				});
			});
		}

		if(jQuery(".social-login.janrain").attr("janrain") !== undefined && window.janrain === undefined) {
			if(dialogWidth < 700)
				dialogWidth = 700; 
			var janrain_appname = jQuery(".social-login.janrain").attr("janrain");
			
		    if (typeof window.janrain !== 'object') window.janrain = {};
		    window.janrain.settings = {};
		    
		    janrain.settings.tokenUrl = 'http://' + window.location.hostname + ff.site_path + '/services/janrain_auth?frmAction=login&ret_url=' + encodeURIComponent(window.location.pathname);

		    function isReady() { janrain.ready = true; };
		    if (document.addEventListener) {
		      document.addEventListener("DOMContentLoaded", isReady, false);
		    } else {
		      window.attachEvent('onload', isReady);
		    }

		    var e = document.createElement('script');
		    e.type = 'text/javascript';
		    e.id = 'janrainAuthWidget';

		    if (document.location.protocol === 'https:') {
		      e.src = 'https://rpxnow.com/js/lib/' + janrain_appname + '/engage.js';
		    } else {
		      e.src = 'http://widget-cdn.rpxnow.com/js/lib/' + janrain_appname + '/engage.js';
		    }

		    var s = document.getElementsByTagName('script')[0];
		    s.parentNode.insertBefore(e, s);
		}

		jQuery(".login-link").bind("click", function() {
			jQuery(".login-box .fb-loading").hide();

			ff.pluginLoad("jquery.ui", "/themes/library/jquery.ui/jquery.ui.js",function() {	
				ff.pluginLoad("jquery.fn.block", "/themes/library/plugins/jquery.blockui/jquery.blockui.js");
				
				jQuery(".login-box").dialog({ resizable: false, width: dialogWidth + 'px' });
				jQuery(".login-username").focus();
			}, false);
		});
	}
	jQuery(".login-ajax").click(function() {
		var blockElem = jQuery(this).closest(".standard-login");
		var thisEl = this;
		
		var ret_url = "";
		if(jQuery(this).attr('src') !== undefined) {
			ret_url = jQuery(this).attr('src');
		}
		
		jQuery(this).val(jQuery(this).val() + "...");
        jQuery(this).css("opacity", "0.6");
        jQuery(this).attr("disabled", "disabled");
	    jQuery.ajax({
		   async: true,    
		   type: "POST",
		   url: ff.site_path + "/login", 
		   data: 'frmAction=login&username=' + jQuery('#' + jQuery(this).attr('rel') + '_username').val() + '&password=' + jQuery('#' + jQuery(this).attr('rel') + '_password').val() + '&ret_url=' + encodeURIComponent(ret_url),
		   cache: false, 
		   success: function(item) {
			    if(typeof jQuery != undefined) {
					jQuery(blockElem).children('.std-error').remove();
					if(item) {
						if(item.substr(0, 1) == "{" && item.substr(item.length - 1, 1) == "}") {
							jsonItem = jQuery.parseJSON( item );
							if(jsonItem.url) {
								if(jQuery.isFunction(jQuery(blockElem).dialog))
									jQuery(blockElem).dialog('close');
								
								ff.pluginLoad("jquery.fn.block", "/themes/library/plugins/jquery.blockui/jquery.blockui.js", function() {
									jQuery.blockUI.defaults.css = {
										padding:	0,
										margin:		0,
										top:		'40%',
										left:		'45%',
										textAlign:	'center',
										cursor:		'wait'
									};
									jQuery.blockUI.defaults.overlayCSS = {};
									jQuery.blockUI({
										message: '<h1 class="block-loader"></h1>'
									});
									window.location.href = jsonItem.url;
								});
							}
						} else {
							jQuery(blockElem).prepend('<div class="std-error">' + item + '</div>');
							jQuery(thisEl).val(jQuery(thisEl).val().replace("...", ""));
						    jQuery(thisEl).css("opacity", "1"); 
						    jQuery(thisEl).removeAttr("disabled");
						}
					} else {
						if(jQuery.isFunction(jQuery(blockElem).dialog))
							jQuery(blockElem).dialog('close'); 
						
						ff.pluginLoad("jquery.fn.block", "/themes/library/plugins/jquery.blockui/jquery.blockui.js", function() {
							jQuery.blockUI.defaults.css = {
								padding:	0,
								margin:		0,
								top:		'40%',
								left:		'45%',
								textAlign:	'center',
								cursor:		'wait'
							};
							jQuery.blockUI.defaults.overlayCSS = {};
							jQuery.blockUI({
								message: '<h1 class="block-loader"></h1>'
							});
							window.location.href = "";
						});
					}
			   }
		    }
		});
		return false;
	});

});
