jQuery(document).ready(function(){
	RegisteredFan.init();
	
	jQuery("#password").keyup(function(e) {
		if (e.keyCode == 13) {
			jQuery("#rfanLoginForm").submit();
		}
	});
	if (RegisteredFan.getLoginError()) {
        jQuery("#messageCaption").html('<font color="#ff0000">' + RegisteredFan.getLoginError() + '</font> ' + jQuery("#messageCaption").html());
        jQuery("#rfanSignedOut").css("width","248px");
        jQuery("#rfanSignedOut").css("background-position","right center");
        //jQuery("#messageCaption").css("color","#ff0000");
        //jQuery("#username").css("background-color","#ff0000");
        //jQuery("#password").css("background-color","#ff0000");
        //RegisteredFan.deleteCookie(RegisteredFan.loginErrorCookieName);
	}
});

var RegisteredFan = {
	
	authCookieName  : "rfclient_auth",
	profileCookieName  : "rfclient_profile",
	loginErrorCookieName  : "rfclient_error",
	
	init : function() {
		jQuery('#rfanSignOut').click(this.logout);
		jQuery('#rfanLoginForm')[0].returnUrl.value = location.href;
		this.refresh();
		jQuery('#rfanCommon').css('display', 'block');
	},

	refresh : function () {
		if(RegisteredFan.getCookie(this.authCookieName) != null) {
			//jQuery('#welcomeArea').height(50);
			jQuery('#rfanSignedOut').css('display', 'none');
			jQuery('#rfanSignedIn').css('display', 'block');
		}
		else {
			jQuery('#rfanSignedOut').css('display', 'block');
			jQuery('#rfanSignedIn').css('display', 'none');
		}
	},

	logout : function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		// must perform a redirect to guarantee logout, as we can't clean up
		// parent domain cookies with JS
		var rnd = Math.random()*10000000000;
		var returnUrl = location.href;
		location.href = this.href + '&returnUrl=' + escape(returnUrl) + '&rnd=' + rnd;
	},
	
	getLoginError : function() {
		var auth = this.getCookie(this.authCookieName);
		if(auth != null) {
			return '';	
		}
		
		var error = this.getCookie(this.loginErrorCookieName);
		if(error == null || error.lenght == 0) {
			return '';	
		}

		return this.base64Decode(error);
	},
	
	isSignedIn : function () {
		return (RegisteredFan.getCookie(this.authCookieName) != null) ? true : false;
	},

	
	getUserName : function() {
		var profile = this.getCookie(this.profileCookieName);
		if(profile == null) {
			return null;	
		}
		
		profile = this.base64Decode(profile);
		var pairs = profile.split(';');
		
		for(var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			if(pair[0] == 'userName') {
				return pair[1];
			}
		}
	},
	
	getFullName : function () {
		var profile = this.getCookie(this.profileCookieName);
		if(profile == null) {
			return null;	
		}

		profile = this.base64Decode(profile);
		var pairs = profile.split(';');
		var name = ' ';
		var id = null;

		for(var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			if(pair[0] == 'firstName') {
				name = pair[1] + name;
			}
			else if(pair[0] == 'lastName') {
				name = name + pair[1];
			}
			else if(pair[0] == 'userName') {
				id = pair[1];
			}
		}

		if(name.length > 1) {
			return name;
		}

		return id;
	},
	
	getFavoriteTeam : function () {
		var profile = this.getCookie(this.profileCookieName);
		if(profile == null) {
			return null;	
		}

		profile = this.base64Decode(profile);
		var pairs = profile.split(';');

		for(var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			if(pair[0] == 'favoriteTeam') {
				return pair[1];
			}
		}

		return null;
	},

	getCookie : function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	deleteCookie : function(name) {
        domain = ".nhl.com";
        if (this.getCookie(name) )  {
        	document.cookie = name + "=" + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    },
	
	base64Decode : function(data) {
    	var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    	var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    	do {  
        	h1 = b64.indexOf(data.charAt(i++));
        	h2 = b64.indexOf(data.charAt(i++));
        	h3 = b64.indexOf(data.charAt(i++));
        	h4 = b64.indexOf(data.charAt(i++));
 
        	bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        	o1 = bits>>16 & 0xff;
        	o2 = bits>>8 & 0xff;
        	o3 = bits & 0xff;
 
        	if (h3 == 64)      enc += String.fromCharCode(o1);
        	else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        	else               enc += String.fromCharCode(o1, o2, o3);
    	} while (i < data.length);
    	return enc;
	}
}